示例#1
0
 def save(self, user):
     user.username = self.cleaned_data['username']
     change = ChangeUsername(user_id = user.id,name = self.cleaned_data['username'])
     change.save()
     # import datetime
     # user.get_profile().update_username_at = datetime.date.today()
     user.save()
示例#2
0
def account_setting(request, edit_profile_form=EditProfileForm,
                 template_name='userena/user_setting_form.html', success_url=None,
                 extra_context=None, **kwargs):

    user = request.user
    profile = user.get_profile()
    ty = request.GET.get("ty",'')
    uname = request.GET.get("uname",'')
    
    user_initial = {'first_name': user.first_name,'last_name': user.last_name,'username': user.username}
    is_change = has_change(user)
    form = edit_profile_form(instance=profile, initial=user_initial)
     
    if request.method == 'POST':
        form = edit_profile_form(request.POST, request.FILES, instance=profile,
                                 initial=user_initial)

        if form.is_valid():
            #profile = form.save()
            new_uname = request.POST.get('username','')
            if new_uname and user_initial['username'] != new_uname:
                match = re.findall(r'^[_a-zA-Z0-9\u4e00-\u9fa5]+$',new_uname)
                if match:
                    is_repeated = User.objects.filter(username=new_uname).count()
                    if is_repeated:
                        return redirect(reverse('userena_account_setting') + "?ty=1&uname=" + new_uname)
                    else:
                        user.username = new_uname
                        user.save()
                        change = ChangeUsername(user_id = user.id,name = new_uname)
                        change.save()
                else:
                    return redirect(reverse('userena_account_setting') + "?ty=2&uname=" + new_uname)
                
            profile = form.save()

            if userena_settings.USERENA_USE_MESSAGES:
                messages.success(request, _('Your profile has been updated.'),
                                 fail_silently=True)

            if success_url: redirect_to = success_url
            else: redirect_to = reverse('userena_account_setting')
            return redirect(redirect_to)

    if not extra_context: extra_context = dict()
    extra_context['form'] = form
    extra_context['user'] = user
    extra_context['profile'] = profile
    extra_context['is_change'] = is_change
    extra_context['ty'] = ty
    extra_context['uname'] = uname
    
    return ExtraContextTemplateView.as_view(template_name=template_name,
                                            extra_context=extra_context)(request)