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
# show base and total desglosed

View File

@ -1,3 +1,4 @@
import textwrap
from functools import partial
from django import forms
@ -35,32 +36,47 @@ class AdminFormMixin(object):
class AdminFormSet(BaseModelFormSet):
def as_admin(self):
prepopulated = {}
fieldsets = [
(None, {'fields': list(self.form().fields.keys())})
]
readonly = getattr(self.form.Meta, 'readonly_fields', ())
if not hasattr(self.modeladmin, 'verbose_name_plural'):
opts = self.modeladmin.model._meta
self.modeladmin.verbose_name_plural = opts.verbose_name_plural
inline_admin_formset = helpers.InlineAdminFormSet(self.modeladmin, self,
fieldsets, prepopulated, readonly, model_admin=self.modeladmin)
template = Template(
'{% include "admin/edit_inline/tabular.html" %}'
template = Template(textwrap.dedent("""\
<div class="inline-group">
<div class="tabular inline-related last-related">
{{ formset.management_form }}
<fieldset class="module">
{{ formset.non_form_errors.as_ul }}
<table id="formset" class="form">
{% for form in formset.forms %}
{% if forloop.first %}
<thead><tr>
{% for field in form.visible_fields %}
<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({
'inline_admin_formset': inline_admin_formset
'formset': self
})
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):
"""
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.core.urlresolvers import reverse
from django.db import transaction
from django.forms.models import modelformset_factory
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.utils import translation, timezone
from django.utils.safestring import mark_safe
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.forms import AdminFormSet
from orchestra.admin.utils import get_object_from_url, change_url
from . import settings
@ -41,7 +42,7 @@ def close_bills(modeladmin, request, queryset, action='close_bills'):
if not bill.is_open:
messages.warning(request, _("Selected bills should be in open state"))
return False
SelectSourceFormSet = adminmodelformset_factory(modeladmin, SelectSourceForm, extra=0)
SelectSourceFormSet = modelformset_factory(modeladmin.model, form=SelectSourceForm, formset=AdminFormSet, extra=0)
formset = SelectSourceFormSet(queryset=queryset)
if request.POST.get('post') == 'generic_confirmation':
formset = SelectSourceFormSet(request.POST, request.FILES, queryset=queryset)

View File

@ -7,7 +7,7 @@ from orchestra.forms import SpanWidget
class SelectSourceForm(forms.ModelForm):
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)
display_type = forms.CharField(label=_("Type"), required=False, widget=SpanWidget)
source = forms.ChoiceField(label=_("Source"), required=False)
@ -16,7 +16,6 @@ class SelectSourceForm(forms.ModelForm):
fields = (
'bill_link', 'display_type', 'account_link', 'show_total', 'source'
)
readonly_fields = ('account_link',)
def __init__(self, *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['bill_link'].widget.display = admin_link('__str__')(bill)
self.fields['display_type'].widget.display = bill.get_type_display()
self.fields['account_link'].widget.display = admin_link('account')(bill)
def clean_source(self):
source_id = self.cleaned_data['source']

View File

@ -312,6 +312,10 @@ class Bill(models.Model):
html = self.html or self.render()
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):
if not self.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.db.models import Q
from django.db.models.functions import Concat, Coalesce
from django.forms.models import modelformset_factory
from django.shortcuts import render
from django.utils.safestring import mark_safe
from django.utils.translation import ungettext, ugettext_lazy as _
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.utils.python import AttrDict
@ -54,8 +54,8 @@ def edit_records(modeladmin, request, queryset):
"but has been automatically added to this list.")
link = '<a href="%(url)s" title="%(title)s">%(name)s</a>' % context
modeladmin_copy.verbose_name_plural = mark_safe(link)
RecordFormSet = adminmodelformset_factory(
modeladmin_copy, RecordForm, formset=RecordEditFormSet, extra=1, can_delete=True)
RecordFormSet = modelformset_factory(
modeladmin.model, form=RecordForm, formset=RecordEditFormSet, extra=1, can_delete=True)
formset = RecordFormSet(queryset=domain.records.all(), prefix=domain.id)
formset.instance = domain
formset.cls = RecordFormSet

View File

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