
    P1i6                         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m	Z	 d dl
mZ d dlZdd	lmZ d
 Zde_         ej"                  e       G d dej$                               Zy)    )admin)format_html)	mark_safe)messages)pathreverse)HttpResponseRedirectN   )liuyaoc                     |D ]  }	 ddl m}  ||        | j                  |d|j                          d       y# t        $ r5}| j                  |d|j                   dt        |              Y d}~rd}~ww xY w)z@Admin action to generate AI analysis for selected LiuYao entriesr   )analyze_liuyaozError generating analysis for : NzAnalysis requested for z LiuYao entries)ai.utils.liuyao_analysisr   	Exceptionmessage_userquestionstrcount)
modeladminrequestquerysetentryr   es         ./home/cursorai/projects/iching/liuyao/admin.pygenerate_ai_analysisr      s    	j?5!	  G'>x~~?O>PP_%`a  	j##G/MennM]]_`cde`f_g-hii	js   <	A:+A55A:zGenerate AI analysisc                       e Zd ZdZdZdZdZedgZdddifd	dd
ifdddifddddfdddiffZ	d Z
de
_        de
_        d Zde_        d Zde_        d Zde_         fdZd Zd Zde_         fd Zd! Zd"e_         xZS )#LiuYaoAdmin)r   user_displayqdate
created_atanalysis_status_displayreport_status_display)r    r   analysis_statusreport_statusanalysis_reported)r   user__first_nameuser__last_nameuser__phonereport_message)	r    last_modified_atuuidanalysis_timestampreport_timestampreport_resolved_byreport_resolved_atai_analysis_displayreset_ai_analysis_buttonresolve_reportszBasic Informationfields)r   userr   r+   zHexagram Data)y1y2y3y4y5y6datazAnalysis & Reading)readingfeedbackr#   r,   r0   r1   Reports)r%   report_categoryr)   r-   r$   report_admin_notesr/   )collapse)r3   classesSystem)r    r*   c                     |j                   sy|j                   j                   d|j                   j                   j                         }|sd}t	        d||j                   j
                        S )z(Display the user's name and phone numberz
Guest User zNo Namez=<strong>{}</strong><br><small style="color: #666;">{}</small>)r4   
first_name	last_namestripr   phone)selfobj	full_names      r   r   zLiuYaoAdmin.user_display3   sc    xx xx**+1SXX-?-?,@AGGI	!IKHHNN
 	
    Userr&   c                     |j                   dk(  rt        d      S |j                   dk(  rt        d      S |j                   dk(  rt        d      S y)z&Format the analysis status with colors	completedu0   <span style="color: green;">✓ Completed</span>pendingu/   <span style="color: orange;">⟳ Pending</span>erroru*   <span style="color: red;">✗ Error</span>zNot Analyzed)r#   r   )rJ   rK   s     r   r!   z#LiuYaoAdmin.analysis_status_displayE   sS    +-QRR  I-PQQ  G+KLL!rM   zAnalysis Statusc                     |j                   st        d      S |j                  }|dk(  rt        d      S |dk(  rt        d      S |dk(  rt        d      S |dk(  rt        d	      S t        d
      S )z'Display report status with color codingu+   <span style="color: #666;">无举报</span>rQ   uD   <span style="color: orange; font-weight: bold;">⏳ 待处理</span>revieweduF   <span style="color: blue; font-weight: bold;">👁️ 已审核</span>resolveduC   <span style="color: green; font-weight: bold;">✅ 已解决</span>	dismisseduB   <span style="color: gray; font-weight: bold;">❌ 已忽略</span>u/   <span style="color: red;">🚩 已举报</span>)r%   r   r$   )rJ   rK   statuss      r   r"   z!LiuYaoAdmin.report_status_displayQ   s~    $$LMM""Yeffz!ghhz!dee{"cddPQQrM   u   举报状态c                    |j                   sy	 |j                   }|j                  dd      xs& |j                  dd      xs |j                  dd      }|j                  dd      }|j                  dd      }|j                  d	d      }|j                  d
d      }|j                  r|j                  j                  d      nd}g }	|r.t	        |      dkD  r	|dd dz   }
n|}
|	j                  d|
 d       |r-t	        |      dkD  r|dd dz   n|}|	j                  d| d       d| d| d| d}t        ddj                  |	       | d      S # t        $ r}t        dt        |            cY d}~S d}~ww xY w)z(Format the AI analysis in a readable wayzNo analysis availableliuyao_analysis analysisresponseproviderUnknownmodelpromptthinkz%Y-%m-%d %H:%M:%Si  Nz...u4  
                    <div style="margin-bottom: 15px; padding: 10px; border-left: 3px solid #007cba; background-color: #f8f9fa;">
                        <h4 style="margin: 0 0 8px 0; color: #007cba; font-size: 14px;">六爻分析</h4>
                        <div style="font-size: 12px; line-height: 1.4;">z2</div>
                    </div>
                i,  u4  
                    <div style="margin-bottom: 15px; padding: 10px; border-left: 3px solid #6f42c1; background-color: #f8f9fa;">
                        <h4 style="margin: 0 0 8px 0; color: #6f42c1; font-size: 14px;">分析思路</h4>
                        <div style="font-size: 12px; line-height: 1.4;">z
                <div style="background-color: #f8f9fa; padding: 10px; margin-top: 10px; border-radius: 4px; border-left: 3px solid #007cba;">
                    <small style="color: #666;">
                        <strong>Provider:</strong> z5<br>
                        <strong>Model:</strong> z9<br>
                        <strong>Generated:</strong> zA
                    </small>
                </div>
            z<div style="max-width: 600px;">z</div>z><span style="color: red;">Error displaying analysis: {}</span>)ai_analysisgetr,   strftimelenappendr   joinr   r   r   )rJ   rK   analysis_dataanalysis_contentr]   r_   r`   thinking	timestamphtml_sectionsdisplay_contentthinking_displaymetadata_htmlr   s                 r   r0   zLiuYaoAdmin.ai_analysis_displayc   s   *8	iOOM !!"3R8 2!!*b12!!*b1 
 %((Y?H!%%gy9E"&&x4F$(("5HPSPfPf..778KLluIM  '(3.&6t&<u&DO&6O$$ *I JYHY Z&  =@]S=P8DS>E#9V^ $$ *I JZHZ [& !4 5=: >116 855>K @M >rww}?U>VWdVeeklmm 	i_adefaghh	is   EE 	E;E60E;6E;zAI Analysisc                     t         |          }t        d| j                  j	                  | j
                        d      g}||z   S )z,Add custom URLs for the reset functionality.z"<int:liuyao_id>/reset-ai-analysis/liuyao_reset_ai_analysis)name)superget_urlsr   
admin_site
admin_viewreset_ai_analysis_view)rJ   urlscustom_urls	__class__s      r   rt   zLiuYaoAdmin.get_urls   sJ    w!4**4+F+FG/
 T!!rM   c                 ~   	 t         j                  j                  |      }d|_        d|_	        d|_
        |j                  g d       t	        j                  |d       t        t        d	|g
            S # t         j                  $ r1 t	        j
                  |d| d       t        t        d            cY S w xY w)zDCustom view to handle AI analysis reset for individual liuyao entry.)idzLiuYao entry with ID z does not exist.zadmin:liuyao_liuyao_changelistN)rb   r,   r#   update_fieldszHAI analysis reset for LiuYao entry. You can now regenerate the analysis.zadmin:liuyao_liuyao_changeargs)r   objectsrc   DoesNotExistr   rR   r	   r   rb   r,   r#   savesuccess)rJ   r   	liuyao_idliuyao_entrys       r   rw   z"LiuYaoAdmin.reset_ai_analysis_view   s    	S!>>---;L $( *.''+$(`a$ln $G,HPY{$[\\ "" 	SNN7&;I;FV$WX'0P(QRR	Ss    A8 8AB<;B<c                     |j                   sy|j                  st        d      S t        d|j                   g      }t        d|      S )zTDisplay a reset button for AI analysis on the individual liuyao entry's change page.z8Save the LiuYao entry first to enable AI analysis reset.zM<span style="color: #666; font-style: italic;">No AI analysis to reset</span>zadmin:liuyao_reset_ai_analysisr   u~  <a href="{}" class="button" style="background-color: #dc3545; color: white; padding: 8px 12px; text-decoration: none; border-radius: 4px; font-size: 13px; display: inline-block; margin-top: 5px;" onclick="return confirm('Are you sure you want to reset the AI analysis for this LiuYao entry? This will clear all analysis data and allow regeneration.');">🗑️ Reset AI Analysis</a>)pkrb   r   r   )rJ   rK   	reset_urls      r   r1   z$LiuYaoAdmin.reset_ai_analysis_button   sO    vvM_  <CFF8L	, 
 	
rM   zReset AI Analysisc                 `   ddl m} |rcd|j                  v rU|j                  j	                  dd      }|j
                  j	                  dd      }|r|sd|_        | j                  |d       |rd|j                  v r|j                  j	                  dd      }|j
                  j	                  dd      }	|	dv r&|j                  |_	        |j                         |_        |	d	k(  r|j                  sy	 dd
lm}
 |j                  rf|j                  j                  rP |
|j                  |d|	|j                         d|_        | j                  |d|j                  j                          t$        | M  ||||       y# t         $ r(}| j                  |dt#        |              Y d}~?d}~ww xY w)zJOverride save_model to send email notifications when report status changesr   timezoner%   Fz7LiuYao report reset - email flag cleared for new reportr$   rZ   )rU   rV   rU   !send_user_resolution_notificationr   TzEmail notification sent to z(Report status updated but email failed: N)django.utilsr   changed_datainitialrc   cleaned_datareport_resolution_email_sentr   r4   r.   nowr/   	api.utilsr   emailr@   r   r   rs   
save_model)rJ   r   rK   formchanger   old_reportednew_reported
old_status
new_statusr   r   rz   s               r   r   zLiuYaoAdmin.save_model   s   ) )T->->><<++,?GL,,001DeLL L380!!'+de o):)::))/2>J**..CJ 66)0&)1& Z'0P0P	dKxxCHHNN9HHc8ZAWAW <@8))'5PQTQYQYQ_Q_P`3ab
 	7Cv6	 ! d%%g1YZ]^_Z`Ya/bccds   1A8E< <	F-F((F-c                 6   ddl m} d}|j                  dd      D ]  }d|_        |j                  |_        |j                         |_        |j                  sP	 ddl	m
} |j                  r=|j                  j                  r' ||j                  |dd|j                         d|_        |j#                  g d       |dz  } | j                  |d| d       y# t        $ r5}| j                  |d	|j                   d
t!        |              Y d}~kd}~ww xY w)z(Admin action to mark reports as resolvedr   r   TrQ   )r%   r$   rU   r   r   z%Report resolved but email failed for r   N)r$   r.   r/   r   r}   r
   u
   已解决 u    个六爻分析举报)r   r   filterr$   r4   r.   r   r/   r   r   r   r   r@   r   r   r   r   r   )rJ   r   r   r   updatedr   r   r   s           r   r2   zLiuYaoAdmin.resolve_reports   s   )__t9_UE",E'.||E$'/||~E$ 55sKzzejj&6&69!JJxUE]E] >B: JJ &J 
 qLG- V0 	'Zy8N#OP ! s%%g1VW\WeWeVffhilmniohp/qrrss   AC	D#+DDu   解决选中的举报)__name__
__module____qualname__list_displaylist_filtersearch_fieldsreadonly_fieldsr   actions	fieldsetsr   short_descriptionadmin_order_fieldr!   r"   r0   rt   rw   r1   r   r2   __classcell__)rz   s   @r   r   r      s   zLbKhM JO#%67G 
;
 	 
B
 	 
  J 
 	 
 b$
 	 
8
 	I&
 &,L"%7L"	" 1B-R  /=+=i~ -:)
"]&
$ 2E.'7RQ: )@O%%rM   r   )django.contribr   django.utils.htmlr   django.utils.safestringr   r   django.urlsr   r   django.httpr	   jsonmodelsr   r   r   register
ModelAdminr    rM   r   <module>r      sa      ) - # % ,  	b *@  & D@%"" D@ D@rM   