示例#1
0
文件: views.py 项目: ruankranz/pychat
def proceed_email_changed(request):
    try:
        with transaction.atomic():
            token = request.GET['token']
            logger.debug('Proceed change email with token %s', token)
            user, verification = utils.get_user_by_code(
                token, Verification.TypeChoices.email)
            new_ver = send_new_email_ver(request, user, verification.email)
            user.email = verification.email
            user.email_verification = new_ver
            user.save(update_fields=('email', 'email_verification'))
            verification.verified = True
            verification.save(update_fields=('verified', ))
            logger.info('Email has been change for token %s user %s(id=%d)',
                        token, user.username, user.id)
            return render_to_response('email_changed.html', {
                'text':
                'Your email has been changed to {}.'.format(verification.email)
            },
                                      context_instance=RequestContext(request))
    except Exception as e:
        return render_to_response('email_changed.html', {
            'text':
            'Unable to change your email because {}'.format(e.message)
        },
                                  context_instance=RequestContext(request))
示例#2
0
 def post(self, request):
     logger.info('Saving profile: %s',
                 hide_fields(request.POST, ("base64_image", ), huge=True))
     user_profile = UserProfile.objects.get(pk=request.user.id)
     image_base64 = request.POST.get('base64_image')
     new_email = request.POST['email']
     if not new_email:
         new_email = None
     if new_email:
         utils.validate_email(new_email)
     utils.validate_user(request.POST['username'])
     if image_base64 is not None:
         image = extract_photo(image_base64)
         request.FILES['photo'] = image
     passwd = request.POST['password']
     if passwd:
         if request.user.password:
             is_valid = authenticate(username=request.user.username,
                                     password=request.POST['old_password'])
             if not is_valid:
                 return HttpResponse("Invalid old password",
                                     content_type='text/plain')
         utils.check_password(passwd)
         request.POST['password'] = make_password(passwd)
     form = UserProfileForm(request.POST,
                            request.FILES,
                            instance=user_profile)
     if form.is_valid():
         if not passwd:
             form.instance.password = form.initial['password']
         if new_email != form.initial['email']:
             if form.initial[
                     'email'] and form.instance.email_verification and form.instance.email_verification.verified:
                 verification = Verification(
                     type_enum=Verification.TypeChoices.email,
                     user_id=user_profile.id,
                     email=new_email)
                 verification.save()
                 send_email_change(request, request.user.username,
                                   form.initial['email'], verification,
                                   new_email)
                 raise ValidationError(
                     "In order to change an email please confirm it from you current address. We send you an verification email to {}."
                     .format(form.initial['email']))
             if new_email:
                 new_ver = send_new_email_ver(request, request.user,
                                              new_email)
                 form.instance.email_verification = new_ver
         profile = form.save()
         if passwd and form.initial['email']:
             send_password_changed(request, form.initial['email'])
         response = profile.photo.url if 'photo' in request.FILES else settings.VALIDATION_IS_OK
     else:
         response = form.errors
     return HttpResponse(response, content_type='text/plain')
示例#3
0
    def profile_save_user(self, in_message):
        message = in_message[VarNames.CONTENT]
        userprofile = UserProfile.objects.get(id=self.user_id)
        email_verification_id = userprofile.email_verification_id
        un = message[UserProfileVarNames.USERNAME]
        email = message[UserProfileVarNames.EMAIL]
        if userprofile.username != un:
            check_user(un)
        if userprofile.email != email:
            check_email(email)
            if userprofile.email and userprofile.email_verification and userprofile.email_verification.verified:
                verification = Verification(
                    type_enum=Verification.TypeChoices.email,
                    user_id=self.id,
                    email=email)
                verification.save()
                send_email_change(self.request, un, userprofile.email,
                                  verification, email)
                self.ws_write(
                    self.default(
                        "In order to change an email please confirm it from you current address. We send you an verification email to {}."
                        .format(userprofile.email), Actions.GROWL_MESSAGE,
                        HandlerNames.WS))
                email = userprofile.email  # Don't change email, we need to verify it!
            elif email:
                new_ver = send_new_email_ver(self.request, userprofile, email)
                email_verification_id = new_ver.id

        sex = message[UserProfileVarNames.SEX]
        UserProfile.objects.filter(id=self.user_id).update(
            username=un,
            name=message[UserProfileVarNames.NAME],
            city=message[UserProfileVarNames.CITY],
            surname=message[UserProfileVarNames.SURNAME],
            email=email,
            birthday=message[UserProfileVarNames.BIRTHDAY],
            contacts=message[UserProfileVarNames.CONTACTS],
            sex=settings.GENDERS_STR[sex],
            email_verification=email_verification_id)
        self.publish(
            self.set_user_profile(in_message[VarNames.JS_MESSAGE_ID], message),
            self.channel)
        if userprofile.sex_str != sex or userprofile.username != un:
            self.publish(self.changed_user_profile(sex, self.user_id, un),
                         settings.ALL_ROOM_ID)