o
    ȟ1iK                     @   s:   d dl mZ d dlmZ d dlmZmZ G dd dZdS )    )get_db)CoreDBError)
AttendanceCoursec                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )AttendanceServicec           	      C   s,  t  }| }|ttj|jktj|jktj|jktj|jktj	
d }| dkrM| }|ttj|jktj	
d j|dd}|  n7ztdi |}|| |  || |W W  d    S  ty } z|  td| d }~ww W d    d S 1 sw   Y  d S )Nr   fetchsynchronize_sessionz!Could not create new attendance:  )r   
model_dumpqueryr   whereattendance_datestaff_idparticipant_id	course_id
deleted_atis_countfirstfilteridupdatecommitaddrefresh	Exceptionrollbackr   )	selfrequestdbpayloadr   instanceresultZnew_attendanceer
   r
   /app/services/attendance.pysave   s<   






"zAttendanceService.savec                 C   s   t  g}|ttj|jktj|jk}|jr!|tj|jk}g }| D ]:}||j	|j
|j|j|j|j|j|jt|jj|jjdt|jj|jj	|jjjdt|jj|jj	|jjdd q'W d    |S 1 smw   Y  |S N)
first_name	last_name)namer   	age_group)r(   r   r)   )attendance_idr   r   r   attendance_statusremarksr   modified_by_adminstaffcourseparticipant)r   r   r   r   r   r   r   allappendr   r   r-   r.   r/   dictr0   r(   r)   r1   r*   r+   labelr2   r   r   userr    r   datarowr
   r
   r%   view_attendance_from_staff%   s4   "

z,AttendanceService.view_attendance_from_staffc                 C   s6  t  }|ttj|jkttjtjk}|j	r$|tj	|j	k}|j
r0|tj
|j
k}|jr<|tj|jk}|jrH|tj|jk}g }| D ]:}||j|j|j|j|j|j|j|jt|jj|jjdt|jj|jj|jjjdt|jj|jj|jjdd qNW d    |S 1 sw   Y  |S r'   )r   r   r   r   r   joinr   r   r   academic_year_idterm_idr   r3   r4   r   r-   r.   r/   r5   r0   r(   r)   r1   r*   r+   r6   r2   r7   r
   r
   r%   view_attendance_from_parent?   s@   (

!!z-AttendanceService.view_attendance_from_parentc                 C   s  t  }|ttj|jktj|jktj|jkttj	tj
k}|jr.|tj|jk}|j	r:|tj
|j	k}g }| D ]:}||j
|j|j	|j|j|j|j|jt|jj|jjdt|jj|jj
|jjjdt|jj|jj
|jjdd q@W d    |S 1 sw   Y  |S r'   )r   r   r   r   r   r=   r>   r   r<   r   r   course_category_idr3   r4   r   r   r-   r.   r/   r5   r0   r(   r)   r1   r*   r+   r6   r2   r7   r
   r
   r%   view_attendance_from_adminc   s8   <

z,AttendanceService.view_attendance_from_adminc                 C   sv   t  .}|jdgd}d|d< |ttj|jktjd j	|dd}|
  W d    d S 1 s4w   Y  d S )Nr,   )excludeTr/   r   r   )r   r   r   r   r   r   r,   r   r   r   r   )r   r   r8   r    r!   r#   r
   r
   r%   update_attendance_from_admin   s   
"z.AttendanceService.update_attendance_from_adminN)__name__
__module____qualname__r&   r;   r?   rA   rC   r
   r
   r
   r%   r      s    $r   N)core.configr   core.security.exceptionsr   modelsr   r   r   r
   r
   r
   r%   <module>   s    