added uai field to user model and corresponding migrations + edited test oauth server to serve uai info for testing
authordurandn
Fri, 11 Mar 2016 16:28:23 +0100
changeset 29 23de98e32b3b
parent 28 2d67738519d6
child 30 3281d2349f6a
added uai field to user model and corresponding migrations + edited test oauth server to serve uai info for testing
oauth/oauth.py
server/src/metaeducation/admin.py
server/src/metaeducation/migrations/0002_user_uai.py
server/src/metaeducation/models.py
server/src/metaeducation/mtdc_oauth_provider/provider.py
server/src/metaeducation/mtdc_oauth_provider/views.py
server/src/metaeducation/settings/__init__.py
server/src/metaeducation/signals.py
--- a/oauth/oauth.py	Fri Mar 11 14:47:56 2016 +0100
+++ b/oauth/oauth.py	Fri Mar 11 16:28:23 2016 +0100
@@ -8,6 +8,7 @@
 from werkzeug.security import gen_salt
 from flask_oauthlib.provider import OAuth2Provider
 from settings.oauth_settings import OAuthSettings
+import uuid
 
 app = Flask(__name__, template_folder='templates')
 app.debug = True
@@ -21,9 +22,9 @@
 
 
 class User(db.Model):
-    id = db.Column(db.Integer, primary_key=True)
+    id = db.Column(db.String(256), primary_key=True)
     username = db.Column(db.String(40), unique=True)
-
+    uai = db.Column(db.String(40), default="uaidefault")
 
 class Client(db.Model):
     client_id = db.Column(db.String(40), primary_key=True)
@@ -128,7 +129,7 @@
         username = request.form.get('username')
         user = User.query.filter_by(username=username).first()
         if not user:
-            user = User(username=username)
+            user = User(id=str(uuid.uuid4()), username=username)
             db.session.add(user)
             db.session.commit()
         session['id'] = user.id
@@ -224,7 +225,7 @@
 @oauth.require_oauth()
 def user_info():
     user = request.oauth.user
-    return jsonify(id=user.id, displayName=user.username)
+    return jsonify(id=user.id, displayName=user.username, ENTPersonStructRattachUAI=user.uai)
 
 @app.route('/rest/oauth/validate/<token>')
 def validate_token(token):
--- a/server/src/metaeducation/admin.py	Fri Mar 11 14:47:56 2016 +0100
+++ b/server/src/metaeducation/admin.py	Fri Mar 11 16:28:23 2016 +0100
@@ -14,7 +14,7 @@
 
     class Meta:
         model = User
-        fields = ('external_id', 'username', 'first_name', 'last_name')
+        fields = ('external_id', 'username', 'uai', 'first_name', 'last_name')
 
     def clean_password2(self):
         # Check that the two password entries match
@@ -42,7 +42,7 @@
 
     class Meta:
         model = User
-        fields = ('external_id', 'username', 'first_name', 'last_name', 'password', 'is_active', 'is_staff')
+        fields = ('external_id', 'username', 'uai', 'first_name', 'last_name', 'password', 'is_active', 'is_staff')
 
 
 class UserAdmin(BaseUserAdmin):
@@ -53,11 +53,11 @@
     # The fields to be used in displaying the User model.
     # These override the definitions on the base UserAdmin
     # that reference specific fields on auth.User.
-    list_display = ('external_id', 'username', 'first_name', 'last_name', 'password', 'is_staff')
+    list_display = ('external_id', 'username', 'uai', 'first_name', 'last_name', 'password', 'is_staff')
     list_filter = ('is_staff',)
     fieldsets = (
         (None, {'fields': ('external_id', 'username')}),
-        ('Personal info', {'fields': ('username', 'first_name', 'last_name')}),
+        ('Personal info', {'fields': ('username', 'uai', 'first_name', 'last_name')}),
         ('Permissions', {'fields': ('is_staff',)}),
     )
     # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
@@ -65,7 +65,7 @@
     add_fieldsets = (
         (None, {
             'classes': ('wide',),
-            'fields': ('external_id', 'username', 'first_name', 'last_name', 'password1', 'password2')}
+            'fields': ('external_id', 'username', 'uai', 'first_name', 'last_name', 'password1', 'password2')}
         ),
     )
     search_fields = ('username',)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/src/metaeducation/migrations/0002_user_uai.py	Fri Mar 11 16:28:23 2016 +0100
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.9.1 on 2016-03-11 15:13
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('metaeducation', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='user',
+            name='uai',
+            field=models.CharField(blank=True, max_length=40),
+        ),
+    ]
--- a/server/src/metaeducation/models.py	Fri Mar 11 14:47:56 2016 +0100
+++ b/server/src/metaeducation/models.py	Fri Mar 11 16:28:23 2016 +0100
@@ -39,6 +39,7 @@
             ),
         ],
     )
+    uai = models.CharField(max_length=40, blank=True)
     first_name = models.CharField(_('first name'), max_length=30, blank=True)
     last_name = models.CharField(_('last name'), max_length=30, blank=True)
     date_joined = models.DateTimeField(default=timezone.now)
--- a/server/src/metaeducation/mtdc_oauth_provider/provider.py	Fri Mar 11 14:47:56 2016 +0100
+++ b/server/src/metaeducation/mtdc_oauth_provider/provider.py	Fri Mar 11 16:28:23 2016 +0100
@@ -18,7 +18,8 @@
     def extract_extra_data(self, data):
         return {
             "username": data.get(settings.MTDC_USERNAME_FIELD_NAME, ""),
-            "external_id": data.get(settings.MTDC_USERID_FIELD_NAME)
+            "external_id": data.get(settings.MTDC_USERID_FIELD_NAME),
+            "uai": data.get(settings.MTDC_UAI_FIELD_NAME)
         }
     
 providers.registry.register(MtdcProvider)
\ No newline at end of file
--- a/server/src/metaeducation/mtdc_oauth_provider/views.py	Fri Mar 11 14:47:56 2016 +0100
+++ b/server/src/metaeducation/mtdc_oauth_provider/views.py	Fri Mar 11 16:28:23 2016 +0100
@@ -60,13 +60,14 @@
     
     def new_user(self, request, sociallogin):
         if 'username' in sociallogin.account.extra_data:
-            user_queryset = get_user_model().objects.filter(username=sociallogin.account.extra_data['username'])
+            user_queryset = get_user_model().objects.filter(external_id=sociallogin.account.extra_data['external_id'], username=sociallogin.account.extra_data['username'])
             if user_queryset.exists():
                 user = user_queryset.first()
             else:
                 user = get_user_model()()
                 user.username = sociallogin.account.extra_data.get('username', '')
                 user.external_id = sociallogin.account.extra_data.get('external_id', '')
+                user.uai = sociallogin.account.extra_data.get('uai', '')
             return user
         else:
             return get_user_model()()
--- a/server/src/metaeducation/settings/__init__.py	Fri Mar 11 14:47:56 2016 +0100
+++ b/server/src/metaeducation/settings/__init__.py	Fri Mar 11 16:28:23 2016 +0100
@@ -128,14 +128,16 @@
 
 MTDC_USERNAME_FIELD_NAME = "displayName"
 MTDC_USERID_FIELD_NAME = "id"
+MTDC_UAI_FIELD_NAME = "ENTPersonStructRattachUAI"
+
+ITOP_PF_CODE = "ITOP" # Parameter for the reference request, should be provided by Itop
+
 
 ANONYMOUS_USER_ID = -1
 
 DEFAULT_RENKAN_ICON = ""
 RENKAN_TOOL_ID = ""
 
-ITOP_PF_CODE = "" # Parameter for the reference request, should be provided by Itop
-
 MTDC_CLIENT_ID = ""
 MTDC_CLIENT_SECRET = ""
 MTDC_AUTH_CODE = ""
--- a/server/src/metaeducation/signals.py	Fri Mar 11 14:47:56 2016 +0100
+++ b/server/src/metaeducation/signals.py	Fri Mar 11 16:28:23 2016 +0100
@@ -22,7 +22,7 @@
                 "idOutil": settings.RENKAN_TOOL_ID,
                 "idUser": instance.creator.external_id,
                 "pf": settings.ITOP_PF_CODE,
-                "uai": "",
+                "uai": instance.creator.uai,
                 "redirect_uri": settings.OAUTH_REDIRECT_URI,
                 "title": instance.title
             })