diff --git a/PyRIGS/settings.py b/PyRIGS/settings.py index 03c7c73a..e9407f81 100644 --- a/PyRIGS/settings.py +++ b/PyRIGS/settings.py @@ -86,11 +86,16 @@ SOCIAL_AUTH_PIPELINE = ( 'social.pipeline.social_auth.social_user', # If already associated, login 'RIGS.discourse.pipeline.new_connection', # Choose a user account, much UI 'social.pipeline.social_auth.associate_user', # Associate the social auth with the user + 'social.pipeline.social_auth.load_extra_data', # Save all the social info we have on this user + 'RIGS.discourse.pipeline.update_avatar', # Load the avatar URL from the API, and save to user model 'social.pipeline.user.user_details', # Save any details that changed ) -DISCOURSE_HOST=os.environ.get('DISCOURSE_HOST') if os.environ.get('DISCOURSE_HOST') else 'http://localhost:4000' -DISCOURSE_SSO_SECRET=os.environ.get('DISCOURSE_SSO_SECRET') if os.environ.get('DISCOURSE_SSO_SECRET') else 'ABCDEFGHIJKLMNOP' +DISCOURSE_HOST = os.environ.get('DISCOURSE_HOST') if os.environ.get('DISCOURSE_HOST') else 'http://localhost:4000' +DISCOURSE_SSO_SECRET = os.environ.get('DISCOURSE_SSO_SECRET') if os.environ.get('DISCOURSE_SSO_SECRET') else 'ABCDEFGHIJKLMNOP' + +DISCOURSE_API_KEY = os.environ.get('DISCOURSE_API_KEY') if os.environ.get('DISCOURSE_HOST') else None +DISCOURSE_API_USER = os.environ.get('DISCOURSE_API_USER') if os.environ.get('DISCOURSE_HOST') else 'system' REGISTRATION_OPEN = False # Disable built-in django registration - must register using forum diff --git a/RIGS/discourse/pipeline.py b/RIGS/discourse/pipeline.py index 06fcbc18..a8dde51e 100644 --- a/RIGS/discourse/pipeline.py +++ b/RIGS/discourse/pipeline.py @@ -2,6 +2,10 @@ from django.core.urlresolvers import reverse from django.contrib.auth import REDIRECT_FIELD_NAME from django.shortcuts import render_to_response from django.core.exceptions import ValidationError +from django.conf import settings + +import json +import requests from social.pipeline.partial import partial @@ -60,3 +64,25 @@ def new_connection(backend, details, response, user=None, is_new=False, social=N context['form'] = form return render_to_response('RIGS/social-associate.html', context) + + +def update_avatar(backend, details, response, user=None, social=None, *args, **kwargs): + host = settings.DISCOURSE_HOST + api_key = settings.DISCOURSE_API_KEY + api_user = settings.DISCOURSE_API_USER + if social is not None: + url = "{}/users/{}.json".format(host, details['username']) + params = { + 'api_key': api_key, + 'api_username': api_user + } + resp = requests.get(url=url, params=params) + extraData = json.loads(resp.text) + + avatar_template = extraData['user']['avatar_template'] + + if avatar_template and user.avatar_template != avatar_template: + user.avatar_template = avatar_template + user.save() + + return diff --git a/RIGS/migrations/0025_profile_avatar_template.py b/RIGS/migrations/0025_profile_avatar_template.py new file mode 100644 index 00000000..e770e8bb --- /dev/null +++ b/RIGS/migrations/0025_profile_avatar_template.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('RIGS', '0024_auto_20160229_2042'), + ] + + operations = [ + migrations.AddField( + model_name='profile', + name='avatar_template', + field=models.CharField(max_length=255, null=True, editable=False, blank=True), + ), + ] diff --git a/RIGS/models.py b/RIGS/models.py index 0650d81c..5eebe574 100644 --- a/RIGS/models.py +++ b/RIGS/models.py @@ -22,6 +22,7 @@ class Profile(AbstractUser): initials = models.CharField(max_length=5, unique=True, null=True, blank=False) phone = models.CharField(max_length=13, null=True, blank=True) api_key = models.CharField(max_length=40, blank=True, editable=False, null=True) + avatar_template = models.CharField(max_length=255, blank=True, editable=False, null=True) @classmethod def make_api_key(cls): @@ -33,8 +34,13 @@ class Profile(AbstractUser): @property def profile_picture(self): url = "" + if settings.DISCOURSE_API_KEY is not None: + if self.avatar_template: + return settings.DISCOURSE_HOST+self.avatar_template.format(size=500) + if settings.USE_GRAVATAR or settings.USE_GRAVATAR is None: url = "https://www.gravatar.com/avatar/" + hashlib.md5(self.email).hexdigest() + "?d=wavatar&s=500" + return url @property