3

\T                 @   sj   d dl mZ d dlmZ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G d
d dejZdS )    )forms)TemplateTemplateSyntaxError)gettext_lazy)normalise_email)	get_modelcustomerCommunicationEventTypeorderOrderc                   s   e Zd ZejeddZejeddejjddiddZ	ejed	dejdZ
ejed
ddZejedddZd! fdd	Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZG dd  d Z  ZS )"CommunicationEventTypeFormzEmail subject template)labelzEmail body text templateTclassplain)attrs)r   requiredwidgetzEmail body HTML templatezOrder numberF)r   r   zPreview emailNc                s<   d| _ d| _|r$d|k| _ d|k| _t j|f|| d S )NFshow_previewsend_preview)r   r   super__init__)selfdataargskwargs)	__class__ D/var/www/html/oscar2019/oscar/apps/dashboard/communications/forms.pyr      s    

z#CommunicationEventTypeForm.__init__c             C   sB   yt | W n0 tk
r< } ztjt|W Y d d }~X nX d S )N)r   r   r   ValidationErrorstr)r   valueer   r   r   validate_template#   s    z,CommunicationEventTypeForm.validate_templatec             C   s   | j d }| j| |S )Nemail_subject_template)cleaned_datar"   )r   subjectr   r   r   clean_email_subject_template)   s    

z7CommunicationEventTypeForm.clean_email_subject_templatec             C   s   | j d }| j| |S )Nemail_body_template)r$   r"   )r   bodyr   r   r   clean_email_body_template.   s    

z4CommunicationEventTypeForm.clean_email_body_templatec             C   s   | j d }| j| |S )Nemail_body_html_template)r$   r"   )r   r(   r   r   r   clean_email_body_html_template3   s    

z9CommunicationEventTypeForm.clean_email_body_html_templatec             C   sn   | j d j }| jj s|S | j r0| j r0|S ytjj|d| _	W n$ tj
k
rh   tjtdY nX |S )Npreview_order_number)numberzNo order found with this number)r$   stripinstanceis_order_relatedr   r   r   objectsgetpreview_orderDoesNotExistr   r   _)r   r-   r   r   r   clean_preview_order_number8   s    
z5CommunicationEventTypeForm.clean_preview_order_numberc             C   s.   t | jd }| js|S |s*tjtd|S )Npreview_emailzPlease enter an email address)r   r$   r   r   r   r5   )r   emailr   r   r   clean_preview_emailE   s    z.CommunicationEventTypeForm.clean_preview_emailc             C   s   i }t | dr| j|d< |S )Nr3   r
   )hasattrr3   )r   ctxr   r   r   get_preview_contextN   s    

z.CommunicationEventTypeForm.get_preview_contextc               @   s    e Zd ZeZddddddgZdS )zCommunicationEventTypeForm.Metanamer#   r'   r*   r,   r7   N)__name__
__module____qualname__r	   modelfieldsr   r   r   r   MetaT   s   rC   )N)r>   r?   r@   r   	CharFieldr5   r#   widgetsTextarear'   r*   r,   
EmailFieldr7   r   r"   r&   r)   r+   r6   r9   r<   rC   __classcell__r   r   )r   r   r      s*   

	r   N)djangor   django.templater   r   django.utils.translationr   r5   oscar.apps.customer.utilsr   oscar.core.loadingr   r	   r   	ModelFormr   r   r   r   r   <module>   s   

