def reset(request, reset_uuid): """View for resetting a user password.""" profile_model = get_profile_model() profile = None # validate the provided uuid try: profile = profile_model.objects.get(password_reset=reset_uuid) # check if the profile model has the password reset field if not hasattr(profile, 'password_reset'): raise PasswordResetFieldException() authenticated = True except profile_model.DoesNotExist: authenticated = False # handle the form if request.method == 'POST' and profile: form = ResetForm(request.POST) if form.is_valid(): data = form.cleaned_data # set the user's password profile.user.set_password(data['password']) profile.user.save() # nullify the reset identifier to disable the old link profile.password_reset = None profile.save() else: form = ResetForm() # build context and render context = RequestContext(request, { 'form': form, 'authenticated': authenticated, }) return render_to_response('accounts/reset.html', context)
def action(self, request): """Sends an email to the user with a link to change their password.""" user = self.user profile_model = get_profile_model() # attempt to retrieve the user's profile try: profile = user.get_profile() except profile_model.DoesNotExist: raise ProfileRequiredException('password reset request') # attempt to set the password reset field reset_uuid = uuid.uuid4() if hasattr(profile, 'password_reset'): profile.password_reset = reset_uuid profile.save() else: raise PasswordResetFieldException() # generate the link to send in the email url = reverse('accounts_reset', args=[reset_uuid]) url = request.build_absolute_uri(url) # build the context for email message template rendering context = {'user': user, 'url': url} # send rendered messages to managers text_content = render_to_string('accounts/forgot_email.txt', context) html_content = render_to_string('accounts/forgot_email.html', context) message = mail.EmailMultiAlternatives( 'Password Reset Request', text_content, settings.DEFAULT_FROM_EMAIL, [user.email] ) message.attach_alternative(html_content, 'text/html') message.send() # return success return True
def avatar_clear(request): """Clears the avatar of a user.""" profile_model = get_profile_model() try: profile = request.user.get_profile() profile.image.delete() except profile_model.DoesNotExist: pass return redirect('accounts_avatar')
def save(self): """Saves the avatar for the indicated user.""" profile_model = get_profile_model() user = self.user try: avatar = self.cleaned_data['avatar'] profile = user.get_profile() profile.image.save(avatar.name, avatar) except profile_model.DoesNotExist: pass return user
def __init__(self, request, *args, **kwargs): kwargs = next_to_initial(kwargs, request) self.user = user = kwargs.pop('user', None) super(BaseProfileForm, self).__init__(*args, **kwargs) # setup intial values from instances if user: # attempt to get the profile for the user profile_model = get_profile_model() try: profile = user.get_profile() except profile_model.DoesNotExist: profile = None # update the initial values self.update_field_values(user) self.update_field_values(profile) # use meta specifications if hasattr(self, 'Meta'): # apply field order if specified if hasattr(self.Meta, 'field_order'): self.fields.keyOrder = self.Meta.field_order
def save(self): """Populates the user and profile objects with data from relevant fields and saves them. """ cleaned_data = self.cleaned_data profile_model = get_profile_model() user = self.user # construct the user if necessary if user is None: user = User() # get or construct the profile try: profile = user.get_profile() except profile_model.DoesNotExist: profile = profile_model() # set relative values on objects for key, value in cleaned_data.items(): # skip excluded fields if key in self.excluded_fields: continue # set field values on user if hasattr(user, key): setattr(user, key, value) # set field values on profile if hasattr(profile, key): setattr(profile, key, value) # set the user password password = cleaned_data.get('password', '') if len(password) > 0: user.set_password(password) # save the user user.save() # save the profile if profile.id is None: profile.user = user profile.save() # return the user object return user