From ba42bdaf37057334f55b840f9594c9677f8153fb Mon Sep 17 00:00:00 2001
From: Flavio Tarsetti <flavio.tarsetti@idiap.ch>
Date: Mon, 8 May 2017 17:02:45 +0200
Subject: [PATCH] [accounts/navigation/settings] added Profile model to User
 and its related admin views and updated settings to link with user

---
 beat/web/accounts/models.py   | 42 +++++++++++++++++++++++++++++++++++
 beat/web/navigation/admin.py  | 23 +++++++++++++++++++
 beat/web/settings/settings.py |  2 ++
 3 files changed, 67 insertions(+)

diff --git a/beat/web/accounts/models.py b/beat/web/accounts/models.py
index 79b0629bd..a61ed582a 100644
--- a/beat/web/accounts/models.py
+++ b/beat/web/accounts/models.py
@@ -27,6 +27,8 @@
 
 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
 
 class AccountSettings(models.Model):
 
@@ -39,3 +41,43 @@ class AccountSettings(models.Model):
     experiment_mail_notifications_enabled = models.BooleanField(default=True)
     database_notifications_enabled = models.BooleanField(default=True)
     environment_notifications_enabled = models.BooleanField(default=True)
+
+class Profile(models.Model):
+    #_____ Constants __________
+
+    ACCEPTED          = 'A'
+    REJECTED          = 'R'
+    WAITINGVALIDATION = 'W'
+    BLOCKED           = 'B'
+
+    PROFILE_STATUS = (
+        (ACCEPTED,    'Accepted'),
+        (REJECTED,    'Rejected'),
+        (WAITINGVALIDATION, 'Waiting Validation'),
+        (BLOCKED, 'Blocked'),
+    )
+
+    #_____ Fields __________
+
+    # This field is required.
+    user = models.OneToOneField(User, on_delete=models.CASCADE)
+
+    # Other fields here
+    status       = models.CharField(max_length=1, choices=PROFILE_STATUS, default=BLOCKED)
+    is_godfather = models.BooleanField(default=False)
+    supervisees  = models.ManyToManyField(User, related_name='users', blank=True)
+
+    def __unicode__(self):
+      return u'User: %s' % self.user.username
+
+@receiver(post_save, sender=User)
+def create_user_profile(sender, instance, created, **kwargs):
+    if created:
+        Profile.objects.create(user=instance)
+    else:
+        return
+
+@receiver(post_save, sender=User)
+def save_user_profile(sender, instance, **kwargs):
+    player, created = Profile.objects.get_or_create(user=instance)
+    instance.profile.save()
diff --git a/beat/web/navigation/admin.py b/beat/web/navigation/admin.py
index 924827c59..23cf9bafa 100644
--- a/beat/web/navigation/admin.py
+++ b/beat/web/navigation/admin.py
@@ -31,6 +31,7 @@ from django.contrib.auth.models import User
 
 from .models import Agreement
 from ..accounts.models import AccountSettings
+from ..accounts.models import Profile
 
 
 #----------------------------------------------------------
@@ -50,6 +51,13 @@ class AccountSettingsInline(admin.StackedInline):
 #----------------------------------------------------------
 
 
+class ProfileInline(admin.StackedInline):
+    model = Profile
+
+
+#----------------------------------------------------------
+
+
 class UserAdmin(UserAdmin):
 
     def agreement_number(self, obj):
@@ -62,6 +70,17 @@ class UserAdmin(UserAdmin):
             int(obj.accountsettings.database_notifications_enabled) + \
             int(obj.accountsettings.environment_notifications_enabled)
 
+    def godfather(self, obj):
+        return obj.profile.is_godfather
+
+    def status(self, obj):
+        return obj.profile.status
+
+    def supervisees(self, obj):
+        return obj.profile.supervisees
+
+
+
     list_display = (
         'username',
         'is_staff',
@@ -69,6 +88,9 @@ class UserAdmin(UserAdmin):
         'notifications',
         'agreement_number',
         'last_login',
+        'godfather',
+        'status',
+        'supervisees',
     )
 
     ordering = (
@@ -80,6 +102,7 @@ class UserAdmin(UserAdmin):
     inlines = (
         AgreementInline,
         AccountSettingsInline,
+        ProfileInline,
     )
 
 admin.site.unregister(User)
diff --git a/beat/web/settings/settings.py b/beat/web/settings/settings.py
index 17a7b1d25..a5c324d86 100755
--- a/beat/web/settings/settings.py
+++ b/beat/web/settings/settings.py
@@ -429,6 +429,8 @@ AUTHENTICATION_BACKENDS = (
 
 ANONYMOUS_USER_ID = -1
 
+AUTH_PROFILE_MODULE = 'accounts.Profile'
+
 ##############################################################################
 #
 # REST FRAMEWORK PERMISSION CLASSES: http://www.django-rest-framework.org/api-guide/permissions/
-- 
GitLab