î
Þ^Q\Õ  ã               @   sC   d  Z  d d l m Z d d l m Z Gd d „  d e j ƒ Z d S)aö  
tl;dr: See FutureModelForm's docstring.

Many apps provide new related managers to extend your django models with. For
example, django-tagulous provides a TagField which abstracts an M2M relation
with the Tag model, django-gm2m provides a GM2MField which abstracts an
relation, django-taggit provides a TaggableManager which abstracts a relation
too, django-generic-m2m provides RelatedObjectsDescriptor which abstracts a
relation again.

While that works pretty well, it gets a bit complicated when it comes to
encapsulating the business logic for saving such data in a form object. This is
three-part problem:

- getting initial data,
- saving instance attributes,
- saving relations like reverse relations or many to many.

Django's ModelForm calls the form field's ``value_from_object()`` method to get
the initial data. ``FutureModelForm`` tries the ``value_from_object()`` method
from the form field instead, if defined. Unlike the model field, the form field
doesn't know its name, so ``FutureModelForm`` passes it when calling the form
field's ``value_from_object()`` method.

Django's ModelForm calls the form field's ``save_form_data()`` in two
occasions:

- in ``_post_clean()`` for model fields in ``Meta.fields``,
- in ``_save_m2m()`` for model fields in ``Meta.virtual_fields`` and
  ``Meta.many_to_many``, which then operate on an instance which as a PK.

If we just added ``save_form_data()`` to form fields like for
``value_from_object()`` then it would be called twice, once in
``_post_clean()`` and once in ``_save_m2m()``. Instead, ``FutureModelForm``
would call the following methods from the form field, if defined:

- ``save_object_data()`` in ``_post_clean()``, to set object attributes for a
  given value,
- ``save_relation_data()`` in ``_save_m2m()``, to save relations for a given
  value.

For example:

- a generic foreign key only sets instance attributes, its form field would do
  that in ``save_object_data()``,
- a tag field saves relations, its form field would do that in
  ``save_relation_data()``.
é    )Úchain)Úformsc                   sU   e  Z d  Z d Z ‡  f d d †  Z ‡  f d d †  Z d d „  Z d d	 d
 „ Z ‡  S)ÚFutureModelForma1  
    ModelForm which adds extra API to form fields.

    Form fields may define new methods for FutureModelForm:

    - ``FormField.value_from_object(instance, name)`` should return the initial
      value to use in the form, overrides ``ModelField.value_from_object()``
      which is what ModelForm uses by default,
    - ``FormField.save_object_data(instance, name, value)`` should set instance
      attributes. Called by ``save()`` **before** writting the database, when
      ``instance.pk`` may not be set, it overrides
      ``ModelField.save_form_data()`` which is normally used in this occasion
      for non-m2m and non-virtual model fields.
    - ``FormField.save_relation_data(instance, name, value)`` should save
      relations required for value on the instance. Called by ``save()``
      **after** writting the database, when ``instance.pk`` is necessarely set,
      it overrides ``ModelField.save_form_data()`` which is normally used in
      this occasion for m2m and virtual model fields.

    For complete rationale, see this module's docstring.
    c                sn   t  t |  ƒ j | | Ž  xN |  j j ƒ  D]= \ } } t | d ƒ sJ q) n  | j |  j | ƒ |  j | <q) Wd S)z:Override that uses a form field's ``value_from_object()``.Úvalue_from_objectN)	Úsuperr   Ú__init__ÚfieldsÚitemsÚhasattrr   ÚinstanceÚinitial)ÚselfÚargsÚkwargsÚnameÚfield)Ú	__class__© ú>/var/www/dbchiro/venv/lib/python3.4/site-packages/dal/forms.pyr   N   s
    zFutureModelForm.__init__c                sq   t  t |  ƒ j ƒ  xW |  j j ƒ  D]F \ } } t | d ƒ sD q# n  | j |  j | |  j j	 | d ƒ ƒ q# Wd S)z;Override that uses the form field's ``save_object_data()``.Úsave_object_dataN)
r   r   Ú_post_cleanr   r	   r
   r   r   Úcleaned_dataÚget)r   r   r   )r   r   r   r   X   s    zFutureModelForm._post_cleanc       	      C   s?  |  j  } |  j j } |  j j } |  j j } g  } xY |  j j ƒ  D]H \ } } t | d ƒ sd qC n  | j |  j | | | ƒ | j | ƒ qC Wx© t	 | j
 | j ƒ D]’ } | j | k rÀ q¥ n  t | d ƒ sÕ q¥ n  | rð | j | k rð q¥ n  | r| j | k rq¥ n  | j | k r¥ | j |  j | | j ƒ q¥ q¥ Wd S)z;Override that uses the form field's ``save_object_data()``.Úsave_relation_dataÚsave_form_dataN)r   Ú_metaÚexcluder   r   r	   r
   r   Úappendr   Zmany_to_manyZvirtual_fieldsr   r   )	r   r   r   r   ÚoptsZhandledr   r   Úfr   r   r   Ú	_save_m2mf   s0    	zFutureModelForm._save_m2mTc             C   ss   |  j  r@ t d |  j j j |  j j j r0 d n d f ƒ ‚ n  | r` |  j j ƒ  |  j ƒ  n |  j |  _	 |  j S)z"Backport from Django 1.9+ for 1.8.z8The %s could not be %s because the data didn't validate.ÚcreatedÚchanged)
ÚerrorsÚ
ValueErrorr   r   Zobject_nameÚ_stateZaddingÚsaver    Zsave_m2m)r   Zcommitr   r   r   r&   Œ   s    	%zFutureModelForm.save)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r    r&   r   r   )r   r   r   7   s
   
&r   N)r*   Ú	itertoolsr   Zdjangor   Z	ModelFormr   r   r   r   r   Ú<module>0   s   