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

another cleansource pass, fix #40: post validation framework

Rafał Pitoń 8 лет назад
Родитель
Сommit
94c90c228f

+ 3 - 3
misago/core/migrations/0002_basic_settings.py

@@ -15,9 +15,9 @@ def create_basic_settings_group(apps, schema_editor):
             'key': 'basic',
             'name': _("Basic forum settings"),
             'description': _(
-            "Those settings control most basic properties "
-            "of your forum like its name or description."
-        ),
+                "Those settings control most basic properties "
+                "of your forum like its name or description."
+            ),
             'settings': [
                 {
                     'setting': 'forum_name',

+ 19 - 19
misago/datamover/settings.py

@@ -47,13 +47,13 @@ SETTING_CONVERTER = {
     'thread_name_max': copy_value('thread_title_length_max'),
     'post_length_min': copy_value('post_length_min'),
     'account_activation': map_value(
-    'account_activation', {
-        'none': 'none',
-        'user': 'user',
-        'admin': 'admin',
-        'block': 'closed',
-    }
-),
+        'account_activation', {
+            'none': 'none',
+            'user': 'user',
+            'admin': 'admin',
+            'block': 'closed',
+        }
+    ),
     'username_length_min': copy_value('username_length_min'),
     'username_length_max': copy_value('username_length_max'),
     'password_length': copy_value('password_length_min'),
@@ -61,20 +61,20 @@ SETTING_CONVERTER = {
     'default_avatar': copy_value('default_avatar'),
     'upload_limit': copy_value('avatar_upload_limit'),
     'subscribe_start': map_value('subscribe_start', {
-    '0': 'no',
-    '1': 'watch',
-    '2': 'watch_email',
-}),
+        '0': 'no',
+        '1': 'watch',
+        '2': 'watch_email',
+    }),
     'subscribe_reply': map_value('subscribe_reply', {
-    '0': 'no',
-    '1': 'watch',
-    '2': 'watch_email',
-}),
+        '0': 'no',
+        '1': 'watch',
+        '2': 'watch_email',
+    }),
     'bots_registration': map_value('captcha_type', {
-    'no': 'no',
-    'recaptcha': 're',
-    'qa': 'qa',
-}),
+        'no': 'no',
+        'recaptcha': 're',
+        'qa': 'qa',
+    }),
     'recaptcha_public': copy_value('recaptcha_site_key'),
     'recaptcha_private': copy_value('recaptcha_secret_key'),
     'qa_test': copy_value('qa_question'),

+ 10 - 10
misago/legal/migrations/0001_initial.py

@@ -43,11 +43,11 @@ def create_legal_settings_group(apps, schema_editor):
                     'setting': 'terms_of_service',
                     'name': _("Terms contents"),
                     'description': _(
-                    "Your forums can have custom terms of "
-                    "service page. To create it, write or "
-                    "paste here its contents. Full Misago "
-                    "markup is available for formatting."
-                ),
+                        "Your forums can have custom terms of "
+                        "service page. To create it, write or "
+                        "paste here its contents. Full Misago "
+                        "markup is available for formatting."
+                    ),
                     'value': "",
                     'form_field': 'textarea',
                     'field_extra': {
@@ -85,11 +85,11 @@ def create_legal_settings_group(apps, schema_editor):
                     'setting': 'privacy_policy',
                     'name': _("Policy contents"),
                     'description': _(
-                    "Your forums can have custom privacy "
-                    "policy page. To create it, write or "
-                    "paste here its contents. Full Misago "
-                    "markup is available for formatting."
-                ),
+                        "Your forums can have custom privacy "
+                        "policy page. To create it, write or "
+                        "paste here its contents. Full Misago "
+                        "markup is available for formatting."
+                    ),
                     'value': "",
                     'form_field': 'textarea',
                     'field_extra': {

+ 3 - 3
misago/threads/api/threadendpoints/merge.py

@@ -51,9 +51,9 @@ def thread_merge_endpoint(request, thread, viewmodel):
         return Response(
             {
                 'detail': _(
-                "The thread you have entered link to doesn't "
-                "exist or you don't have permission to see it."
-            )
+                    "The thread you have entered link to doesn't "
+                    "exist or you don't have permission to see it."
+                )
             },
             status=400,
         )

+ 15 - 15
misago/threads/forms.py

@@ -60,25 +60,25 @@ class AttachmentTypeForm(forms.ModelForm):
         help_texts = {
             'extensions': _("List of comma separated file extensions associated with this attachment type."),
             'mimetypes': _(
-            "Optional list of comma separated mime types associated with this attachment type."
-        ),
+                "Optional list of comma separated mime types associated with this attachment type."
+            ),
             'size_limit': _(
-            "Maximum allowed uploaded file size for this type, in kb. "
-            "May be overriden via user permission."
-        ),
+                "Maximum allowed uploaded file size for this type, in kb. "
+                "May be overriden via user permission."
+            ),
             'status': _("Controls this attachment type availability on your site."),
             'limit_uploads_to': _(
-            "If you wish to limit option to upload files of this type to users with specific "
-            "roles, select them on this list. Otherwhise don't select any roles to allow all "
-            "users with permission to upload attachments to be able to upload attachments of "
-            "this type."
-        ),
+                "If you wish to limit option to upload files of this type to users with specific "
+                "roles, select them on this list. Otherwhise don't select any roles to allow all "
+                "users with permission to upload attachments to be able to upload attachments of "
+                "this type."
+            ),
             'limit_downloads_to': _(
-            "If you wish to limit option to download files of this type to users with "
-            "specific roles, select them on this list. Otherwhise don't select any roles to "
-            "allow all users with permission to download attachments to be able to download "
-            "attachments of this type."
-        ),
+                "If you wish to limit option to download files of this type to users with "
+                "specific roles, select them on this list. Otherwhise don't select any roles to "
+                "allow all users with permission to download attachments to be able to download "
+                "attachments of this type."
+            ),
         }
         widgets = {
             'limit_uploads_to': forms.CheckboxSelectMultiple,

+ 3 - 3
misago/threads/migrations/0002_threads_settings.py

@@ -57,9 +57,9 @@ def create_threads_settings_group(apps, schema_editor):
                     'setting': 'post_length_max',
                     'name': _("Maximum length"),
                     'description': _(
-                    "Maximum allowed user post length. Enter zero to disable. "
-                    "Longer posts are more costful to parse and index."
-                ),
+                        "Maximum allowed user post length. Enter zero to disable. "
+                        "Longer posts are more costful to parse and index."
+                    ),
                     'python_type': 'int',
                     'value': 60000,
                     'field_extra': {

+ 3 - 3
misago/threads/migrations/0004_update_settings.py

@@ -57,9 +57,9 @@ def update_threads_settings(apps, schema_editor):
                     'setting': 'post_length_max',
                     'name': _("Maximum length"),
                     'description': _(
-                    "Maximum allowed user post length. Enter zero to disable. "
-                    "Longer posts are more costful to parse and index."
-                ),
+                        "Maximum allowed user post length. Enter zero to disable. "
+                        "Longer posts are more costful to parse and index."
+                    ),
                     'python_type': 'int',
                     'default_value': 60000,
                     'field_extra': {

+ 2 - 2
misago/threads/search.py

@@ -47,8 +47,8 @@ class SearchThreads(SearchProvider):
 
         results = {
             'results': FeedSerializer(posts, many=True, context={
-        'user': self.request.user,
-    }).data
+            'user': self.request.user,
+        }).data
         }
         results.update(paginator)
 

+ 44 - 28
misago/threads/tests/test_validate_post.py

@@ -16,48 +16,64 @@ class ValidatePostTests(AuthenticatedUserTestCase):
 
     def test_title_validation(self):
         """validate_post tests title"""
-        response = self.client.post(self.api_link, data={
-            'category': self.category.pk,
-            'title': 'Check our l33t CaSiNo!',
-            'post': 'Lorem ipsum dolor met!',
-        })
+        response = self.client.post(
+            self.api_link,
+            data={
+                'category': self.category.pk,
+                'title': 'Check our l33t CaSiNo!',
+                'post': 'Lorem ipsum dolor met!',
+            }
+        )
         self.assertContains(response, "Don't discuss gambling!", status_code=400)
 
         # clean title passes validation
-        response = self.client.post(self.api_link, data={
-            'category': self.category.pk,
-            'title': 'Check our l33t place!',
-            'post': 'Lorem ipsum dolor met!',
-        })
+        response = self.client.post(
+            self.api_link,
+            data={
+                'category': self.category.pk,
+                'title': 'Check our l33t place!',
+                'post': 'Lorem ipsum dolor met!',
+            }
+        )
         self.assertEqual(response.status_code, 200)
 
     def test_post_validation(self):
         """validate_post tests post content"""
-        response = self.client.post(self.api_link, data={
-            'category': self.category.pk,
-            'title': 'Lorem ipsum dolor met!',
-            'post': 'Check our l33t CaSiNo!',
-        })
+        response = self.client.post(
+            self.api_link,
+            data={
+                'category': self.category.pk,
+                'title': 'Lorem ipsum dolor met!',
+                'post': 'Check our l33t CaSiNo!',
+            }
+        )
         self.assertContains(response, "Don't discuss gambling!", status_code=400)
 
         # clean post passes validation
-        response = self.client.post(self.api_link, data={
-            'category': self.category.pk,
-            'title': 'Lorem ipsum dolor met!',
-            'post': 'Check our l33t place!',
-        })
+        response = self.client.post(
+            self.api_link,
+            data={
+                'category': self.category.pk,
+                'title': 'Lorem ipsum dolor met!',
+                'post': 'Check our l33t place!',
+            }
+        )
         self.assertEqual(response.status_code, 200)
 
     def test_empty_input(self):
         """validate_post handles empty input"""
-        response = self.client.post(self.api_link, data={
-            'category': self.category.pk,
-        })
+        response = self.client.post(
+            self.api_link, data={
+                'category': self.category.pk,
+            }
+        )
         self.assertEqual(response.status_code, 400)
 
-        response = self.client.post(self.api_link, data={
-            'category': self.category.pk,
-            'title': '',
-            'post': '',
-        })
+        response = self.client.post(
+            self.api_link, data={
+                'category': self.category.pk,
+                'title': '',
+                'post': '',
+            }
+        )
         self.assertEqual(response.status_code, 400)

+ 3 - 6
misago/threads/tests/test_validators.py

@@ -1,12 +1,9 @@
-from rest_framework import serializers
-
 from django.core.exceptions import ValidationError
 from django.test import TestCase
 
 from misago.conf import settings
 from misago.core.testproject.validators import test_post_validator
-from misago.threads.validators import (
-    load_post_validators, validate_post, validate_post_length, validate_title)
+from misago.threads.validators import load_post_validators, validate_post_length, validate_title
 
 
 class LoadPostValidatorsTests(TestCase):
@@ -25,12 +22,12 @@ class LoadPostValidatorsTests(TestCase):
     def test_load_nonexistant_validator(self):
         """nonexistant validator raises"""
         with self.assertRaises(ImportError):
-            validators = load_post_validators([
+            load_post_validators([
                 'misago.core.yaddayadda.yaddayadda',
             ])
 
         with self.assertRaises(AttributeError):
-            validators = load_post_validators([
+            load_post_validators([
                 'misago.core.yaddayadda',
             ])
 

+ 2 - 2
misago/threads/views/thread.py

@@ -45,8 +45,8 @@ class ThreadBase(View):
     def get_template_context(self, request, thread, posts):
         context = {
             'url_name': ':'.join(request.resolver_match.namespaces + [
-            request.resolver_match.url_name,
-        ])
+                request.resolver_match.url_name,
+            ])
         }
 
         context.update(thread.get_template_context())

+ 28 - 28
misago/users/forms/admin.py

@@ -227,15 +227,15 @@ def UserFormFactory(FormType, instance):
 def StaffFlagUserFormFactory(FormType, instance):
     staff_fields = {
         'is_staff': YesNoSwitch(
-        label=EditUserForm.IS_STAFF_LABEL,
-        help_text=EditUserForm.IS_STAFF_HELP_TEXT,
-        initial=instance.is_staff
-    ),
+            label=EditUserForm.IS_STAFF_LABEL,
+            help_text=EditUserForm.IS_STAFF_HELP_TEXT,
+            initial=instance.is_staff
+        ),
         'is_superuser': YesNoSwitch(
-        label=EditUserForm.IS_SUPERUSER_LABEL,
-        help_text=EditUserForm.IS_SUPERUSER_HELP_TEXT,
-        initial=instance.is_superuser
-    ),
+            label=EditUserForm.IS_SUPERUSER_LABEL,
+            help_text=EditUserForm.IS_SUPERUSER_HELP_TEXT,
+            initial=instance.is_superuser
+        ),
     }
 
     return type('StaffUserForm', (FormType, ), staff_fields)
@@ -244,17 +244,17 @@ def StaffFlagUserFormFactory(FormType, instance):
 def UserIsActiveFormFactory(FormType, instance):
     is_active_fields = {
         'is_active': YesNoSwitch(
-        label=EditUserForm.IS_ACTIVE_LABEL,
-        help_text=EditUserForm.IS_ACTIVE_HELP_TEXT,
-        initial=instance.is_active
-    ),
+            label=EditUserForm.IS_ACTIVE_LABEL,
+            help_text=EditUserForm.IS_ACTIVE_HELP_TEXT,
+            initial=instance.is_active
+        ),
         'is_active_staff_message': forms.CharField(
-        label=EditUserForm.IS_ACTIVE_STAFF_MESSAGE_LABEL,
-        help_text=EditUserForm.IS_ACTIVE_STAFF_MESSAGE_HELP_TEXT,
-        initial=instance.is_active_staff_message,
-        widget=forms.Textarea(attrs={'rows': 3}),
-        required=False
-    ),
+            label=EditUserForm.IS_ACTIVE_STAFF_MESSAGE_LABEL,
+            help_text=EditUserForm.IS_ACTIVE_STAFF_MESSAGE_HELP_TEXT,
+            initial=instance.is_active_staff_message,
+            widget=forms.Textarea(attrs={'rows': 3}),
+            required=False
+        ),
     }
 
     return type('UserIsActiveForm', (FormType, ), is_active_fields)
@@ -326,17 +326,17 @@ def SearchUsersForm(*args, **kwargs):
 
     extra_fields = {
         'rank': forms.TypedChoiceField(
-        label=_("Has rank"),
-        coerce=int,
-        required=False,
-        choices=ranks_choices,
-    ),
+            label=_("Has rank"),
+            coerce=int,
+            required=False,
+            choices=ranks_choices,
+        ),
         'role': forms.TypedChoiceField(
-        label=_("Has role"),
-        coerce=int,
-        required=False,
-        choices=roles_choices,
-    )
+            label=_("Has role"),
+            coerce=int,
+            required=False,
+            choices=roles_choices,
+        )
     }
 
     FinalForm = type('SearchUsersFormFinal', (SearchUsersFormBase, ), extra_fields)

+ 8 - 8
misago/users/forms/auth.py

@@ -17,8 +17,8 @@ class MisagoAuthMixin(object):
         '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."
-    ),
+            "Your account has to be activated by Administrator before you will be able to sign in."
+        ),
     }
 
     def confirm_user_active(self, user):
@@ -135,13 +135,13 @@ class ResendActivationForm(GetUserForm):
 class ResetPasswordForm(GetUserForm):
     error_messages = {
         'inactive_user': _(
-        "You have to activate your account before "
-        "you will be able to request new password."
-    ),
+            "You have to activate your account before "
+            "you will be able to request new password."
+        ),
         'inactive_admin': _(
-        "Administrator has to activate your account before "
-        "you will be able to request new password."
-    ),
+            "Administrator has to activate your account before "
+            "you will be able to request new password."
+        ),
     }
 
     def confirm_allowed(self, user):

+ 10 - 10
misago/users/migrations/0002_users_settings.py

@@ -15,8 +15,8 @@ def create_users_settings_group(apps, schema_editor):
             'key': 'users',
             'name': _("Users"),
             'description': _(
-            "Those settings control user accounts default behaviour and features availability."
-        ),
+                "Those settings control user accounts default behaviour and features availability."
+            ),
             'settings': [
                 {
                     'setting': 'account_activation',
@@ -77,11 +77,11 @@ def create_users_settings_group(apps, schema_editor):
                     'name': _("Allow custom avatars"),
                     'legend': _("Avatars"),
                     'description': _(
-                    "Turning this option off will forbid "
-                    "forum users from using avatars from "
-                    "outside forums. Good for forums "
-                    "adressed at young users."
-                ),
+                        "Turning this option off will forbid "
+                        "forum users from using avatars from "
+                        "outside forums. Good for forums "
+                        "adressed at young users."
+                    ),
                     'python_type': 'bool',
                     'value': True,
                     'form_field': 'yesno',
@@ -103,9 +103,9 @@ def create_users_settings_group(apps, schema_editor):
                     'setting': 'default_gravatar_fallback',
                     'name': _("Fallback for default gravatar"),
                     'description': _(
-                    "Select which avatar to use when user has no "
-                    "gravatar associated with his e-mail address."
-                ),
+                        "Select which avatar to use when user has no "
+                        "gravatar associated with his e-mail address."
+                    ),
                     'value': 'dynamic',
                     'form_field': 'select',
                     'field_extra': {

+ 10 - 10
misago/users/migrations/0006_update_settings.py

@@ -16,8 +16,8 @@ def update_users_settings(apps, schema_editor):
             'key': 'users',
             'name': _("Users"),
             'description': _(
-            "Those settings control user accounts default behaviour and features availability."
-        ),
+                "Those settings control user accounts default behaviour and features availability."
+            ),
             'settings': [
                 {
                     'setting': 'account_activation',
@@ -63,11 +63,11 @@ def update_users_settings(apps, schema_editor):
                     'name': _("Allow custom avatars"),
                     'legend': _("Avatars"),
                     'description': _(
-                    "Turning this option off will forbid "
-                    "forum users from using avatars from "
-                    "outside forums. Good for forums "
-                    "adressed at young users."
-                ),
+                        "Turning this option off will forbid "
+                        "forum users from using avatars from "
+                        "outside forums. Good for forums "
+                        "adressed at young users."
+                    ),
                     'python_type': 'bool',
                     'value': True,
                     'form_field': 'yesno',
@@ -89,9 +89,9 @@ def update_users_settings(apps, schema_editor):
                     'setting': 'default_gravatar_fallback',
                     'name': _("Fallback for default gravatar"),
                     'description': _(
-                    "Select which avatar to use when user has no "
-                    "gravatar associated with his e-mail address."
-                ),
+                        "Select which avatar to use when user has no "
+                        "gravatar associated with his e-mail address."
+                    ),
                     'value': 'dynamic',
                     'form_field': 'select',
                     'field_extra': {

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

@@ -82,9 +82,9 @@ class UsersList(UserAdmin, generic.ListView):
             'name': _("Delete all"),
             'icon': 'fa fa-eraser',
             'confirmation': _(
-            "Are you sure you want to delete selected users? "
-            "This will also delete all content associated with their accounts."
-        ),
+                "Are you sure you want to delete selected users? "
+                "This will also delete all content associated with their accounts."
+            ),
             'is_atomic': False,
         },
     ]