lostplaces-backend/django_lostplaces/lostplaces/models.py

243 lines
6.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
2020-09-10 00:21:26 +02:00
'''
(Data)models which describe the structure of data to be saved into
database.
'''
import os
import uuid
from django.urls import reverse
2020-07-19 00:13:49 +02:00
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
2020-09-03 20:06:06 +02:00
from django.core.validators import MaxValueValidator, MinValueValidator
2020-09-18 21:02:07 +02:00
from django.utils import timezone
2020-08-03 19:14:13 +02:00
from easy_thumbnails.fields import ThumbnailerImageField
from easy_thumbnails.files import get_thumbnailer
2020-08-30 18:39:45 +02:00
from taggit.managers import TaggableManager
2020-07-19 00:13:49 +02:00
# Create your models here.
2020-09-13 15:49:15 +02:00
class Explorer(models.Model):
"""
Profile that is linked to the a User.
Every user has a profile.
"""
user = models.OneToOneField(
User,
on_delete=models.CASCADE,
related_name='explorer'
)
def __str__(self):
return self.user.username
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Explorer.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.explorer.save()
2020-09-12 12:24:27 +02:00
class Taggable(models.Model):
2020-09-13 14:39:21 +02:00
'''
This abstract model represtens an object that is taggalble
using django-taggit
'''
2020-09-12 12:24:27 +02:00
class Meta:
abstract = True
tags = TaggableManager(blank=True)
2020-09-14 15:18:21 +02:00
class Mapable(models.Model):
2020-09-13 14:39:21 +02:00
'''
This abstract model class represents an object that can be
displayed on a map.
'''
2020-09-12 12:24:27 +02:00
class Meta:
abstract = True
name = models.CharField(max_length=50)
latitude = models.FloatField(
validators=[
MinValueValidator(-90),
MaxValueValidator(90)
]
)
longitude = models.FloatField(
validators=[
MinValueValidator(-180),
MaxValueValidator(180)
]
)
2020-09-13 14:39:21 +02:00
class Submittable(models.Model):
'''
This abstract model class represents an object that can be submitted by
an explorer.
'''
class Meta:
abstract = True
submitted_when = models.DateTimeField(auto_now_add=True, null=True)
submitted_by = models.ForeignKey(
Explorer,
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name='%(class)s'
)
2020-08-01 13:11:07 +02:00
class Voucher(models.Model):
"""
Vouchers are authorization tokens to allow the registration of new users.
2020-09-10 00:21:26 +02:00
A voucher has a code, a creation and a deletion date, which are all
positional. Creation date is being set automatically during voucher
creation.
"""
2020-08-01 13:11:07 +02:00
2020-08-27 17:16:03 +02:00
code = models.CharField(unique=True, max_length=30)
created_when = models.DateTimeField(auto_now_add=True)
expires_when = models.DateTimeField()
2020-08-01 13:11:07 +02:00
2020-09-18 21:02:07 +02:00
@property
def valid(self):
return timezone.now() <= self.expires_when
def __str__(self):
2020-09-18 20:30:25 +02:00
return "Voucher " + str(self.pk)
2020-08-01 13:11:07 +02:00
2020-09-14 15:18:21 +02:00
class Place(Submittable, Taggable, Mapable):
"""
Place defines a lost place (location, name, description etc.).
"""
2020-09-13 19:29:30 +02:00
location = models.CharField(max_length=50)
description = models.TextField()
2020-09-12 11:42:18 +02:00
def get_absolute_url(self):
return reverse('place_detail', kwargs={'pk': self.pk})
2020-09-12 11:34:49 +02:00
@classmethod
2020-09-10 00:21:26 +02:00
# Get center position of LP-geocoordinates.
2020-09-12 11:34:49 +02:00
def average_latlon(cls, place_list):
amount = len(place_list)
# Init fill values to prevent None
2020-08-27 17:17:28 +02:00
longitude = 0
latitude = 0
2020-08-27 17:17:28 +02:00
if amount > 0:
for place in place_list:
longitude += place.longitude
latitude += place.latitude
2020-09-12 11:34:49 +02:00
return {'latitude':latitude / amount, 'longitude': longitude / amount}
2020-08-27 17:17:28 +02:00
2020-09-12 11:34:49 +02:00
return {'latitude': latitude, 'longitude': longitude}
def __str__(self):
return self.name
def generate_image_upload_path(instance, filename):
"""
Callback for generating path for uploaded images.
Returns filename as: placepk-placename{-rndstring}.jpg
"""
return 'places/' + str(instance.place.pk) + '-' + str(instance.place.name) + '.' + filename.split('.')[-1]
2020-09-13 19:29:30 +02:00
class PlaceImage (Submittable):
"""
PlaceImage defines an image file object that points to a file in uploads/.
Intermediate image sizes are generated as defined in THUMBNAIL_ALIASES.
PlaceImage references a Place to which it belongs.
"""
description = models.TextField(blank=True)
filename = ThumbnailerImageField(
upload_to=generate_image_upload_path,
resize_source=dict(size=(2560, 2560),
sharpen=True)
)
place = models.ForeignKey(
Place,
on_delete=models.CASCADE,
2020-09-13 14:39:21 +02:00
related_name='placeimages'
)
2020-09-13 19:29:30 +02:00
def __str__(self):
"""
Returning the name of the corresponding place + id
of this image as textual represntation of this instance
"""
2020-09-18 20:37:54 +02:00
return 'Image ' + str(self.pk)
# These two auto-delete files from filesystem when they are unneeded:
@receiver(models.signals.post_delete, sender=PlaceImage)
def auto_delete_file_on_delete(sender, instance, **kwargs):
"""
Deletes file (including thumbnails) from filesystem
when corresponding `PlaceImage` object is deleted.
"""
if instance.filename:
# Get and delete all files and thumbnails from instance
thumbmanager = get_thumbnailer(instance.filename)
thumbmanager.delete(save=False)
@receiver(models.signals.pre_save, sender=PlaceImage)
def auto_delete_file_on_change(sender, instance, **kwargs):
"""
Deletes old file from filesystem
when corresponding `PlaceImage` object is updated
with new file.
"""
if not instance.pk:
return False
try:
old_file = PlaceImage.objects.get(pk=instance.pk).filename
except PlaceImage.DoesNotExist:
return False
# No need to delete thumbnails, as they will be overwritten on regeneration.
new_file = instance.filename
if not old_file == new_file:
if os.path.isfile(old_file.path):
os.remove(old_file.path)
2020-08-26 21:36:10 +02:00
2020-08-26 21:36:10 +02:00
class ExternalLink(models.Model):
2020-08-30 18:39:45 +02:00
url = models.URLField(max_length=200)
label = models.CharField(max_length=100)
submitted_by = models.ForeignKey(
2020-08-26 21:36:10 +02:00
Explorer,
on_delete=models.SET_NULL,
null=True,
blank=True,
2020-08-26 21:36:10 +02:00
related_name='external_links'
)
2020-08-30 18:39:45 +02:00
submitted_when = models.DateTimeField(auto_now_add=True, null=True)
2020-08-26 21:36:10 +02:00
2020-08-26 21:36:10 +02:00
class PhotoAlbum(ExternalLink):
2020-08-30 18:39:45 +02:00
place = models.ForeignKey(
2020-08-26 21:36:10 +02:00
Place,
on_delete=models.CASCADE,
related_name='photo_albums',
2020-08-30 18:39:45 +02:00
null=True
2020-09-10 00:21:26 +02:00
)