Another Step in the Quest to clean up the code base.
This commit is contained in:
@@ -1,30 +1,24 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
"""ORM Models for Users that could be members and user activation requests."""
|
||||
|
||||
import hashlib
|
||||
import random
|
||||
from datetime import timedelta
|
||||
from os import path
|
||||
import random
|
||||
import hashlib
|
||||
|
||||
from django.utils import timezone
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.db import models
|
||||
from django.utils import timezone
|
||||
from django.utils.translation import ugettext as _
|
||||
from . import PAID_MEMBERSHIP_GROUP
|
||||
from easy_thumbnails.fields import ThumbnailerImageField
|
||||
|
||||
from utils import OverwriteStorage
|
||||
|
||||
|
||||
GENDER_CHOICES = (
|
||||
('m', _('Male')),
|
||||
('f', _('Female')),
|
||||
)
|
||||
from . import PAID_MEMBERSHIP_GROUP, GENDER_CHOICES
|
||||
|
||||
|
||||
def get_upload_path(instance, filename):
|
||||
"""
|
||||
Erstellt den Pfad und Dateinamen für den Upload dynmisch.
|
||||
"""Erstellt den Pfad und Dateinamen für den Upload dynmisch.
|
||||
|
||||
@param instance: The Membership Object for the uploaded image
|
||||
@param filename: the filename of the uploaded image
|
||||
@@ -34,9 +28,7 @@ def get_upload_path(instance, filename):
|
||||
|
||||
|
||||
class ActivationManager(models.Manager):
|
||||
"""
|
||||
Manages pending user registrations
|
||||
"""
|
||||
"""Manages pending user registrations."""
|
||||
|
||||
def activate(self, activation_key):
|
||||
"""
|
||||
@@ -61,17 +53,17 @@ class ActivationManager(models.Manager):
|
||||
return False
|
||||
|
||||
def create_pending_registration(self, user):
|
||||
"""
|
||||
creates a PendingActivation instance with an random activation key.
|
||||
"""Creates a PendingActivation instance with an random activation key.
|
||||
@param user: the user that requests activation.
|
||||
"""
|
||||
salt = str(user.pk * random.random()) + \
|
||||
user.registration_date.isoformat()
|
||||
activation_key = hashlib.sha1(salt.encode('utf-8')).hexdigest()
|
||||
|
||||
user.registration_date.isoformat()
|
||||
activation_key = hashlib.sha1(salt.encode()).hexdigest()
|
||||
return self.create(user=user, activation_key=activation_key)
|
||||
|
||||
def expired(self):
|
||||
"""Return all ActivationRequest that haven't been activated and
|
||||
whose valid activation time has been expired"""
|
||||
timespan = timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS)
|
||||
expiration_date = timezone.now() - timespan
|
||||
return self.filter(
|
||||
@@ -101,20 +93,24 @@ class ActivationRequest(models.Model):
|
||||
return _("user registration for %s") % self.user
|
||||
|
||||
def activate(self):
|
||||
"""Activate the user account and delete this ActivationRequest"""
|
||||
self.user.is_active = True
|
||||
self.user.save()
|
||||
self.delete()
|
||||
|
||||
@property
|
||||
def expiration_date(self):
|
||||
"""The datetime until this user activation request is valid."""
|
||||
timespan = timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS)
|
||||
return self.user.date_joined + timespan
|
||||
|
||||
@property
|
||||
def email(self):
|
||||
"""The email adress of the user that should be activated."""
|
||||
return self.user.email
|
||||
|
||||
def expired(self):
|
||||
"""True if user is inactive and expiration_date is in the past."""
|
||||
if self.user.is_active:
|
||||
return False
|
||||
elif timezone.now() >= self.expiration_date:
|
||||
@@ -126,24 +122,28 @@ class ActivationRequest(models.Model):
|
||||
|
||||
@property
|
||||
def first_name(self):
|
||||
"""First name of the user that should be activated."""
|
||||
return self.user.first_name
|
||||
|
||||
@property
|
||||
def last_name(self):
|
||||
"""Last name of the user that should be activated."""
|
||||
return self.user.last_name
|
||||
|
||||
@property
|
||||
def registration_date(self):
|
||||
"""Date when the user joined/registered his account."""
|
||||
return self.user.date_joined
|
||||
|
||||
@property
|
||||
def username(self):
|
||||
"""Loing name of the user that should be activated."""
|
||||
return self.user.username
|
||||
|
||||
|
||||
class Membership(AbstractUser):
|
||||
# user = models.OneToOneField(settings.AUTH_USER_MODEL)
|
||||
# nickname = models.SlugField(_('Nickname'), unique=True)
|
||||
"""An expanded Django User with additional data to manage club memberships.
|
||||
"""
|
||||
gender = models.CharField(
|
||||
_("Gender"),
|
||||
max_length=1,
|
||||
@@ -151,9 +151,6 @@ class Membership(AbstractUser):
|
||||
blank=True,
|
||||
null=True
|
||||
)
|
||||
# first_name = models.CharField(_("Given Name"), max_length=30)
|
||||
# last_name = models.CharField(_("Last Name"), max_length=30)
|
||||
# email = models.EmailField(_('Email'), unique=True)
|
||||
website = models.URLField(blank=True)
|
||||
avatar = ThumbnailerImageField(
|
||||
upload_to=get_upload_path,
|
||||
@@ -207,11 +204,13 @@ class Membership(AbstractUser):
|
||||
default=False,
|
||||
help_text=_('This person has paid the membership fee.')
|
||||
)
|
||||
|
||||
# comment = models.TextField(blank=True)
|
||||
# objects = MembershipManager()
|
||||
|
||||
class Meta(object):
|
||||
ordering = ('username', )
|
||||
"""To manage object ordering and dispalynames on the admin interface."""
|
||||
ordering = ('username',)
|
||||
swappable = 'AUTH_USER_MODEL'
|
||||
verbose_name = _('Membership')
|
||||
verbose_name_plural = _('Memberships')
|
||||
@@ -220,12 +219,16 @@ class Membership(AbstractUser):
|
||||
return self.username
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse(
|
||||
'membership-details',
|
||||
kwargs={'username': self.username}
|
||||
)
|
||||
""":return the URL for this Membership DetailView"""
|
||||
return reverse('membership-details', kwargs={'username': self.username})
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
"""Save the Useraccount, and add him tho the "Paid Membership" group
|
||||
if he activated the "membership" checkbox and has been validated.
|
||||
|
||||
:param args: passed through the save() method from django
|
||||
:param kwargs: passed through the save() method from django
|
||||
"""
|
||||
super(Membership, self).save(*args, **kwargs)
|
||||
if self.confirmed:
|
||||
self.groups.add(PAID_MEMBERSHIP_GROUP)
|
||||
|
||||
Reference in New Issue
Block a user