From 472eb70cb0aa973e0423621d482c756a215346b6 Mon Sep 17 00:00:00 2001 From: Marc Aymerich Date: Wed, 17 Jun 2015 10:34:14 +0000 Subject: [PATCH] Added order by on admin related fields --- orchestra/contrib/accounts/admin.py | 8 ++++ orchestra/contrib/databases/forms.py | 2 +- orchestra/contrib/mailboxes/forms.py | 3 +- .../contrib/mailer/migrations/0001_initial.py | 38 +++++++++++++++++++ .../migrations/0002_auto_20150617_1021.py | 24 ++++++++++++ .../migrations/0003_auto_20150617_1024.py | 19 ++++++++++ .../contrib/mailer/migrations/__init__.py | 0 orchestra/contrib/mailer/models.py | 7 +++- 8 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 orchestra/contrib/mailer/migrations/0001_initial.py create mode 100644 orchestra/contrib/mailer/migrations/0002_auto_20150617_1021.py create mode 100644 orchestra/contrib/mailer/migrations/0003_auto_20150617_1024.py create mode 100644 orchestra/contrib/mailer/migrations/__init__.py diff --git a/orchestra/contrib/accounts/admin.py b/orchestra/contrib/accounts/admin.py index 2470e25b..3ebaa6cc 100644 --- a/orchestra/contrib/accounts/admin.py +++ b/orchestra/contrib/accounts/admin.py @@ -234,11 +234,19 @@ class AccountAdminMixin(object): formfield.widget.render = render # Filter related object by account formfield.queryset = formfield.queryset.filter(account=self.account) + # Apply heuristic order by + if not formfield.queryset.query.order_by: + related_fields = db_field.related_model._meta.get_all_field_names() + if 'name' in related_fields: + formfield.queryset = formfield.queryset.order_by('name') + elif 'username' in related_fields: + formfield.queryset = formfield.queryset.order_by('username') elif db_field.name == 'account': if self.account: formfield.initial = self.account.pk elif Account.objects.count() == 1: formfield.initial = 1 + formfield.queryset = formfield.queryset.order_by('username') return formfield def get_formset(self, request, obj=None, **kwargs): diff --git a/orchestra/contrib/databases/forms.py b/orchestra/contrib/databases/forms.py index 78000f0b..41a13f84 100644 --- a/orchestra/contrib/databases/forms.py +++ b/orchestra/contrib/databases/forms.py @@ -48,7 +48,7 @@ class DatabaseCreationForm(DatabaseUserCreationForm): super(DatabaseCreationForm, self).__init__(*args, **kwargs) account_id = self.initial.get('account', self.initial_account) if account_id: - qs = self.fields['user'].queryset.filter(account=account_id) + qs = self.fields['user'].queryset.filter(account=account_id).order_by('username') choices = [ (u.pk, "%s (%s)" % (u, u.get_type_display())) for u in qs ] self.fields['user'].queryset = qs self.fields['user'].choices = [(None, '--------'),] + choices diff --git a/orchestra/contrib/mailboxes/forms.py b/orchestra/contrib/mailboxes/forms.py index 2f515058..549bc5f8 100644 --- a/orchestra/contrib/mailboxes/forms.py +++ b/orchestra/contrib/mailboxes/forms.py @@ -37,7 +37,8 @@ class MailboxForm(forms.ModelForm): return mark_safe(output) self.fields['addresses'].widget.render = render queryset = self.fields['addresses'].queryset - self.fields['addresses'].queryset = queryset.filter(account=self.modeladmin.account.pk) + realted_addresses = queryset.filter(account=self.modeladmin.account.pk).order_by('name') + self.fields['addresses'].queryset = realted_addresses if self.instance and self.instance.pk: self.fields['addresses'].initial = self.instance.addresses.all() diff --git a/orchestra/contrib/mailer/migrations/0001_initial.py b/orchestra/contrib/mailer/migrations/0001_initial.py new file mode 100644 index 00000000..4b19ca88 --- /dev/null +++ b/orchestra/contrib/mailer/migrations/0001_initial.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Message', + fields=[ + ('id', models.AutoField(auto_created=True, serialize=False, verbose_name='ID', primary_key=True)), + ('state', models.CharField(choices=[('QUEUED', 'Queued'), ('SENT', 'Sent'), ('DEFERRED', 'Deferred'), ('FAILED', 'Failes')], default='QUEUED', verbose_name='State', max_length=16)), + ('priority', models.PositiveIntegerField(choices=[(0, 'Critical (not queued)'), (1, 'High'), (2, 'Normal'), (3, 'Low')], default=2, verbose_name='Priority')), + ('to_address', models.CharField(max_length=256)), + ('from_address', models.CharField(max_length=256)), + ('subject', models.CharField(max_length=256, verbose_name='subject')), + ('content', models.TextField(verbose_name='content')), + ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='created at')), + ('retries', models.PositiveIntegerField(default=0, verbose_name='retries')), + ('last_retry', models.DateTimeField(auto_now=True, verbose_name='last try')), + ], + ), + migrations.CreateModel( + name='SMTPLog', + fields=[ + ('id', models.AutoField(auto_created=True, serialize=False, verbose_name='ID', primary_key=True)), + ('result', models.CharField(choices=[('SUCCESS', 'Success'), ('FAILURE', 'Failure')], default='SUCCESS', max_length=16)), + ('date', models.DateTimeField(auto_now_add=True)), + ('log_message', models.TextField()), + ('message', models.ForeignKey(to='mailer.Message', editable=False, related_name='logs')), + ], + ), + ] diff --git a/orchestra/contrib/mailer/migrations/0002_auto_20150617_1021.py b/orchestra/contrib/mailer/migrations/0002_auto_20150617_1021.py new file mode 100644 index 00000000..d0415e27 --- /dev/null +++ b/orchestra/contrib/mailer/migrations/0002_auto_20150617_1021.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('mailer', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='message', + name='last_retry', + field=models.DateTimeField(verbose_name='last try'), + ), + migrations.AlterField( + model_name='message', + name='subject', + field=models.TextField(verbose_name='subject'), + ), + ] diff --git a/orchestra/contrib/mailer/migrations/0003_auto_20150617_1024.py b/orchestra/contrib/mailer/migrations/0003_auto_20150617_1024.py new file mode 100644 index 00000000..22bfb79c --- /dev/null +++ b/orchestra/contrib/mailer/migrations/0003_auto_20150617_1024.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('mailer', '0002_auto_20150617_1021'), + ] + + operations = [ + migrations.AlterField( + model_name='message', + name='last_retry', + field=models.DateTimeField(null=True, verbose_name='last try'), + ), + ] diff --git a/orchestra/contrib/mailer/migrations/__init__.py b/orchestra/contrib/mailer/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/orchestra/contrib/mailer/models.py b/orchestra/contrib/mailer/models.py index e0e3c3b8..4d396c13 100644 --- a/orchestra/contrib/mailer/models.py +++ b/orchestra/contrib/mailer/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.utils import timezone from django.utils.translation import ugettext_lazy as _ from . import settings @@ -31,12 +32,12 @@ class Message(models.Model): priority = models.PositiveIntegerField(_("Priority"), choices=PRIORITIES, default=NORMAL) to_address = models.CharField(max_length=256) from_address = models.CharField(max_length=256) - subject = models.CharField(_("subject"), max_length=256) + subject = models.TextField(_("subject")) content = models.TextField(_("content")) created_at = models.DateTimeField(_("created at"), auto_now_add=True) retries = models.PositiveIntegerField(_("retries"), default=0) # TODO rename to last_try - last_retry = models.DateTimeField(_("last try"), auto_now=True) + last_retry = models.DateTimeField(_("last try"), null=True) def __str__(self): return '%s to %s' % (self.subject, self.to_address) @@ -46,6 +47,8 @@ class Message(models.Model): # Max tries if self.retries >= len(settings.MAILER_DEFERE_SECONDS): self.state = self.FAILED + self.retries += 1 + self.last_retry = timezone.now() self.save(update_fields=('state', 'retries', 'last_retry')) def sent(self):