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
|
|
|
|
from django.db.models.signals import post_save
|
|
|
|
from django.dispatch import receiver
|
2020-12-24 17:24:02 +01:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2020-09-22 21:56:51 +02:00
|
|
|
|
|
|
|
from lostplaces.models.abstract_models import Expireable
|
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
|
|
|
return 'explorers/' + str(instance.explorer.pk) + '-' + str(instance.explorer.username) + '.' + filename.split('.')[-1]
|
|
|
|
|
2020-09-22 21:56:51 +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'
|
|
|
|
)
|
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'),
|
|
|
|
help_text=_('Optional profile pic for display in explorer profile')
|
|
|
|
)
|
2020-09-22 21:56:51 +02:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
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)
|