§
    ¨MºfÞ&  ã                   ó(  — d Z ddlZddl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mZmZ  ej        e¦  «        Zd	„ Z G d
„ dej        ¦  «        Zd„ Zd„ Z G d„ dej        ¦  «        Z G d„ dej        ¦  «        Z G d„ dej        ¦  «        ZdS )ap  
Django ORM models for dash applications

Copyright (c) 2018 Gibbs Consulting and others - see CONTRIBUTIONS.md

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
é    N)Úmodels)Úadmin)Úslugify)Úget_object_or_404é   )Úget_local_stateless_by_nameÚget_local_stateless_listÚwid2strc                 ó    — t          | ¦  «        S )z,Locate stateless app instance given its name)r   )Únames    úX/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/django_plotly_dash/models.pyÚget_stateless_by_namer   &   s   € å& tÑ,Ô,Ð,ó    c                   ó|   ‡ — e Zd ZdZ ej        dddd¬¦  «        Z ej        ddd¬¦  «        Zd„ Z	ˆ fd	„Z
d
„ Zˆ xZS )ÚStatelessAppzj
    A stateless Dash app. An instance of this model represents a dash app without any specific state
    éd   FT)Ú
max_lengthÚblankÚnullÚuniqueén   ©r   r   r   c                 ó   — | j         S ©N©Úapp_name©Úselfs    r   Ú__str__zStatelessApp.__str__2   s
   € ØŒ}Ðr   c                 ó^  •— | j         rt          | j         ¦  «        dk     rut          | j        ¦  «        | _         t          j                             | j         ¬¦  «                             ¦   «         }|dk    r| j         t          |dz   ¦  «        z   | _          t          ¦   «         j
        |i |¤ŽS )Né   )Úslug__startswithr   r   )ÚslugÚlenr   r   r   ÚobjectsÚfilterÚcountÚstrÚsuperÚsave)r   ÚargsÚkwargsÚexist_countÚ	__class__s       €r   r*   zStatelessApp.save5   s•   ø€ ØŒyð 	;�C ¤	™NœN¨QÒ.Ð.Ý ¤Ñ.Ô.ˆDŒIÝ&Ô.×5Ò5ÀtÄyÐ5ÑQÔQ×WÒWÑYÔYˆKØ˜QŠˆØ œI­¨K¸©MÑ(:Ô(:Ñ:�”	Ø�u‰wŒwŒ|˜TÐ, VÐ,Ð,Ð,r   c                 óv   — t          | dd¦  «        }|s%t          | j        ¦  «        }t          | d|¦  «         |S )zF
        Return a DjangoDash instance of the dash application
        Ú_stateless_dash_app_instanceN)Úgetattrr   r   Úsetattr)r   Údateless_dash_apps     r   Úas_dash_appzStatelessApp.as_dash_app=   sJ   € õ $ DÐ*HÈ$ÑOÔOÐØ ð 	MÝ 5°d´mÑ DÔ DÐÝ�DÐ8Ð:KÑLÔLÐLØ Ð r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú	CharFieldr   Ú	SlugFieldr#   r   r*   r4   Ú__classcell__©r.   s   @r   r   r   +   s—   ø€ € € € € ðð ð  ˆvÔ¨3°eÀ%ÐPTÐUÑUÔU€HØˆ6Ô s°4¸tÐDÑDÔD€Dðð ð ð-ð -ð -ð -ð -ð!ð !ð !ð !ð !ð !ð !r   r   c                 óä   — 	 t           j                             | ¬¦  «        }|                     ¦   «         S #  Y nxY wt	          | ¦  «        }t          | ¬¦  «        }|                     ¦   «          |S )zá
    Find stateless app given its name

    First search the Django ORM, and if not found then look the app up in a local registry.
    If the app does not have an ORM entry then a StatelessApp model instance is created.
    r   )r   r%   Úgetr4   r   r*   )r   Údsa_appÚdash_apps      r   Úfind_stateless_by_namerA   G   so   € ðÝÔ&×*Ò*°DÐ*Ñ9Ô9ˆØ×"Ò"Ñ$Ô$Ð$øðØˆøøøå$ TÑ*Ô*€HÝ DÐ)Ñ)Ô)€GØ‡L‚L�N„N€NØ€Os   ‚36 ¶:c                  óª   — t          ¦   «         D ]C} 	 t          | ¦  «         Œ#  t                               dt	          | ¦  «        z   ¦  «         Y ŒAxY wd S )Nz9django-plotly-dash: Unable to create stateless instance: )r	   rA   ÚloggerÚwarningr(   )Úuas    r   Úcheck_stateless_loadedrF   Z   sn   € Ý&Ñ(Ô(ð `ð `ˆð	`Ý" 2Ñ&Ô&Ð&Ð&øð	`Ý�NŠNÐVÕWZÐ[]ÑW^ÔW^Ñ^Ñ_Ô_Ð_Ð_Ð_øøøð	`ð `s	   ’"¢,Ac                   ó<   — e Zd ZdZddgZddgZd„ Zde_        egZdS )ÚStatelessAppAdminz*Admin for StatelessApp ORM model instancesr   r#   c                 óÀ   — |                      ¦   «         D ]H}	 |                     ¦   «         }Œ#  t                               dt	          |¦  «        z   ¦  «         Y ŒFxY wd S )Nz2django-plotly-dash: Unable to load stateless app: )Úallr4   rC   rD   r(   )Ú
modeladminÚrequestÚquerysetÚsaÚqs        r   Úcheck_registeredz"StatelessAppAdmin.check_registeredg   sp   € à—,’,‘.”.ð 	]ð 	]ˆBð]Ø—N’NÑ$Ô$��øð]Ý—’ÐSÕTWÐXZÑT[ÔT[Ñ[Ñ\Ô\Ð\Ð\Ð\øøøð		]ð 	]s	   ˜-­,AzCheck stateless appsN)	r5   r6   r7   r8   Úlist_displayÚlist_filterrP   Úshort_descriptionÚactions© r   r   rH   rH   b   sN   € € € € € Ø0Ð0Ø Ð(€LØ˜vÐ'€Kð]ð ]ð ]ð *@ÐÔ&àÐ!€G€G€Gr   rH   c                   óz  ‡ — e Zd ZdZ ej        eej        ddd¬¦  «        Z ej	        dddd¬¦  «        Z
 ej        ddd¬¦  «        Z ej        dd	¬
¦  «        Z ej        d¬¦  «        Z ej        d¬¦  «        Z ej        dd¬
¦  «        Zd„ Zˆ fd„Zd„ Zd„ Zd„ Zd„ Zdd„Zd„ Zd„ Zedd„¦   «         Zˆ xZS )ÚDashAppzX
    An instance of this model represents a dash application and its internal state
    F)Ú	on_deleter   r   r   r   T)r   r   r   r   r   r   z{})r   Údefault)Úauto_now_add)Úauto_nowc                 ó   — | j         S r   )Úinstance_namer   s    r   r   zDashApp.__str__†   s   € ØÔ!Ð!r   c                 óJ  •— | j         sIt          j                             ¦   «                              ¦   «         }d| j        j        |dz   fz  | _         | j        rt          | j        ¦  «        dk     rt          | j         ¦  «        | _         t          ¦   «         j        |i |¤Ž d S )Nz%s-%ir   r!   )r]   rW   r%   rJ   r'   Ústateless_appr   r#   r$   r   r)   r*   )r   r+   r,   Úexisting_countr.   s       €r   r*   zDashApp.save‰   s›   ø€ ØÔ!ð 	ZÝ$œ_×0Ò0Ñ2Ô2×8Ò8Ñ:Ô:ˆNØ!(¨4Ô+=Ô+FÈÐWXÑHXÐ*YÑ!YˆDÔØŒyð 	4�C ¤	™NœN¨QÒ.Ð.Ý Ô 2Ñ3Ô3ˆDŒIØ�‰ŒŒ�dÐ%˜fÐ%Ð%Ð%Ð%Ð%r   c                 óÔ   — t          | dd¦  «        rR| j        rMt          j        t          | di ¦  «        ¦  «        }|| j        k    r!|| _        |                      ¦   «          dS dS dS dS )zÂ
        Check to see if the current hydrated state and the saved state are different.

        If they are, then persist the current state in the database by saving the model instance.
        Ú_current_state_hydrated_changedFÚ_current_state_hydratedN)r1   Úsave_on_changeÚjsonÚdumpsÚ
base_stater*   )r   Únew_base_states     r   Úhandle_current_statezDashApp.handle_current_state‘   s   € õ �4Ð:¸EÑBÔBð 	ÀtÔGZð 	Ý!œZ­°Ð6OÐQSÑ(TÔ(TÑUÔUˆNØ ¤Ò0Ð0Ø"0�”Ø—	’	‘”���ð		ð 	ð 	ð 	à0Ð0r   c                 óx   — |                       ¦   «         }|                     t          |¦  «        i ¦  «        }||v S )z;Return True if there is a cached current state for this app)Úcurrent_stater>   r
   )r   ÚwidÚkeyÚcscollÚc_states        r   Úhave_current_state_entryz DashApp.have_current_state_entry�   s6   € à×#Ò#Ñ%Ô%ˆØ—*’*�W S™\œ\¨2Ñ.Ô.ˆØ�gˆ~Ðr   c                 óè   — |                       ¦   «         }|                     t          |¦  «        i ¦  «        }||v r4|                     |d¦  «        }||k    r|||<   t          | dd¦  «         dS dS dS )zŸ
        Update current state with a (possibly new) value associated with key

        If the key does not represent an existing entry, then ignore it
        Nrb   T)rk   r>   r
   r2   )r   rl   rm   Úvaluern   ro   Úcurrent_values          r   Úupdate_current_statezDashApp.update_current_state£   s„   € ð ×#Ò#Ñ%Ô%ˆØ—*’*�W S™\œ\¨2Ñ.Ô.ˆØ�'ˆ>ˆ>Ø#ŸKšK¨¨TÑ2Ô2ˆMØ Ò%Ð%Ø$�˜‘Ý˜Ð?ÀÑFÔFÐFÐFÐFð	 ˆ>à%Ð%r   c                 ó¢   — t          | dd¦  «        }|s;t          j        | j        ¦  «        }t	          | d|¦  «         t	          | dd¦  «         |S )z»
        Return the current internal state of the model instance.

        This is not necessarily the same as the persisted state
        stored in the self.base_state variable.
        rc   Nrb   F)r1   re   Úloadsrg   r2   )r   ro   s     r   rk   zDashApp.current_state±   s[   € õ ˜$Ð 9¸4Ñ@Ô@ˆØð 	DÝ”j ¤Ñ1Ô1ˆGÝ�DÐ3°WÑ=Ô=Ð=Ý�DÐ;¸UÑCÔCÐCØˆr   Nc                 ó–   — | j                              ¦   «         }|                      ¦   «         }|                     || j        |¬¦  «        S )z:Return a dash application instance for this model instance)ÚreplacementsÚspecific_identifierÚcache_id)r_   r4   rk   Údo_form_dash_instancer#   )r   rz   r@   Úbases       r   Úas_dash_instancezDashApp.as_dash_instance¿   sQ   € àÔ%×1Ò1Ñ3Ô3ˆØ×!Ò!Ñ#Ô#ˆØ×-Ò-¸4ØBFÄ)Ø7?ð .ñ Aô Að 	Ar   c                 ó  — | j                              ¦   «                              ¦   «         } |                     d¦  «        ¦   «         }t	          j        |j                             d¦  «        ¦  «        }i }|                     ||¦  «         |S )zg
        Get the base state of the object, as defined by the app.layout code, as a python dict
        zdash-layoutzutf-8)	r_   r4   r}   Úlocate_endpoint_functionre   rv   ÚdataÚdecodeÚwalk_tree_and_extract)r   Úbase_app_instÚ	base_respÚbase_objÚobjs        r   Ú_get_base_statezDashApp._get_base_stateÇ   sƒ   € ð Ô*×6Ò6Ñ8Ô8×IÒIÑKÔKˆð J�M×:Ò:¸=ÑIÔIÑKÔKˆ	å”:˜iœn×3Ò3°GÑ<Ô<Ñ=Ô=ˆð ˆØ×+Ò+¨H°cÑ:Ô:Ð:Øˆ
r   c                 ó`   — |                       ¦   «         }t          j        |¦  «        | _        dS )zJ
        Add values from the underlying dash layout configuration
        N)r‡   re   rf   rg   )r   r†   s     r   Úpopulate_valueszDashApp.populate_values×   s(   € ð ×"Ò"Ñ$Ô$ˆÝœ* S™/œ/ˆŒˆˆr   c                 ó†   — |rt          | ¦  «        }nt          t          | ¬¦  «        }|                     |¬¦  «        }||fS )zgLocate a dash application, given either the
        slug of an instance or the name for a stateless app)r#   )rz   )rA   r   rW   r}   )ÚidentÚ	statelessrz   r@   Úapps        r   Úlocate_itemzDashApp.locate_itemÞ   sM   € ð ð 	>Ý-¨eÑ4Ô4ˆHˆHå(­°uÐ=Ñ=Ô=ˆHà×'Ò'°Ð'Ñ:Ô:ˆØ˜ˆ}Ðr   r   )FN) r5   r6   r7   r8   r   Ú
ForeignKeyr   ÚPROTECTr_   r9   r]   r:   r#   Ú	TextFieldrg   ÚDateTimeFieldÚcreationÚupdateÚBooleanFieldrd   r   r*   ri   rp   rt   rk   r}   r‡   r‰   ÚstaticmethodrŽ   r;   r<   s   @r   rW   rW   u   s¦  ø€ € € € € ðð ð &�FÔ% l¸f¼nØ-2¸ÀeðMñ Mô M€Mà$�FÔ$°¸DØ+/°eð=ñ =ô =€Màˆ6Ô s°4¸tÐDÑDÔD€Dð "�Ô! u°dÐ;Ñ;Ô;€Jà#ˆvÔ#°Ð6Ñ6Ô6€HØ!ˆVÔ!¨4Ð0Ñ0Ô0€FØ(�VÔ(¨e¸UÐCÑCÔC€Nð"ð "ð "ð&ð &ð &ð &ð &ð
ð 
ð 
ðð ð ðGð Gð Gðð ð ðAð Að Að Aðð ð ð *ð *ð *ð ð	ð 	ð 	ñ „\ð	ð 	ð 	ð 	ð 	r   rW   c                   óR   — e Zd ZdZg d¢Zg d¢Zd„ Zde_        d„ Zde_        dd	gZ	d
S )ÚDashAppAdminzAdmin class for DashApp model)r]   r_   r#   r“   r”   rd   )r“   r”   rd   r_   c                 ó`   — |D ]*}|                      ¦   «          |                     ¦   «          Œ+d S r   )r‰   r*   )r   rL   rM   r@   s       r   Ú_populate_valueszDashAppAdmin._populate_valuesð   s<   € Ø ð 	ð 	ˆHØ×$Ò$Ñ&Ô&Ð&Ø�MŠM‰OŒOˆOˆOð	ð 	r   zPopulate app instancec                 óz   — |D ]7}t          |j        |j        |j        ¬¦  «        }|                     ¦   «          Œ8d S )N)r_   rg   rd   )rW   r_   rg   rd   r*   )r   rL   rM   r@   Úndas        r   Ú_clonezDashAppAdmin._cloneö   sT   € Ø ð 	ð 	ˆHÝ¨Ô(>Ø%-Ô%8Ø)1Ô)@ðBñ Bô BˆCð �HŠH‰JŒJˆJˆJð		ð 	r   zClone app instancerš   r�   N)
r5   r6   r7   r8   rQ   rR   rš   rS   r�   rT   rU   r   r   r˜   r˜   ê   sn   € € € € € Ø#Ð#ð=ð =ð =€LàLÐLÐL€Kðð ð ð *AÐÔ&ðð ð ð  4€FÔà! 8Ð-€G€G€Gr   r˜   )r8   re   ÚloggingÚ	django.dbr   Údjango.contribr   Údjango.utils.textr   Údjango.shortcutsr   Údash_wrapperr   r	   r
   Ú	getLoggerr5   rC   r   ÚModelr   rA   rF   Ú
ModelAdminrH   rW   r˜   rU   r   r   ú<module>r§      s�  ððð ð0 €€€Ø €€€à Ð Ð Ð Ð Ð Ø  Ð  Ð  Ð  Ð  Ð  Ø %Ð %Ð %Ð %Ð %Ð %Ø .Ð .Ð .Ð .Ð .Ð .à XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ Xà	ˆÔ	˜8Ñ	$Ô	$€ð-ð -ð -ð
!ð !ð !ð !ð !�6”<ñ !ô !ð !ð8ð ð ð&`ð `ð `ð"ð "ð "ð "ð "˜Ô(ñ "ô "ð "ð&sð sð sð sð sˆfŒlñ sô sð sðj.ð .ð .ð .ð .�5Ô#ñ .ô .ð .ð .ð .r   