Ejemplo n.º 1
0
    def edit(self, request, pk):
        obj = self.Model.objects.get(id=pk)
        form = self.Form(request.POST or None, instance=obj)

        user_form = UserForm(request.POST or None, instance=obj.user)

        if request.method == "GET":
            return render(
                request, "shared/editor.html", {
                    "sidebar": self.name,
                    "forms": [form, user_form, PasswordForm],
                    "index_link": self.Model.list_link()
                })
        elif request.method == "POST":
            password_form = PasswordForm(request.POST or None)
            if user_form.is_valid() and form.is_valid(
            ) and password_form.is_valid():
                user, partner = user_form.save(commit=False), form.save(
                    commit=False)
                user.set_password(password_form.cleaned_data.get("password"))
                user.save()
                form.user = user
                form.save()
                return redirect(self.Model.list_link())
            return render(
                request, "shared/editor.html", {
                    "sidebar": self.name,
                    "forms": [form, user_form, PasswordForm],
                    "index_link": self.Model.list_link()
                })
Ejemplo n.º 2
0
 def add(self, request):
     forms = [self.Form, UserForm, PasswordForm]
     if request.method == "GET":
         return render(
             request, "shared/editor.html", {
                 "sidebar": self.name,
                 "forms": forms,
                 "index_link": self.Model.list_link(),
             })
     elif request.method == "POST":
         user_form = UserForm(request.POST)
         password_form = PasswordForm(request.POST)
         form = self.Form(request.POST)
         if user_form.is_valid() and password_form.is_valid(
         ) and form.is_valid():
             user, account = user_form.save(commit=False), form.save(
                 commit=False)
             user.role = self.role
             user.set_password(password_form.cleaned_data.get("password"))
             user.save()
             account.user = user
             account.save()
             return redirect(self.Model.list_link())
         return render(
             request, "shared/editor.html", {
                 "sidebar": self.name,
                 "form": [form, user_form, password_form],
                 "index_link": self.Model.list_link()
             })
Ejemplo n.º 3
0
def pwd_post(request):
    request.session['tab'] = 'tab2'
    form = PasswordForm(request.POST)
    print request.POST.get('oldpassword', '')
    print request.user
    print form.is_valid()
    if form.is_valid():
        oldpassword = request.POST.get('oldpassword', '')
        print request.user.check_password(oldpassword)
        if request.user.check_password(oldpassword):
            newpassword = request.POST.get('newpassword1', '')
            request.user.set_password(newpassword)
            request.user.save()
        else:
            form.errors['oldpassword'] = ErrorList([u'原密码不正确啊'])
            return profile(request, error_pwd_form=form)
    else:
        return profile(request, error_pwd_form=form)
    return redirect(to='profile')
Ejemplo n.º 4
0
def edit(request):

    forms = []

    if request.user.role.name == "client":
        client = Client.objects.get(user=request.user)
        forms.append(ClientForm(request.POST or None, instance=client))
    elif request.user.role.name == "partner":
        partner = Partner.objects.get(user=request.user)
        forms.append(PartnerForm(request.POST or None, instance=partner))

    user_form = UserForm(request.POST or None, instance=request.user)

    forms += [user_form, PasswordForm]

    if request.method == "GET":
        return render(request, "shared/editor.html", {
            "forms": forms,
            "index_link": reverse("index")
        })
    elif request.method == "POST":
        password_form = PasswordForm(request.POST)

        additional = True

        if request.user.role.name == "client":
            client = Client.objects.get(user=request.user)
            form = ClientForm(request.POST or None, instance=client)
            forms.append(form)
            additional = form.is_valid()
        elif request.user.role.name == "partner":
            partner = Partner.objects.get(user=request.user)
            form = PartnerForm(request.POST or None, instance=partner)
            forms.append(form)
            additional = form.is_valid()

        if user_form.is_valid() and password_form.is_valid() and additional:
            if request.user.role.name in ("client", "partner"):
                form.save()
            user = user_form.save(commit=False)
            password = password_form.cleaned_data.get("password")
            if password and not user.check_password(password):
                user.set_password(password)
                user.save()
                return redirect("auth:logout")
            user.save()
            return redirect("index")
        else:
            return render(request, "shared/editor.html", {
                "forms": forms,
                "index_link": reverse("index")
            })