def populate_user(self,request,sociallogin,data): socialaccount = sociallogin.account username = data.get('username') first_name = data.get('first_name') last_name = data.get('last_name') email = data.get('email') name = data.get('name') user = sociallogin.account.user user_model = get_user_model() try: query = {"username" + '__iexact': username} user_model.objects.get(**query) user_username(user,generate_unique_username([first_name,last_name,email,'user'])) except Exception as e: if username == None: user_username(user,generate_unique_username([first_name,last_name,email,'user'])) else: user_username(user,username.replace(".","")) user_email(user, valid_email_or_none(email) or '') name_parts = (name or '').partition(' ') user_field(user, 'first_name', first_name or name_parts[0]) user_field(user, 'last_name', last_name or name_parts[2]) return user
def populate_user(self, request, sociallogin, data): first_name = data.get('first_name') last_name = data.get('last_name') email = data.get('email') name = data.get('name') username = data.get('username') user = sociallogin.account.user print(sociallogin.account.provider) # TODO: Consider using the super class for the stuff that is not unique to facebook! if sociallogin.account.provider == 'facebook': user_email(user, valid_email_or_none(email) or '') name_parts = (name or '').partition(' ') user_field(user, 'first_name', first_name or name_parts[0]) user_field(user, 'last_name', last_name or name_parts[2]) # Save a good username if username: username_suggestions = [username, first_name, last_name, email, 'user'] else: username_suggestions = [first_name, last_name, email, 'user'] user_username(user, generate_unique_username(username_suggestions)) return user elif sociallogin.account.provider == 'twitter': if "screen_name" in sociallogin.account.extra_data.keys(): username = sociallogin.account.extra_data['screen_name'] user_email(user, valid_email_or_none(email) or '') name_parts = (name or '').partition(' ') user_field(user, 'first_name', first_name or name_parts[0]) user_field(user, 'last_name', last_name or name_parts[2]) # Save a good username if username: username_suggestions = [username, first_name, last_name, email, 'user'] else: username_suggestions = [first_name, last_name, email, 'user'] user_username(user, generate_unique_username(username_suggestions)) return user elif sociallogin.account.provider == 'google': #if "screen_name" in sociallogin.account.extra_data.keys(): # username = sociallogin.account.extra_data['screen_name'] user_email(user, valid_email_or_none(email) or '') name_parts = (name or '').partition(' ') user_field(user, 'first_name', first_name or name_parts[0]) user_field(user, 'last_name', last_name or name_parts[2]) print("sociallogin.account.extra_data:", str(sociallogin.account.extra_data).encode('utf-8')) if username: username_suggestions = [username, first_name, last_name, email, 'user'] else: username_suggestions = [first_name, last_name, email, 'user'] user_username(user, generate_unique_username(username_suggestions)) return user else: return super(SocialAccountAdapter, self).populate_user(request, sociallogin, data)
def populate_user(self, request, sociallogin, data): first_name = data.get("first_name") last_name = data.get("last_name") email = data.get("email") name = data.get("name") username = data.get("username") user = sociallogin.account.user # TODO: Consider using the super class for the stuff that is not unique to facebook! if sociallogin.account.provider == "facebook": user_email(user, valid_email_or_none(email) or "") name_parts = (name or "").partition(" ") user_field(user, "first_name", first_name or name_parts[0]) user_field(user, "last_name", last_name or name_parts[2]) # Save a good username if username: username_suggestions = [username, first_name, last_name, email, "user"] else: username_suggestions = [first_name, last_name, email, "user"] user_username(user, generate_unique_username(username_suggestions)) return user elif sociallogin.account.provider == "twitter": if "screen_name" in sociallogin.account.extra_data.keys(): username = sociallogin.account.extra_data["screen_name"] user_email(user, valid_email_or_none(email) or "") name_parts = (name or "").partition(" ") user_field(user, "first_name", first_name or name_parts[0]) user_field(user, "last_name", last_name or name_parts[2]) # Save a good username if username: username_suggestions = [username, first_name, last_name, email, "user"] else: username_suggestions = [first_name, last_name, email, "user"] user_username(user, generate_unique_username(username_suggestions)) return user elif sociallogin.account.provider == "google": # if "screen_name" in sociallogin.account.extra_data.keys(): # username = sociallogin.account.extra_data['screen_name'] user_email(user, valid_email_or_none(email) or "") name_parts = (name or "").partition(" ") user_field(user, "first_name", first_name or name_parts[0]) user_field(user, "last_name", last_name or name_parts[2]) if username: username_suggestions = [username, first_name, last_name, email, "user"] else: username_suggestions = [first_name, last_name, email, "user"] user_username(user, generate_unique_username(username_suggestions)) return user else: return super(SocialAccountAdapter, self).populate_user(request, sociallogin, data)
def save_user(self, request, user, form): data = form.cleaned_data email = data['email'] first_name = data["first_name"] last_name = data["last_name"] username = generate_unique_username([first_name,last_name,email,'user']) user_email(user, email) user_username(user, username) user_field(user, 'first_name', first_name or '') user_field(user, 'last_name', last_name or '') password = data.get("password1") if password: user.set_password(password) else: user.set_unusable_password() user.save() avatar = Photo.objects.create(caption=first_name + " " + last_name + " Avatar", \ user_post=user,image=DEFAULT_IMAGE_PATH_MAPPING['default_male_icon'], unique_id=generate_unique_id("photo"),photo_type='user_profile') cover_picture = Photo.objects.create(caption=first_name + " " + last_name + " Cover Picture", \ user_post=user,image=DEFAULT_IMAGE_PATH_MAPPING['default_cover_picture'], unique_id=generate_unique_id("photo"),photo_type='user_profile') get_location_from_client_ip(request,user) user.get_profile().avatar = avatar user.get_profile().cover_picture = cover_picture user.get_profile().save() return user
def _process_signup(request, sociallogin): # If email is specified, check for duplicate and if so, no auto signup. auto_signup = app_settings.AUTO_SIGNUP email = sociallogin.account.user.email if auto_signup: # Let's check if auto_signup is really possible... if email: if account_settings.UNIQUE_EMAIL: try: user = User.objects.get(email__iexact=email) # Oops, another user already has this address. We # cannot simply connect this social account to the # existing user. Reason is that the email adress may # not be verified, meaning, the user may be a hacker # that has added your email address to his account in # the hope that you fall in his trap. To solve this, # we reset the password of the email account and connect # the user. user.set_password(md5.new('%s:%s ' % (email, time.time())).hexdigest()) user.save() context = create_password_reset_context(user) get_account_adapter().send_mail('account/email/link_facebook', email, context) sociallogin.account.user = user sociallogin.save() return perform_login(request, user, redirect_url=sociallogin.get_redirect_url(request)) except User.DoesNotExist: # No user exists with this email. Let's auto sign up the user. auto_signup = True elif account_settings.EMAIL_REQUIRED: # Nope, email is required and we don't have it yet... auto_signup = False if not auto_signup: request.session['socialaccount_sociallogin'] = sociallogin url = reverse('socialaccount_signup') ret = HttpResponseRedirect(url) else: # FIXME: This part contains a lot of duplication of logic # ("closed" rendering, create user, send email, in active # etc..) try: if not get_account_adapter().is_open_for_signup(request): return render(request, "account/signup_closed.html") except ImmediateHttpResponse, e: return e.response u = sociallogin.account.user u.username = generate_unique_username(u.username or email or 'user') u.last_name = (u.last_name or '') \ [0:User._meta.get_field('last_name').max_length] u.first_name = (u.first_name or '') \ [0:User._meta.get_field('first_name').max_length] u.email = email or '' u.set_unusable_password() sociallogin.save() send_email_confirmation(request, u) ret = complete_social_signup(request, sociallogin)
def _process_signup(request, sociallogin): # If email is specified, check for duplicate and if so, no auto signup. auto_signup = app_settings.AUTO_SIGNUP email = sociallogin.account.user.email print 'AUTO SIGNUP - %s' % auto_signup print email if auto_signup: # Let's check if auto_signup is really possible... if email: if account_settings.UNIQUE_EMAIL: if email_address_exists(email): # Oops, another user already has this address. We # cannot simply connect this social account to the # existing user. Reason is that the email adress may # not be verified, meaning, the user may be a hacker # that has added your email address to his account in # the hope that you fall in his trap. We cannot check # on 'email_address.verified' either, because # 'email_address' is not guaranteed to be verified. auto_signup = False # FIXME: We redirect to signup form -- user will # see email address conflict only after posting # whereas we detected it here already. elif account_settings.EMAIL_REQUIRED: # Nope, email is required and we don't have it yet... auto_signup = False print 'AUTO SIGNUP 2 - %s' % auto_signup if not auto_signup: request.session['socialaccount_sociallogin'] = sociallogin url = reverse('socialaccount_signup') print url ret = HttpResponseRedirect(url) else: print '555555555' # FIXME: There is some duplication of logic inhere # (create user, send email, in active etc..) u = sociallogin.account.user print '66666666666' u.username = generate_unique_username(u.username or email or 'user') u.last_name = (u.last_name or '') \ [0:User._meta.get_field('last_name').max_length] u.first_name = (u.first_name or '') \ [0:User._meta.get_field('first_name').max_length] u.email = email or '' print '777777777' u.set_unusable_password() print '888888888' sociallogin.save() print '99999999' print '--------' #send_email_confirmation(request, u) print '00000000' ret = complete_social_signup(request, sociallogin) print 'RET' print ret return ret
def _process_signup(request, sociallogin): # If email is specified, check for duplicate and if so, no auto signup. auto_signup = app_settings.AUTO_SIGNUP email = user_email(sociallogin.account.user) if auto_signup: # Let's check if auto_signup is really possible... if email: if account_settings.UNIQUE_EMAIL: if email_address_exists(email): # Oops, another user already has this address. We # cannot simply connect this social account to the # existing user. Reason is that the email adress may # not be verified, meaning, the user may be a hacker # that has added your email address to his account in # the hope that you fall in his trap. We cannot check # on 'email_address.verified' either, because # 'email_address' is not guaranteed to be verified. auto_signup = False # FIXME: We redirect to signup form -- user will # see email address conflict only after posting # whereas we detected it here already. elif app_settings.EMAIL_REQUIRED: # Nope, email is required and we don't have it yet... auto_signup = False if not auto_signup: request.session['socialaccount_sociallogin'] = sociallogin url = reverse('socialaccount_signup') ret = HttpResponseRedirect(url) else: # FIXME: This part contains a lot of duplication of logic # ("closed" rendering, create user, send email, in active # etc..) try: if not get_account_adapter().is_open_for_signup(request): return render(request, "account/signup_closed.html") except ImmediateHttpResponse as e: return e.response u = sociallogin.account.user if account_settings.USER_MODEL_USERNAME_FIELD: user_username(u, generate_unique_username(user_username(u) or email or 'user')) for field in ['last_name', 'first_name']: if hasattr(u, field): truncated_value = (user_field(u, field) or '') \ [0:User._meta.get_field(field).max_length] user_field(u, field, truncated_value) user_email(u, email or '') if u.password: pass else: u.set_unusable_password() sociallogin.save(request) ret = complete_social_signup(request, sociallogin) return ret
def _process_signup(request, sociallogin): log.debug('socialaccount.helpers._process_signup') # If email is specified, check for duplicate and if so, no auto signup. log.debug("_process_signup") auto_signup = app_settings.AUTO_SIGNUP email = sociallogin.account.user.email if auto_signup: # Let's check if auto_signup is really possible... log.debug("auto_signup") if email: if account_settings.UNIQUE_EMAIL: if email_address_exists(email): # Oops, another user already has this address. We # cannot simply connect this social account to the # existing user. Reason is that the email adress may # not be verified, meaning, the user may be a hacker # that has added your email address to his account in # the hope that you fall in his trap. We cannot check # on 'email_address.verified' either, because # 'email_address' is not guaranteed to be verified. auto_signup = False # FIXME: We redirect to signup form -- user will # see email address conflict only after posting # whereas we detected it here already. elif account_settings.EMAIL_REQUIRED: # Nope, email is required and we don't have it yet... log.debug("no email available") auto_signup = False if not auto_signup: log.debug("not auto signup") request.session['socialaccount_sociallogin'] = sociallogin url = reverse('socialaccount_signup') ret = HttpResponseRedirect(url) else: # FIXME: There is some duplication of logic inhere # (create user, send email, in active etc..) log.debug("Auto sign up again") u = sociallogin.account.user u.username = generate_unique_username(u.username or email or 'user') u.last_name = (u.last_name or '') \ [0:User._meta.get_field('last_name').max_length] u.first_name = (u.first_name or '') \ [0:User._meta.get_field('first_name').max_length] u.email = email or '' u.set_unusable_password() log.debug(u) sociallogin.save() log.debug("Sending email to the following:") log.debug(email) send_email_confirmation(request, u) log.debug("Email confirmation sent") ret = complete_social_signup(request, sociallogin) log.debug(ret) return ret
def create(self, validated_data): user = User( email=validated_data.get('email'), first_name=validated_data.get('first_name'), last_name=validated_data.get('last_name'), username=generate_unique_username([ validated_data.get('first_name'), validated_data.get('last_name'), validated_data.get('email'), 'user' ]) ) user.set_password(validated_data.get('password')) user.save() return user
def create(self, validated_data): user = User( email=validated_data.get("email"), name=validated_data.get("name"), username=generate_unique_username([ validated_data.get("name"), validated_data.get("email"), "user" ]), ) user.set_password(validated_data.get("password")) user.save() request = self._get_request() setup_user_email(request, user, []) return user
def _process_signup(request, sociallogin): # If email is specified, check for duplicate and if so, no auto signup. auto_signup = app_settings.AUTO_SIGNUP email = sociallogin.account.user.email if auto_signup: # Let's check if auto_signup is really possible... if email: if account_settings.UNIQUE_EMAIL: if email_address_exists(email): # Oops, another user already has this address. We # cannot simply connect this social account to the # existing user. Reason is that the email adress may # not be verified, meaning, the user may be a hacker # that has added your email address to his account in # the hope that you fall in his trap. We cannot check # on 'email_address.verified' either, because # 'email_address' is not guaranteed to be verified. auto_signup = False # FIXME: We redirect to signup form -- user will # see email address conflict only after posting # whereas we detected it here already. elif account_settings.EMAIL_REQUIRED: # Nope, email is required and we don't have it yet... auto_signup = False if not auto_signup: request.session['socialaccount_sociallogin'] = sociallogin url = reverse('socialaccount_signup') ret = HttpResponseRedirect(url) else: # FIXME: This part contains a lot of duplication of logic # ("closed" rendering, create user, send email, in active # etc..) try: if not get_account_adapter().is_open_for_signup(request): return render(request, "account/signup_closed.html") except ImmediateHttpResponse as e: return e.response u = sociallogin.account.user u.username = generate_unique_username(u.username or email or 'user') u.last_name = (u.last_name or '') \ [0:User._meta.get_field('last_name').max_length] u.first_name = (u.first_name or '') \ [0:User._meta.get_field('first_name').max_length] u.email = email or '' u.set_unusable_password() sociallogin.save(request) send_email_confirmation(request, u) ret = complete_social_signup(request, sociallogin) return ret
def _process_signup(request, data, account): # If email is specified, check for duplicate and if so, no auto signup. auto_signup = app_settings.AUTO_SIGNUP email = data.get("email") if auto_signup: # Let's check if auto_signup is really possible... if email: if account_settings.UNIQUE_EMAIL: if email_address_exists(email): # Oops, another user already has this address. We # cannot simply connect this social account to the # existing user. Reason is that the email address may # not be verified, meaning, the user may be a hacker # that has added your email address to his account in # the hope that you fall in his trap. We cannot check # on 'email_address.verified' either, because # 'email_address' is not guaranteed to be verified. auto_signup = False # FIXME: We redirect to signup form -- user will # see email address conflict only after posting # whereas we detected it here already. elif account_settings.EMAIL_REQUIRED: # Nope, email is required and we don't have it yet... auto_signup = False if not auto_signup: request.session["socialaccount_signup"] = dict(data=data, account=account) url = reverse("socialaccount_signup") next = request.REQUEST.get("next") if next: url = url + "?" + urlencode(dict(next=next)) ret = HttpResponseRedirect(url) else: # FIXME: There is some duplication of logic in here # (create user, send email, in active etc..) username = generate_unique_username(data.get("username", email or "user")) u = User( username=username, email=email or "", last_name=data.get("last_name", ""), first_name=data.get("first_name", ""), ) u.set_unusable_password() u.is_active = not account_settings.EMAIL_VERIFICATION u.save() account.user = u account.save() send_email_confirmation(u, request=request) ret = complete_social_signup(request, u, account) return ret
def create_userprofile(self, user): username = generate_unique_username([self.cleaned_data['email']]) password = make_password(self.cleaned_data['password']) new_user = User(email=self.cleaned_data['email'], first_name=self.cleaned_data['first_name'], last_name=self.cleaned_data['last_name'], password=password, username=username) new_user.save() new_user.userprofile.customer = user.userprofile.customer new_user.userprofile.role = self.cleaned_data['role'] new_user.userprofile.staff = True new_user.userprofile.save() return new_user.userprofile
def create(self, validated_data): user = User( email=validated_data.get('email'), name=validated_data.get('name'), username=generate_unique_username([ validated_data.get('name'), validated_data.get('email'), 'user' ]) ) user.set_password(validated_data.get('password')) user.save() request = self._get_request() setup_user_email(request, user, []) return user
def revert_usernames(apps, schema_editor): User = apps.get_model('auth', 'User') for user in User.objects.exclude(username='******'): old_username = user.username user_username( user, generate_unique_username([ user_field(user, 'first_name'), user_field(user, 'last_name'), user_email(user), user_username(user), ])) if user_username(user) != old_username: user.save()
def save(self, *args, **kwargs): if not self.username: self.username = generate_unique_username([ self.first_name, self.last_name, self.email, self.username, 'user' ]) self.first_name = ' '.join(self.first_name.split()) self.last_name = ' '.join(self.last_name.split()) if self.role is None: role = UserRole.objects.filter(role='normal').first() if role: self.role = role return super(User, self).save(*args, **kwargs)
def revert_usernames(apps, schema_editor): User = apps.get_model('auth', 'User') for user in User.objects.exclude(username='******'): old_username = user.username user_username( user, generate_unique_username([ user_field(user, 'first_name'), user_field(user, 'last_name'), user_email(user), user_username(user), ]) ) if user_username(user) != old_username: user.save()
def fix_usernames(apps, schema_editor): User = apps.get_model('auth', 'User') for user in User.objects.exclude(username='******'): if user.email and '@' in user.email: email_username = user.email.split('@')[0] old_username = user.username user_username( user, generate_unique_username([ email_username, user_field(user, 'first_name'), user_field(user, 'last_name'), user_email(user), user_username(user), ])) if user_username(user) != old_username: user.save()
def create_userprofile(self, user): username = generate_unique_username([self.cleaned_data['email']]) password = make_password(self.cleaned_data['password']) new_user = User( email=self.cleaned_data['email'], first_name=self.cleaned_data['first_name'], last_name=self.cleaned_data['last_name'], password=password, username=username ) new_user.save() new_user.userprofile.customer = user.userprofile.customer new_user.userprofile.role = self.cleaned_data['role'] new_user.userprofile.staff = True new_user.userprofile.save() return new_user.userprofile
def create(self, validated_data): user = User( first_name=validated_data.get('first_name'), last_name=validated_data.get('last_name'), email=validated_data.get('email'), username=generate_unique_username([ validated_data.get('first_name'), validated_data.get('last_name'), validated_data.get('email'), 'user' ]), user_type='teacher', is_active=True, ) user.set_password(validated_data.get('password')) user.save() return user
def import_reviewer_csv(filename): num_users_created = 0 num_existing_users = 0 with open(filename, "r", encoding="UTF-8-SIG") as csvfile: csvreader = csv.reader(csvfile) # Skip header row. next(csvreader) for row in csvreader: try: first_name = row[1].strip() last_name = row[2].strip() email = row[3].lower().strip() except IndexError: return False # Get an existing user account with the same email address. # If none exists, create a new user account with a username # generated from the first name or email address. if first_name: username = first_name else: username = email user, user_created = User.objects.get_or_create( email=email, defaults={"username": generate_unique_username([username])}, ) if user_created: num_users_created += 1 EmailAddress.objects.create(user=user, email=email) else: num_existing_users += 1 if not user.first_name: user.first_name = first_name if not user.last_name: user.last_name = last_name # Make sure user has reviewing permissions by adding # them to the right Group. reviewers_group = Group.objects.get(name="Reviewers") user.groups.add(reviewers_group) user.save() return (num_users_created, num_existing_users)
def fix_usernames(apps, schema_editor): User = apps.get_model('auth', 'User') for user in User.objects.exclude(username='******'): if user.email and '@' in user.email: email_username = user.email.split('@')[0] old_username = user.username user_username( user, generate_unique_username([ email_username, user_field(user, 'first_name'), user_field(user, 'last_name'), user_email(user), user_username(user), ]) ) if user_username(user) != old_username: user.save()
def signup(self, request, user): texts = [] texts.append(self.cleaned_data['email']) texts.append(self.cleaned_data['name']) texts.append(self.cleaned_data['surname']) user.username = generate_unique_username(texts) user.first_name = self.cleaned_data['name'] user.last_name = self.cleaned_data['surname'] user.save() custom = CustomUser.objects.get(user=user) custom.name = self.cleaned_data['name'] custom.surname = self.cleaned_data['surname'] custom.agree_hr = self.cleaned_data['agree_hr'] custom.agree_terms = self.cleaned_data['agree_terms'] custom.save()
def create_user(self, commit=True): user = User() # data collected by providers, if any, is passed as `initial` # signup form data. This may contain fields such as # `first_name`, whereas these may not have field counterparts # in the form itself. So let's pick these up here... data = self.initial user.last_name = data.get("last_name", "") user.first_name = data.get("first_name", "") user.email = self.cleaned_data["email"].strip().lower() if app_settings.USERNAME_REQUIRED: user.username = self.cleaned_data["username"] else: user.username = generate_unique_username(user.first_name or user.last_name or user.email) user.set_unusable_password() if commit: user.save() return user
def create(self, first_name, last_name, email, password1, password2, *args, **kwargs): assert not User.objects.filter(email=email).exists() user = User() user.first_name = first_name user.last_name = last_name user.email = email user.set_password(password1) user.username = generate_unique_username( [first_name, last_name, email, None, 'user']) user.save() # needed for django-allauth EmailAddress(user=user, email=email, primary=True, verified=False).save() return user
def create(self, validated_data): user_type = validated_data.get("user_type") user = User( email=validated_data.get("email"), name=validated_data.get("name"), username=generate_unique_username([ validated_data.get("name"), validated_data.get("email"), "user" ]), user_type=user_type, ) user.set_password(validated_data.get("password")) user.save() if user_type == User.USER_TYPE_STUDENT: school = School.objects.filter( student_code=validated_data.get("code")).first() Student.objects.create( user=user, phone_number=validated_data.get("phone_number"), school=school, ) elif user_type == User.USER_TYPE_PARENT: parent = Parent.objects.create( user=user, phone_number=validated_data.get("phone_number"), ) student = Student.objects.filter( student_id=validated_data.get("code")).first() parent.students.add(student) elif user_type == User.USER_TYPE_TEACHER: school = School.objects.filter( teacher_code=validated_data.get("code")).first() Teacher.objects.create( user=user, phone_number=validated_data.get("phone_number"), school=school, ) request = self._get_request() setup_user_email(request, user, []) return user
def signup(self, request, user): data = self.cleaned_data user.email = data['email'] user.first_name = data['first_name'] user.last_name = data['last_name'] user.gender = data['gender'] user.dob = data['dob'] user.address = data['address'] if 'password1' in data: user.set_password(data['password1']) else: user.set_unusable_password() user.username = generate_unique_username( [data['first_name'], data['last_name'], data['email'], 'user']) user.save() return user
def create(self, validated_data): user = User( email=validated_data.get("email"), name=validated_data.get("name"), signup_frequent_purchase=validated_data.get( "signup_frequent_purchase"), username=generate_unique_username([ validated_data.get("name"), validated_data.get("email"), "user" ]), ) user.set_password(validated_data.get("password")) user.save() request = self.context.get("request") self.set_user_signup_prod(user, request) setup_user_email(request, user, []) return user
def create(self, validated_data): user = User(email=validated_data.get('email'), name=validated_data.get('name'), username=generate_unique_username([ validated_data.get('name'), validated_data.get('email'), 'user' ])) user.dob = validated_data.get('dob') user.country = validated_data.get('country') user.state = validated_data.get('state') user.face_data = validated_data.get('face_data') user.first_name = validated_data.get('first_name') user.last_name = validated_data.get('last_name') user.set_password(validated_data.get('password')) user.save() request = self._get_request() setup_user_email(request, user, []) return user
def create_user(self, commit=True): user = User() # data collected by providers, if any, is passed as `initial` # signup form data. This may contain fields such as # `first_name`, whereas these may not have field counterparts # in the form itself. So let's pick these up here... data = self.initial user.last_name = data.get('last_name', '') user.first_name = data.get('first_name', '') user.email = self.cleaned_data["email"].strip().lower() if app_settings.USERNAME_REQUIRED: user.username = self.cleaned_data["username"] else: user.username = generate_unique_username(user.first_name or user.last_name or user.email) user.set_unusable_password() if commit: user.save() return user
def create(self, validated_data): email = get_adapter().clean_email(validated_data.get("email")) if allauth_settings.UNIQUE_EMAIL: if email and email_address_exists(email): raise serializers.ValidationError({ "email": [ _("A user is already registered with this e-mail address." ) ] }) name = f'{validated_data.get("first_name")} {validated_data.get("last_name")}' user = User(email=validated_data.get("email"), name=name, first_name=validated_data.get("first_name"), last_name=validated_data.get("last_name"), phone=validated_data.get("phone"), username=generate_unique_username( [name, validated_data.get("email"), "user"]), role="simple_user") user.save() return user
def create_job_seeker_by_proxy(cls, proxy_user, **fields): """ Used when a "prescriber" user creates another user of kind "job seeker". Minimum required keys in `fields` are: { "email": "*****@*****.**", "first_name": "Foo", "last_name": "Foo", } """ username = generate_unique_username( [fields["first_name"], fields["last_name"], fields["email"]]) fields["is_job_seeker"] = True fields["created_by"] = proxy_user user = cls.objects.create_user( username, email=fields.pop("email"), password=cls.objects.make_random_password(), **fields) return user
def create(self, validated_data): if validated_data.get('username') is None: validated_data['username'] = generate_unique_username([ validated_data.get('first_name'), validated_data.get('last_name'), validated_data.get('email'), 'user' ]) user = User( email=validated_data.get('email'), first_name=validated_data.get('first_name'), last_name=validated_data.get('last_name'), username=validated_data.get('username') ) user.set_password(validated_data.get('password')) user.save() #user_profiles = UserProfile.objects.filter(user=user) #print(user_profiles) #profile = user_profiles[0] #ROLE=2 FOR OFFICE AND 1 FOR CITIZEN UserProfile.objects.create( user=user, role=1, birthdate=validated_data.get('birthdate'), height=validated_data.get('height'), weight=validated_data.get('weight'), street_address=validated_data.get('street_address'), zip_code=validated_data.get('zip_code'), state=validated_data.get('state'), city=validated_data.get('city'), phone_no=validated_data.get('phone_no'), approved_user=0, gender=validated_data.get('gender'), driver_licence_no=validated_data.get('driver_licence_no'), licence_photo=decode_based64_file(validated_data.get('licence_photo')), profile_image=decode_based64_file(validated_data.get('profile_image')), ) validated_data['success'] = True return validated_data
def convert_to_full_acct(request): user = request.user if request.method == 'POST': form = UpgradeUserForm(request.POST) try: profile = Profile.objects.get( user=user) # Check that there is a profile for this user except: return render(request, 'invalid_acct.html', {}) if (user.is_authenticated() and profile.temporary_acct): if form.is_valid(): email = form.cleaned_data['email'] new_password = form.cleaned_data['password'] user.email = email user.set_password(new_password) user.username = generate_unique_username((email, )) user.save() profile.temporary_acct = False profile.save() request.session.set_expiry( 0 ) # Make user logged in for the duration of the session, as would be the # case when logged in with any other regular account. return HttpResponseRedirect('/') else: return render(request, 'invalid_acct.html', {}) elif (user.is_authenticated() == False): return render(request, 'invalid_acct.html', {}) else: form = UpgradeUserForm() return render(request, 'upgrade_temp_acct.html', {'form': form})
def create(self, validated_data): first_name = validated_data.get('first_name') last_name = validated_data.get('last_name') name = f"{first_name} {last_name}" user = User( email=validated_data.get('email'), name=name, first_name=first_name, last_name=last_name, role=validated_data.get('role'), organisation=validated_data.get('organisation'), username=generate_unique_username([ name, validated_data.get('email'), 'user' ]) ) user.set_password(validated_data.get('password')) user.save() request = self._get_request() setup_user_email(request, user, []) return user
def create(self, validated_data): user = User( email=validated_data.get("email"), name=validated_data.get("name"), first_name=validated_data.get("first_name"), last_name=" ", username=generate_unique_username([ validated_data.get("name"), validated_data.get("email"), "user" ]), ) user.set_password(validated_data.get("password")) user.save() # add user to group group = Group.objects.get(name=validated_data.get('user_type')) user.groups.add(group) # Create user profile Profile.objects.create(user=user) request = self._get_request() setup_user_email(request, user, []) return user
def clean_username(self): if self.cleaned_data['username']: return generate_unique_username([self.cleaned_data['username']])
def populate_username(user): first_name = user.first_name last_name = user.last_name email = user.email return generate_unique_username([first_name, last_name, email, 'user'])
def _process_signup(request, data, account): # If email is specified, check for duplicate and if so, no auto signup. auto_signup = app_settings.AUTO_SIGNUP email = data.get("email") if auto_signup: # Let's check if auto_signup is really possible... if email: if account_settings.UNIQUE_EMAIL: email_address = get_email_address(email) if email_address: # Oops, another user already has this address. We # cannot simply connect this social account to the # existing user. Reason is that the email adress may # not be verified, meaning, the user may be a hacker # that has added your email address to his account in # the hope that you fall in his trap. We cannot check # on 'email_address.verified' either, because # 'email_address' is not guaranteed to be verified. auto_signup = False # FIXME: We redirect to signup form -- user will # see email address conflict only after posting # whereas we detected it here already. # Jason C. Make them log in first... then # redirect so they can make the association messages.add_message( request, messages.INFO, _( "The email associated with this social account is already" " in use, you must first login to that account to connect." ), ) return HttpResponseRedirect( "%s?next=%s" % (reverse("account_login"), reverse("socialaccount_connections")) ) elif account_settings.EMAIL_REQUIRED: # Nope, email is required and we don't have it yet... auto_signup = False if not auto_signup: request.session["socialaccount_signup"] = dict(data=data, account=account) ret = HttpResponseRedirect(reverse("socialaccount_signup")) else: # FIXME: There is some duplication of logic inhere # (create user, send email, in active etc..) username = generate_unique_username(data.get("username", email or "user")) u = User( username=username, email=email or "", last_name=data.get("last_name", "")[0 : User._meta.get_field("last_name").max_length], first_name=data.get("first_name", "")[0 : User._meta.get_field("first_name").max_length], ) u.set_unusable_password() u.is_active = not account_settings.EMAIL_VERIFICATION u.save() account.user = u account.save() send_email_confirmation(u, request=request) ret = complete_social_signup(request, u, account) return ret
def generate_unique_username(self, txts, regex=None): return generate_unique_username(txts, regex)
def _process_signup(request, sociallogin): # If email is specified, check for duplicate and if so, no auto signup. auto_signup = app_settings.AUTO_SIGNUP email = user_email(sociallogin.account.user) if auto_signup: # Let's check if auto_signup is really possible... if email: if account_settings.UNIQUE_EMAIL: if email_address_exists(email): # Oops, another user already has this address. We # cannot simply connect this social account to the # existing user. Reason is that the email adress may # not be verified, meaning, the user may be a hacker # that has added your email address to his account in # the hope that you fall in his trap. We cannot check # on 'email_address.verified' either, because # 'email_address' is not guaranteed to be verified. auto_signup = False # FIXME: We redirect to signup form -- user will # see email address conflict only after posting # whereas we detected it here already. elif app_settings.EMAIL_REQUIRED: # Nope, email is required and we don't have it yet... auto_signup = False # Stop here if invitations are required if auth_settings.INVITATION_REQUIRED: # Check for valid invitation key in session if 'invitation_key' not in request.session \ or not InvitationKey.objects.is_key_valid(request.session['invitation_key']): return HttpResponseRedirect(auth_settings.NO_INVITATION_REDIRECT) if not auto_signup: request.session['socialaccount_sociallogin'] = sociallogin url = reverse('socialaccount_signup') ret = HttpResponseRedirect(url) else: # If we're still on auto signup, stop if invitations are required if auth_settings.INVITATION_REQUIRED: # Check for valid invitation key in session if 'invitation_key' not in request.session \ or not InvitationKey.objects.is_key_valid(request.session['invitation_key']): return HttpResponseRedirect( auth_settings.NO_INVITATION_REDIRECT) # Continue with auto signup # FIXME: This part contains a lot of duplication of logic # ("closed" rendering, create user, send email, in active # etc..) try: if not get_account_adapter().is_open_for_signup(request): return render(request, "account/signup_closed.html") except ImmediateHttpResponse as e: return e.response u = sociallogin.account.user if account_settings.USER_MODEL_USERNAME_FIELD: user_username( u, generate_unique_username(user_username(u) or email or 'user')) for field in ['last_name', 'first_name']: if hasattr(u, field): truncated_value = (user_field(u, field) or '') \ [0:User._meta.get_field(field).max_length] user_field(u, field, truncated_value) user_email(u, email or '') u.set_unusable_password() sociallogin.save(request) # Make sure the user has a primary email address if EmailAddress.objects.filter(user=u).count() == 0: setup_user_email(request, u) ret = complete_social_signup(request, sociallogin) return ret
def acuity_webhook_changed(request): post_data = request.POST action = post_data['action'] if action == 'changed' or action == 'rescheduled' or action == 'canceled' or action == 'scheduled': appointment = Appointments.appointment_detail(post_data['id']) if appointment: appointment_id = appointment['id'] first_name = appointment['firstName'] last_name = appointment['lastName'] phone_number = format_phone_number(appointment) email = appointment['email'].lower() # try: # user = User.objects.get(email=email) # except: # user = None # print(user) try: user = User.objects.get(email=email) # user.phone_number = phone_number # user.first_name = first_name # user.last_name = last_name # user.save() print('user found') except User.DoesNotExist: print('error: not user found') print('creating new') user = User( first_name=first_name, last_name=last_name, email=email, username=generate_unique_username( [first_name, last_name, email, 'user']), phone_number=phone_number, is_patient=True, ) user.save() setup_user_email(request, user, []) print(user) if user: try: user_profile = UserProfile.objects.get(user=user) # user_profile.contact_number = phone_number user_profile.acuity_form_data = appointment['forms'] user_profile.save() except UserProfile.DoesNotExist: user_profile = UserProfile( user=user, contact_number=phone_number, acuity_form_data=appointment['forms']) user_profile.save() try: object_item = AcuityAppointment.objects.get( user=user, acuity_appointment_id=appointment_id) object_item.appointment_data = appointment object_item.first_name = first_name object_item.last_name = last_name object_item.save() except AcuityAppointment.DoesNotExist: AcuityAppointment.objects.create( user=user, acuity_appointment_id=appointment_id, first_name=first_name, last_name=last_name, appointment_data=appointment) return HttpResponse(status=200)
def extract_common_fields(self, data): return { 'username': generate_unique_username(data['email']), 'email': data['email'], }
def generate_unique_username(self, txts, regex=None): if txts[2] != "": return txts[2] else: return generate_unique_username(txts, regex)
def _process_signup(request, sociallogin): # If email is specified, check for duplicate and if so, no auto signup. auto_signup = app_settings.AUTO_SIGNUP email = user_email(sociallogin.account.user) if auto_signup: # Let's check if auto_signup is really possible... if email: if account_settings.UNIQUE_EMAIL: if email_address_exists(email): # Oops, another user already has this address. We # cannot simply connect this social account to the # existing user. Reason is that the email adress may # not be verified, meaning, the user may be a hacker # that has added your email address to his account in # the hope that you fall in his trap. We cannot check # on 'email_address.verified' either, because # 'email_address' is not guaranteed to be verified. auto_signup = False # FIXME: We redirect to signup form -- user will # see email address conflict only after posting # whereas we detected it here already. else: # Extra stuff hacked in here to integrate with # the account whitelist app. # Will be ignored if the whitelist app can't be # imported, thus making this slightly less hacky. whitelist_model_setting = getattr( settings, 'SOCIALACCOUNT_WHITELIST_MODEL', None ) if whitelist_model_setting: whitelist_model_path = whitelist_model_setting.split(r'.') whitelist_model_str = whitelist_model_path[-1] whitelist_path_str = r'.'.join(whitelist_model_path[:-1]) try: whitelist_app = __import__(whitelist_path_str, fromlist=[whitelist_path_str]) whitelist_model = getattr(whitelist_app, whitelist_model_str, None) if whitelist_model: try: guest = whitelist_model.objects.get(email=email) if not guest.active: auto_signup = False except whitelist_model.DoesNotExist: auto_signup = False except ImportError: pass elif app_settings.EMAIL_REQUIRED: # Nope, email is required and we don't have it yet... auto_signup = False if not auto_signup: url = reverse('socialaccount_login_error') ret = HttpResponseRedirect(url) else: # FIXME: This part contains a lot of duplication of logic # ("closed" rendering, create user, send email, in active # etc..) try: if not get_account_adapter().is_open_for_signup(request): return render(request, "account/signup_closed.html") except ImmediateHttpResponse as e: return e.response u = sociallogin.account.user if account_settings.USER_MODEL_USERNAME_FIELD: user_username(u, generate_unique_username(user_username(u) or email or 'user')) u.last_name = (u.last_name or '') \ [0:User._meta.get_field('last_name').max_length] u.first_name = (u.first_name or '') \ [0:User._meta.get_field('first_name').max_length] user_email(u, email or '') u.set_unusable_password() sociallogin.save(request) ret = complete_social_signup(request, sociallogin) return ret
def get(request): max_items = request.GET.get('max') params = { 'max': max_items } appointments = Appointments.appointment_list(params) for appointment in appointments: appointment_id = appointment['id'] first_name = appointment['firstName'] last_name = appointment['lastName'] phone_number = appointment['phone'] email = appointment['email'] # try: # user = User.objects.get(email=email) # except: # user = None # print(user) try: user = User.objects.get(email=email) user.phone_number = phone_number user.first_name = first_name user.last_name = last_name user.save() print('user found') except User.DoesNotExist: print('error: not user found') print('creating new') user = User( first_name=first_name, last_name=last_name, email=email, username=generate_unique_username([ first_name, last_name, email, 'user' ]), phone_number=phone_number, is_patient=True, ) user.save() setup_user_email(request, user, []) print(user) if user: try: user_profile = UserProfile.objects.get(user=user) user_profile.contact_number = phone_number user_profile.acuity_form_data = appointment['forms'] user_profile.save() except UserProfile.DoesNotExist: user_profile = UserProfile( user=user, contact_number=phone_number, acuity_form_data=appointment['forms'] ) user_profile.save() try: object_item = AcuityAppointment.objects.get(user=user, acuity_appointment_id=appointment_id) object_item.appointment_data = appointment object_item.save() except AcuityAppointment.DoesNotExist: AcuityAppointment.objects.create(user=user, acuity_appointment_id=appointment_id, appointment_data=appointment) return Response(appointments)