Fixed strange bug on adminformset_factory

This commit is contained in:
Marc Aymerich 2015-10-30 12:09:01 +00:00
parent 5365cbeb0a
commit 18582237bd
7 changed files with 52 additions and 27 deletions

View file

@ -444,3 +444,6 @@ mkhomedir_helper or create ssh homes with bash.rc and such
# mailscanner phishing, spam, whitelist choices # mailscanner phishing, spam, whitelist choices
# show base and total desglosed

View file

@ -1,3 +1,4 @@
import textwrap
from functools import partial from functools import partial
from django import forms from django import forms
@ -35,32 +36,47 @@ class AdminFormMixin(object):
class AdminFormSet(BaseModelFormSet): class AdminFormSet(BaseModelFormSet):
def as_admin(self): def as_admin(self):
prepopulated = {} template = Template(textwrap.dedent("""\
fieldsets = [ <div class="inline-group">
(None, {'fields': list(self.form().fields.keys())}) <div class="tabular inline-related last-related">
] {{ formset.management_form }}
readonly = getattr(self.form.Meta, 'readonly_fields', ()) <fieldset class="module">
if not hasattr(self.modeladmin, 'verbose_name_plural'): {{ formset.non_form_errors.as_ul }}
opts = self.modeladmin.model._meta <table id="formset" class="form">
self.modeladmin.verbose_name_plural = opts.verbose_name_plural {% for form in formset.forms %}
inline_admin_formset = helpers.InlineAdminFormSet(self.modeladmin, self, {% if forloop.first %}
fieldsets, prepopulated, readonly, model_admin=self.modeladmin) <thead><tr>
template = Template( {% for field in form.visible_fields %}
'{% include "admin/edit_inline/tabular.html" %}' <th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr></thead>
{% endif %}
<tr class="{% cycle row1,row2 %}">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</fieldset>
</div>
</div>""")
) )
context = Context({ context = Context({
'inline_admin_formset': inline_admin_formset 'formset': self
}) })
return template.render(context) return template.render(context)
def adminmodelformset_factory(modeladmin, form, formset=AdminFormSet, **kwargs):
model = kwargs.pop('model', modeladmin.model)
formset = modelformset_factory(model, form=form, formset=formset, **kwargs)
formset.modeladmin = modeladmin
return formset
class AdminPasswordChangeForm(forms.Form): class AdminPasswordChangeForm(forms.Form):
""" """
A form used to change the password of a user in the admin interface. A form used to change the password of a user in the admin interface.

View file

@ -6,14 +6,15 @@ from django.contrib import messages
from django.contrib.admin import helpers from django.contrib.admin import helpers
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.db import transaction from django.db import transaction
from django.forms.models import modelformset_factory
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.utils import translation, timezone from django.utils import translation, timezone
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils.translation import ungettext, ugettext_lazy as _ from django.utils.translation import ungettext, ugettext_lazy as _
from orchestra.admin.forms import adminmodelformset_factory
from orchestra.admin.decorators import action_with_confirmation from orchestra.admin.decorators import action_with_confirmation
from orchestra.admin.forms import AdminFormSet
from orchestra.admin.utils import get_object_from_url, change_url from orchestra.admin.utils import get_object_from_url, change_url
from . import settings from . import settings
@ -41,7 +42,7 @@ def close_bills(modeladmin, request, queryset, action='close_bills'):
if not bill.is_open: if not bill.is_open:
messages.warning(request, _("Selected bills should be in open state")) messages.warning(request, _("Selected bills should be in open state"))
return False return False
SelectSourceFormSet = adminmodelformset_factory(modeladmin, SelectSourceForm, extra=0) SelectSourceFormSet = modelformset_factory(modeladmin.model, form=SelectSourceForm, formset=AdminFormSet, extra=0)
formset = SelectSourceFormSet(queryset=queryset) formset = SelectSourceFormSet(queryset=queryset)
if request.POST.get('post') == 'generic_confirmation': if request.POST.get('post') == 'generic_confirmation':
formset = SelectSourceFormSet(request.POST, request.FILES, queryset=queryset) formset = SelectSourceFormSet(request.POST, request.FILES, queryset=queryset)

View file

@ -7,7 +7,7 @@ from orchestra.forms import SpanWidget
class SelectSourceForm(forms.ModelForm): class SelectSourceForm(forms.ModelForm):
bill_link = forms.CharField(label=_("Number"), required=False, widget=SpanWidget) bill_link = forms.CharField(label=_("Number"), required=False, widget=SpanWidget)
account_link = forms.CharField(label=_("Account"), required=False) account_link = forms.CharField(label=_("Account"), required=False, widget=SpanWidget)
show_total = forms.CharField(label=_("Total"), required=False, widget=SpanWidget) show_total = forms.CharField(label=_("Total"), required=False, widget=SpanWidget)
display_type = forms.CharField(label=_("Type"), required=False, widget=SpanWidget) display_type = forms.CharField(label=_("Type"), required=False, widget=SpanWidget)
source = forms.ChoiceField(label=_("Source"), required=False) source = forms.ChoiceField(label=_("Source"), required=False)
@ -16,7 +16,6 @@ class SelectSourceForm(forms.ModelForm):
fields = ( fields = (
'bill_link', 'display_type', 'account_link', 'show_total', 'source' 'bill_link', 'display_type', 'account_link', 'show_total', 'source'
) )
readonly_fields = ('account_link',)
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(SelectSourceForm, self).__init__(*args, **kwargs) super(SelectSourceForm, self).__init__(*args, **kwargs)
@ -34,6 +33,7 @@ class SelectSourceForm(forms.ModelForm):
self.fields['show_total'].widget.display = total self.fields['show_total'].widget.display = total
self.fields['bill_link'].widget.display = admin_link('__str__')(bill) self.fields['bill_link'].widget.display = admin_link('__str__')(bill)
self.fields['display_type'].widget.display = bill.get_type_display() self.fields['display_type'].widget.display = bill.get_type_display()
self.fields['account_link'].widget.display = admin_link('account')(bill)
def clean_source(self): def clean_source(self):
source_id = self.cleaned_data['source'] source_id = self.cleaned_data['source']

View file

@ -312,6 +312,10 @@ class Bill(models.Model):
html = self.html or self.render() html = self.html or self.render()
return html_to_pdf(html, pagination=self.has_multiple_pages) return html_to_pdf(html, pagination=self.has_multiple_pages)
def updated(self):
self.updated_on = timezone.now()
self.save(update_fields=('updated_on',))
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
if not self.type: if not self.type:
self.type = self.get_type() self.type = self.get_type()

View file

@ -4,12 +4,12 @@ from django.contrib import messages
from django.contrib.admin import helpers from django.contrib.admin import helpers
from django.db.models import Q from django.db.models import Q
from django.db.models.functions import Concat, Coalesce from django.db.models.functions import Concat, Coalesce
from django.forms.models import modelformset_factory
from django.shortcuts import render from django.shortcuts import render
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils.translation import ungettext, ugettext_lazy as _ from django.utils.translation import ungettext, ugettext_lazy as _
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from orchestra.admin.forms import adminmodelformset_factory
from orchestra.admin.utils import get_object_from_url, change_url, admin_link from orchestra.admin.utils import get_object_from_url, change_url, admin_link
from orchestra.utils.python import AttrDict from orchestra.utils.python import AttrDict
@ -54,8 +54,8 @@ def edit_records(modeladmin, request, queryset):
"but has been automatically added to this list.") "but has been automatically added to this list.")
link = '<a href="%(url)s" title="%(title)s">%(name)s</a>' % context link = '<a href="%(url)s" title="%(title)s">%(name)s</a>' % context
modeladmin_copy.verbose_name_plural = mark_safe(link) modeladmin_copy.verbose_name_plural = mark_safe(link)
RecordFormSet = adminmodelformset_factory( RecordFormSet = modelformset_factory(
modeladmin_copy, RecordForm, formset=RecordEditFormSet, extra=1, can_delete=True) modeladmin.model, form=RecordForm, formset=RecordEditFormSet, extra=1, can_delete=True)
formset = RecordFormSet(queryset=domain.records.all(), prefix=domain.id) formset = RecordFormSet(queryset=domain.records.all(), prefix=domain.id)
formset.instance = domain formset.instance = domain
formset.cls = RecordFormSet formset.cls = RecordFormSet

View file

@ -58,6 +58,7 @@ class BillsBackend(object):
order_billed_until=line.order.old_billed_until order_billed_until=line.order.old_billed_until
) )
self.create_sublines(billine, line.discounts) self.create_sublines(billine, line.discounts)
bill.updated()
return bills return bills
# def format_period(self, ini, end): # def format_period(self, ini, end):