From 1e425f5f2973e3f9bccab69325d49f156fbe0527 Mon Sep 17 00:00:00 2001 From: Emmanuel Cazenave Date: Wed, 9 Dec 2020 15:19:06 +0100 Subject: [PATCH] misc: move Meta and Media at the beginning of their outer class (#49290) --- docbow_project/docbow/forms.py | 86 +++++++++++++++++--------------- docbow_project/docbow/models.py | 60 +++++++++++----------- docbow_project/docbow/tables.py | 5 +- docbow_project/docbow/widgets.py | 17 ++++--- 4 files changed, 87 insertions(+), 81 deletions(-) diff --git a/docbow_project/docbow/forms.py b/docbow_project/docbow/forms.py index f28979c..3c286a8 100644 --- a/docbow_project/docbow/forms.py +++ b/docbow_project/docbow/forms.py @@ -61,6 +61,9 @@ class ForwardingForm(RecipientForm, Form): recipients = RecipientField(label=_('Recipients'), required=True) sender = ModelChoiceField(label=_('Sender'), queryset=User.objects.all()) + class Media: + js = ('js/askdirtyform.js',) + def __init__(self, *args, **kwargs): self.user = kwargs.pop('user', None) assert self.user @@ -87,8 +90,6 @@ class ForwardingForm(RecipientForm, Form): cleaned_data['sender'] = self.default_sender return cleaned_data - class Media: - js = ('js/askdirtyform.js',) def max_filename_length(): '''Compute the maximum filename length from the possible maximum length of @@ -103,6 +104,19 @@ class FileForm(RecipientForm, ModelForm): user = None recipients = RecipientField(label=_('Recipients')) + class Meta: + model = Document + exclude = ('filetype', 'date', 'to_user', 'to_list', '_timestamp', 'real_sender', + 'reply_to') + + class Media: + css = { + 'all': ( + 'docbow/css/send-file.css', + 'docbow/css/send_file_form.css' + ) + } + js = ('js/askdirtyform.js', 'js/url-preload.js') def __init__(self, *args, **kwargs): '''Initialize the form.''' @@ -167,20 +181,6 @@ class FileForm(RecipientForm, ModelForm): choices.insert(0, ('---','---')) return choices - class Meta: - model = Document - exclude = ('filetype', 'date', 'to_user', 'to_list', '_timestamp', 'real_sender', - 'reply_to') - - class Media: - css = { - 'all': ( - 'docbow/css/send-file.css', - 'docbow/css/send_file_form.css' - ) - } - js = ('js/askdirtyform.js', 'js/url-preload.js') - def clean(self): '''Validate that there is at least one file attached to this mailing.''' cleaned_data = super(FileForm, self).clean() @@ -253,12 +253,6 @@ class AnonymousContactForm(ContactForm): class MailingListForm(ModelForm): '''Admin form to edit MailingList objects''' - def __init__(self, *args, **kwargs): - '''Orders members by their username, use their username to display them.''' - ModelForm.__init__(self, *args, **kwargs) - self.fields['members'].queryset = non_guest_users() \ - .order_by('username') - self.fields['members'].label_from_instance = lambda y: username(y) class Meta: model = MailingList @@ -267,6 +261,13 @@ class MailingListForm(ModelForm): 'members': AdminFilteredSelectMultiple(_('Persons'), False), } + def __init__(self, *args, **kwargs): + '''Orders members by their username, use their username to display them.''' + ModelForm.__init__(self, *args, **kwargs) + self.fields['members'].queryset = non_guest_users() \ + .order_by('username') + self.fields['members'].label_from_instance = lambda y: username(y) + class UserChoiceField(ModelChoiceField): def label_from_instance(self, user): @@ -372,6 +373,14 @@ class ProfileForm(ModelForm): phone. ''' + class Meta: + model = DocbowProfile + fields = () + if app_settings.MOBILE_PHONE: + fields += ('accept_notifications', 'mobile_phone') + if app_settings.PERSONAL_EMAIL: + fields += ('personal_email',) + def __init__(self, request, *args, **kwargs): '''Initialize the form object. Define a custom help text. @@ -425,14 +434,6 @@ class ProfileForm(ModelForm): instance.save() return instance - class Meta: - model = DocbowProfile - fields = () - if app_settings.MOBILE_PHONE: - fields += ('accept_notifications', 'mobile_phone') - if app_settings.PERSONAL_EMAIL: - fields += ('personal_email',) - import unicodedata from django.utils.six import PY3 @@ -539,6 +540,15 @@ class FilterForm(forms.Form): not_after = forms.DateField(label=_('To'), required=False, localize=True) search_terms = forms.CharField(label=_('Search terms'), required=False) + class Media: + js = ( + 'jquery-ui/js/jquery-ui-1.12.1-autocomplete-datepicker.min.js', + 'docbow/js/filter-form.js', + ) + css = { + 'all': ('jquery-ui/css/jquery-ui-1.12.1.css',), + } + def __init__(self, *args, **kwargs): request = kwargs.pop('request') outbox = kwargs.pop('outbox', False) @@ -558,14 +568,6 @@ class FilterForm(forms.Form): raise ValidationError(_('From must be inferior or equal to To')) return cleaned_data - class Media: - js = ( - 'jquery-ui/js/jquery-ui-1.12.1-autocomplete-datepicker.min.js', - 'docbow/js/filter-form.js', - ) - css = { - 'all': ('jquery-ui/css/jquery-ui-1.12.1.css',), - } class EmailForm(ModelForm): old_email = forms.EmailField(label=_('Old email'), required=False, @@ -577,6 +579,10 @@ class EmailForm(ModelForm): required=True, widget=forms.TextInput()) + class Meta: + model = User + fields = ('email',) + def __init__(self, *args, **kwargs): super(EmailForm, self).__init__(*args, **kwargs) self.initial['email'] = '' @@ -596,11 +602,9 @@ class EmailForm(ModelForm): self._errors['email'] = self.error_class([_('emails are not equal')]) return cleaned_data - class Meta: - model = User - fields = ('email',) class NotificationPreferencesForm(Form): + class Media: js = ('docbow/js/checkall.js',) diff --git a/docbow_project/docbow/models.py b/docbow_project/docbow/models.py index a78b909..ccee52a 100644 --- a/docbow_project/docbow/models.py +++ b/docbow_project/docbow/models.py @@ -74,14 +74,14 @@ class FileType(NameNaturalKey, Model): name = CharField(max_length=128, unique=True) is_active = BooleanField(verbose_name=_('is active'), default=True, blank=True) - def __str__(self): - return self.name - class Meta: ordering = ['name'] verbose_name = _('File type') verbose_name_plural = _('File types') + def __str__(self): + return self.name + class FileTypeAttachedFileKindManager(Manager): def get_by_natural_key(self, name, file_type_name): @@ -108,6 +108,12 @@ class FileTypeAttachedFileKind(Model): verbose_name=_('minimum number of files')) position = PositiveSmallIntegerField(verbose_name=_('position')) + class Meta: + ordering = ('file_type', 'position', 'name') + unique_together = (('name', 'file_type'),) + verbose_name = _('file type attached file kind') + verbose_name_plural = _('file type attached file kinds') + def clean(self): if self.cardinality != 0 and not self.minimum <= self.cardinality: raise ValidationError(_('minimum must be inferior to maximum number of files if maximum is not zero')) @@ -128,12 +134,6 @@ class FileTypeAttachedFileKind(Model): def __str__(self): return self.name - class Meta: - ordering = ('file_type', 'position', 'name') - unique_together = (('name', 'file_type'),) - verbose_name = _('file type attached file kind') - verbose_name_plural = _('file type attached file kinds') - @python_2_unicode_compatible class Content(Model): @@ -142,14 +142,14 @@ class Content(Model): description = CharField(max_length=128, unique=True) - def __str__(self): - return self.description - class Meta: ordering = ['description'] verbose_name = _('Content') verbose_name_plural = _('Contents') + def __str__(self): + return self.description + def username(user): '''Return the full name of a user if it has one, the username otherwise.''' @@ -468,6 +468,10 @@ class AutomaticForwarding(Model): forward_to_list = ManyToManyField('MailingList', blank=True, verbose_name=_('Groups to forward to'), related_name='as_recipient_forwarding_rules') + class Meta: + verbose_name = _('Automatic forwarding rule') + verbose_name_plural = _('Automatic forwarding rules') + def __str__(self): '''Return a display string for the forwarding rule.''' ctx = { @@ -501,11 +505,6 @@ class AutomaticForwarding(Model): recipients_count=recipients_count, document_forwarded=document_forwarded) - class Meta: - verbose_name = _('Automatic forwarding rule') - verbose_name_plural = _('Automatic forwarding rules') - - @python_2_unicode_compatible class AttachedFile(Model): @@ -557,16 +556,19 @@ class Delegation(Model): ''' Delegate account, managable by user themselves. ''' + + by = ForeignKey(User, related_name='delegations_to', on_delete=CASCADE, + verbose_name=pgettext_lazy('delegation from', "From")) + to = ForeignKey(User, related_name='delegations_by', on_delete=CASCADE, + verbose_name=pgettext_lazy('delegation to', "To")) + class Meta: ordering = [ 'by' ] verbose_name = _('Account delegation') verbose_name_plural = _('Account delegations') db_table = 'auth_delegation' unique_together = (('by', 'to'),) - by = ForeignKey(User, related_name='delegations_to', on_delete=CASCADE, - verbose_name=pgettext_lazy('delegation from', "From")) - to = ForeignKey(User, related_name='delegations_by', on_delete=CASCADE, - verbose_name=pgettext_lazy('delegation to', "To")) + def __str__(self): return u'delegation from {0}:{0.id} to {1}:{1.id}'.format(self.by, self.to) @@ -599,10 +601,7 @@ class MailingListManager(GetByNameManager): @python_2_unicode_compatible class MailingList(NameNaturalKey, Model): '''A list of recipients.''' - class Meta: - ordering = [ 'name' ] - verbose_name = _('Mailing list') - verbose_name_plural = _('Mailing lists') + name = CharField(max_length=400, verbose_name=_('Name')) members = ManyToManyField(User, verbose_name=_('Members'), blank=True, related_name='mailing_lists') @@ -614,6 +613,11 @@ class MailingList(NameNaturalKey, Model): objects = MailingListManager() + class Meta: + ordering = [ 'name' ] + verbose_name = _('Mailing list') + verbose_name_plural = _('Mailing lists') + def recursive_members(self, sublist_traversed=None): '''Traverse this list and all its recursive sublist and accumulate members.''' @@ -784,12 +788,12 @@ class Notification(Model): failure = TextField(blank=True, null=True) ctx = PickledObjectField(blank=True, null=True) - def __str__(self): - return _(u'notification {0}:{1}').format(self.kind, self.id) - class Meta: ordering = ('-id',) + def __str__(self): + return _(u'notification {0}:{1}').format(self.kind, self.id) + class NotificationPreference(Model): '''Store preferences of users toward notification methods''' diff --git a/docbow_project/docbow/tables.py b/docbow_project/docbow/tables.py index 06bd619..3c3e3c5 100644 --- a/docbow_project/docbow/tables.py +++ b/docbow_project/docbow/tables.py @@ -10,6 +10,7 @@ from docbow_project.docbow import models class MailboxTable(tables.Table): + class Meta: model = models.Document attrs = {"class": "paleblue"} @@ -32,7 +33,6 @@ class OutboxCsvTable(tables.Table): date = tables.Column(accessor='date', verbose_name=_('date_header')) - class Meta: model = models.Document fields = ('official_sender',) @@ -93,7 +93,6 @@ class OutboxTable(OutboxBaseTable): orderable=False, verbose_name=' ') - class Meta: model = models.Document fields = ('official_sender',) @@ -114,7 +113,6 @@ class InboxCsvTable(tables.Table): date = tables.Column( accessor='date', verbose_name=_('date_header')) - class Meta: model = models.Document attrs = {"class": "paleblue mailbox-table"} @@ -168,7 +166,6 @@ class InboxTable(InboxBaseTable): delete = tables.TemplateColumn(template_name='docbow/inbox_delete_column.html', orderable=False, verbose_name=' ') - class Meta: model = models.Document fields = ('select', 'seen', 'filetype',) diff --git a/docbow_project/docbow/widgets.py b/docbow_project/docbow/widgets.py index ec811c4..df8d092 100644 --- a/docbow_project/docbow/widgets.py +++ b/docbow_project/docbow/widgets.py @@ -64,6 +64,15 @@ class JqueryFileUploadFileInput(MultiFileInput): template_name = 'docbow/upload-multiwidget.html' + class Media: + js = ('jquery-ui/js/jquery-ui-1.8.4.min.js', + 'jquery-plugin/js/jquery.tmpl.min-beta1.js', + 'jquery-plugin/js/jquery.iframe-transport.js', + 'jquery-plugin/js/jquery.fileupload.js', + 'jquery-plugin/js/jquery.fileupload-ui.js') + css = {'all': ('jquery-ui/css/jquery-ui-1.8.4.css', + 'jquery-plugin/css/jquery.fileupload-ui.css',)} + def __init__(self, extensions=[], attached_file_kind=None, *args,**kwargs): self.extensions = extensions self.attached_file_kind = attached_file_kind @@ -92,14 +101,6 @@ class JqueryFileUploadFileInput(MultiFileInput): output = render_to_string('docbow/upload.html', self._get_context(name)) return mark_safe(output) - class Media: - js = ('jquery-ui/js/jquery-ui-1.8.4.min.js', - 'jquery-plugin/js/jquery.tmpl.min-beta1.js', - 'jquery-plugin/js/jquery.iframe-transport.js', - 'jquery-plugin/js/jquery.fileupload.js', - 'jquery-plugin/js/jquery.fileupload-ui.js') - css = {'all': ('jquery-ui/css/jquery-ui-1.8.4.css', - 'jquery-plugin/css/jquery.fileupload-ui.css',)} class JqueryFileUploadInput(MultiWidget): needs_multipart_form = True