Преглед изворни кода

HUE-8758 [useradmin] Adding custom data field to user profile

Romain пре 5 година
родитељ
комит
207d09b1ed

+ 20 - 0
apps/useradmin/src/useradmin/migrations/0002_userprofile_json_data.py

@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.20 on 2019-12-29 05:32
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('useradmin', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='userprofile',
+            name='json_data',
+            field=models.TextField(default='{}'),
+        ),
+    ]

+ 15 - 0
apps/useradmin/src/useradmin/models.py

@@ -88,6 +88,7 @@ class UserProfile(models.Model):
   creation_method = models.CharField(editable=True, null=False, max_length=64, default=CreationMethod.HUE.name)
   first_login = models.BooleanField(default=True, verbose_name=_t('First Login'), help_text=_t('If this is users first login.'))
   last_activity = models.DateTimeField(auto_now=True, db_index=True)
+  json_data = models.TextField(default='{}')
 
   def get_groups(self):
     return self.user.groups.all()
@@ -126,6 +127,18 @@ class UserProfile(models.Model):
     else:
       raise PopupException(_t("You do not have permissions to %(description)s.") % {'description': perm.description})
 
+  @property
+  def data(self):
+    if not self.json_data:
+      self.json_data = json.dumps({})
+    return json.loads(self.json_data)
+
+  @data.setter
+  def data(self, val):
+    data_dict = self.data
+    data_dict.update(val)
+    self.data = json.dumps(data_dict)
+
 
 def get_profile(user):
   """
@@ -214,6 +227,7 @@ def get_default_user_group(**kwargs):
 
   return group
 
+
 def update_app_permissions(**kwargs):
   """
   Inserts missing permissions into the database table.
@@ -354,5 +368,6 @@ def install_sample_user():
 
   return user
 
+
 def orm_user_lookup():
   return 'email' if ENABLE_ORGANIZATIONS.get() else 'username'