3

\2                 @   sF  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 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e
ddZe
ddZeedddgdZG dd dejZ G dd  d ejZ!G d!d" d"ej"Z#d#d$ Z$d%d& Z%d'd( Z&d)d* Z'd+d, Z(d-d. Z)d/d0 Z*d1d2 Z+d3d4 Z,d5d6 Z-d7d8 Z.d9d: Z/d;d< Z0G d=d> d>ej"Z1G d?d@ d@ejZ2G dAdB dBej"Z3G dCdD dDej"Z4G dEdF dFej"Z5G dGdH dHej"Z6G dIdJ dJej"Z7G dKdL dLej"Z8G dMdN dNej"Z9G dOdP dPej"Z:dQS )R    )forms)
exceptions)gettext_lazy)movenodeform_factory)	get_class	get_model)slugify)DateTimePickerInput
ImageInput	catalogueProductProductClassProductAttributeCategorypartnerStockRecordProductCategoryProductImageProductRecommendationAttributeOptionGroupAttributeOptionOptionzdashboard.catalogue.widgetsProductSelectzdashboard.widgetsRelatedFieldWidgetWrappernamedescriptionimage)fieldsc                   s>   e Zd ZdZejededejj	 dZ
 fddZ  ZS )ProductClassSelectFormzS
    Form which is used before creating a product to select it's product class
    zCreate a new product of typez-- Choose type --)labelempty_labelquerysetc                sF   t  j|| | jd j}|jd rBt|dkrB|d | jd _dS )zB
        If there's only one product class, pre-select it
        product_classinitial   r   N)super__init__r   r!   getlenr#   )selfargskwargsqs)	__class__ ?/var/www/html/oscar2019/oscar/apps/dashboard/catalogue/forms.pyr&   (   s    zProductClassSelectForm.__init__)__name__
__module____qualname____doc__r   ModelChoiceField_r   objectsallr"   r&   __classcell__r.   r.   )r-   r/   r      s   r   c                   sD   e Zd ZejddeddZejddeddZ fddZ  Z	S )	ProductSearchForm   FUPC)
max_lengthrequiredr      zProduct titlec                s.   t  j }|d j |d< |d j |d< |S )Nupctitle)r%   cleanstrip)r)   cleaned_data)r-   r.   r/   rA   7   s    
zProductSearchForm.clean)
r0   r1   r2   r   	CharFieldr5   r?   r@   rA   r8   r.   r.   )r-   r/   r9   2   s   r9   c                   s*   e Zd Z fddZG dd dZ  ZS )StockRecordFormc                s   || _ t j|| | j js0| j jj | jd _|jsXxDdD ]}|| jkr<| j|= q<W n$x"dD ]}|| jkr^d| j| _	q^W d S )Nr   num_in_stocklow_stock_thresholdprice_excl_taxT)rF   rG   )rH   rF   )
userr%   r&   is_staffpartnersr7   r   r!   track_stockr=   )r)   r"   rI   r*   r+   
field_name)r-   r.   r/   r&   @   s    



zStockRecordForm.__init__c               @   s$   e Zd ZeZddddddddgZd	S )
zStockRecordForm.Metar   partner_skuprice_currencyrH   price_retail
cost_pricerF   rG   N)r0   r1   r2   r   modelr   r.   r.   r.   r/   MetaT   s   rS   )r0   r1   r2   r&   rS   r8   r.   r.   )r-   r/   rE   >   s   rE   c             C   s   t j| j| jdS )N)r   r=   )r   rD   r   r=   )	attributer.   r.   r/   _attr_text_field]   s    rU   c             C   s   t j| jt j | jdS )N)r   widgetr=   )r   rD   r   Textarear=   )rT   r.   r.   r/   _attr_textarea_fieldb   s    rX   c             C   s   t j| j| jdS )N)r   r=   )r   IntegerFieldr   r=   )rT   r.   r.   r/   _attr_integer_fieldh   s    rZ   c             C   s   t j| j| jdS )N)r   r=   )r   BooleanFieldr   r=   )rT   r.   r.   r/   _attr_boolean_fieldm   s    r\   c             C   s   t j| j| jdS )N)r   r=   )r   
FloatFieldr   r=   )rT   r.   r.   r/   _attr_float_fieldr   s    r^   c             C   s   t j| j| jt jjdS )N)r   r=   rV   )r   	DateFieldr   r=   widgets	DateInput)rT   r.   r.   r/   _attr_date_fieldw   s    rb   c             C   s   t j| j| jt dS )N)r   r=   rV   )r   DateTimeFieldr   r=   r	   )rT   r.   r.   r/   _attr_datetime_field}   s    rd   c             C   s   t j| j| j| jjj dS )N)r   r=   r!   )r   r4   r   r=   option_groupoptionsr7   )rT   r.   r.   r/   _attr_option_field   s    rg   c             C   s   t j| j| j| jjj dS )N)r   r=   r!   )r   ModelMultipleChoiceFieldr   r=   re   rf   r7   )rT   r.   r.   r/   _attr_multi_option_field   s    ri   c             C   s   d S )Nr.   )rT   r.   r.   r/   _attr_entity_field   s    rj   c             C   s   t j| j| jdS )N)r   r=   )r   r]   r   r=   )rT   r.   r.   r/   _attr_numeric_field   s    rk   c             C   s   t j| j| jdS )N)r   r=   )r   	FileFieldr   r=   )rT   r.   r.   r/   _attr_file_field   s    rm   c             C   s   t j| j| jdS )N)r   r=   )r   
ImageFieldr   r=   )rT   r.   r.   r/   _attr_image_field   s    ro   c                   s   e Zd Zeeeeeee	e
eeeeedZG dd dZd fdd	Zdd Zd	d
 ZdddZdd Zdd Z fddZ  ZS )ProductForm)textrichtextintegerbooleanfloatdatedatetimeoptionmulti_optionentitynumericfiler   c               @   s*   e Zd ZeZdddddgZdej iZdS )zProductForm.Metar@   r?   r   is_discountable	structureN)	r0   r1   r2   r   rR   r   r   HiddenInputr`   r.   r.   r.   r/   rS      s   rS   Nc                s   | j ||| t j|f|| |rN|| j_tj| j_tj| jj_| j	  n|| j_
| j|| jj d| jkrtjddid| jd _d S )Nr@   autocompleteoff)attrs)set_initialr%   r&   instanceparentr   CHILDr~   PARENTdelete_non_child_fieldsr"   add_attribute_fields	is_parentr   r   	TextInputrV   )r)   r"   datar   r*   r+   )r-   r.   r/   r&      s    


zProductForm.__init__c             C   s2   d|kri |d< | j || |r.tj|d d< dS )z
        Set initial data for the form. Sets the correct product structure
        and fetches initial values for the dynamically constructed attribute
        fields.
        r#   r~   N)set_initial_attribute_valuesr   r   )r)   r"   r   r+   r.   r.   r/   r      s
    zProductForm.set_initialc             C   sl   |j d}|dkrdS xP|jj D ]B}y|jj |dj}W n tjk
rP   Y q"X ||d d|j < q"W dS )z
        Update the kwargs['initial'] value to have the initial values based on
        the product instance's attributes
        r   N)rT   r#   zattr_%s)r'   
attributesr7   attribute_valuesvaluer   ObjectDoesNotExistcode)r)   r"   r+   r   rT   r   r.   r.   r/   r      s    
z(ProductForm.set_initial_attribute_valuesFc             C   sL   xF|j j D ]8}| j|}|r|| jd|j < |rd| jd|j  _qW dS )z
        For each attribute specified by the product class, this method
        dynamically adds form fields to the product form.
        zattr_%sFN)r   r7   get_attribute_fieldr   r   r=   )r)   r"   r   rT   fieldr.   r.   r/   r      s    
z ProductForm.add_attribute_fieldsc             C   s   | j |j |S )zI
        Gets the correct form field for a given attribute type.
        )FIELD_FACTORIEStype)r)   rT   r.   r.   r/   r      s    zProductForm.get_attribute_fieldc             C   s$   xdD ]}|| j kr| j |= qW dS )z
        Deletes any fields not needed for child products. Override this if
        you want to e.g. keep the description field.
        r   r}   N)r   r}   )r   )r)   rM   r.   r.   r/   r     s    

z#ProductForm.delete_non_child_fieldsc                s`   | j jj  xD| j jj D ]4}d|j }|| jkr| j| }t| j j|j| qW t j  dS )z
        Set attributes before ModelForm calls the product's clean method
        (which it does in _post_clean), which in turn validates attributes.
        zattr_%sN)	r   attrinitiate_attributesget_all_attributesr   rC   setattrr%   _post_clean)r)   rT   rM   r   )r-   r.   r/   r     s    


zProductForm._post_clean)NN)F)r0   r1   r2   rU   rX   rZ   r\   r^   rb   rd   rg   ri   rj   rk   rm   ro   r   rS   r&   r   r   r   r   r   r   r8   r.   r.   )r-   r/   rp      s*   
	rp   c               @   s   e Zd ZejeddZdS )StockAlertSearchFormStatus)r   N)r0   r1   r2   r   rD   r5   statusr.   r.   r.   r/   r     s   r   c               @   s   e Zd ZG dd dZdS )ProductCategoryFormc               @   s   e Zd ZeZdZdS )zProductCategoryForm.MetacategoryN)r   )r0   r1   r2   r   rR   r   r.   r.   r.   r/   rS   $  s   rS   N)r0   r1   r2   rS   r.   r.   r.   r/   r   "  s   r   c                   s2   e Zd ZG dd dZ fddZdd Z  ZS )ProductImageFormc               @   s$   e Zd ZeZdddgZde iZdS )zProductImageForm.MetaproductoriginalcaptionN)r0   r1   r2   r   rR   r   r
   r`   r.   r.   r.   r/   rS   +  s   
rS   c                s,   d|d< t  j||}| j |_|j  |S )NFcommit)r%   saveget_display_orderdisplay_order)r)   r*   r+   obj)r-   r.   r/   r   5  s
    
zProductImageForm.savec             C   s   | j jdj S )N-)prefixsplitpop)r)   r.   r.   r/   r   >  s    z"ProductImageForm.get_display_order)r0   r1   r2   rS   r   r   r8   r.   r.   )r-   r/   r   )  s   
	r   c               @   s   e Zd ZG dd dZdS )ProductRecommendationFormc               @   s"   e Zd ZeZdddgZdeiZdS )zProductRecommendationForm.MetaprimaryrecommendationrankingN)r0   r1   r2   r   rR   r   r   r`   r.   r.   r.   r/   rS   D  s   
rS   N)r0   r1   r2   rS   r.   r.   r.   r/   r   B  s   r   c               @   s   e Zd ZG dd dZdS )ProductClassFormc               @   s   e Zd ZeZddddgZdS )zProductClassForm.Metar   requires_shippingrL   rf   N)r0   r1   r2   r   rR   r   r.   r.   r.   r/   rS   N  s   rS   N)r0   r1   r2   rS   r.   r.   r.   r/   r   L  s   r   c                   s2   e Zd Z fddZdd ZG dd dZ  ZS )ProductAttributesFormc                sZ   t  j|| d| jd _td| jd _| jjjjdj	}t
| jd j|| jd _d S )NFr   zSelect an option groupre   )r%   r&   r   r=   r5   	help_text_metarR   	get_fieldremote_fieldr   rV   )r)   r*   r+   r   )r-   r.   r/   r&   U  s    zProductAttributesForm.__init__c             C   s.   | j jd}| j jd}| r*|r*t|}|S )Nr   r   )rC   r'   r   )r)   r   r@   r.   r.   r/   
clean_codeb  s
    
z ProductAttributesForm.clean_codec               @   s   e Zd ZeZdddddgZdS )zProductAttributesForm.Metar   r   r   re   r=   N)r0   r1   r2   r   rR   r   r.   r.   r.   r/   rS   k  s   rS   )r0   r1   r2   r&   r   rS   r8   r.   r.   )r-   r/   r   S  s   	r   c               @   s   e Zd ZG dd dZdS )AttributeOptionGroupFormc               @   s   e Zd ZeZdgZdS )zAttributeOptionGroupForm.Metar   N)r0   r1   r2   r   rR   r   r.   r.   r.   r/   rS   r  s   rS   N)r0   r1   r2   rS   r.   r.   r.   r/   r   p  s   r   c               @   s   e Zd ZG dd dZdS )AttributeOptionFormc               @   s   e Zd ZeZdgZdS )zAttributeOptionForm.Metarx   N)r0   r1   r2   r   rR   r   r.   r.   r.   r/   rS   y  s   rS   N)r0   r1   r2   rS   r.   r.   r.   r/   r   w  s   r   c               @   s   e Zd ZG dd dZdS )
OptionFormc               @   s   e Zd ZeZddgZdS )zOptionForm.Metar   r   N)r0   r1   r2   r   rR   r   r.   r.   r.   r/   rS     s   rS   N)r0   r1   r2   rS   r.   r.   r.   r/   r   ~  s   r   N);djangor   django.corer   django.utils.translationr   r5   Ztreebeard.formsr   oscar.core.loadingr   r   oscar.core.utilsr   Zoscar.forms.widgetsr	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   CategoryFormFormr   r9   	ModelFormrE   rU   rX   rZ   r\   r^   rb   rd   rg   ri   rj   rk   rm   ro   rp   r   r   r   r   r   r   r   r   r   r.   r.   r.   r/   <module>   sb   











v
