From e542001bfaf1a8fc48491b320c3017ca010ce328 Mon Sep 17 00:00:00 2001 From: Charles Connell Date: Tue, 21 Jan 2014 10:55:12 -0500 Subject: [PATCH] Capture names from 3rd party auth accounts --- karmaworld/apps/users/forms.py | 16 ++++++++++++++++ karmaworld/apps/users/models.py | 26 ++++++-------------------- karmaworld/settings/common.py | 4 +++- 3 files changed, 25 insertions(+), 21 deletions(-) create mode 100644 karmaworld/apps/users/forms.py diff --git a/karmaworld/apps/users/forms.py b/karmaworld/apps/users/forms.py new file mode 100644 index 0000000..ee12d51 --- /dev/null +++ b/karmaworld/apps/users/forms.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python +# -*- coding:utf8 -*- +# Copyright (C) 2013 FinalsClub Foundation +from django import forms + + +class SignupForm(forms.Form): + first_name = forms.CharField(max_length=255, required=False, label='Given name') + last_name = forms.CharField(max_length=255, required=False, label='Family name') + email = forms.EmailField(label='Email address') + + def save(self, user): + user.first_name = self.cleaned_data['first_name'] + user.last_name = self.cleaned_data['last_name'] + user.email = self.cleaned_data['email'] + user.save() diff --git a/karmaworld/apps/users/models.py b/karmaworld/apps/users/models.py index 788b528..fc50308 100644 --- a/karmaworld/apps/users/models.py +++ b/karmaworld/apps/users/models.py @@ -3,7 +3,8 @@ # Copyright (C) 2013 FinalsClub Foundation import random import logging -from allauth.account.signals import user_logged_in +from allauth.account.signals import user_logged_in, user_signed_up, email_confirmed, email_changed, email_added +from allauth.socialaccount.signals import pre_social_login from django.contrib.auth.models import User from django.core.exceptions import ObjectDoesNotExist from django.db.models.signals import post_save, pre_save @@ -27,29 +28,14 @@ class UserProfile(models.Model): def user_display_name(user): """Return the best way to display a user's name to them on the site.""" - if user.first_name or user.last_name: + if hasattr(user, 'first_name') and user.first_name and \ + hasattr(user, 'last_name') and user.last_name: return user.first_name + ' ' + user.last_name + elif hasattr(user, 'email') and user.email: + return user.email else: return user.username - -@receiver(pre_save, sender=User, weak=True) -def assign_username(sender, instance, **kwargs): - # If a user does not have a username, they need - # one before we save to the database - if not instance.username: - if instance.email: - try: - # See if any other users have this email address - others = User.objects.get(email=instance.email) - except ObjectDoesNotExist: - instance.username = instance.email - else: - instance.username = 'user' + str(random.randint(10000, 100000)) - else: - instance.username = 'user' + str(random.randint(10000, 100000)) - - @receiver(post_save, sender=User, weak=True) def create_user_profile(sender, instance, created, **kwargs): if created: diff --git a/karmaworld/settings/common.py b/karmaworld/settings/common.py index 4c92b55..09ab1c4 100644 --- a/karmaworld/settings/common.py +++ b/karmaworld/settings/common.py @@ -266,11 +266,13 @@ ACCOUNT_AUTHENTICATION_METHOD = "email" ACCOUNT_CONFIRM_EMAIL_ON_GET = False ACCOUNT_EMAIL_VERIFICATION = "optional" ACCOUNT_EMAIL_SUBJECT_PREFIX = "KarmaNotes.org -- " -ACCOUNT_USER_MODEL_EMAIL_FIELD = "email" ACCOUNT_USERNAME_REQUIRED = False SOCIALACCOUNT_EMAIL_REQUIRED = True SOCIALACCOUNT_EMAIL_VERIFICATION = "optional" +SOCIALACCOUNT_QUERY_EMAIL = True +SOCIALACCOUNT_AUTO_SIGNUP = False ACCOUNT_USER_DISPLAY = 'karmaworld.apps.users.models.user_display_name' +ACCOUNT_SIGNUP_FORM_CLASS = 'karmaworld.apps.users.forms.SignupForm' AUTH_PROFILE_MODULE = 'users.UserProfile' -- 2.25.1