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

#923: removed dead code after serializers creation, updated project's template

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

+ 1 - 1
misago/core/testproject/urls.py

@@ -6,7 +6,7 @@ from django.views.decorators.cache import cache_page
 from django.views.decorators.http import last_modified
 from django.views.i18n import JavaScriptCatalog
 
-from misago.users.forms.auth import AdminAuthenticationForm
+from misago.admin.forms import AdminAuthenticationForm
 
 from . import views
 

+ 1 - 1
misago/project_template/project_name/urls.py

@@ -22,7 +22,7 @@ from django.views.decorators.cache import cache_page
 from django.views.decorators.http import last_modified
 from django.views.i18n import JavaScriptCatalog
 
-from misago.users.forms.auth import AdminAuthenticationForm
+from misago.admin.forms import AdminAuthenticationForm
 
 
 admin.autodiscover()

+ 0 - 0
misago/users/forms/admin.py → misago/users/forms.py


+ 0 - 0
misago/users/forms/__init__.py


+ 0 - 97
misago/users/forms/auth.py

@@ -1,97 +0,0 @@
-from django import forms
-from django.contrib.auth import authenticate, get_user_model
-from django.contrib.auth.forms import AuthenticationForm as BaseAuthenticationForm
-from django.core.exceptions import ValidationError
-from django.core.validators import validate_email
-from django.utils.translation import ugettext_lazy as _
-
-from misago.users.bans import get_user_ban
-
-
-UserModel = get_user_model()
-
-
-class MisagoAuthMixin(object):
-    error_messages = {
-        'empty_data': _("Fill out both fields."),
-        'invalid_login': _("Login or password is incorrect."),
-        'inactive_user': _("You have to activate your account before you will be able to sign in."),
-        'inactive_admin': _(
-            "Your account has to be activated by Administrator before you will be able to sign in."
-        ),
-    }
-
-    def confirm_user_active(self, user):
-        if user.requires_activation_by_admin:
-            raise ValidationError(self.error_messages['inactive_admin'], code='inactive_admin')
-
-        if user.requires_activation_by_user:
-            raise ValidationError(self.error_messages['inactive_user'], code='inactive_user')
-
-    def confirm_user_not_banned(self, user):
-        if not user.is_staff:
-            self.user_ban = get_user_ban(user)
-            if self.user_ban:
-                raise ValidationError('', code='banned')
-
-    def get_errors_dict(self):
-        error = self.errors.as_data()['__all__'][0]
-        if error.code == 'banned':
-            return self.user_ban.ban.get_serialized_message()
-        else:
-            error.message = error.messages[0]
-
-        return {'detail': error.message, 'code': error.code}
-
-
-class AuthenticationForm(MisagoAuthMixin, BaseAuthenticationForm):
-    """
-    Base class for authenticating users, Floppy-forms and
-    Misago login field compliant
-    """
-    username = forms.CharField(
-        label=_("Username or e-mail"),
-        required=False,
-        max_length=254,
-    )
-    password = forms.CharField(
-        label=_("Password"),
-        strip=False,
-        required=False,
-        widget=forms.PasswordInput,
-    )
-
-    def clean(self):
-        username = self.cleaned_data.get('username')
-        password = self.cleaned_data.get('password')
-
-        if username and password:
-            self.user_cache = authenticate(username=username, password=password)
-
-            if self.user_cache is None or not self.user_cache.is_active:
-                raise ValidationError(self.error_messages['invalid_login'], code='invalid_login')
-            else:
-                self.confirm_login_allowed(self.user_cache)
-        else:
-            raise ValidationError(self.error_messages['empty_data'], code='empty_data')
-
-        return self.cleaned_data
-
-    def confirm_login_allowed(self, user):
-        self.confirm_user_active(user)
-        self.confirm_user_not_banned(user)
-
-
-class AdminAuthenticationForm(AuthenticationForm):
-    required_css_class = 'required'
-
-    def __init__(self, *args, **kwargs):
-        self.error_messages.update({
-            'not_staff': _("Your account does not have admin privileges."),
-        })
-
-        super(AdminAuthenticationForm, self).__init__(*args, **kwargs)
-
-    def confirm_login_allowed(self, user):
-        if not user.is_staff:
-            raise forms.ValidationError(self.error_messages['not_staff'], code='not_staff')

+ 0 - 75
misago/users/forms/register.py

@@ -1,75 +0,0 @@
-from django import forms
-from django.contrib.auth import get_user_model
-from django.contrib.auth.password_validation import validate_password
-from django.core.exceptions import ValidationError
-from django.utils.translation import ugettext as _
-
-from misago.users import validators
-from misago.users.bans import get_email_ban, get_ip_ban, get_username_ban
-
-
-UserModel = get_user_model()
-
-
-class RegisterForm(forms.Form):
-    username = forms.CharField(validators=[validators.validate_username])
-    email = forms.CharField(validators=[validators.validate_email])
-    password = forms.CharField(strip=False)
-
-    # placeholder field for setting captcha errors on form
-    captcha = forms.CharField(required=False)
-
-    def __init__(self, *args, **kwargs):
-        self.request = kwargs.pop('request')
-        super(RegisterForm, self).__init__(*args, **kwargs)
-
-    def clean_username(self):
-        data = self.cleaned_data['username']
-
-        ban = get_username_ban(data, registration_only=True)
-        if ban:
-            if ban.user_message:
-                raise ValidationError(ban.user_message)
-            else:
-                raise ValidationError(_("This usernane is not allowed."))
-        return data
-
-    def clean_email(self):
-        data = self.cleaned_data['email']
-
-        ban = get_email_ban(data, registration_only=True)
-        if ban:
-            if ban.user_message:
-                raise ValidationError(ban.user_message)
-            else:
-                raise ValidationError(_("This e-mail address is not allowed."))
-        return data
-
-    def full_clean_password(self, cleaned_data):
-        if cleaned_data.get('password'):
-            validate_password(
-                cleaned_data['password'],
-                user=UserModel(
-                    username=cleaned_data.get('username'),
-                    email=cleaned_data.get('email'),
-                ),
-            )
-
-    def clean(self):
-        cleaned_data = super(RegisterForm, self).clean()
-
-        ban = get_ip_ban(self.request.user_ip, registration_only=True)
-        if ban:
-            if ban.user_message:
-                raise ValidationError(ban.user_message)
-            else:
-                raise ValidationError(_("New registrations from this IP address are not allowed."))
-
-        try:
-            self.full_clean_password(cleaned_data)
-        except forms.ValidationError as e:
-            self.add_error('password', e)
-
-        validators.validate_new_registration(self.request, self, cleaned_data)
-
-        return cleaned_data

+ 1 - 1
misago/users/views/admin/bans.py

@@ -2,7 +2,7 @@ from django.contrib import messages
 from django.utils.translation import ugettext_lazy as _
 
 from misago.admin.views import generic
-from misago.users.forms.admin import BanForm, SearchBansForm
+from misago.users.forms import BanForm, SearchBansForm
 from misago.users.models import Ban
 
 

+ 1 - 1
misago/users/views/admin/ranks.py

@@ -4,7 +4,7 @@ from django.urls import reverse
 from django.utils.translation import ugettext_lazy as _
 
 from misago.admin.views import generic
-from misago.users.forms.admin import RankForm
+from misago.users.forms import RankForm
 from misago.users.models import Rank
 
 

+ 1 - 1
misago/users/views/admin/users.py

@@ -13,7 +13,7 @@ from misago.core.mail import mail_users
 from misago.core.pgutils import batch_update
 from misago.threads.models import Thread
 from misago.users.avatars.dynamic import set_avatar as set_dynamic_avatar
-from misago.users.forms.admin import (
+from misago.users.forms import (
     BanUsersForm, EditUserForm, EditUserFormFactory, NewUserForm, SearchUsersForm)
 from misago.users.models import Ban
 from misago.users.profilefields import profilefields

+ 5 - 8
misago/users/views/forgottenpassword.py

@@ -49,14 +49,11 @@ def reset_password_form(request, pk, token):
             }, status=400
         )
 
-    request.frontend_context['url'].update({
-        'change_forgotten_password': reverse(
-            'misago:api:change-forgotten-password',
-            kwargs={
-                'pk': pk,
-                'token': token,
-            },
-        ),
+    request.frontend_context['store'].update({
+        'forgotten_password': {
+            'id': pk,
+            'token': token,
+        },
     })
 
     return render(request, 'misago/forgottenpassword/form.html')