models.py 5.48 KB
Newer Older
André Anjos's avatar
André Anjos committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

###############################################################################
#                                                                             #
# Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/           #
# Contact: beat.support@idiap.ch                                              #
#                                                                             #
# This file is part of the beat.web module of the BEAT platform.              #
#                                                                             #
# Commercial License Usage                                                    #
# Licensees holding valid commercial BEAT licenses may use this file in       #
# accordance with the terms contained in a written agreement between you      #
# and Idiap. For further information contact tto@idiap.ch                     #
#                                                                             #
# Alternatively, this file may be used under the terms of the GNU Affero      #
# Public License version 3 as published by the Free Software and appearing    #
# in the file LICENSE.AGPL included in the packaging of this file.            #
# The BEAT platform is distributed in the hope that it will be useful, but    #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  #
# or FITNESS FOR A PARTICULAR PURPOSE.                                        #
#                                                                             #
# You should have received a copy of the GNU Affero Public License along      #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/.           #
#                                                                             #
###############################################################################

from django.db import models
from django.contrib.auth.models import User
30 31
from django.db.models.signals import post_save
from django.dispatch import receiver
André Anjos's avatar
André Anjos committed
32

33 34 35
import random
import string

André Anjos's avatar
André Anjos committed
36

37 38 39 40 41 42
class AccountSettingsManager(models.Manager):
    def get_by_natural_key(self, owner):
        return self.get(owner=owner)


class AccountSettings(models.Model):
André Anjos's avatar
André Anjos committed
43 44 45 46 47 48 49 50
    class Meta:
        verbose_name_plural = "account settings"

    owner = models.OneToOneField(User)
    daily_summary = models.BooleanField(default=True)
    experiment_mail_notifications_enabled = models.BooleanField(default=True)
    database_notifications_enabled = models.BooleanField(default=True)
    environment_notifications_enabled = models.BooleanField(default=True)
51

52 53 54 55 56
    objects = AccountSettingsManager()

    def natural_key(self):
        return (self.owner,)

57

58 59
class SupervisionTrack(models.Model):

60
    # _____ Fields __________
61

62 63 64 65 66 67 68 69 70
    supervisee = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name="supervisors"
    )
    supervisor = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name="supervisees"
    )
    is_valid = models.BooleanField(default=False)
    start_date = models.DateTimeField(null=True, blank=True)
    expiration_date = models.DateTimeField(null=True, blank=True)
71
    last_validation_date = models.DateTimeField(null=True, blank=True)
72
    supervision_key = models.CharField(max_length=40, null=True, blank=True)
73 74

    def __unicode__(self):
75 76 77 78 79 80 81 82 83 84
        return u"Supervisor: %s, Supervisee, %s, Validity: %s" % (
            self.supervisor.username,
            self.supervisee.username,
            self.is_valid,
        )


class ProfileManager(models.Manager):
    def get_by_natural_key(self, user):
        return self.get(user=user)
85 86


87
class Profile(models.Model):
88 89 90 91 92 93 94 95 96 97 98
    # _____ Constants __________

    # New account creation 'N'/'W'
    # Supervisor acceptance/rejection 'A'/'R'
    # Yearly revalidation/blockage  'Y'/'B'
    NEWUSER = "N"
    WAITINGVALIDATION = "W"
    ACCEPTED = "A"
    REJECTED = "R"
    YEARREVALIDATION = "Y"
    BLOCKED = "B"
99 100

    PROFILE_STATUS = (
101 102 103 104 105 106
        (NEWUSER, "New User"),
        (WAITINGVALIDATION, "Waiting Validation"),
        (ACCEPTED, "Accepted"),
        (REJECTED, "Rejected"),
        (YEARREVALIDATION, "Yearly revalidation"),
        (BLOCKED, "Blocked no supervisor"),
107 108
    )

109
    # _____ Fields __________
110 111 112 113 114

    # This field is required.
    user = models.OneToOneField(User, on_delete=models.CASCADE)

    # Other fields here
115
    status = models.CharField(max_length=1, choices=PROFILE_STATUS, default=BLOCKED)
116
    is_supervisor = models.BooleanField(default=False)
117 118 119
    supervision = models.ManyToManyField(
        SupervisionTrack, related_name="profiles", blank=True
    )
120 121
    supervision_key = models.CharField(max_length=40, null=True, blank=True)
    registration_date = models.DateTimeField(null=True, blank=True)
122
    rejection_date = models.DateTimeField(null=True, blank=True)
123

124 125
    objects = ProfileManager()

126
    def __unicode__(self):
127
        return u"User: %s" % self.user.username
128

129
    def _generate_current_supervision_key(self):
130 131 132 133 134 135 136 137 138
        length = 40
        return "".join(
            random.choice(string.ascii_letters + string.digits)  # nosec
            for _ in range(length)
        )

    def natural_key(self):
        return (self.user,)

139

140 141 142 143 144 145 146
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)
    else:
        return

147

148 149 150 151
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    player, created = Profile.objects.get_or_create(user=instance)
    instance.profile.save()