
    Mf&                     (   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uniquen   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	NNQ....DI&.55ty5QQWWYYKQ IKM(:(::	uww|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 5dm 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3e%PTUUUH6s4tDDDD  - - - - -! ! ! ! ! ! !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LLNNN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&&&&	`NNVWZ[]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]NN$$]STWXZT[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] ] ] *@&!GGGr   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WXHX*Y!YDy 	4C	NNQ.. 233DI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 	tGZ 	!Z6OQS(T(TUUN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    ##%%**WS\\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     ##%%**WS\\2..'>>#KKT22M%%$?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[     $ 94@@ 	Dj11GD3W===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!!##--4BF)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	:in33G<<== ++Hc:::
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4HH(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%lfn-2eM M MM$F$D+/e= = =M6s4tDDDD "!ud;;;J#v#666H!V!4000F(V(eUCCCN" " "& & & & &
 
 
  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$$&&&MMOOOO	 	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 HHJJJJ		 	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LK   *A&    4F!8-GGGr   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fl s s sj. . . . .5# . . . . .r   