
    P1i(                     X   d Z 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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mZ ddlmZ ddlZddlmZ ddlmZ ddl m!Z!m"Z" ddl#m$Z$m%Z% ed        Z& G d dee      Z' G d dee      Z( edg      d        Z) edg      d        Z*y)z<
Custom admin views for AI reporting and conversation views
    )staff_member_required)LoginRequiredMixin)HttpResponseJsonResponse)TemplateResponse)QCount)timezone)ListView
DetailView)require_http_methods)csrf_exempt)get_object_or_404redirect)	timedeltaN)Person)liuyao)ConversationMessage)create_conversationsend_conversation_messagec                 l
   t         j                  j                  d      j                  dd      }t         j                  j                  d      j                  dd      }t        j                  j                  d      j                  dd	      }|j                         |j                         z   |j                         z   }|j                  d
      j                         }|j                  d
      j                         }|j                  d
      j                         }||z   |z   }t        j                         t        d      z
  }	|j                  |	      j                         }
|j                  |	      j                         }|j                  |	      j                         }|
|z   |z   }g }|j                  d      dd D ]  }|j                  dd|j                  |j                  |j                  |j                  |j                  d|j                    d|j"                  r*t%        |j"                        dkD  r|j"                  dd dz   n|j"                  d	        |j                  d      dd D ]  }|j                  dd|j                  |j                  |j&                  |j(                  |j*                  d|j                    d|j,                  r*t%        |j,                        dkD  r|j,                  dd dz   n|j,                  d	        |j                  d       dd D ]  }|j                  d!d"|j.                  dd# t%        |j.                        d#kD  rdnd$z   |j0                  |j2                  |j4                  |j6                  d%|j                    d|j8                  r*t%        |j8                        dkD  r|j8                  dd dz   n|j8                  d	        |j;                  d& d'       |dd( }i }g d)}|D ]o  }|j                  |*      j                         |j                  |+      j                         z   |j                  |,      j                         z   }|d-kD  sk|||<   q i d.d/d0|d1|d2|d3|j                         d4|j                         d5|j                         d6|d7|d8|d9|
d:|d;|d<|d=|d>d?d@|j                         dAdBdC|j                         dAdDdE|j                         dAdFdG|dAdHdI|dAdJdK|dAg}t=        | dL|      S )MzH
    Custom dashboard view for AI reports across all analysis types
    T)bazi_analysis_reported
created_bybazi_report_resolved_by)number_analysis_reportednumber_report_resolved_by)analysis_reporteduserreport_resolved_bypending)bazi_report_status)number_report_status)report_status   )days)bazi_report_timestamp__gte)number_report_timestamp__gte)report_timestamp__gtez-bazi_report_timestampN   BaZiu   🔮z/admin/bazi/person/z/change/d   z...)	typeiconnamer   categorystatus	timestamp	admin_urlmessagez-number_report_timestampNumberu   🔢z-report_timestampLiuYaou   ☯️2    z/admin/liuyao/liuyao/c                     | d   xs8 t         j                  j                  j                  t         j                        S )Nr2   )tzinfo)r
   datetimeminreplaceutc)xs    */home/cursorai/projects/iching/ai/views.py<lambda>z&ai_reports_dashboard.<locals>.<lambda>^   s1    an&j8I8I8M8M8U8U]e]i]i8U8j&j    )keyreverse
   )inappropriate_contentinaccurate_analysisoffensive_languagetechnical_errorother)bazi_report_category)number_report_category)report_categoryr   titlezAI Analysis Reports Dashboardtotal_reportstotal_pendingtotal_recentbazi_reportsnumber_reportsliuyao_reportspending_bazipending_numberpending_liuyaorecent_bazirecent_numberrecent_liuyaolatest_reportscategory_statsquick_linkszAll BaZi Reportsz3/admin/bazi/person/?bazi_analysis_reported__exact=1)r/   urlcountzAll Number Reportsz5/admin/bazi/person/?number_analysis_reported__exact=1zAll LiuYao Reportsz0/admin/liuyao/liuyao/?analysis_reported__exact=1zPending BaZi Reportsz5/admin/bazi/person/?bazi_report_status__exact=pendingzPending Number Reportsz7/admin/bazi/person/?number_report_status__exact=pendingzPending LiuYao Reportsz2/admin/liuyao/liuyao/?report_status__exact=pendingzadmin/ai/reports_dashboard.html)r   objectsfilterselect_relatedr   r_   r
   nowr   order_byappendr/   r   rK   r"   bazi_report_timestampidbazi_report_messagelenrL   r#   number_report_timestampnumber_report_messagequestionr   rM   r$   report_timestampreport_messagesortr   )requestrR   rS   rT   rO   rU   rV   rW   rP   week_agorX   rY   rZ   rQ   r[   personentryr\   all_categoriesr0   r_   contexts                         r@   ai_reports_dashboardrv      s    >>(((ETTUac|}L^^**D*IXXYe  hC  DN^^**T*BQQRXZnoN !&&(>+?+?+AANDXDXDZZM&&)&DJJLL#**	*JPPRN#***CIIKN >1NBM ||~	q 11H%%%JPPRK"))x)PVVXM")))IOOQM.>L N ''(@A"1EKK%%33//55.vyykBCIC]C]befl  gA  gA  cB  EH  cHv11$37%?  NT  Nh  Nh

 
	 F !))*DEbqIKK%%551177.vyykBEKEaEafijp  kG  kG  gH  KN  gNv33DS9EA  TZ  Tp  Tp

 
	 J  (()<=bqANN3B'C4G"4L5RTUJJ--))//0
(C=B=Q=QVYZ_ZnZnVoruVuu++DS1E9  |A  |P  |P

 
	 B jtxy#CR(N NwN"X>DDF!!!BHHJK!!(!;AACD 	
 19',N8$ #0000 	0 		0
 	**,0 	...00 	...00 	0 	.0 	.0 	{0 	0 	0 	.0 	.0  	*L%++- -N'--/ -I'--/ /N% 1P' 1K'5
!0Gd G%FPPrB   c                   &    e Zd ZdZeZdZdZdZd Z	y)ConversationListViewz,List all conversations for the current user.zai/conversation_list.htmlconversations   c                     t         j                  j                  | j                  j                        j                  d      j                  d      S )z'Get conversations for the current user.r   rr   messages)r   r`   ra   rp   r   rb   prefetch_relatedselfs    r@   get_querysetz!ConversationListView.get_queryset   s>    ##**0A0A*BQQRZ[llmwxxrB   N)
__name__
__module____qualname____doc__r   modeltemplate_namecontext_object_namepaginate_byr    rB   r@   rx   rx      s     6E/M)KyrB   rx   c                   4     e Zd ZdZeZdZdZd Z fdZ	 xZ
S )ConversationDetailViewz,View a single conversation and its messages.zai/conversation_detail.htmlconversationc                     t         j                  j                  | j                  j                        j                  dd      S )z1Only allow users to view their own conversations.r|   rr   r   )r   r`   ra   rp   r   rb   r   s    r@   r   z#ConversationDetailView.get_queryset   s5    ##**0A0A*BQQRZ\bccrB   c                     t        |   di |}| j                  j                  j	                         j                  d      |d<   | j                  j                  |d<   |S )zAdd messages to context.
created_atr}   rr   r   )superget_context_dataobjectr}   allrd   rr   )r   kwargsru   	__class__s      r@   r   z'ConversationDetailView.get_context_data   sV    '*4V4"kk22668AA,O
 KK..rB   )r   r   r   r   r   r   r   r   r   r   __classcell__)r   s   @r@   r   r      s&    6E1M(d rB   r   POSTc                    | j                   j                  st        ddid      S t        t        || j                         }t        j                  | j                        }|j                  dd|j                         }	 t        | j                   ||      }t        d	|j                  d
|j                   dd      S # t        $ r"}t        dt        |      id      cY d}~S d}~ww xY w)z4Create a new conversation for a person's BaZi chart.errorAuthentication required  r1   )pkr   rN   zConversation about )r   rr   rN   Tz/ai/conversations//)successconversation_idredirect_url  N)r   is_authenticatedr   r   r   jsonloadsbodygetr/   r   rg   	Exceptionstr)rp   	person_idrr   datarN   r   es          r@   create_conversation_viewr      s     <<((W&?@MMv)MF::gll#DHHW 3FKK=ABE;*

 +00AC
  	
  ;Wc!f-c::;s   >C   	C+	C& C+&C+c           
         | j                   j                  st        ddid      S t        t        || j                         }t        j                  | j                        }|j                  dd      j                         }|j                  dd	      }|st        dd
id      S 	 t        |||      }t        d|j                  |j                  |j                  |j                  j                         |j                   |j"                  dd      S # t$        $ r"}t        dt'        |      id      cY d}~S d}~ww xY w)z5Send a message in a conversation and get AI response.r   r   r   r   )r   r   r4   r8   languagezzh-hanszMessage cannot be emptyi  )r   user_messager   T)rg   rolecontentr   providerr   )r   r4   r   N)r   r   r   r   r   r   r   r   r   stripr   rg   r   r   r   	isoformatr   r   r   r   )rp   r   r   r   r   r   assistant_messager   s           r@   send_message_viewr      s*    <<((W&?@MM$\\L ::gll#D88Ir*002Lxx
I.HW&?@MM;5%%
 '**)..,44/::DDF-66*00

 
 
	  ;Wc!f-c::;s   %A,D 	D=D82D=8D=)+r   %django.contrib.admin.views.decoratorsr   django.contrib.auth.mixinsr   django.httpr   r   django.template.responser   django.db.modelsr   r	   django.utilsr
   django.views.genericr   r   django.views.decorators.httpr   django.views.decorators.csrfr   django.shortcutsr   r   r;   r   r   bazi.modelsr   liuyao.modelsr   	ai.modelsr   r   ai.utils.conversationr   r   rv   rx   r   r   r   r   rB   r@   <module>r      s    H 9 2 5 % ! 5 = 4 8      + P HQ HQV	y-x 	y/ $ vh;  ;2 vh%;  %;rB   