CoolFace
Apppublic

ashnaali22/phase-3-h2

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
task_service.cpython-313.pyc198 linesDownload Raw Back to __pycache__
1�

2��Oi�5���SSKJr SSKJr SSKJr SSKJrJrJr SSK	J3r4Jr SSKJ
r
Jr SSKJr SSKJrJr SS	KJrJr SS5KJrJr SSKJr "SS
5rg)�)�Session)�desc)�UUID)�datetime�	timedelta�timezone)�List�Optional)�
HTTPException�status)�Task)�TaskHistory�6ActionType)�classify_priority�reclassify_priority_on_update)�
validate_tags�normalize_tags)�NotificationServicec��\rSrSrSr\SSS/4S\S\S\S\S\\	S	\7\S8\4Sjj5r\S\S\4Sj5r
\S\S
\S\S9\4Sj5r\SS\S
\S\S\S\S\\	S	\\10\S\\S11\4Sjj5r\S\S
\S\S12\4Sj5r\S\S
\S\S13\4Sj5r\S\S
\S\S14\4Sj5r\SS\S
\S\S\S\S\4Sjj5r\S\S\S\4Sj5rSrg)�TaskService�
z/Service for task operations with user isolationN�db�title�description�user_id�due_date�tags�returnc��[U5n[U5upxU(d[[RUS9e[X5n	[
UUUUUU	SSS9n15URU165 UR5 URU175 [RX18R[RSX:R5 [R!X19U5 U20$)a�21Create a new task for the authenticated user with automatic priority classification.22 23Args:24    db: Database session25    title: Task title26    description: Task description (optional)27    user_id: User UUID who owns this task28    due_date: Task due date (optional)29    tags: Task tags (optional, max 5 from standard categories)30 31Returns:32    Created task instance33 34Raises:35    HTTPException 400: If tags are invalid36��status_code�detail�NOT_STARTEDF)rrrrr�priorityr�is_completedzTask created)rrrr�HTTP_400_BAD_REQUESTrr
�add�commit�refreshr�log_history�idr�CREATEDr�check_and_create_notification)rrrrrr�normalized_tags�is_valid�
error_messager$�tasks           �TD:\code\Q4\hackathon-2\todo-app\phase-3\backend\src\backend\services\task_service.py�create_task�TaskService.create_tasks���6)��.��#0��"@�����"�7�7�$��
�%�U�5����#��� �� ��	37��	���t��38�	�	��39�40�41�4��	����G�G�Z�-?�-?��QX�Zd�Zd�e�	�1�1�"�G�D���c��UR[5R[RU:H5R	[R42R
5[[R55R5$)z�43Get all tasks for a specific user, ordered with incomplete first.44 45Args:46    db: Database session47    user_id: User UUID to filter tasks48 49Returns:50    List of tasks owned by the user51)52�queryr
�filterr�order_byr%�ascr�53created_at�all)rrs  r2�
get_all_tasks�TaskService.get_all_tasksPs\���x�x��~�$�$��L�L�G�#�54�55�(����!�!�#�����!�56��#�%�	r5�task_idc���UR[5R[RU:H5R	5nU(dgUR57U:wa[
[RSS9eU$)aJ58Get a specific task by ID, verifying user ownership.59 60Args:61    db: Database session62    task_id: Task UUID63    user_id: User UUID to verify ownership64 65Returns:66    Task instance if found and owned by user67 68Raises:69    HTTPException 403: If task exists but belongs to different user70    HTTPException 404: If task does not exist71Nz@Access forbidden: You do not have permission to access this taskr )	r7r
r8r+�firstrrr�HTTP_403_FORBIDDEN�rr?rr1s    r2�get_task�TaskService.get_taskcsb��"�x�x��~�$�$�T�W�W��%7�8�>�>�@�����<�<�7�"��"�5�5�Y��
�72�r5rc���[RXU5nU(dgURn	URn73URn/nUb#X(lX):waURSU	SUS35 UbX8lX::waURS5 UbXXlURS5 UbJ[U5n
[U
5up�U(d[URUS9eX�lURS5 Ub�US	;a[URS74S9eXxlURSUSUS35 US:Xa>S
Ul[R"[R 5R#SS9UlOSUlSUlUcUbC['UUURURS9UlURSUR(35 [R"[R 5R#SS9UlUR-5 UR/U5 [R1X[2R4SR7U5XHR5 UcUb[R9XU5 U$)ab75Update task fields with automatic priority re-classification, verifying user ownership.76 77Args:78    db: Database session79    task_id: Task UUID80    title: New title (optional)81    description: New description (optional)82    user_id: User UUID to verify ownership83    due_date: New due date (optional, can be explicitly None to clear)84    tags: New tags (optional, max 5)85    status: New status (optional: NOT_STARTED | IN_PROGRESS | COMPLETED)86 87Returns:88    Updated task instance89 90Raises:91    HTTPException 400: If tags are invalid or status is invalid92    HTTPException 403: If task belongs to different user93Nztitle: 'z' -> '�'zdescription updatedzdue_date updatedr ztags updated)r#�IN_PROGRESS�	COMPLETEDz:Status must be one of: NOT_STARTED, IN_PROGRESS, COMPLETEDz	status: 'rIT��tzinfoF)rr�
current_title�current_due_datezpriority recalculated: z; )rrDrrr�appendrrrrr&rr%r�nowr�utc�replace�completed_atrr$�94updated_atr(r)r*r�UPDATED�joinr-)rr?rrrrrrr1�	old_title�old_desc�95old_status�change_detailsr.r/r0s                r2�update_task�TaskService.update_task�sb��>�#�#�B��9�����J�J�	��#�#���[�[�96������J��!��%�%����6�%���&J�K��"�*���&��%�%�(;�=���$�M��!�!�$4�6���,�T�2�O�'4�O�&D�#�H��#� &� ;� ;�(���97(�I��!�!�L�2����H�H�#� &� ;� ;�W���98!�K��!�!�I�j�\���x�q�"I�J���$�$(��!�$,�L�L����$>�$F�$F�d�$F�$S��!�$)��!�$(��!���� 4�9��!�"�j�j�!%���	�D�M�
�!�!�$;�D�M�M�?�"K�L�"�,�,�x�|�|�4�<�<�D�<�I���99�	�	��100�101�102�4��	����Z�-?�-?����>�AZ�\c�eo�eo�p���� 4��5�5�b��H��r5c���[RXU5nU(dg[RX[RSUR1033U5 UR
U5 UR5 g)z�104Delete a task, verifying user ownership.105 106Args:107    db: Database session108    task_id: Task UUID109    user_id: User UUID to verify ownership110 111Returns:112    True if deleted successfully113 114Raises:115    HTTPException 403: If task belongs to different user116FzTask deleted: T)rrDr*r�DELETEDr�deleter(rCs    r2�delete_task�TaskService.delete_task�s_�� �#�#�B��9����	����Z�-?�-?�>�RV�R\�R\�Q]�A^�`g�h�117�	�	�$��118�	�	��r5c���[RXU5nU(dgSUl[R"[119R5RSS9UlSUl	[R"[120R5RSS9Ul121UR5 URU5 [RX[RSU5 U$)z�122Mark task as completed, verifying user ownership.123 124Args:125    db: Database session126    task_id: Task UUID127    user_id: User UUID to verify ownership128 129Returns:130    Updated task instance131 132Raises:133    HTTPException 403: If task belongs to different user134NTrJrIzTask marked as completed)rrDr%rrOrrPrQrRrrSr(r)r*rrIrCs    r2�
mark_complete�TaskService.mark_completes��� �#�#�B��9���� ���$�L�L����6�>�>�d�>�K���!���"�,�,�x�|�|�4�<�<�D�<�I���135�	�	��136�137�138�4������Z�-A�-A�C]�_f�g��r5c�f�[RXU5nU(dgSUlSUlSUl[139R"[R5RSS9Ul140UR5 URU5 [RX[RSU5 U$)z�141Mark task as incomplete, verifying user ownership.142 143Args:144    db: Database session145    task_id: Task UUID146    user_id: User UUID to verify ownership147 148Returns:149    Updated task instance150 151Raises:152    HTTPException 403: If task belongs to different user153NFr#rJzTask marked as incomplete)rrDr%rRrrrOrrPrQrSr(r)r*r�INCOMPLETEDrCs    r2�mark_incomplete�TaskService.mark_incomplete0s��� �#�#�B��9����!��� ���#���"�,�,�x�|�|�4�<�<�D�<�I���154�	�	��155�156�157�4������Z�-C�-C�E`�bi�j��r5�action�158task_titlec��U(dXUR[5R[RU:H5R	5nU(aUR159OSn[
UUUUUS9nURU5 UR5 g)a;160Log an action to task history with user tracking and task title preservation.161 162Args:163    db: Database session164    task_id: Task UUID165    action: Action type enum166    description: Description of the action167    user_id: User UUID who performed the action168    task_title: Task title (for preservation after deletion)169zUnknown Task)r?ri�action_typerrN)	r7r
r8r+rArrr'r()rr?rhrrrir1�historys        r2r*�TaskService.log_historyOsm����8�8�D�>�(�(����G�);�<�B�B�D�D�'+�����J���!��#��170��	���w��171�	�	�r5r1c�p�URS:wagURS:XdUR(agUR(dg[R172"[R5RSS9nURRc(URR[RS9nOURnURUR(a[ROSS9nXE-173n[S5Us=:a
[SS9::a�O g[UR5S-5n[UR5S-S	-5nUS:�aS174USUS:�aS
OS3n	OS175USUS:�aS
OS3n	SURSU	3n176[R"UUR UU177URS9 gg)z�178Check if task qualifies for notification and create it.179 180Creates notification if:181- Task priority is VERY_IMPORTANT182- Task is not completed183- Task is due within 6 hours184 185Args:186    db: Database session187    task: Task to check188    user_id: User UUID189�VERY_IMPORTANTNrIrJr�)�hoursi�<zin z hour��s�z minutezTask 'z' due )rr?r�messager$)r$rr%rrrOrrPrQrKr�int�
total_secondsrr�create_notificationr+)rr1rrO�task_due�	now_aware�time_until_duerq�minutes�
relative_timervs           r2r-�)TaskService.check_and_create_notificationks}�� �=�=�,�,���;�;�+�%��):�):���}�}���l�l�8�<�<�(�0�0��0�=���=�=���'��}�}�,�,�H�L�L�,�A�H��}�}�H��K�K�x���x�|�|�D�K�Q�	�!�-���Q�<�.�>�I�A�,>�>���4�4�6��=�>�E��>�7�7�9�D�@�B�F�G�G��q�y�"%�e�W�E����#��1K� L�
�"%�g�Y�g�W�q�[�c�b�5Q� R�
��t�z�z�l�&���@�G��3�3����������
�?r5�)NNNNNN)NNN)�__name__�190__module__�__qualname__�__firstlineno__�__doc__�staticmethodr�strr191rr	r
r3r=rrDrZ�boolr_rbrfrr*r-�__static_attributes__r�r5r2rr
s]��9�� ��'+��
=��=��=��=��	=�192�8�$�=��3�i�
=�193�=��=�~��'��C����$��W��t��c��d����<����'+�$(� $�q��q��q��q��	q�194�q��8�$�
q��t�C�y�!�q���
�q�195�q��q�f����$��������4��'��D��3��4����<��G��d��S��T����<����$��196��QT��fi��B����6�7�'�7��7��7��7r5rN)�sqlalchemy.ormr�197sqlalchemyr�uuidrrrr�typingr	r198�fastapirr�backend.models.taskr
�backend.models.task_historyrr�$backend.services.priority_classifierrr�backend.services.tag_validatorrr�%backend.services.notification_servicerrr�r5r2�<module>r�s5��"���2�2�!�)�$�?�a�H�E�V�Vr5