Просмотр исходного кода

WIP Usercp: change email/pass (incomplete)

Rafał Pitoń 11 лет назад
Родитель
Сommit
a9bbc1449b

+ 23 - 14
misago/templates/misago/usercp/change_email_password.html

@@ -1,10 +1,10 @@
 {% extends "misago/usercp/base.html" %}
-{% load i18n %}
+{% load i18n misago_forms %}
 
 
 {% block page %}
 <div class="form-panel">
-  <form method="POST" role="form">
+  <form method="POST" role="form" class="form-horizontal">
     {% csrf_token %}
 
     <div class="form-header">
@@ -16,23 +16,32 @@
 
     {% include "misago/form_errors.html" %}
 
-    <div class="form-body no-fieldsets">
+    <div class="form-body">
+      {% with label_class="col-md-3" field_class="col-md-9" %}
+      <fieldset>
+        <legend>{% trans "Fill out at least one field" %}</legend>
 
-      <div class="form-group">
-        <div class="control-input">
-          <input type="text" name="username" class="form-control input-lg" placeholder="{% trans "Username or e-mail" %}" {% if form.username.value %}value="{{ form.username.value }}"{% endif %}>
-        </div>
-      </div>
-      <div class="form-group">
-        <div class="control-input">
-          <input type="password" name="password" class="form-control input-lg" placeholder="{% trans "Password" %}">
-        </div>
-      </div>
+        {% form_row form.new_email label_class field_class %}
+        {% form_row form.new_password label_class field_class %}
+
+      </fieldset>
+      <fieldset>
+        <legend>{% trans "Confirm change" %}</legend>
 
+        {% form_row form.current_password label_class field_class %}
+
+      </fieldset>
+      {% endwith %}
     </div>
 
     <div class="form-footer">
-      <button class="btn btn-primary btn-block">{% trans "Sign in" %}</button>
+      <div class="row">
+        <div class="col-md-9 col-md-offset-3">
+
+          <button class="btn btn-primary">{% trans "Save changes" %}</button>
+
+        </div>
+      </div>
     </div>
 
   </form>

+ 40 - 4
misago/users/forms/usercp.py

@@ -69,7 +69,43 @@ class ChangeUsernameForm(forms.Form):
         return data
 
 
-class ChangeEmailPasswordForm(forms.ModelForm):
-    class Meta:
-        model = get_user_model()
-        fields = ['username', 'email', 'title']
+class ChangeEmailPasswordForm(forms.Form):
+    current_password = forms.CharField(
+        label=_("Current password"),
+        max_length=200,
+        required=False,
+        widget=forms.PasswordInput())
+
+    new_email = forms.CharField(
+        label=_("New e-mail"),
+        max_length=200,
+        required=False)
+
+    new_password = forms.CharField(
+        label=_("New password"),
+        max_length=200,
+        required=False,
+        widget=forms.PasswordInput())
+
+    def __init__(self, *args, **kwargs):
+        self.user = kwargs.pop('user', None)
+        super(ChangeEmailPasswordForm, self).__init__(*args, **kwargs)
+
+    def clean(self):
+        data = super(ChangeEmailPasswordForm, self).clean()
+
+        current_password = data.get('current_password')
+        new_email = data.get('new_email')
+        new_password = data.get('new_password')
+
+        if not data.get('current_password'):
+            message = _("You have to enter your current password.")
+            raise forms.ValidationError(message)
+
+        if not self.user.check_password(current_password):
+            message = _("Entered password is invalid.")
+            raise forms.ValidationError(message)
+
+        raise NotImplementedError("change email/pass form is incomplete")
+
+        return data

+ 10 - 2
misago/users/views/usercp.py

@@ -5,7 +5,8 @@ from django.utils.translation import ugettext as _
 
 from misago.users.decorators import deny_guests
 from misago.users.forms.usercp import (ChangeForumOptionsForm,
-                                       ChangeUsernameForm)
+                                       ChangeUsernameForm,
+                                       ChangeEmailPasswordForm)
 from misago.users.sites import usercp
 from misago.users.namechanges import UsernameChanges
 
@@ -65,4 +66,11 @@ def change_username(request):
 
 @deny_guests
 def change_email_password(request):
-    return render(request, 'misago/usercp/change_email_password.html')
+    form = ChangeEmailPasswordForm()
+    if request.method == 'POST':
+        form = ChangeEmailPasswordForm(request.POST, user=request.user)
+        if form.is_valid():
+            pass
+
+    return render(request, 'misago/usercp/change_email_password.html',
+                  {'form': form})