3

\                 @   s   d dl mZ d dlmZ d dlmZ d dlmZ eddZ	eddZ
edd	Zedd
ZG dd dejZG dd dejZG dd dejZG dd dejZdS )    )forms)gettext_lazy)	get_model)widgetsvoucherVoucher
VoucherSetofferBenefitRangec                   s*  e Zd ZdZejeddZejeddZej	e
j eddZej	e
j eddZejejedd	Zejed
ejj dZejedfejedfejedfejedfejedffZejeedd	ZejeddZ ej!deddZ"d fdd	Z#dd Z$dd Z% fddZ&  Z'S ) VoucherFormzH
    A specialised form for creating a voucher and offer
    model.
    Name)labelCodezStart datetime)widgetr   zEnd datetimeUsage)choicesr   zWhich products get a discount?)r   querysetz#Percentage off of products in rangez%Fixed amount off of products in rangez1Discount is a percentage off of the shipping costz/Discount is a fixed amount of the shipping costzGet shipping for a fixed pricezDiscount typezDiscount valueFz5Exclusive offers cannot be combined on the same items)requiredr   Nc                s   || _ t j|| d S )N)r   super__init__)selfr   argskwargs)	__class__ >/var/www/html/oscar2019/oscar/apps/dashboard/vouchers/forms.pyr   6   s    zVoucherForm.__init__c             C   s`   | j d }ytjj|d}W n tjk
r2   Y n*X | j sJ|j| jjkr\tjt	d| |S )Nname)r   zThe name '%s' is already in use)
cleaned_datar   objectsgetDoesNotExistr   idr   ValidationError_)r   r   r   r   r   r   
clean_name:   s    

zVoucherForm.clean_namec             C   sz   | j d j j }|s$tjtdytjj|d}W n tj	k
rL   Y n*X | j
 sd|j| j
jkrvtjtd| |S )NcodezPlease enter a voucher code)r&   zThe code '%s' is already in use)r   stripupperr   r#   r$   r   r   r    r!   r   r"   )r   r&   r   r   r   r   
clean_codeF   s    
zVoucherForm.clean_codec                s@   t  j }|jd}|jd}|r<|r<||k r<tjtd|S )Nstart_datetimeend_datetimez*The start date must be before the end date)r   cleanr    r   r#   r$   )r   r   r*   r+   )r   r   r   r,   T   s    


zVoucherForm.clean)N)(__name__
__module____qualname____doc__r   	CharFieldr$   r   r&   DateTimeFieldr   DateTimePickerInputr*   r+   ChoiceFieldr   USAGE_CHOICESusageModelChoiceFieldr   r   allbenefit_ranger
   
PERCENTAGEFIXEDSHIPPING_PERCENTAGESHIPPING_ABSOLUTESHIPPING_FIXED_PRICEtype_choicesbenefit_typeDecimalFieldbenefit_valueBooleanField	exclusiver   r%   r)   r,   __classcell__r   r   )r   r   r      s@   r   c               @   s\   e Zd ZejdeddZejdeddZejdeddZ	ejdeddZ
dd Zd	S )
VoucherSearchFormFr   )r   r   r   z
Is Active?zIn Voucherset?c             C   s   | j d j S )Nr&   )r   r(   )r   r   r   r   r)   e   s    zVoucherSearchForm.clean_codeN)r-   r.   r/   r   r1   r$   r   r&   rC   	is_activein_setr)   r   r   r   r   rF   ^   s   rF   c                   s   e Zd ZG dd dZejedejj	 dZ
ejedfejedfejedfejedfejed	ffZejeed
dZejeddZd fdd	Z  ZS )VoucherSetFormc               @   s2   e Zd ZeZddddddgZej ej dZdS )	zVoucherSetForm.Metar   code_lengthdescriptionr*   r+   count)r*   r+   N)r-   r.   r/   r   modelfieldsr   r3   r   r   r   r   Metaj   s   rO   zWhich products get a discount?)r   r   z#Percentage off of products in rangez%Fixed amount off of products in rangez1Discount is a percentage off of the shipping costz/Discount is a fixed amount of the shipping costzGet shipping for a fixed pricezDiscount type)r   r   zDiscount value)r   Tc                s   t  j|}|r|j  |S )N)r   savegenerate_vouchers)r   commitinstance)r   r   r   rP      s    zVoucherSetForm.save)T)r-   r.   r/   rO   r   r7   r$   r   r   r8   r9   r
   r:   r;   r<   r=   r>   r?   r4   r@   rA   rB   rP   rE   r   r   )r   r   rI   i   s"   rI   c               @   s8   e Zd ZejdeddZejdeddZdd Z	dS )VoucherSetSearchFormFr   )r   r   z
Is Active?c             C   s   | j d j S )Nr&   )r   r(   )r   r   r   r   r)      s    zVoucherSetSearchForm.clean_codeN)
r-   r.   r/   r   r1   r$   r&   rC   rG   r)   r   r   r   r   rT      s   rT   N)djangor   django.utils.translationr   r$   oscar.core.loadingr   oscar.formsr   r   r   r
   r   Formr   rF   	ModelFormrI   rT   r   r   r   r   <module>   s   



Q+