2020-09-22 21:56:51 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
'''
|
|
|
|
(Data)models which describe the structure of data to be saved into
|
|
|
|
database.
|
|
|
|
'''
|
|
|
|
|
2020-12-24 17:24:02 +01:00
|
|
|
import os
|
2020-09-22 21:56:51 +02:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
from django.contrib.auth.models import User
|
2020-12-25 15:42:02 +01:00
|
|
|
from django.db.models.signals import post_save, pre_save
|
2020-09-22 21:56:51 +02:00
|
|
|
from django.dispatch import receiver
|
2022-09-17 18:07:29 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2020-09-22 21:56:51 +02:00
|
|
|
|
|
|
|
from lostplaces.models.abstract_models import Expireable
|
2020-12-24 14:48:11 +01:00
|
|
|
from lostplaces.models.place import Place
|
2020-09-22 21:56:51 +02:00
|
|
|
|
2020-12-24 17:24:02 +01:00
|
|
|
from easy_thumbnails.fields import ThumbnailerImageField
|
|
|
|
from easy_thumbnails.files import get_thumbnailer
|
|
|
|
|
|
|
|
def generate_profile_image_filename(instance, filename):
|
|
|
|
"""
|
|
|
|
Callback for generating filename for uploaded explorer profile images.
|
|
|
|
Returns filename as: explorer_pk-username.jpg
|
|
|
|
"""
|
|
|
|
|
2020-12-24 18:14:40 +01:00
|
|
|
return 'explorers/' + str(instance.user.pk) + '-' + str(instance.user.username) + '.' + filename.split('.')[-1]
|
2020-12-24 17:24:02 +01:00
|
|
|
|
2021-10-01 09:28:45 +02:00
|
|
|
EXPLORER_LEVELS = (
|
|
|
|
(1, 'Newbie'),
|
|
|
|
(2, 'Scout'),
|
|
|
|
(3, 'Explorer'),
|
|
|
|
(4, 'Journalist'),
|
|
|
|
(5, 'Housekeeper')
|
|
|
|
)
|
|
|
|
|
2020-09-22 21:56:51 +02:00
|
|
|
class Explorer(models.Model):
|
|
|
|
"""
|
2020-12-25 01:41:09 +01:00
|
|
|
Profile that is linked to the Django user.
|
2020-09-22 21:56:51 +02:00
|
|
|
Every user has a profile.
|
2020-12-25 01:41:09 +01:00
|
|
|
Provides additional attributes for user profile.
|
2020-09-22 21:56:51 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
user = models.OneToOneField(
|
|
|
|
User,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name='explorer'
|
|
|
|
)
|
2020-12-24 18:53:30 +01:00
|
|
|
bio = models.TextField(
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
verbose_name=_('Biography / Description'),
|
|
|
|
help_text=_('Describe yourself, your preferences, etc. in a few sentences.')
|
|
|
|
)
|
2020-12-24 17:24:02 +01:00
|
|
|
profile_image = ThumbnailerImageField(
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
upload_to=generate_profile_image_filename,
|
|
|
|
resize_source=dict(size=(400, 400),
|
|
|
|
sharpen=True),
|
|
|
|
verbose_name=_('Profile image'),
|
2020-12-25 01:31:43 +01:00
|
|
|
help_text=_('Optional profile image for display in Explorer profile')
|
2020-12-24 17:24:02 +01:00
|
|
|
)
|
2020-12-24 15:56:02 +01:00
|
|
|
favorite_places = models.ManyToManyField(
|
|
|
|
Place,
|
2020-12-24 19:25:37 +01:00
|
|
|
related_name='explorer_favorites',
|
2020-12-24 16:42:27 +01:00
|
|
|
verbose_name='Explorers favorite places',
|
|
|
|
blank=True
|
2020-12-24 15:56:02 +01:00
|
|
|
)
|
2020-12-25 19:32:54 +01:00
|
|
|
visited_places = models.ManyToManyField(
|
|
|
|
Place,
|
|
|
|
related_name='explorer_visits',
|
|
|
|
verbose_name='Explorers visited places',
|
|
|
|
blank=True
|
|
|
|
)
|
2020-12-24 14:48:11 +01:00
|
|
|
|
2021-10-01 09:28:45 +02:00
|
|
|
level = models.IntegerField(
|
|
|
|
default=1,
|
|
|
|
choices=EXPLORER_LEVELS
|
|
|
|
)
|
|
|
|
|
2022-09-24 12:10:19 +02:00
|
|
|
def get_place_list_to_display(self):
|
|
|
|
'''
|
|
|
|
Gets the list of places to show on the homepage
|
|
|
|
and the list views
|
|
|
|
'''
|
|
|
|
if self.user.is_superuser:
|
2022-09-25 18:03:48 +02:00
|
|
|
return Place.objects.filter(mode='live').order_by('submitted_when')
|
2022-09-24 12:10:19 +02:00
|
|
|
|
2022-09-25 18:03:48 +02:00
|
|
|
return (Place.objects.filter(
|
2022-09-24 12:10:19 +02:00
|
|
|
level__lte=self.level,
|
|
|
|
mode='live'
|
|
|
|
) | Place.objects.filter(
|
|
|
|
submitted_by=self,
|
|
|
|
mode='live'
|
2022-09-25 18:03:48 +02:00
|
|
|
)).order_by('submitted_when')
|
2022-09-24 12:10:19 +02:00
|
|
|
|
2021-10-01 22:38:44 +02:00
|
|
|
def get_places_eligible_to_see(self):
|
2021-10-02 00:03:45 +02:00
|
|
|
if self.user.is_superuser:
|
2022-09-24 12:10:19 +02:00
|
|
|
return Place.objects.filter(mode='live')
|
2021-10-02 00:03:45 +02:00
|
|
|
return Place.objects.all().filter(level__lte=self.level) | self.places.all()
|
2021-10-01 22:38:44 +02:00
|
|
|
|
2021-10-01 23:41:34 +02:00
|
|
|
def is_eligible_to_see(self, place):
|
2021-10-01 23:44:46 +02:00
|
|
|
return (
|
|
|
|
self.user.is_superuser or
|
|
|
|
place.submitted_by == self or
|
|
|
|
place in self.get_places_eligible_to_see()
|
|
|
|
)
|
2021-10-01 23:41:34 +02:00
|
|
|
|
2022-09-25 18:03:48 +02:00
|
|
|
def get_drafts(self):
|
|
|
|
return Place.objects.filter(
|
|
|
|
submitted_by=self,
|
|
|
|
mode='draft'
|
|
|
|
)
|
|
|
|
|
2020-09-22 21:56:51 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.user.username
|
2020-12-25 13:19:34 +01:00
|
|
|
|
2020-09-22 21:56:51 +02:00
|
|
|
@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-12-25 15:42:02 +01:00
|
|
|
@receiver(pre_save, sender=Explorer)
|
|
|
|
def auto_delete_file_on_change(sender, instance, **kwargs):
|
|
|
|
"""
|
|
|
|
Deletes old file from filesystem
|
|
|
|
when corresponding `Explorer` object is updated
|
|
|
|
with new file.
|
|
|
|
"""
|
|
|
|
if not instance.pk:
|
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
|
|
|
old_file = Explorer.objects.get(pk=instance.pk).profile_image
|
|
|
|
except Explorer.DoesNotExist:
|
|
|
|
return False
|
|
|
|
new_file = instance.profile_image
|
|
|
|
if not old_file == new_file:
|
|
|
|
old_file.delete(save=False)
|
|
|
|
|
2020-09-22 21:56:51 +02:00
|
|
|
class Voucher(Expireable):
|
|
|
|
"""
|
2020-12-24 15:13:31 +01:00
|
|
|
Vouchers are authorization tokens to allow the registration of new users.
|
2020-09-22 21:56:51 +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-12-24 15:13:31 +01:00
|
|
|
|
|
|
|
created_when = models.DateTimeField(auto_now_add=True)
|
|
|
|
expires_when = models.DateTimeField()
|
2020-09-22 21:56:51 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
code = models.CharField(unique=True, max_length=30)
|
2020-10-04 22:12:44 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def valid(self):
|
|
|
|
return not self.is_expired
|
2020-09-22 21:56:51 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "Voucher " + str(self.code)
|