
    Mf                         d Z ddl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 n
#  d ZY nxY wd	d
lmZmZ d	dlmZmZ d ZddZddZddZddZddZddZd dZd dZd ZddlmZ d!dZd"dZd Z dS )#a  
Views for serving up dash applications and their constituents

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)JSONDecodeError)HttpResponseHttpResponseRedirect)redirect)PreventUpdate)check_fingerprintc                 
    | d fS )N )resources    W/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/django_plotly_dash/views.pyr   r   '   s    ~       )DashAppcheck_stateless_loaded)get_initial_argumentsstatic_pathc                      t           )zReturn routes)NotImplementedError)argskwargss     r   routesr   .   s    
r   Fc                    t          j        ||          \  }}|                                5  |                    d          } |            }t	          |j        |j                  cddd           S # 1 swxY w Y   dS )zReturn the dependencieszdash-dependenciescontent_typeN)r   locate_itemapp_contextlocate_endpoint_functionr   datamimetype)requestident	statelessr   _app	view_funcresps           r   dependenciesr'   2   s     	22FAs			 8 8001DEE	y{{DI)-8 8 88 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8s   :A44A8;A8c                     t          j        ||          \  }}|                    d          } |            }t          | |          }	|                    ||	          \  }
}t          |
|          S )z)Return the layout of the dash applicationzdash-layoutr   )r   r   r   r   augment_initial_layoutr   )r    r!   r"   cache_idr   r#   r$   r%   r&   initial_argumentsresponse_datar   s               r   layoutr-   <   s}     	22FAs,,];;I9;;D-gx@@!88?PQQM8%-/ / / /r   c                 b    	 t          | ||fi |S # t          $ r t          d          cY S w xY w)N   status)_updater   r   )r    r!   r"   r   s       r   updater3   I   sS    (wy;;F;;; ( ( (3''''''(s    ..c                 H   t          j        ||          \  }}	 t          j        | j                            d                    }n'# t          t          f$ r t          d          cY S w xY w| j	        
                    dt                                }||| j        | |d}|                    ||          }	|| j	        d<   |                                 t          |	          dk    rt          d          S 	 |	j        }
|	j        }n#  |	}
d}Y nxY wt          |
|	          S )
zGenerate update json responsezutf-8   r0   django_plotly_dash)dash_app_iddash_appuserr    session_stateEDGECASEEXIT zapplication/jsonr   )r   r   jsonloadsbodydecoder   UnicodeDecodeErrorr   sessiongetdictr9   dispatch_with_argshandle_current_statestrr   r   )r    r!   r"   r   r8   r$   request_body	app_statearg_mapr&   rdatartypes               r   r2   r2   O   sP   'y99MHc(z',"5"5g">">??/0 ( ( (3''''''( ##$8$&&AAI##|  )	+ +G
 !!,88D,5GO()!!### 4yyN""B#	#"%*, , , ,s   ,A !A+*A+9D Dc                     t          j        |||          \  }}|                                } |            }t          |          S )zMain view for a dash app)r*   )r   r   r   r   )	r    r!   r"   r*   r   r#   r$   r%   r&   s	            r   	main_viewrN   r   sG     	HEEEFAs,,..I9;;Dr   c                 $    t          | f||dd|S )FReturn part of a client-side component, served locally for some reasonz_components/)r   	componentextra_elementcomponent_suites)r    r   rQ   r   s       r   component_component_suitesrU   z   s3    G &%-&/*8& & %	& & &r   r<   c                 ,    t          | f|||| dd|S )rP   z/build)r   rQ   rR   cpe2rS   )r    r   rQ   rR   rW   r   s         r   component_suites_buildrX      s<    G &%-&/*7$(	& &
 %& & &r   c                     |r| dnd}| j                                         }|r	 t          |          \  }}	t          d|d|||          }t	          |          S )rP   /r<   Fzdash/component/?redirect_to)GET	urlencoder   r   r   )
r    r   rQ   rR   rW   r   extra_path_part
get_params
redone_url_fingerprints
             r   rT   rT      s     %)0jjjjbO&&((J u u "38!<!<, [yyy//[h[hjrjr!stt
J7777r   c                     | j                                         }d}|rt          d|d|          }nt          d|z            }t          |          S )zLReturn a local dash app asset, served up through the Django static frameworkr<   zdash/assets/r[   zdash/assets/%sr\   )r^   r_   r   r   )r    r   ra   
extra_partrb   s        r   
app_assetsrf      sc    &&((JJ @ [zzz::!NOO

 !1J!>??
J7777r   )TemplateResponse
index.htmlc                     | j                             dt                                }|                    dd          }|dz   |d<   || j         d<   t          | |i           S )zTAdd some info to a session in a place that django-plotly-dash can pass to a callbackr6   add_counterr   r   )rB   rC   rD   rg   )r    template_namer   r6   session_add_counts        r   add_to_sessionrm      sf     !,,-A466JJ*..}a@@(9A(=}%,>GO()G]B777r   c                 z    t          j        ||          \  }}|                    |          }t          |          S )z&Redirect static assets for a component)r   r   get_asset_static_urlr   )r    pathr!   r"   r   Xr$   r   s           r   asset_redirectionrr      s=      	22FAs ((..KK   r   c                 <    t                       t          d          S )zKCheck all registered stateless apps and create ORM entries that are missingz0admin:django_plotly_dash_statelessapp_changelist)r   r   )r    r   s     r   add_stateless_appsrt      s    FGGGr   )F)FN)NN)NNr<   N)rh   )NF)!__doc__r=   r   django.httpr   r   django.shortcutsr   dash.exceptionsr   dash.fingerprintr   modelsr   r   utilr   r   r   r'   r-   r3   r2   rN   rU   rX   rT   rf   django.template.responserg   rm   rr   rt   r
   r   r   <module>r}      s   4              : : : : : : : : % % % % % % ) ) ) ) ) )2222222     4 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4  8 8 8 8/ / / /( ( ( (!, !, !, !,F   & & & && & & &8 8 8 8	8 	8 	8 6 5 5 5 5 5	8 	8 	8 	8! ! ! !H H H H Hs   ) 0