403Webshell
Server IP : 65.108.144.40  /  Your IP : 216.73.217.165
Web Server : Apache/2.4.52 (Ubuntu)
System : Linux ubuntu-8gb-hel1-1 5.15.0-173-generic #183-Ubuntu SMP Fri Mar 6 13:29:34 UTC 2026 x86_64
User : dev ( 1000)
PHP Version : 8.2.30
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : ON
Directory :  /var/www/erp/horilla_api/api_views/attendance/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/erp/horilla_api/api_views/attendance/__pycache__/views.cpython-310.pyc
o

hͦh��@s�ddlmZmZmZmZddlmZddlmZddlm	Z	ddl
mZmZm
Z
mZmZddlmZddlmZddlmZdd	lmZdd
lmZddlmZddlmZdd
lmZm Z m!Z!ddl"Tddl"m#Z#ddl$m%Z%m&Z&m'Z'ddl(Tddl)m*Z*ddl+m,Z,m-Z-ddl.m/Z/ddl0m1Z1ddl2m3Z3m4Z4ddl5m6Z6m7Z7ddl8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>dd�Z?Gdd�de�Z@Gdd�de�ZAGdd �d e�ZBGd!d"�d"e�ZCGd#d$�d$e�ZDGd%d&�d&e�ZEGd'd(�d(e�ZFGd)d*�d*e�ZGGd+d,�d,e�ZHGd-d.�d.e�ZIGd/d0�d0e�ZJGd1d2�d2e�ZKGd3d4�d4e�ZLGd5d6�d6e�ZMGd7d8�d8e�ZNGd9d:�d:e�ZOGd;d<�d<e�ZPGd=d>�d>e�ZQd?S)@�)�date�datetime�	timedelta�timezone)�template)�settings)�EmailMessage)�Case�	CharField�F�Value�When)�	QueryDict)�get_object_or_404)�method_decorator)�PageNumberPagination)�IsAuthenticated)�Response)�APIView)�
Attendance�AttendanceActivity�EmployeeShiftDay)�*)�	clock_out)�find_expected_attendances�find_late_come�find_on_time)�ConfiguredEmailBackend)�generate_pdf�is_reportingmanager)�EmployeeFilter)�RecruitmentMailTemplate�)�manager_permission_required�permission_required)�groupby_queryset�permission_based_queryset)�AttendanceActivitySerializer�$AttendanceLateComeEarlyOutSerializer�AttendanceOverTimeSerializer�AttendanceRequestSerializer�AttendanceSerializer�MailTemplateSerializercCsRtddd�}|��D]\}}t|t�r|D]}|�||�qq
|�||i�q
|S)N�T)�mutable)r�items�
isinstance�list�
appendlist�update)�data�
query_dict�key�value�item�r9�6/var/www/erp/horilla_api/api_views/attendance/views.pyr5.s
�r5c@�eZdZdZegZdd�ZdS)�ClockInAPIViewz�
    Allows authenticated employees to clock in, determining the correct shift and attendance date, including handling night shifts.

    Methods:
        post(request): Processes and records the clock-in time.
    cCsFt|�\}}t��}|j�d�r|j}|r�|dur�|j}t��}|j�d�r)|j}|}|�d��	�}t
jj|d�}t���d�}	|j�d�rL|j�d�}	t
|	�}
t
d�}t||d�\}}
}|
|kr�||
kr�|td	d
�}|�d��	�}t
jj|d�}t||d�\}}
}|}|}t|||||	|||
||d�
tdd
idd�Stddi�S)Nrr�%A)�dayz%H:%M�timez12:00)r>�shift�)�days)
�employee�
date_today�attendance_dater>�nowr@�minimum_hour�
start_time�end_time�in_datetime�messagez
Clocked-In����status�errorzOYou Don't have work information filled or your employee detail neither entered )�employee_existsrrF�__dict__�get�shift_idr�today�strftime�lowerr�objectsr?�strtime_seconds�shift_schedule_todayr� clock_in_attendance_and_activityr)�self�requestrC�	work_info�datetime_nowr@rDrEr>rF�now_sec�mid_day_secrG�start_time_sec�end_time_sec�date_yesterday�
day_yesterdayr9r9r:�postCs^�����zClockInAPIView.postN��__name__�
__module__�__qualname__�__doc__r�permission_classesrer9r9r9r:r<9�r<c@r;)�ClockOutAPIViewz�
    Allows authenticated employees to clock out, updating the latest attendance record and handling early outs.

    Methods:
        post(request): Records the clock-out time.
    c
Cs�t��}t����}t��}ztt|j|||d��tddidd�WSt	y:}zt
�d|�WYd}~nd}~wwtddidd�S)N)�userrr?rrKzClocked-OutrLrMzGot an error in clock_out)rrTrrFr?r�Requestrnr�	Exception�loggerrO)r[r\�current_date�current_time�current_datetimerOr9r9r:re�s$����zClockOutAPIView.postNrfr9r9r9r:rm}rlrmc@sdeZdZdZegZeZdd�Zddd�Z	e
d�dd	��Zee
d
��dd��Zee
d
��dd��ZdS)�AttendanceViewa�
    Handles CRUD operations for attendance records.

    Methods:
        get_queryset(request, type): Returns filtered attendance records.
        get(request, pk=None, type=None): Retrieves a specific record or a list of records.
        post(request): Creates a new attendance record.
        put(request, pk): Updates an existing attendance record.
        delete(request, pk): Deletes an attendance record and adjusts related overtime if needed.
    cCs�|dkrtj��}td�}|durt|j�}tjj|dd�}n|dkr+tjjdd�}n|dkr7tjjdd�}ntj��}|j}d	}t	|||dd
�}|S)N�otz00:30T)�overtime_second__gte�attendance_validated�	validated�rxz
non-validatedF�attendance.view_attendance)�user_obj)
�AttendanceValidationConditionrW�firstrX�minimum_overtime_to_approver�filter�allrnr&)r[r\�type�	condition�minot�querysetrn�permr9r9r:�get_queryset�s&

��
zAttendanceView.get_querysetNcCs�|rtt|d�}t|d�}t|jdd�S|�||�}|j|j|d�j}|j�	dd�}|r7|�
�}	t||	||�St�}
|
�
||�}t|dd�}|
�|j�S)	N��pk��instancerLrM)r��
groupby_fieldT��many)rrr+rr4r��filterset_class�GET�qsrR�build_absolute_urir%r�paginate_queryset�get_paginated_response)r[r\r�r��
attendance�
serializer�attendances�attendances_filter_queryset�
field_name�url�	paginater�pager9r9r:rR�s(
���zAttendanceView.getzattendance.add_attendancecCs|t|jd�}|��r|��t|jdd�S|j�d�}|j�dt���}tj	j
||d���r7tddgid	d�St|jd	d�S)
N�r4rLrM�employee_idrE�r�rErO�@Attendance for this employee on the current date already exists.�)
r+r4�is_valid�saverrRrrTrrWr��exists�errors�r[r\r�r�rEr9r9r:re�s&�����zAttendanceView.post�attendance.change_attendancecCs�z	tjj|d�}Wntjytddidd�YSwt||jd�}|��r2|��t|jdd�S|j	}d|j	vrHd	}||j	dvrHdd
gi}t|dd�S)N��id�detailzAttendance record not found.�rM�r�r4rL�non_field_errorsz?The fields employee_id, attendance_date must make a unique set.z1The employee already has attendance on this date.r�)
rrWrR�DoesNotExistrr+r4r�r�r�)r[r\r�r�r��serializer_errors�unique_error_msgr9r9r:�put�s&�
���zAttendanceView.putzattendance.delete_attendancec	
Cs(tjj|d�}|j}|�d���}|jjj|d��	�}|durk|j
rBt|j�}t|j
�}||kr5||}n||}t|�|_|��z
|��tddhdd�WStyj}ztd|�hd	d�WYd}~Sd}~wwz
|��tddhdd�WSty�}ztd|�hd	d�WYd}~Sd}~ww)
Nr�z%B)�monthrN�deletedrLrMzerror:r�)rrWrRrErUrVr��employee_overtimer��last�attendance_overtime_approverX�overtime�attendance_overtime�format_timer��deleterrp)	r[r\r�r�r�r��total_overtime�attendance_overtime_secondsrOr9r9r:r�s:
�

����zAttendanceView.delete)NN)rgrhrirjrrk�AttendanceFiltersr�r�rRr#rerr$r�r�r9r9r9r:ru�s




ruc@�eZdZdZdd�ZdS)�ValidateAttendanceViewz�
    Validates an attendance record and sends a notification to the employee.

    Method:
        put(request, pk): Marks the attendance as validated and notifies the employee.
    cCs�tjj|d�jdd�}tjj|d���}z1tj|jj|j	j
d|j�d�d|j��d|j�d�d	|j�d
�d|j�d�d
dd|j	��d�
WnYtdd�S)Nr�TrzzYour attendance for the date z
 is validatedu+تم تحقيق حضورك في تاريخ u!Deine Anwesenheit für das Datum u ist bestätigt.z&Se valida tu asistencia para la fecha �.uVotre présence pour la date u est validée.z/attendance/view-my-attendance�	checkmarkz&/api/attendance/attendance?employee_id�	�	recipient�verb�verb_ar�verb_de�verb_es�verb_fr�redirect�icon�api_redirectrLrM)
rrWr�r3r~�notify�sendrn�employee_getr��employee_user_idrEr�r[r\r�r�r9r9r:r�4s$


�
zValidateAttendanceView.putN�rgrhrirjr�r9r9r9r:r�,�r�c@r�)�OvertimeApproveViewz�
    Approves overtime for an attendance record and sends a notification to the employee.

    Method:
        put(request, pk): Marks the overtime as approved and notifies the employee.
    cCs�z
tjj|d�jdd�}Wnty)}ztdt|�idd�WYd}~Sd}~wwtjj|d���}z.tj	|j
j|jj
d|j�d�d	|j�d
�d|j�d�d
|j�d
�d|j�d�dddd�
WnYtdd�S)Nr�T)r�rOr�rMzYour z 's attendance overtime approved.u^تمت الموافقة على إضافة ساعات العمل الإضافية لتاريخ r�uDie Überstunden für den z wurden genehmigt.z5Se ha aprobado el tiempo extra de asistencia para el u)Les heures supplémentaires pour la date u ont été approuvées.z$/attendance/attendance-overtime-viewr�z(/api/attendance/attendance-hour-account/r�rL)rrWr�r3rpr�strr~r�r�rnr�r�r�rE�r[r\r�r��Er9r9r:r�Qs2
� ��
�
zOvertimeApproveView.putNr�r9r9r9r:r�Ir�r�c@s<eZdZdZeZegZd
dd�Zdd�Z	e
d�dd	��ZdS)�AttendanceRequestViewaJ
    Handles requests for creating, updating, and viewing attendance records.

    Methods:
        get(request, pk=None): Retrieves a specific attendance request by `pk` or a filtered list of requests.
        post(request): Creates a new attendance request.
        put(request, pk): Updates an existing attendance request.
    NcCs�|rtjj|d�}t|d�}t|jdd�Stjjdd�}t|d|d�}|tjj|jdd	�B}t	|j
|�j}|j
�d
d�}|rI|��}t
||||�St�}	|	�||�}
|j|
dd�}|	�|j�S)Nr�r�rLrMT)�is_validate_requestr{)r\r�r�)�employee_id__employee_user_idr�r�r�)rrWrRr*rr4r��filtersubordinatesrnr�r�r�r�r%rr��serializer_classr�)r[r\r�r�r��requests�request_filtered_querysetr�r��
pagenationr�r9r9r:rRys2
���zAttendanceRequestView.getcCs|t|jd�}|��r|��t|jdd�S|j�d�}|j�dt���}tj	j
||d���r7tddgid	d�St|jd
d�S)Nr�rLrMr�rEr�rOr�r�r�)
r*r4r�r�rrRrrTrrWr�r�r�r�r9r9r:re�s&�����zAttendanceRequestView.postzattendance.update_attendancecCs�tjj|d�}t||jd�}|��rH|��}|j|_|j|_|j	dkr7t
�|���|_
|j|_d|_|��n
d|_d|_|��t|jdd�St|jdd�S)	Nr�r��create_requestTFrLrMr�)rrWrRr*r4r�r�r�r��request_type�json�dumps�	serialize�requested_data�request_descriptionr��is_validate_request_approvedrr�)r[r\r�r�r�r�r9r9r:r��s 

zAttendanceRequestView.put�N)rgrhrirjr*r�rrkrRrer#r�r9r9r9r:r�ls	
r�c@s eZdZdZed�dd��ZdS)�AttendanceRequestApproveViewz�
    Approves and updates an attendance request.

    Method:
        put(request, pk): Approves the attendance request, updates attendance records, and handles related activities.
    r�c

Csxz�tjj|d�}|j}|j}|j}d|_d|_d|_d|_	|�
�|jdur`t�
|j�}|ddkr4dn|d|d<|ddkrBdn|d|d<tjj|d�jdi|��tjj|d�}|�
�|jdusj|jdur�d|_tjj|j|||d�}|r�|j|j|j|j|jd�ntjj|j|j|j|jd�Wnty�}	ztdt|	�id	d
�WYd}	~	Sd}	~	wwtddid
d
�S)Nr�TF�attendance_clock_out�None�attendance_clock_out_date)r�rE�
clock_in_date�clock_inrOr�rMrN�approvedrLr9)rrWrRrE�attendance_clock_in_date�attendance_clock_inrxr�r�r�r�r�r��loadsr�r3r�r�rr��createrprr�)
r[r\r�r��prev_attendance_date�prev_attendance_clock_in_date�prev_attendance_clock_inr��activityr�r9r9r:r��sf
����

���� ��z AttendanceRequestApproveView.putN)rgrhrirjr#r�r9r9r9r:r��sr�c@r�)�AttendanceRequestCancelViewz�
    Cancels an attendance request.

    Method:
        put(request, pk): Cancels the attendance request, resetting its status and data, and deletes the request if it was a create request.
    c
Cs�z6tjj|d�}|jj|jkst|�s|j�d�r5d|_d|_	d|_
d|_d|_|�
�|jdkr5|��WntyR}ztdt|�idd�WYd}~Sd}~wwtdd	id
d�S)Nr�r�Fr�rOr�rMrN�successrL)rrWrRr�r�rnr�has_permr�r�r�r�r�r�r�rprr�r�r9r9r:r�s*�
�
� ��zAttendanceRequestCancelView.putNr�r9r9r9r:r�r�r�c@sTeZdZdZegZddd�Zed�dd��Zed�d	d
��Z	e
ed��dd
��ZdS)�AttendanceOverTimeViewa~
    Manages CRUD operations for attendance overtime records.

    Methods:
        get(request, pk=None): Retrieves a specific overtime record by `pk` or a list of records with filtering and pagination.
        post(request): Creates a new overtime record.
        put(request, pk): Updates an existing overtime record.
        delete(request, pk): Deletes an overtime record.
    Nc
Cs�|rtt|d�}t|�}t|jdd�St|j�}|j}|j|j	d�}t
||d�}||B}|j�dd�}	|	r@|��}
t
||
|	|�St�}|�||�}t|dd�}|�|j�S)	Nr�rLrM)r�z"attendance.view_attendanceovertimer�Tr�)r�AttendanceOverTimer)rr4�AttendanceOverTimeFilterr�r�r�rnr�rRr�r%rr�r�)
r[r\r��
attendance_otr�r�r��self_accountr&r�r�r�r�r9r9r:rR/s&
�zAttendanceOverTimeView.getz!attendance.add_attendanceovertimecCs8t|jd�}|��r|��t|jdd�St|jdd�S)Nr�rLrMr�)r)r4r�r�rr�)r[r\r�r9r9r:reGs
zAttendanceOverTimeView.postz$attendance.change_attendanceovertimecCsFtt|d�}t||jd�}|��r|��t|jdd�St|jdd�S)Nr�r�rLrMr�)rr�r)r4r�r�rr�)r[r\r�r�r�r9r9r:r�Os�zAttendanceOverTimeView.putz$attendance.delete_attendanceovertimecC�$tt|d�}|��tddidd�S)Nr�rKzOvertime deleted successfully��rM)rr�r�rr�r9r9r:r�ZszAttendanceOverTimeView.deleter�)
rgrhrirjrrkrRr#rer�rr$r�r9r9r9r:r�"s





r�c@s*eZdZdZegZddd�Zddd�ZdS)�LateComeEarlyOutViewa
    Handles retrieval and deletion of late come and early out records.

    Methods:
        get(request, pk=None): Retrieves a list of late come and early out records with filtering.
        delete(request, pk=None): Deletes a specific late come or early out record by `pk`.
    NcCs&t|j�}t|jdd�}t|jdd�S�NTr�rLrM)�LateComeEarlyOutFilterr�r(r�rr4�r[r\r�r4r�r9r9r:rRms
zLateComeEarlyOutView.getcCr�)Nr�rKzAttendance deleted successfullyrrM)r�AttendanceLateComeEarlyOutr�rr�r9r9r:r�rszLateComeEarlyOutView.deleter�)rgrhrirjrrkrRr�r9r9r9r:rbs

rc@seZdZdZddd�ZdS)�AttendanceActivityViewz�
    Retrieves attendance activity records.

    Method:
        get(request, pk=None): Retrieves a list of all attendance activity records.
    NcCs$tj��}t|dd�}t|jdd�Sr)rrWr�r'rr4rr9r9r:rR��
zAttendanceActivityView.getr��rgrhrirjrRr9r9r9r:rxsrc@r�)�TodayAttendancez�
    Provides the ratio of marked attendances to expected attendances for the current day.

    Method:
        get(request): Calculates and returns the attendance ratio for today.
    c
Cstt��}|�d���}t|||d�}t|d�}t|�}||}t|d�}d}	|dkr2||dd�}	td|	id	d
�S)Nr=)rT�week_day)�
start_date)r
r�dz.2f�marked_attendances_ratiorLrM)	rrTrUrVrr�lenrr)
r[r\rTr
�on_time�	late_come�
late_come_obj�marked_attendances�expected_attendancesr
r9r9r:rR�s

��zTodayAttendance.getNrr9r9r9r:r	�r�r	c@r�)�OfflineEmployeesCountViewz�
    Retrieves the count of active employees who have not clocked in today.

    Method:
        get(request): Returns the number of active employees who are not yet clocked in.
    cCs6tdt��i�jjdd�jdd���}td|idd�S)N�
not_in_yetT��employee_work_info__isnull��	is_active�countrLrM)r rrTr��excluder�rr)r[r\rr9r9r:rR�s���zOfflineEmployeesCountView.getNrr9r9r9r:r�r�rc@s eZdZdZdd�Zdd�ZdS)�OfflineEmployeesListViewz�
    Lists active employees who have not clocked in today, including their leave status.

    Method:
        get(request): Retrieves and paginates a list of employees not clocked in today with their leave status.
    cCsHtdt��i�jjdd�jdd�}|�|�}t�}|�||�}|�	|�S)NrTrr)
r rrTr�rr��get_leave_statusrr�r�)r[r\r��leave_statusr�r�r9r9r:rR�s���

zOfflineEmployeesListView.getc	Cs�t��}|��}|jtt||dtd�d�t||dtd�d�t||td�d�t|td�d	�td
�t�d�td�d
��	dddddd�}|D]}|drRt
j|d|d<qC|S)Nr�zOn Leave)�leaverequest__start_date__lte�leaverequest__end_date__gte�leaverequest__status�then�	requestedzWaiting ApprovalzCanceled / Rejected)rr r"�Working)�%employee_attendances__attendance_dater"zExpected working)�default�output_field�#employee_work_info__job_position_id)r�job_position_id�employee_first_name�employee_last_namer�employee_profiler�r))rrT�distinct�annotater	r
rr
r�valuesr�	MEDIA_URL)r[r�rT�employees_with_leave_statusrCr9r9r:r�sX��������$��z)OfflineEmployeesListView.get_leave_statusN)rgrhrirjrRrr9r9r9r:r�src@s*eZdZdZegZedd��Zdd�ZdS)�CheckingStatusz�
    Checks and provides the current attendance status for the authenticated user.

    Method:
        get(request): Returns the attendance status, duration at work, and clock-in time if available.
    cCs6|d}|dd}|d}|d�d|d�d|d��S)Ni�<�02�:r9)�cls�seconds�hours�minutesr9r9r:�_format_secondsszCheckingStatus._format_secondsc	Cs�tjj|jjd��d���}d}|jj��d}t�	t
|��}d}d}t��}tjj|jj|d��d���}|rP|j
�d�}|jrDd}nd}t|||d	�d
d�St|||d�d
d�S)
N)r�z-id�forecasted_at_work_secondsF)r�r�rJz%I:%M %pT)rN�durationr�rLrM)rNr<�
clock_in_time)rrWr�rnr��order_byr~�get_forecasted_at_workr2r:�intrrFr�rU�clock_out_dater)	r[r\�attendance_activityr<�work_secondsrNr=rT�attendance_activity_firstr9r9r:rR
s>�
���
�
�zCheckingStatus.getN)	rgrhrirjrrk�classmethodr:rRr9r9r9r:r2�s
r2c@r;)�MailTemplateViewz�
    Retrieves a list of recruitment mail templates.

    Method:
        get(request): Returns all recruitment mail templates.
    cCs$tj��}t|dd�}t|jdd�Sr)r!rWr�r,rr4)r[r\�	instancesr�r9r9r:rR=rzMailTemplateView.getN)rgrhrirjrrkrRr9r9r9r:rF3rlrFc@r;)�ConvertedMailTemplateConvertz�
    Renders a recruitment mail template with data from a specified employee.

    Method:
        put(request): Renders the mail template body with employee and user data and returns the result.
    c	Csr|j�dd�}|j�dd�}tjj|d���}tjj|d���}t�|j	�}t�
||jjd��}|�
|�}t|�S)N�template_idr�r��r�r[)r4rR�EmployeerWr�r~r!r�Template�body�Contextrnr��renderr)	r[r\rIr�rC�bdy�template_bdy�context�
render_bdyr9r9r:r�Ms�
z ConvertedMailTemplateConvert.putN)rgrhrirjrrkr�r9r9r9r:rHCrlrHc@r;)�OfflineEmployeeMailsendz�
    Sends an email with attachments and rendered templates to a specified employee.

    Method:
        post(request): Renders email templates with employee and user data, attaches files, and sends the email.
    c
Cs�|j�d�}|j�dd�}|j�dd�}|j�d�}dd�|D�}t�}|j}tjj|d�}	|j�d	�}
tt	jj
|
d
�jddd��}|D]%}t�
|�}
t�|	|jjd
��}|
�|�}|�dt|iddd�jdf�qDt�
|�}
t�|	|jjd
��}|
�|�}t||||	jjg�}d|_||_z|��|	jjr�td|	�����WStd|	�����WSty�}z
td�WYd}~Sd}~ww)Nr��subjectr-rM�other_attachmentscSsg|]}|j|��|jf�qSr9)�name�read�content_type)�.0�filer9r9r:�
<listcomp>is�z0OfflineEmployeeMailsend.post.<locals>.<listcomp>r��template_attachments)�id__inT)�flatrJ�DocumentF)�path�titlezapplication/pdf�htmlz
Mail sent to zEmail not set for zSomething went wrong)�POSTrR�FILES�getlistr�dynamic_usernamerKrWr1r!r��values_listrrLrNrnr�rO�appendr�contentr�employee_work_info�email�content_subtype�attachmentsr�r�
get_full_namerp)r[r\r�rUrPrVrn�
email_backend�hostrC�template_attachment_ids�bodysrcrQrRrSrl�er9r9r:redsh����
�
��
�
���zOfflineEmployeeMailsend.postNrfr9r9r9r:rTZrlrTN)Rrrrr�djangor�django.confr�django.core.mailr�django.db.modelsr	r
rrr
�django.httpr�django.shortcutsr�django.utils.decoratorsr�rest_framework.paginationr�rest_framework.permissionsr�rest_framework.responser�rest_framework.viewsr�attendance.modelsrrr�attendance.views.clock_in_outr�attendance.views.dashboardrrr�attendance.views.views�
base.backendsr�base.methodsrr�employee.filtersr �recruitment.modelsr!�api_decorators.base.decoratorsr#r$�api_methods.base.methodsr%r&�&api_serializers.attendance.serializersr'r(r)r*r+r,r5r<rmrur�r�r�r�r�r�rrr	rrr2rFrHrTr9r9r9r:�<module>sX D #UC@E7

Youez - 2016 - github.com/yon3zu
LinuXploit