Added order by on admin related fields
This commit is contained in:
parent
5494e5b7a4
commit
472eb70cb0
|
@ -234,11 +234,19 @@ class AccountAdminMixin(object):
|
||||||
formfield.widget.render = render
|
formfield.widget.render = render
|
||||||
# Filter related object by account
|
# Filter related object by account
|
||||||
formfield.queryset = formfield.queryset.filter(account=self.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':
|
elif db_field.name == 'account':
|
||||||
if self.account:
|
if self.account:
|
||||||
formfield.initial = self.account.pk
|
formfield.initial = self.account.pk
|
||||||
elif Account.objects.count() == 1:
|
elif Account.objects.count() == 1:
|
||||||
formfield.initial = 1
|
formfield.initial = 1
|
||||||
|
formfield.queryset = formfield.queryset.order_by('username')
|
||||||
return formfield
|
return formfield
|
||||||
|
|
||||||
def get_formset(self, request, obj=None, **kwargs):
|
def get_formset(self, request, obj=None, **kwargs):
|
||||||
|
|
|
@ -48,7 +48,7 @@ class DatabaseCreationForm(DatabaseUserCreationForm):
|
||||||
super(DatabaseCreationForm, self).__init__(*args, **kwargs)
|
super(DatabaseCreationForm, self).__init__(*args, **kwargs)
|
||||||
account_id = self.initial.get('account', self.initial_account)
|
account_id = self.initial.get('account', self.initial_account)
|
||||||
if account_id:
|
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 ]
|
choices = [ (u.pk, "%s (%s)" % (u, u.get_type_display())) for u in qs ]
|
||||||
self.fields['user'].queryset = qs
|
self.fields['user'].queryset = qs
|
||||||
self.fields['user'].choices = [(None, '--------'),] + choices
|
self.fields['user'].choices = [(None, '--------'),] + choices
|
||||||
|
|
|
@ -37,7 +37,8 @@ class MailboxForm(forms.ModelForm):
|
||||||
return mark_safe(output)
|
return mark_safe(output)
|
||||||
self.fields['addresses'].widget.render = render
|
self.fields['addresses'].widget.render = render
|
||||||
queryset = self.fields['addresses'].queryset
|
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:
|
if self.instance and self.instance.pk:
|
||||||
self.fields['addresses'].initial = self.instance.addresses.all()
|
self.fields['addresses'].initial = self.instance.addresses.all()
|
||||||
|
|
|
@ -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')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
|
@ -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'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -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'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,4 +1,5 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.utils import timezone
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from . import settings
|
from . import settings
|
||||||
|
@ -31,12 +32,12 @@ class Message(models.Model):
|
||||||
priority = models.PositiveIntegerField(_("Priority"), choices=PRIORITIES, default=NORMAL)
|
priority = models.PositiveIntegerField(_("Priority"), choices=PRIORITIES, default=NORMAL)
|
||||||
to_address = models.CharField(max_length=256)
|
to_address = models.CharField(max_length=256)
|
||||||
from_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"))
|
content = models.TextField(_("content"))
|
||||||
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
|
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
|
||||||
retries = models.PositiveIntegerField(_("retries"), default=0)
|
retries = models.PositiveIntegerField(_("retries"), default=0)
|
||||||
# TODO rename to last_try
|
# 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):
|
def __str__(self):
|
||||||
return '%s to %s' % (self.subject, self.to_address)
|
return '%s to %s' % (self.subject, self.to_address)
|
||||||
|
@ -46,6 +47,8 @@ class Message(models.Model):
|
||||||
# Max tries
|
# Max tries
|
||||||
if self.retries >= len(settings.MAILER_DEFERE_SECONDS):
|
if self.retries >= len(settings.MAILER_DEFERE_SECONDS):
|
||||||
self.state = self.FAILED
|
self.state = self.FAILED
|
||||||
|
self.retries += 1
|
||||||
|
self.last_retry = timezone.now()
|
||||||
self.save(update_fields=('state', 'retries', 'last_retry'))
|
self.save(update_fields=('state', 'retries', 'last_retry'))
|
||||||
|
|
||||||
def sent(self):
|
def sent(self):
|
||||||
|
|
Loading…
Reference in New Issue