diff options
author | Étienne Loks <etienne.loks@peacefrogs.net> | 2011-06-09 15:52:35 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@peacefrogs.net> | 2011-06-09 15:52:35 +0200 |
commit | 253759d5bd43158f75386b0935da9a58cced024b (patch) | |
tree | fac8f6216b0bca2a573ca3f6df9986a308fc79bf | |
parent | 53bf03b4801527dbb442fe3e22a3f66605891d9a (diff) | |
download | Ishtar-253759d5bd43158f75386b0935da9a58cced024b.tar.bz2 Ishtar-253759d5bd43158f75386b0935da9a58cced024b.zip |
"Shadow" the base item in forms - creation and modification (closes #445)
-rw-r--r-- | ishtar/furnitures/forms.py | 10 | ||||
-rw-r--r-- | ishtar/furnitures/forms_context_records.py | 5 | ||||
-rw-r--r-- | ishtar/furnitures/forms_items.py | 30 | ||||
-rw-r--r-- | ishtar/furnitures/forms_operations.py | 2 | ||||
-rw-r--r-- | ishtar/furnitures/models.py | 23 |
5 files changed, 36 insertions, 34 deletions
diff --git a/ishtar/furnitures/forms.py b/ishtar/furnitures/forms.py index 5f3c1f0d7..b5d29b7d4 100644 --- a/ishtar/furnitures/forms.py +++ b/ishtar/furnitures/forms.py @@ -301,7 +301,7 @@ class Wizard(NamedUrlSessionFormWizard): value = None dct[key] = value return self.save_model(dct, m2m, whole_associated_models, request, - storage, form_list, return_object=return_object) + storage, form_list, return_object) def get_saved_model(self): """ @@ -316,7 +316,7 @@ class Wizard(NamedUrlSessionFormWizard): return self.get_current_object(request, storage) def save_model(self, dct, m2m, whole_associated_models, request, storage, - form_list, return_object=False): + form_list, return_object): dct = self.get_extra_model(dct, request, storage, form_list) obj = self.get_current_saved_object(request, storage) @@ -338,6 +338,12 @@ class Wizard(NamedUrlSessionFormWizard): setattr(obj, k, dct[k]) for dependant_item in other_objs: c_item = getattr(obj, dependant_item) + # manage ManyToMany if only one associated + if hasattr(c_item, "all"): + c_items = c_item.all() + if len(c_items) != 1: + continue + c_item = c_items[0] if c_item: # to check # for k in other_objs[dependant_item]: diff --git a/ishtar/furnitures/forms_context_records.py b/ishtar/furnitures/forms_context_records.py index cd5f4e56d..7cd477c64 100644 --- a/ishtar/furnitures/forms_context_records.py +++ b/ishtar/furnitures/forms_context_records.py @@ -71,12 +71,11 @@ class RecordWizard(Wizard): return self.get_current_object(request, storage) def save_model(self, dct, m2m, whole_associated_models, request, storage, - form_list, return_object=False): + form_list, return_object): if 'pk' in dct: dct.pop('pk') return super(RecordWizard, self).save_model(dct, m2m, - whole_associated_models, request, storage, form_list, - return_object=return_object) + whole_associated_models, request, storage, form_list, return_object) class RecordModifWizard(RecordWizard): model = models.ContextRecord diff --git a/ishtar/furnitures/forms_items.py b/ishtar/furnitures/forms_items.py index 7831d2a8a..d4cec0ce9 100644 --- a/ishtar/furnitures/forms_items.py +++ b/ishtar/furnitures/forms_items.py @@ -47,22 +47,13 @@ class ItemWizard(Wizard): dct['base_items__context_record'] = dct.pop('pk') return dct - def save_model(self, *args, **kwargs): - print args - print kwargs - kwargs['return_object'] = True - returned, resp = super(ItemWizard, self).save_model(*args, **kwargs) - print returned - return resp - -#class ItemFormBase(forms.Form): class ItemForm(forms.Form): form_label = _("Item") base_model = 'base_items' associated_models = {'material_type':models.MaterialType,} - base_items__label = forms.CharField(label=_(u"ID"), + label = forms.CharField(label=_(u"ID"), validators=[validators.MaxLengthValidator(60)]) - base_items__description = forms.CharField(label=_("Description"), + description = forms.CharField(label=_("Description"), widget=forms.Textarea) base_items__is_isolated = forms.NullBooleanField(label=_(u"Is isolated?"), required=False) @@ -71,20 +62,7 @@ class ItemForm(forms.Form): volume = forms.IntegerField(label=_(u"Volume"), required=False) weight = forms.IntegerField(label=_(u"Weight"), required=False) item_number = forms.IntegerField(label=_(u"Item number"), required=False) -""" -class ItemForm(forms.Form): - form_label = _("Item") - associated_models = {'material_type':models.MaterialType,} - label = forms.CharField(label=_(u"ID"), - validators=[validators.MaxLengthValidator(60)]) - description = forms.CharField(label=_("Precise description"), - widget=forms.Textarea, required=False) - material_type = forms.ChoiceField(label=_("Material type"), - choices=models.MaterialType.get_types()) - volume = forms.IntegerField(label=_(u"Volume"), required=False) - weight = forms.IntegerField(label=_(u"Weight"), required=False) - item_number = forms.IntegerField(label=_(u"Item number"), required=False) -""" + class DateForm(forms.Form): form_label = _("Dating") base_model = 'dating' @@ -102,7 +80,7 @@ class DateForm(forms.Form): required=False, choices=models.DatingType.get_types()) item_creation_wizard = ItemWizard([ - ('selecrecord-item_creation', RecordFormSelection),#('base_item-record_creation', ItemFormBase), + ('selecrecord-item_creation', RecordFormSelection), ('item-item_creation', ItemForm), ('dating-item_creation', DateForm), ('final-item_creation', FinalForm)], diff --git a/ishtar/furnitures/forms_operations.py b/ishtar/furnitures/forms_operations.py index f3f487061..c6b6b9880 100644 --- a/ishtar/furnitures/forms_operations.py +++ b/ishtar/furnitures/forms_operations.py @@ -499,7 +499,7 @@ class OperationAdministrativeActWizard(OperationWizard): return self.get_current_object(request, storage) def save_model(self, dct, m2m, whole_associated_models, request, storage, - form_list, return_object=False): + form_list, return_object): associated_item = self.get_associated_item(request, storage, dct) if not associated_item: return self.render(request, storage, form_list[-1]) diff --git a/ishtar/furnitures/models.py b/ishtar/furnitures/models.py index db8773ad4..02c3661a5 100644 --- a/ishtar/furnitures/models.py +++ b/ishtar/furnitures/models.py @@ -28,6 +28,7 @@ from django.utils.translation import ugettext_lazy as _, ugettext from django.db.utils import DatabaseError from django.utils.safestring import SafeUnicode from django.db.models import Q, Max +from django.db.models.signals import m2m_changed from django.contrib.auth.models import User from django.contrib.gis.db import models @@ -298,7 +299,7 @@ class Address(BaseHistorizedItem): address_complement = models.TextField(_(u"Address complement"), null=True, blank=True) postal_code = models.CharField(_(u"Postal code"), max_length=10, null=True, - blank=True) + blank=True) town = models.CharField(_(u"Town"), max_length=30, null=True, blank=True) country = models.CharField(_(u"Country"), max_length=30, null=True, blank=True) @@ -868,7 +869,6 @@ class BaseItem(BaseHistorizedItem, OwnPerms): self.index, self.context_record.label, self.label)]) - class Item(BaseHistorizedItem, OwnPerms): TABLE_COLS = ['base_items.context_record.parcel.town', 'base_items.context_record.parcel.operation.year', @@ -926,6 +926,25 @@ class Item(BaseHistorizedItem, OwnPerms): base_item.save() super(Item, self).save(*args, **kwargs) +""" +def initialize_item(sender, **kwargs): + # initialize base items with the item + if kwargs['action'] != 'post_add': + return + item = kwargs['instance'] + # item already initialized + if item.label: + return + base_items = item.base_items.all() + if len(base_items) != 1: + return + base_item = base_items[0] + item.label = base_item.label + item.description = base_item.description + item.save() +m2m_changed.connect(initialize_item, + sender=Item.base_items.through) +""" class ParcelOwner(LightHistorizedItem): owner = models.ForeignKey(Person, verbose_name=_(u"Owner")) |