lostplaces-backend/django_lostplaces/lostplaces/models/place.py

141 lines
4.1 KiB
Python
Raw Normal View History

Squashed commit of the following: commit 0d62e72d72922a84e41c9f2cc21977b794784d1c Merge: 79fee63 85f2a81 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:55:18 2020 +0200 Merge branch 'develop' into refactor/models commit 79fee631d7ac28509067ecdd74078f1a2f6e0be2 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:54:32 2020 +0200 Updating references for related name commit 8e07e79df2de2601f2e2eadfdd37eb7c719c51b0 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:53:31 2020 +0200 Generating of related names fix commit 5fd804f37a805ae4707e13c3d941bdde3660afea Merge: 8cc1d3e 3b526c9 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:01:48 2020 +0200 Merge branch 'develop' into refactor/models commit 8cc1d3e690211dba6451e86569f00078b23e0621 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 20:21:08 2020 +0200 Tests commit 7c0591e5397f892b1f6fb80725a693c21f90468a Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 23:53:39 2020 +0200 Testing PlaceAsset commit 2e7b49ad1a15173565c81e7eb8bb3f35b9f622a6 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 22:25:08 2020 +0200 Restructuring models commit eb7d03b08b326f9115e70d0fd9ed5d0fc229a362 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 22:01:54 2020 +0200 Abstract class Expireable commit 2b51e741bb5734c5a578beeadef7819fe58b2223 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 21:54:07 2020 +0200 Abstract Model for PlaceAsset (i.e. Photoalbums)
2020-09-22 21:56:51 +02:00
import os
from django.db import models
from django.urls import reverse
from django.dispatch import receiver
from django.db.models.signals import post_delete, pre_save
2020-10-06 17:01:29 +02:00
from django.utils.translation import ugettext_lazy as _
Squashed commit of the following: commit 0d62e72d72922a84e41c9f2cc21977b794784d1c Merge: 79fee63 85f2a81 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:55:18 2020 +0200 Merge branch 'develop' into refactor/models commit 79fee631d7ac28509067ecdd74078f1a2f6e0be2 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:54:32 2020 +0200 Updating references for related name commit 8e07e79df2de2601f2e2eadfdd37eb7c719c51b0 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:53:31 2020 +0200 Generating of related names fix commit 5fd804f37a805ae4707e13c3d941bdde3660afea Merge: 8cc1d3e 3b526c9 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:01:48 2020 +0200 Merge branch 'develop' into refactor/models commit 8cc1d3e690211dba6451e86569f00078b23e0621 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 20:21:08 2020 +0200 Tests commit 7c0591e5397f892b1f6fb80725a693c21f90468a Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 23:53:39 2020 +0200 Testing PlaceAsset commit 2e7b49ad1a15173565c81e7eb8bb3f35b9f622a6 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 22:25:08 2020 +0200 Restructuring models commit eb7d03b08b326f9115e70d0fd9ed5d0fc229a362 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 22:01:54 2020 +0200 Abstract class Expireable commit 2b51e741bb5734c5a578beeadef7819fe58b2223 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 21:54:07 2020 +0200 Abstract Model for PlaceAsset (i.e. Photoalbums)
2020-09-22 21:56:51 +02:00
from lostplaces.models.abstract_models import Submittable, Taggable, Mapable
from easy_thumbnails.fields import ThumbnailerImageField
from easy_thumbnails.files import get_thumbnailer
class Place(Submittable, Taggable, Mapable):
"""
Place defines a lost place (location, name, description etc.).
"""
2020-10-11 02:07:14 +02:00
location = models.CharField(
max_length=50,
verbose_name=_('Location'),
)
description = models.TextField(
help_text=_('Description of the place: e.g. how to get there, where to be careful, the place\'s history...'),
verbose_name=_('Description'),
)
Squashed commit of the following: commit 0d62e72d72922a84e41c9f2cc21977b794784d1c Merge: 79fee63 85f2a81 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:55:18 2020 +0200 Merge branch 'develop' into refactor/models commit 79fee631d7ac28509067ecdd74078f1a2f6e0be2 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:54:32 2020 +0200 Updating references for related name commit 8e07e79df2de2601f2e2eadfdd37eb7c719c51b0 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:53:31 2020 +0200 Generating of related names fix commit 5fd804f37a805ae4707e13c3d941bdde3660afea Merge: 8cc1d3e 3b526c9 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:01:48 2020 +0200 Merge branch 'develop' into refactor/models commit 8cc1d3e690211dba6451e86569f00078b23e0621 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 20:21:08 2020 +0200 Tests commit 7c0591e5397f892b1f6fb80725a693c21f90468a Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 23:53:39 2020 +0200 Testing PlaceAsset commit 2e7b49ad1a15173565c81e7eb8bb3f35b9f622a6 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 22:25:08 2020 +0200 Restructuring models commit eb7d03b08b326f9115e70d0fd9ed5d0fc229a362 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 22:01:54 2020 +0200 Abstract class Expireable commit 2b51e741bb5734c5a578beeadef7819fe58b2223 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 21:54:07 2020 +0200 Abstract Model for PlaceAsset (i.e. Photoalbums)
2020-09-22 21:56:51 +02:00
def get_absolute_url(self):
return reverse('place_detail', kwargs={'pk': self.pk})
@classmethod
# Get center position of LP-geocoordinates.
def average_latlon(cls, place_list):
amount = len(place_list)
# Init fill values to prevent None
longitude = 0
latitude = 0
if amount > 0:
for place in place_list:
longitude += place.longitude
latitude += place.latitude
return {'latitude':latitude / amount, 'longitude': longitude / amount}
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: place_pk-placename{-rnd_string}.jpg
"""
return 'places/' + str(instance.place.pk) + '-' + str(instance.place.name) + '.' + filename.split('.')[-1]
class PlaceAsset(Submittable):
"""
Assets to a place, i.e. images
"""
class Meta:
abstract = True
place = models.ForeignKey(
Place,
on_delete=models.CASCADE,
related_name='%(class)ss',
null=True
)
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.
"""
2020-10-11 02:07:14 +02:00
description = models.TextField(
blank=True,
verbose_name=_('Description'),
)
Squashed commit of the following: commit 0d62e72d72922a84e41c9f2cc21977b794784d1c Merge: 79fee63 85f2a81 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:55:18 2020 +0200 Merge branch 'develop' into refactor/models commit 79fee631d7ac28509067ecdd74078f1a2f6e0be2 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:54:32 2020 +0200 Updating references for related name commit 8e07e79df2de2601f2e2eadfdd37eb7c719c51b0 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:53:31 2020 +0200 Generating of related names fix commit 5fd804f37a805ae4707e13c3d941bdde3660afea Merge: 8cc1d3e 3b526c9 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:01:48 2020 +0200 Merge branch 'develop' into refactor/models commit 8cc1d3e690211dba6451e86569f00078b23e0621 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 20:21:08 2020 +0200 Tests commit 7c0591e5397f892b1f6fb80725a693c21f90468a Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 23:53:39 2020 +0200 Testing PlaceAsset commit 2e7b49ad1a15173565c81e7eb8bb3f35b9f622a6 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 22:25:08 2020 +0200 Restructuring models commit eb7d03b08b326f9115e70d0fd9ed5d0fc229a362 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 22:01:54 2020 +0200 Abstract class Expireable commit 2b51e741bb5734c5a578beeadef7819fe58b2223 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 21:54:07 2020 +0200 Abstract Model for PlaceAsset (i.e. Photoalbums)
2020-09-22 21:56:51 +02:00
filename = ThumbnailerImageField(
upload_to=generate_image_upload_path,
resize_source=dict(size=(2560, 2560),
sharpen=True),
2020-10-11 02:07:14 +02:00
verbose_name=_('Filename(s)'),
help_text=_('Optional: One or more images to upload')
Squashed commit of the following: commit 0d62e72d72922a84e41c9f2cc21977b794784d1c Merge: 79fee63 85f2a81 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:55:18 2020 +0200 Merge branch 'develop' into refactor/models commit 79fee631d7ac28509067ecdd74078f1a2f6e0be2 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:54:32 2020 +0200 Updating references for related name commit 8e07e79df2de2601f2e2eadfdd37eb7c719c51b0 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:53:31 2020 +0200 Generating of related names fix commit 5fd804f37a805ae4707e13c3d941bdde3660afea Merge: 8cc1d3e 3b526c9 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 21:01:48 2020 +0200 Merge branch 'develop' into refactor/models commit 8cc1d3e690211dba6451e86569f00078b23e0621 Author: reverend <reverend@reverend2048.de> Date: Tue Sep 22 20:21:08 2020 +0200 Tests commit 7c0591e5397f892b1f6fb80725a693c21f90468a Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 23:53:39 2020 +0200 Testing PlaceAsset commit 2e7b49ad1a15173565c81e7eb8bb3f35b9f622a6 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 22:25:08 2020 +0200 Restructuring models commit eb7d03b08b326f9115e70d0fd9ed5d0fc229a362 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 22:01:54 2020 +0200 Abstract class Expireable commit 2b51e741bb5734c5a578beeadef7819fe58b2223 Author: reverend <reverend@reverend2048.de> Date: Fri Sep 18 21:54:07 2020 +0200 Abstract Model for PlaceAsset (i.e. Photoalbums)
2020-09-22 21:56:51 +02:00
)
place = models.ForeignKey(
Place,
on_delete=models.CASCADE,
related_name='placeimages'
)
def __str__(self):
"""
Returning the name of the corresponding place + id
of this image as textual representation of this instance
"""
return 'Image ' + str(self.pk)
# These two auto-delete files from filesystem when they are unneeded:
@receiver(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(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)