o
    Oygi]4                  	   @  s~  d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	 d dl
mZ ddlmZ ddlmZmZ eG d	d
 d
ZeddG dd dZdd ZdddUddZdd dVd!d"Zdd dVd#d$Zd%d dWd(d)Zd*d dVd+d,ZdXd.d/Zd0d1d1d2d2d3d4d5d6Zd7d dYd9d:ZdZd;d<Zd[d=d>Zdd d\dBdCZdDd dVdEdFZdd dVdGdHZ dId dVdJdKZ!dLdLd7dMd]dSdTZ"dLS )^    )annotations)	dataclass)Path)ListOptionalSequence)Page   )shot)log_steppausec                   @  s*   e Zd ZU ded< ded< dZded< dS )UploadResultboolokstrfilename reasonN)__name__
__module____qualname____annotations__r    r   r   b/var/www/html/flask_server/apps/aroflo_connector_app/ui_automation/flows/users_upload_documents.pyr      s   
 r   T)frozenc                   @  s.   e Zd ZU ded< dZded< dZded< dS )DocSpecz
str | Pathfiler   r   commentfilterN)r   r   r   r   r   r   r   r   r   r   r      s   
 r   c              	   C  s@   | D ]}z|r|  dkr|jW   S W q ty   Y qw d S )Nr   )countfirst	Exception)locatorslocr   r   r   
_first_any   s   r$   visible)statepager   	selectorsSequence[str]
timeout_msintr&   r   returnNonec                C  sf   d }|D ]"}z| j |||d W  d S  ty& } z|}W Y d }~qd }~ww td|d| d|)Ntimeoutr&   z)None of the expected selectors appeared: z (state=))wait_for_selectorr!   RuntimeError)r'   r(   r*   r&   last_errseler   r   r   _wait_any_selector(   s   r6   i N  r*   c                C  sr   |  d|  d|  d|  d| jdddg}t|}|s"td|j|d	 | d
 t| g d|dd d S )Nz-a.extTabLink.documenttab[href="#documenttab"]z!a.extTabLink[href="#documenttab"]z a:has-text("Documents & Photos")za:has-text("Documents")link	Documents)namez0Documents tab not found (no matching selectors).r/   i,  )#documenttabbutton#btnUploadNew#dlgUploadNewattached)r(   r*   r&   )locatorget_by_roler$   r2   clickwait_for_timeoutr6   )r'   r*   
candidatestabr   r   r   users_open_documents_tab7   s"   

rF   c                C  sv   t | d| d| d| d| dg}|std|j|d | d | jd	|d
d | jd|dd d S )Nr=   z)button:has-text("Add Documents & Photos")z button:has-text("Add Documents")z$a:has-text("Add Documents & Photos")za:has-text("Add Documents")z1Upload button not found (Add Documents & Photos).r;      r>   r%   r.   z#dlgUploadNew #s3FileUploadr?   )r$   r@   r2   rB   rC   r1   )r'   r*   btnr   r   r   _open_upload_modalQ   s   	
rI   i0u  	file_pathr   c                C  sH   | j d|dd | d}|jd|d |jt||d | d d S )	Nr>   r%   r.   z #dlgUploadNew input#s3FileUploadr?   r&   r/   r;   iX  )r1   r@   wait_forset_input_filesr   rC   )r'   rJ   r*   inpr   r   r   _choose_file_in_modale   s
   
rO   '  c                C  s   | j d|dd | d}| dkrtd|jj|d | j dd|d	 z| d
}| dkr:|jjd|d	 W n	 tyD   Y nw | d d S )Nr>   r%   r.   zcdiv.ui-dialog:has(#dlgUploadNew) .ui-dialog-buttonpane .ui-dialog-buttonset button:has-text("Done")r   z*Done button not found inside upload modal.r;   hiddenrK   zdiv.ui-widget-overlayrG   )	r1   r@   r   r2   r    rB   rL   r!   rC   )r'   r*   done_btnoverlayr   r   r   _close_modal_doneo   s"   
rT   sc                 C  s   d | pd   S )N r   )joinstripsplitlower)rU   r   r   r   _normalize_filter_label   s   r[   zInternal OnlyzInternal Admin Only (Legacy)z(Internal Admin and Manager Only (Legacy)zShow ClientzShow ContractorzShow All)zinternal onlyzinternal admin only(legacy)zinternal admin only (legacy)z'internal admin and manager only(legacy)z(internal admin and manager only (legacy)zshow clientzshow contractorzshow alliȯ  r   c                C  s   t |j}| dj}|jd|d |djjd|d |djjd|d t| |d |d| d	j}z
|jd|d |W S  tyK   Y nw |d| d	j}|jd|d | pbd
	 }|ph|S )Nr<   r?   rK   zdiv[id^="gbox_tblSearchBox"]r%   ztable[id^="tblSearchBox"]r7   z.td[aria-describedby*="_document"] a:has-text("z")r   )
r   stemr@   r    rL   _wait_documents_grid_idler!   last
inner_textrX   )r'   r   r*   r\   	grid_root
exact_link	stem_link	real_namer   r   r   _wait_upload_visible_in_list   s2   


rd   c                 C  s8   |  dj|d}| dkrtd||j dS )Nz=table[id^="tblSearchBox"] td[aria-describedby*="_document"] a)has_textr   z*Could not find document link by filename: z-xpath=ancestor::tr[contains(@class,'jqgrow')])r@   r   r   r2   r^   )r'   r   r8   r   r   r   _find_document_row_by_filename   s   rf   c                 C  s:   |  dj}| dkrtd|d}|std|S )Nz[data-documentid]r   zDCould not find any element with data-documentid in the document row.zdata-documentidz#data-documentid attribute is empty.)r@   r    r   r2   get_attribute)roweldoc_idr   r   r   _extract_document_id_from_row   s   
rk   rj   r   filter_labelc                C  s  |r7|  d| d| d}|jd|d || |  | d| dd | d| dd | d |rt|}|tvrQtd	|d
t	t
t  |  d| d| d}|jd|d |jt| d |  | d| d | d d S d S )Nztextarea[name="docCommentz"][data-documentid="z"]r?   rK   inputchangerG   zInvalid filter value: z. Allowed: zselect#Documentfilterz[data-documentid=")label)r@   rL   fillblurdispatch_eventrC   r[   FILTER_LABELSr2   sortedsetvaluesselect_option)r'   rj   r   rl   r*   takeyr4   r   r   r   _set_comment_and_filter_for_doc   s*   

rz   i  c                C  s   z| j d | d W n	 ty   Y nw z$| d}| dkr9| djddd|dd	 | d W d S W d S  tyE   Y d S w )
NEscape   zul.ui-menu:visibler   body   )xyT)positionr/   force)keyboardpressrC   r!   r@   r   rB   )r'   r*   	open_menur   r   r   _dismiss_open_menus   s   
r   c                C  s   |  d}|  d}z| dkr|jjd|d W n	 ty#   Y nw z| dkr3|jjd|d W n	 ty=   Y nw | d d S )Nz<div.ui-widget-overlay.jqgrid-overlay[id^="lui_tblSearchBox"]z$div.loading[id^="load_tblSearchBox"]r   rQ   rK   r|   )r@   r   r    rL   r!   rC   )r'   r*   rS   loaderr   r   r   r]      s"   

r]   :  c                C  sf   |  dj}| dkr1|  |j|dd | d z
| jd|d W d S  ty0   Y d S w d S )Nz#btnSaveDocumentsr   T)r/   r   i   networkidler;   )r@   r    r   scroll_into_view_if_neededrB   rC   wait_for_load_stater!   )r'   r*   	save_docsr   r   r   _click_save_if_present  s   
r   N)cfgrun_dirr*   docsList[DocSpec]r   Path | NoneList[UploadResult]c                C  s  g }|D ]}t |j  }| std| || qt| |d |r:t| |d t	d|  |r:t
|d g }tt||ddD ])\}	\}
}|j}zt| |d |rkt| |d|	d t	d|	d|  t| ||d |rt| |d	|	dd
|  t	d	|	dd
| |  t| dd t|  t| |d |rt| |d|	dd
|  t	d|	dd
| |  t| ||d}t| |d t| |}|jd|d |  | d t|}t| ||
jpd|
jpd|d |
js|
jrt|  t| |d t| dd t|  t| |d |r*t| |d|	dd
|  t	d|	dd
| |  |td|d W qE tyo } z.|rYt| |d|	dd
|  t	d|	dd
| |  |td|t|d W Y d }~qEd }~ww t dd |D }t dd |D }|r|rt|  t| |d |rt| |d t	d|  t| dd t|  t| |d |rt| |d t	d|  |S )NzUpload file does not exist: r7   zuser-docs-01-tab-openzDocuments tab opened   )startzuser-docs-02-upload-modal-02dzuser-docs-03-file-selected--rP   zuser-docs-04-modal-closed-r?   rK      r   r   zuser-docs-05-meta-set-T)r   r   zuser-docs-ERR-F)r   r   r   c                 s  s    | ]}|j V  qd S )N)r   ).0rr   r   r   	<genexpr>t  s    z)users_upload_documents.<locals>.<genexpr>c                 s  s,    | ]}|j pd  p|jpd  V  qdS )r   N)r   rX   r   )r   dr   r   r   r   u  s   * zuser-docs-89-before-savezuser-docs-90-save-clicked)!r   r   
expanduserresolveexistsFileNotFoundErrorappendrF   r
   r   r   	enumeratezipr:   rI   rO   rT   r   r]   rd   rf   rL   r   rC   rk   rz   r   r   r   r   r!   r   any)r'   r   r   r   r*   
file_pathsr   presultsidxdocr   real_filenamerh   rj   r5   any_okany_metar   r   r   users_upload_documents  s   


 

	$

r   )
r'   r   r(   r)   r*   r+   r&   r   r,   r-   )r'   r   r*   r+   r,   r-   )r'   r   rJ   r   r*   r+   r,   r-   )rU   r   r,   r   )r'   r   r   r   r*   r+   r,   r   )r'   r   r   r   )r,   r   )r'   r   rj   r   r   r   rl   r   r*   r+   r,   r-   )
r'   r   r   r   r   r   r*   r+   r,   r   )#
__future__r   dataclassesr   pathlibr   typingr   r   r   playwright.sync_apir   core.artifactsr
   core.logr   r   r   r   r$   r6   rF   rI   rO   rT   r[   rs   rd   rf   rk   rz   r   r]   r   r   r   r   r   r   <module>   sN    	



"
 