3

\7                 @   sb  d dl mZ d dlmZ d dlmZ d dlmZ d dl	m
Z
mZ eddZeddZedd	Zedd
ZeddZeddZeddZeddZeddZeddZe
dd+\ZZZZZZeeeeddZG dd deZeeeedddZG dd  d eZeeeed!dZ G d"d# d#e Z!eeeed$d%d&Z"G d'd( d(e"Z#eeeed)dZ$eeeed)dZ%d*S ),    )forms)
exceptions)inlineformset_factory)gettext_lazy)get_classes	get_model	catalogueProductProductClassProductAttributeCategorypartnerStockRecordProductCategoryProductImageProductRecommendationAttributeOptionGroupAttributeOptionzdashboard.catalogue.formsStockRecordFormProductCategoryFormProductImageFormProductRecommendationFormProductAttributesFormAttributeOptionForm   )formextrac                   s8   e Zd Z fddZdd Z fddZdd Z  ZS )	StockRecordFormSetc                sl   || _ |j | _|| _|j rRd|krRd|krR|jdtjj|d |jj	 di t
 j|| | j  d S )Ninstancequeryset)productZpartner__in)useris_staffrequire_user_stockrecordproduct_classupdater   objectsfilterpartnersallsuper__init__set_initial_data)selfr$   r!   argskwargs)	__class__ B/var/www/html/oscar2019/oscar/apps/dashboard/catalogue/formsets.pyr+   (   s    
zStockRecordFormSet.__init__c             C   s`   | j r\y| jjj }W n tjtjfk
r2   Y n*X | jd jjdd}|r\|j	dkr\||_	dS )a  
        If user has only one partner associated, set the first
        stock record's partner to it. Can't pre-select for staff users as
        they're allowed to save a product without a stock record.

        This is intentionally done after calling __init__ as passing initial
        data to __init__ creates a form for each list item. So depending on
        whether we can pre-select the partner or not, we'd end up with 1 or 2
        forms for an unbound form.
        r   r   N)
r#   r!   r(   getr   ObjectDoesNotExistMultipleObjectsReturnedr   fieldsinitial)r-   Zuser_partnerZpartner_fieldr1   r1   r2   r,   8   s    z#StockRecordFormSet.set_initial_datac                s$   | j |d< | j|d< t j|f|S )Nr$   r!   )r$   r!   r*   _construct_form)r-   ir/   )r0   r1   r2   r8   N   s    

z"StockRecordFormSet._construct_formc             C   sR   t | jrdS | jrNtdd | jD }t| jjj }||@ sNtj	t
ddS )z
        If the user isn't a staff user, this validation ensures that at least
        one stock record's partner is associated with a users partners.
        Nc             S   s   g | ]}|j jd dqS )r   N)cleaned_datar3   ).0r   r1   r1   r2   
<listcomp>\   s   z,StockRecordFormSet.clean.<locals>.<listcomp>zOAt least one stock record must be set to a partner that you're associated with.)anyerrorsr#   setr   r!   r(   r)   r   ValidationError_)r-   Zstockrecord_partnersZuser_partnersr1   r1   r2   cleanT   s    
zStockRecordFormSet.clean)__name__
__module____qualname__r+   r,   r8   rB   __classcell__r1   r1   )r0   r2   r   &   s   r   T)r   r   
can_deletec                   s,   e Zd Z fddZdd Zdd Z  ZS )ProductCategoryFormSetc                s   t  j|| d S )N)r*   r+   )r-   r$   r!   r.   r/   )r0   r1   r2   r+   l   s    zProductCategoryFormSet.__init__c             C   sJ   | j j r$| j dkr$tjtd| j jrF| j dkrFtjtdd S )Nr   z?Stand-alone and parent products must have at least one categoryz*A child product should not have categories)r   is_childget_num_categoriesr   r@   rA   )r-   r1   r1   r2   rB   p   s    
zProductCategoryFormSet.cleanc             C   sZ   d}xPt d| j D ]>}| j| }t|dr|jjdd r|jjdd r|d7 }qW |S )Nr   r:   categoryDELETEFr   )rangetotal_form_countr   hasattrr:   r3   )r-   Znum_categoriesr9   r   r1   r1   r2   rJ   y   s    

z)ProductCategoryFormSet.get_num_categories)rC   rD   rE   r+   rB   rJ   rF   r1   r1   )r0   r2   rH   j   s   	rH      c                   s   e Zd Z fddZ  ZS )ProductImageFormSetc                s   t  j|| d S )N)r*   r+   )r-   r$   r!   r.   r/   )r0   r1   r2   r+      s    zProductImageFormSet.__init__)rC   rD   rE   r+   rF   r1   r1   )r0   r2   rQ      s   rQ      primary)r   r   fk_namec                   s   e Zd Z fddZ  ZS )ProductRecommendationFormSetc                s   t  j|| d S )N)r*   r+   )r-   r$   r!   r.   r/   )r0   r1   r2   r+      s    z%ProductRecommendationFormSet.__init__)rC   rD   rE   r+   rF   r1   r1   )r0   r2   rU      s   rU      N)r   r   r   r   r   r   )&djangor   django.corer   django.forms.modelsr   django.utils.translationr   rA   oscar.core.loadingr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ZBaseStockRecordFormSetr   ZBaseProductCategoryFormSetrH   ZBaseProductImageFormSetrQ   Z BaseProductRecommendationFormSetrU   ProductAttributesFormSetAttributeOptionFormSetr1   r1   r1   r2   <module>   sV   









     ?
