
    si*                     T   d Z ddlZddlZ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 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 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" ddl#m$Z$ ddl%m&Z&  eed       e ed      d       G d de&jN                                      Z( eed       e ed      d       G d de&jR                                      Z* eed       e ed      d       G d de&jV                                      Z, eed       e ed      d       G d de&jZ                                      Z. eed       e ed      d       G d d e&j^                                      Z0 eed       e ed!      d       G d" d#e                    Z1y)$z#
horilla_automations/views/cbvs.py
    N)Any)settings)messages)serializers)HttpResponse)render)reversereverse_lazy)method_decorator)gettext_lazy)View)HorillaMailTemplate)login_requiredpermission_required)models)AutomationFilter)AutomationForm)viewsdispatch)namez'horilla_automations.view_mailautomationc                   >    e Zd ZdZ ed      Z ed      ZdZdgZdZ	y)AutomationSectionViewz
    AutomationSectionView
    zmail-automations-navmail-automations-list-viewlistContainerz/automation/automation.jsz%horilla_automations/section_view.htmlN)
__name__
__module____qualname____doc__r
   nav_urlview_urlview_container_idscript_static_pathstemplate_name     B/var/www/python-projects/worksol/horilla_automations/views/cbvs.pyr   r      s9    
 12G89H' 	$ <Mr%   r   c                   H     e Zd ZdZd fdZ ed      Z ed      ZdZ	 xZ
S )AutomationNavViewz
    AutomationNavView
    c                    t        |   |i | g | _        | j                  j                  j                  d      r?dt        d       d| _        | j                  j                  ddt        d       dd	       | j                  j                  j                  d      r,| j                  j                  d
dt        d       dd	       y y )N&horilla_automations.add_mailautomationz
                hx-get="zcreate-automationz"
                hx-target="#genericModalBody"
                data-target="#genericModal"
                data-toggle="oh-modal-toggle"
            zLoad Automationsz
                        data-toggle="oh-modal-toggle"
                        data-target="#genericModal"
                        hx-target="#genericModalBody"
                        hx-get="zload-automationszG"
                        style="cursor: pointer;"
                    )actionattrszRefresh Automationsz!
                        hx-get="zrefresh-automationsz"
                        hx-target="#reloadMessages"
                        class="oh-btn oh-btn--light-bkg"
                    )	super__init__actionsrequestuserhas_permr
   create_attrsappend)selfargskwargs	__class__s      r&   r.   zAutomationNavView.__init__9   s    $)&)<<%%&NO%%&9:; <!D LL0"! "..@!A B C		 <<%%&NOLL3"!!-.C!D E F	 Pr%   Automationsr   z#listContainer)returnN)r   r   r   r   r.   _	nav_titler
   
search_urlsearch_swap_target__classcell__r8   s   @r&   r(   r(   0   s+    
"H - I:;J)r%   r(   z)horilla_automations.change_mailautomationc                        e Zd ZdZeZej                  Z e	d      Z
dZdeddf fdZ fdZd	edej                   f fd
Z xZS )AutomationFormViewz
    AutomationFormView
    zNew Automationz(horilla_automations/automation_form.htmlr7   r:   Nc                 2    t        |   di | d| _        y )N
automationr$   )r-   r.   view_id)r5   r7   r8   s     r&   r.   zAutomationFormView.__init__p   s    "6"#r%   c                     t         |          }t        j                  j                  j                  | j                  d         j                         }||d<   |S )Npk)rG   instance)r-   get_form_kwargsr   MailAutomationobjectsfilterr7   first)r5   r7   rH   r8   s      r&   rI   z"AutomationFormView.get_form_kwargst   sP    (*((00774;;t;L7MSSU%zr%   formc                    |j                         rcd}|j                  j                  rd}|j                          t	        j
                  | j                  t        |             | j                         S t        | )  |      S )NzNew automation addedzAutomation updated)is_validrH   rG   saver   successr0   r;   r   r-   
form_valid)r5   rN   messager8   s      r&   rS   zAutomationFormView.form_validz   sc    ==?,G}}.IIKT\\1W:6$$&&w!$''r%   )r   r   r   r   r   
form_classr   rJ   modelr;   new_display_titler#   r   r.   rI   r   r   rS   r?   r@   s   @r&   rB   rB   b   sa    
  J!!E*+>M$ $ $(~ (%2D2D ( (r%   rB   c                   h    e Zd ZdZdZej                  Z ed      Z	e
Zdddddd	d
dgZddiZg dZy)AutomationListViewz
    AutomationListView
    z
            hx-get="{detailed_url}?instance_ids={ordered_ids}"
            hx-target="#genericModalBody"
            data-toggle="oh-modal-toggle"
            data-target="#genericModal"
        r   Editcreate-outlinea  
                class="oh-btn oh-btn--light-bkg w-100"
                hx-get="{edit_url}?instance_ids={ordered_ids}"
                hx-target="#genericModalBody"
                data-target="#genericModal"
                data-toggle="oh-modal-toggle"
            r+   iconr,   Deletetrash-outlinez
            class="oh-btn oh-btn--light-bkg w-100 tex-danger"
            onclick="
                event.stopPropagation();
                confirm('Do you want to delete the automation?','{delete_url}')
            "
            r+   zstyle='width:100px;'))TitletitleModelrV   Triggertrigger_display)zDelivery Channelget_delivery_channel_display)zEmail Mappingget_mail_to_displayN)r   r   r   r   	row_attrsr   rJ   rV   r
   r=   r   filter_classr/   header_attrscolumnsr$   r%   r&   rY   rY      sj    
I !!E:;J#L $
	
 #
	
G0 45LGr%   rY   c                   X    e Zd ZdZej
                  ZdZddddZg dZ	ddd	d
dddd
gZ
y)AutomationDetailedViewz 
    AutomationDetailedView
    zDetailed Viewra   
get_avatar)ra   subtitleavatar)rb   )zMail Templatesmail_template)zMail Torh   )zMail Ccget_mail_cc_displayrd   rZ   r[   z
            hx-get="{edit_url}?instance_ids={ordered_ids}"
            hx-target="#genericModalBody"
            data-toggle="oh-modal-toggle"
            data-target="#genericModal"
            class="oh-btn oh-btn--info w-50"
            r\   r^   r_   z
            class="oh-btn oh-btn--danger w-50"
            onclick="
                confirm('Do you want to delete the automation?','{delete_url}')
            "
            N)r   r   r   r   r   rJ   rV   ra   headerbodyr/   r$   r%   r&   rn   rn      s[    
 !!EEF
D $
	
 #		
Gr%   rn   r*   c                       e Zd ZdZej
                  j                  ej                  dd      Z	ej
                  j                  ej                  dd      Z
d Zd Zd Zy)	LoadAutomationsViewz(horilla_automations/load_automation.html	load_datazmail_templates.jsonzmail_automations.jsonc                    t        | j                  d      5 }t        j                  |      }d d d        t        | j                  d      5 }t        j                  |      }d d d        |fS # 1 sw Y   AxY w# 1 sw Y   fS xY w)Nr)opentemplate_filejsonloadautomation_file)r5   tftemplates_rawafautomations_raws        r&   load_json_filesz#LoadAutomationsView.load_json_files   s{    $$$c* 	*b IIbMM	*$&&, 	,"iimO	,o--		* 	*	,o--s   A-A9-A69Bc                 <   | j                         \  }}|D ci c]  }|d   |d   d    }}g }|D ]L  }|j                         }|d   j                  d      }	|j                  |	d      |d<   |j                  |       N t	        || j
                  d|i      S c c}w )NrG   fieldsru   rr    template_bodyautomations)r   copygetr4   r   r#   )
r5   r0   r   r   itemtemplate_lookupprocessed_automationsrD   	processedtemplate_pks
             r&   r   zLoadAutomationsView.get   s    )-)=)=)?&JWX$4:tH~f'==XX ") 	4J")I$X.22?CK)8)<)<[")MIo&!((3		4 12
 	
 Ys   Bc           	         | j                         \  }}|D ci c]  }|d   |d   d    }}|j                  j                         D cg c]  }|j                         st	        |        }}|D cg c]  }|d   |v s| }	}|	D ch c]*  }|d   j                  d      r|d   j                  d      , }
}|D ]  }|d   |
v st        t        j                  dt        j                  |g                  d   j                  }t        j                  j                  |j                        j!                         }|rd |_        |j%                           |	D ]B  }t        t        j                  dt        j                  |g                  d   }|j                  }|d   j                  d      }|j                  |      }t        j                  j                  |      j!                         }||_        t(        j*                  j                  j                  |j                        j-                         s=d |_        |j%                          t/        j0                  |d	|j                   d
       t/        j2                  |d	|j                   d       E d}t5        |      S c c}w c c}w c c}w c c}w )NrG   r   ru   rr   r}   r   )ra   )ru   zAutomation 'z' added successfully.z' already exists.z
            <script>
                $("#reloadMessagesButton").click();
                $('#applyFilter').click();
                $('.oh-modal--show').first().removeClass('oh-modal--show');
            </script>
        )r   POSTkeysisdigitintr   listr   deserializer}   dumpsobjectr   rK   rL   ra   rM   rG   rQ   rr   r   rJ   existsr   rR   warningr   )r5   r0   r   r   r   r   kselected_idsaselected_automationsrequired_template_pkstemplate_jsontemplate_dataexistingautomation_jsondeserializedautomation_objr   r   rr   scripts                        r&   postzLoadAutomationsView.post  s   )-)=)=)?&JWX$4:tH~f'==XX(/(9(9(;K1qyy{AKK+:Vaag>UVV *!
{/ hKOOO,!
 !
 + 
	)MT"&;; $++FDJJ4OP!!V  /66=='-- > %'   '+M$!&&(
	)  4 	O''

O;L0MNL *00N)(377HK+//<M/77>>" ? eg  ,9N(((0077$** 8 fh %)!##%  |N,@,@+AAVW   |N,@,@+AARS/	6 F##s YKV!
s#   J0	J5J52J:?J:	/J?N)r   r   r   r#   ospathjoinr   BASE_DIRr|   r   r   r   r   r$   r%   r&   rw   rw      sU    
 ?MGGLL!2!2KAVWMggll;(?O.
$<$r%   rw   )2r   r}   r   typingr   django.confr   django.contribr   django.corer   django.httpr   django.shortcutsr   django.urlsr	   r
   django.utils.decoratorsr   django.utils.translationr   r;   _transdjango.viewsr   base.modelsr   horilla.decoratorsr   r   horilla_automationsr   horilla_automations.filtersr   horilla_automations.formsr   horilla_views.generic.cbvr   HorillaSectionViewr   HorillaNavViewr(   HorillaFormViewrB   HorillaListViewrY   HorillaDetailedViewrn   rw   r$   r%   r&   <module>r      s    	    # # $ # - 4 6 ;  + B & 8 4 + .z2AB<E44 < 3<  .z2AB+*,, +* 3+*\ .z2CD:(.. ( 3(> .z2AB/.. / 3/d .z2AB)U66 ) 3)X .z2@A
\$$ \$ 3\$r%   