o
    ȟ1iS'                     @   s   d dl Z d dlmZ d dl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mZmZ d d	lmZmZmZmZmZmZ d d
lmZ d dlmZ d dlmZ G dd dZ dS )    N)Path)date)get_db	BASE_PATH)GenericError)ProfileRegistration)Course)ProfileRegistrationCreateSchemaProfileRegistrationUpdateSchema!ProfileRegistrationResponseSchema)SimpleDocTemplateTable
TableStyle	ParagraphSpacer	PageBreak)colors)A4)getSampleStyleSheetc                   @   s   e Zd Zdd ZdefddZdefddZd	d
 Zdede	fddZ
defddZdededefddZdedefddZdS )ProfileRegistrationServicec                    s   |j r?t|j jj}t  | }td|  }|j  I d H }t|d}|	| W d    |S 1 s8w   Y  |S d S )Nzuploads/wb)
filer   filenamesuffixuuiduuid4r   readopenwrite)selfrequestfile_extensionnew_filenamefile_locationcontentf r&   %/app/services/profile_registration.pyupload_file   s   
z&ProfileRegistrationService.upload_filer    c                    s   t  ]}z@|jdgd}| |I d H }|r||d< |d |d< |d |d< td	i |}|| |  || |W W  d    S  ty` } z|  t	ddt
| dd }~ww 1 sdw   Y  d S )
Nr   )excludeiduser_id	user_type  zCould not create registration: status_codeexcr&   )r   
model_dumpr(   r   addcommitrefresh	Exceptionrollbackr   str)r   r    userdbpayloadr   registrationer&   r&   r'   create_registration    s*   

z.ProfileRegistrationService.create_registrationregistration_idc                 C   sd   t  %}|ttj|ktj|d k }|stddd|W  d    S 1 s+w   Y  d S )Nr,     Registration not foundr.   )r   queryr   filterr*   r,   firstr   )r   r>   r8   r9   r;   r&   r&   r'   get_registration_by_id4   s   &$z1ProfileRegistrationService.get_registration_by_idc                 C   sj   t  (}|ttj|d k }|D ]}|jr!d|j |_q|W  d    S 1 s.w   Y  d S )Nr,   z	/uploads/)r   rA   r   rB   r,   allr   )r   r8   r9   registrationsr;   r&   r&   r'   get_all_registrations<   s   $z0ProfileRegistrationService.get_all_registrationsc                    s  t  v}zY|ttj|k }|stddd|jddgd}| |I d H }|r1||d< |d |d< |d	 |d	< |	 D ]
\}}	t
|||	 qA|  || |W W  d    S  tyy }
 z|  td
dt|
 dd }
~
ww 1 s}w   Y  d S )Nr?   r@   r.   Tr   )exclude_unsetr)   r*   r+   r,   r-   zCould not update registration: )r   rA   r   rB   r*   rC   r   r1   r(   itemssetattrr3   r4   r5   r6   r7   )r   r>   r    r8   r9   r;   Zupdate_datar   keyvaluer<   r&   r&   r'   update_registrationE   s0   
z.ProfileRegistrationService.update_registrationc                 C   sn   t  *}|ttj|k }|stddd|| |  ddiW  d    S 1 s0w   Y  d S )Nr?   r@   r.   messagez!Registration deleted successfully)	r   rA   r   rB   r*   rC   r   deleter3   )r   r>   r9   r;   r&   r&   r'   delete_registration_   s   
$z.ProfileRegistrationService.delete_registration	course_idr+   r,   c                 C   sX   t  }|ttj|ktj|ktj|k }|W  d    S 1 s%w   Y  d S )N)r   rA   r   rB   rQ   r+   r,   rE   )r   rQ   r+   r,   r9   rF   r&   r&   r'   list_content_by_course_and_userj   s   
$z:ProfileRegistrationService.list_content_by_course_and_userparticipant_idc              
   C   s4  zxt  ;}|ttj|ktj|k }dd |D }i }|r9|ttj	t
| }dd |D }W d   n1 sCw   Y  t }	t|	tddddd}
t }g }td|d	}|| |td
d |s~|td|d ndd tjjD }h d}|d}|d}t|D ]\}}|td|j |d |td
d g }|D ]}||v r|dkrd}||jd}|pd}nR|dkrd}|jrt|jnd}nAqzt||}W n ty   d}Y nw t|tr|rdnd}nt|tr|d}n|du rd}nt|}|dd }td| d |}t|d!d"|}|||g qt |d#d$gd%d&}|!t"d'd(d)d*d+t#j$fd,d-g || |t%|d
 krj|t&  q|
'| |	(d. |	) W S  t*y     ty } zt*d/d0t| d1d}~ww )2zgGenerate a PDF (bytes) containing profile registrations for a given course_id and the user's user_type.c                 S   s   h | ]}|j r|j qS r&   )rQ   ).0rr&   r&   r'   	<setcomp>   s    zDProfileRegistrationService.generate_pdf_by_course.<locals>.<setcomp>c                 S   s   i | ]}|j |jqS r&   )r*   namerT   cr&   r&   r'   
<dictcomp>   s    zEProfileRegistrationService.generate_pdf_by_course.<locals>.<dictcomp>N   )ZpagesizeZ
leftMarginZrightMarginZ	topMarginZbottomMarginzWac Nites FormZTitle      z'No registrations found for this course.ZNormalc                 S   s   g | ]}|j qS r&   )rW   rX   r&   r&   r'   
<listcomp>   s    zEProfileRegistrationService.generate_pdf_by_course.<locals>.<listcomp>>   rQ   r+   r   r*   rS   r,   ZBodyTextzRegistration ID: ZHeading2   rQ   zCourse Name rS   zParticipant IDZYesNoz%Y-%m-%d_ z<b>z</b>
z<br/>   i|  ZLEFT)Z	colWidthsZhAlign)ZVALIGNr   r   rh   ZTOPZ	LINEBELOWrf   rg   g      ?)ZLEFTPADDINGrf   rg   r_   )ZRIGHTPADDINGrf   rg   r_   r   r-   zCould not generate PDF: r.   )+r   rA   r   rB   rQ   rS   rE   r   r*   in_listioBytesIOr   r   r   r   getappendr   	__table__columns	enumerater7   getattrr5   
isinstanceboolr   strftimereplacetitler   ZsetStyler   r   	lightgreylenr   buildseekgetvaluer   )r   rQ   rS   r8   r9   rF   Z
course_idsZcourse_name_mapcoursesbufferdocstyleselementsrw   colsZexclude_colslabel_styleZvalue_styleidxrU   Z
table_datacollabelcourse_namerL   valZlabel_pZvalue_ptabler<   r&   r&   r'   generate_pdf_by_coursew   s   
 








z1ProfileRegistrationService.generate_pdf_by_courseN)__name__
__module____qualname__r(   r	   r=   intrD   rG   r
   rM   rP   r7   rR   r   r&   r&   r&   r'   r      s    	r   )!r   pathlibr   rk   datetimer   core.configr   r   core.security.exceptionsr   Zmodels.profile_registrationr   Zmodels.courser   schema.profile_registrationr	   r
   r   Zreportlab.platypusr   r   r   r   r   r   Zreportlab.libr   Zreportlab.lib.pagesizesr   Zreportlab.lib.stylesr   r   r&   r&   r&   r'   <module>   s     