2020-07-31 14:55:26 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
''' (Data)models which describe the structure of data to be saved into database. '''
|
|
|
|
|
2020-07-27 21:32:59 +02:00
|
|
|
import os
|
|
|
|
import uuid
|
|
|
|
|
2020-07-19 00:13:49 +02:00
|
|
|
from django.db import models
|
2020-07-27 21:32:59 +02:00
|
|
|
from django.dispatch import receiver
|
2020-07-28 20:15:34 +02:00
|
|
|
from django.contrib.auth.models import AbstractUser
|
2020-08-03 19:14:13 +02:00
|
|
|
from easy_thumbnails.fields import ThumbnailerImageField
|
2020-07-19 00:13:49 +02:00
|
|
|
|
|
|
|
# Create your models here.
|
|
|
|
|
2020-07-28 20:15:34 +02:00
|
|
|
class Explorer(AbstractUser):
|
2020-08-03 19:14:13 +02:00
|
|
|
"""
|
|
|
|
Custom user model
|
|
|
|
Addtional fields wbd
|
|
|
|
"""
|
2020-07-28 20:15:34 +02:00
|
|
|
|
2020-08-03 19:14:13 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.username
|
2020-07-28 20:15:34 +02:00
|
|
|
|
2020-08-01 13:11:07 +02:00
|
|
|
class Voucher(models.Model):
|
2020-08-03 19:14:13 +02:00
|
|
|
"""
|
|
|
|
Vouchers are authorization tokens to allow the registration of new users.
|
|
|
|
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-03 19:14:13 +02:00
|
|
|
code = models.CharField(unique=True, max_length=10)
|
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
expires = models.DateField()
|
2020-08-01 13:11:07 +02:00
|
|
|
|
2020-08-03 19:14:13 +02:00
|
|
|
def __str__(self):
|
|
|
|
return "Voucher " + str(self.pk)
|
2020-08-01 13:11:07 +02:00
|
|
|
|
2020-07-19 00:13:49 +02:00
|
|
|
class Place (models.Model):
|
2020-08-03 19:14:13 +02:00
|
|
|
"""
|
|
|
|
Place defines a lost place (location, name, description etc.).
|
|
|
|
"""
|
|
|
|
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
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='places'
|
|
|
|
)
|
|
|
|
location = models.CharField(max_length=50)
|
|
|
|
latitude = models.FloatField()
|
|
|
|
longitude = models.FloatField()
|
|
|
|
description = models.TextField()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2020-07-27 20:34:18 +02:00
|
|
|
|
2020-07-30 13:09:00 +02:00
|
|
|
def generate_image_upload_path(instance, filename):
|
2020-08-03 19:14:13 +02:00
|
|
|
"""
|
|
|
|
Callback for generating path for uploaded images.
|
|
|
|
"""
|
2020-08-01 11:25:26 +02:00
|
|
|
|
2020-08-03 19:14:13 +02:00
|
|
|
return 'places/' + str(uuid.uuid4())+'.'+filename.split('.')[-1]
|
2020-07-30 13:09:00 +02:00
|
|
|
|
2020-07-27 20:34:18 +02:00
|
|
|
class PlaceImage (models.Model):
|
2020-08-03 19:14:13 +02:00
|
|
|
"""
|
|
|
|
PlaceImage defines an image file object that points to a file in uploads/.
|
|
|
|
Intermediate image sizes are generated as defined in SIZES.
|
|
|
|
PlaceImage references a Place to which it belongs.
|
|
|
|
"""
|
|
|
|
|
|
|
|
SIZES=(
|
|
|
|
{'code': 'thumbnail', 'wxh': '390x390'},
|
|
|
|
{'code': 'hero', 'wxh': '700x700'},
|
|
|
|
{'code': 'large', 'wxh': '1920x1920'}
|
|
|
|
)
|
|
|
|
|
|
|
|
description = models.TextField(blank=True)
|
|
|
|
filename = ThumbnailerImageField(upload_to=generate_image_upload_path)
|
|
|
|
place = models.ForeignKey(
|
|
|
|
Place,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name='images'
|
|
|
|
)
|
|
|
|
submitted_by = models.ForeignKey(
|
|
|
|
Explorer,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
related_name='images'
|
|
|
|
)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"""
|
|
|
|
Returning the name of the corresponding place + id
|
|
|
|
of this image as textual represntation of this instance
|
|
|
|
"""
|
|
|
|
|
|
|
|
return ' '.join([self.place.name, str(self.pk)])
|
2020-07-27 21:32:59 +02:00
|
|
|
|
|
|
|
# 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):
|
2020-08-03 19:14:13 +02:00
|
|
|
"""
|
|
|
|
Deletes file from filesystem
|
|
|
|
when corresponding `PlaceImage` object is deleted.
|
|
|
|
"""
|
|
|
|
if instance.filename:
|
|
|
|
if os.path.isfile(instance.filename.path):
|
|
|
|
os.remove(instance.filename.path)
|
2020-07-27 21:32:59 +02:00
|
|
|
|
|
|
|
@receiver(models.signals.pre_save, sender=PlaceImage)
|
|
|
|
def auto_delete_file_on_change(sender, instance, **kwargs):
|
2020-08-03 19:14:13 +02:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
|
|
new_file = instance.filename
|
|
|
|
if not old_file == new_file:
|
|
|
|
if os.path.isfile(old_file.path):
|
|
|
|
os.remove(old_file.path)
|