
    P1i                        d dl m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 d dlmZ d dlmZ d d	lmZ d d
lmZ d Zed        Zd dlZd dlmZ d dlmZ ed        Zd dlmZ d dlmZmZ d d	lmZ d dl m!Z!  G d de      Z"d dl#m$Z$ d dl%m
Z
 d dlm&Z& d dl'm(Z( d dl)m*Z* d dl+m,Z,  e edd       edd       ed d!       ed"d#       ed$d%       ed&d'      (       G d) d*e$jZ                               Z.y)+    )render   )liuyao)APIViewstatus)ResponseN)login_required)	Paginator)datetime)settingsc                     dt        j                  t        j                  j                        j                  d      i}t        | d|      S )z!Main liuyao page with entry form.current_datetimez%Y-%m-%d %H:%Mzliuyao/liuyao-entry.html)r   nowiutilsbzgMYTimezonestrftimer   )requestcontexts     ./home/cursorai/projects/iching/liuyao/views.pyliuyao_entryr      sA     	HLL)>)>?HHIYZG '5w??    c                    t         j                  j                  | j                  j                        j                  d      }| j                  j                  d      }|s| j                  j                  d      }|r3|j                         r#dt        |      cxk  rdk  rn nt        |      }n1t        j                  j                  dt        j                  d         }t        ||      }| j                  j                  d	      }|j                  |      }t        t!        |j"                  d
z
  d      t%        |j"                  d
z   |j&                  j(                        dz         }|||d}t+        | d|      S )z'Display list of existing liuyao charts.user_idz-idliuyao_items_per_pageper_page   d   r   defaultpage   r   )items
page_rangeitems_per_pagezliuyao/liuyao-list.html)r   objectsfilteruseridorder_bysessiongetGETisdigitintr   PAGINATE_BYr
   get_pagerangemaxnumbermin	paginator	num_pagesr   )	r   r#   r%   	url_valuer6   page_numbercurrent_pager$   r   s	            r   
LiuyaoListr;      s>    NN!!',,//!:CCEJE __(()@ANKKOOJ/	**,c)n1K1K ^N%1155h@T@TU^@_`N%0I++//&)K%%k2L L!#Q'L!#\%;%;%E%EFJJ  (G
 '4g>>r   )model_to_dict)ObjectDoesNotExistc                    fd	 t         j                  j                  || j                  j                        }t        |      }t        j                   j                  ||j                        } |      }t        j                  |      }ddlm} |j                         }t        | d	|||d
      S # t        $ r t        | dd      cY S w xY w)Nc                     t        | t              r| j                  d      S t        | t              r*| j	                         D ci c]  \  }}| |       c}}S t        | t
              r| D cg c]
  } |       c}S | S c c}}w c c}w )Nz%Y-%m-%d %H:%M:%S)
isinstancer   r   dictr#   list)objkeyvalueitemconvert_datetime_to_isos       r   rG   z-LiuyaoDetail.<locals>.convert_datetime_to_iso?   s    c8$<< 344T"JM))+V+JCC077+VVT">ABcd+D1cBB
 WBs   B1B)pkr   )datezerrors/404.htmli  )r   r   )ConversationConfigzliuyao/liuyao-detail.html)rF   	item_jsonmax_messages)r   r&   r,   r(   r)   r<   r   recalculate_liuyao_dataqdatejsondumpsr=   r   	ai.modelsrJ   get_max_messages)r   rH   rF   	item_dictrK   rJ   rL   rG   s          @r   LiuyaoDetailrT   <   s    	>~~!!R!A!$'	 MM99)$**9U	+I6	JJy)	
 -%668L'6$9    >g0==>s   BB7 7CC)LiuyaoCalculatorSerializer)extend_schemaextend_schema_view)	QueryDictc            	       >    e Zd ZeZ eddeegedd      d        Zy)LiuyaoCalculatorzCalculate Liuyao resultzLThis endpoint calculates the Liuyao result based on the provided input data.zInvalid input data)   i  )summarydescriptionr   
parameters	responsesc                    t        |j                        }|j                  d       |j                  }|j	                  dd      }|r-t        j                  t        j                  j                        nCt        |d   |d   |d   gt        t        |j	                  d	d
      j                  d             }|d   |d   |d   |d   |d   |d   i d}t        j                  j                  ||      }|d   |d   d   |d   d   |d   d   |d   d   d|d   |d    d|d    d|d    d|d    d|d    d|d    d}	t        |	      S )N)dataT)raise_exceptionusecurFyearmonthdaytimez00:00:y1y2y3y4y5y6)ri   rj   rk   rl   rm   rn   ra   questionra   r   god6lyrel)ro   r   rp   rq   rr   |)guaro   yao)rU   query_paramsis_validvalidated_datar,   r   r   r   r   r   mapr/   splitr   rM   r   )
selfr   argskwargs
serializerrx   rc   rI   	item_dataresults
             r   r,   zLiuyaoCalculator.getd   s    0W5I5IJ
D1#22  ##He4  LL../v&w'u% S.,,VW=CCCHI	 	 !& & & & & &
	 MM99)TJ	 +:6'-!&)&1'- (/ 'z2$T*+1^D-A,B!NSWDXCYYZ[ijn[oZppq  sA  BF  sG  rH  HI  JX  Y]  J^  I_  `

 r   N)__name__
__module____qualname__rU   serializer_classrV   r,    r   r   rZ   rZ   a   s=    1)b*&
 ,%
- - r   rZ   )viewsets)utils)LiuyaoSerializer)IsAuthenticated)LoginRequiredMixin)SessionAuthenticationzList all Liuyao objectsz4Retrieve a list of all Liuyao objects in the system.)r\   r]   zRetrieve a Liuyao objectz2Get details of a specific Liuyao object by its ID.zCreate a Liuyao objectz&Add a new Liuyao object to the system.zUpdate a Liuyao objectz+Update an existing Liuyao object by its ID.z Partially update a Liuyao objectz5Partially update an existing Liuyao object by its ID.zDelete a Liuyao objectz*Delete a specific Liuyao object by its ID.)rB   retrievecreateupdatepartial_updatedestroyc                   Z    e Zd Zej                  j                         ZeZe	gZ
g Zd Zd Zy)liuyaoViewSetc                     t        j                  | j                        }| j                  j                  }|j                  r|j
                  nd }|j                  ||       y )N)uuidr   )r   getUUIDr   r(   is_authenticatedr)   save)r{   r~   
uuid_valuer(   r   s        r   perform_createzliuyaoViewSet.perform_create   sJ    ]]4<<0
 ||  !22$'' 	Z9r   c                     | j                   j                  }|j                  r*t        j                  j                  |j                        S t        j                  j                         S )Nr   )r   r(   r   r   r&   r'   r)   none)r{   r(   s     r   get_querysetzliuyaoViewSet.get_queryset   sK    ||    >>(((99 ~~""$$r   N)r   r   r   r   r&   allquerysetr   r   r   authentication_classespermission_classesr   r   r   r   r   r   r      s5    6 ~~!!#H'34	:%r   r   )/django.shortcutsr   modelsr   rest_framework.viewsr   r   rest_framework.responser   iching.utilsr   r   django.contrib.auth.decoratorsr	   django.core.paginatorr
   r   django.confr   r   r;   rO   django.forms.modelsr<   django.core.exceptionsr=   rT   api.serializersrU   drf_spectacular.utilsrV   rW   django.httprX   rZ   rest_frameworkr   main.visitorr   rest_framework.permissionsr   django.contrib.auth.mixinsr   rest_framework.authenticationr   ModelViewSetr   r   r   r   <module>r      s   #  0 ,  #  9 +   @ ? ?>  - 5 @ 7 C  !< w < | $  , 6 9 ?	)J
 *H (< (A !2K (@+4%H)) %54%r   