a
    8XhH                     @   s  d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZ ddlmZmZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZ ddlmZ dZdZdZg dZ ej!dej"dej#dej$dej%dej&dej'diZ(dd Z)G dd dZ*d"ddZ+dd Z,d d! Z-dS )#z
modelviz.py - DOT file generator for Django Models

Based on:
  Django model to DOT (Graphviz) converter
  by Antonio Cavedoni <antonio@cavedoni.org>
  Adapted to be used with django-extensions
    N)apps)deletion)
ForeignKeyManyToManyFieldOneToOneFieldRelatedField)OneToOneRelManyToOneRel)GenericRelation)ContextTemplateloader)	force_str)	mark_safe)activatez1.1Python)z'Bas van Oostveen <v.oostveen@gmail.com>)zQAntonio Cavedoni <https://cavedoni.com/>Stefano J. Attardi <https://attardi.org/>ZlimodouzCarlo C8E Mironz Andre Campos <cahenan@gmail.com>z#Justin Findlay <jfindlay@gmail.com>z&Alexander Houben <alexander@houben.ch>z Joern Hees <gitdev@joernhees.de>z%Kevin Cherepski <cherepski@gmail.com>z&Jose Tomas Tocino <theom3ga@gmail.com>z#Adam Dobrawy <naczelnik@jawnosc.tk>z6Mikkel Munch Mortensen <https://www.detfalskested.dk/>z+Andrzej Bistram <andrzej.bistram@gmail.com>z%Daniel Lipsitt <danlipsitt@gmail.com>z5Tobias Mitterdorfer <tobias.mitterdorfer97@gmail.com>ZredZblueZorangeZgreenZyellowZgreyZpurplec                 C   sZ   | sg S t | tttfr| S d| vrFtj| rFdd t|  D S dd | 	dD S )N,c                 S   s   g | ]}|  qS  strip.0er   r   j/var/www/viveiro_mudafortebrasil/venv/lib/python3.9/site-packages/django_extensions/management/modelviz.py
<listcomp>E       z&parse_file_or_list.<locals>.<listcomp>c                 S   s   g | ]}|  qS r   r   r   r   r   r   r   F   r   )

isinstancelisttuplesetospathisfileopen	readlinessplit)argr   r   r   parse_file_or_list?   s    r'   c                   @   s   e Zd Zdd Zdd Zd0ddZdd	 Zd1d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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S )2
ModelGraphc                 K   sF  g | _ |dd | _|dd| _|dd| _t|dd| _|dd| _|dd| _|d	d| _	|d
d| _
|dd| _|dd| _|dd| _|dd | _| jd urt| j t|dd| _t|dd| _|dd| _|d| _|dd| _| jr0dd t D | _n|| _|d| _d S )Ncli_optionsdisable_fieldsFdisable_abstract_fieldsinclude_models all_applicationsZgroup_modelsverbose_namesinheritanceTrelations_as_fieldsrelation_fields_onlysort_fieldslanguageexclude_columnsexclude_modelshide_edge_labelsarrow_shapecolor_code_deletionsc                 S   s   g | ]
}|j qS r   )label)r   appr   r   r   r   f   r   z'ModelGraph.__init__.<locals>.<listcomp>rankdir)graphsgetr)   r*   r+   r'   r,   r.   use_subgraphr/   r0   r1   r2   r3   r4   activate_languager5   r6   r7   r8   r9   r   Zget_app_configs
app_labelsr<   )selfrA   kwargsr   r   r   __init__J   s<    




zModelGraph.__init__c                 C   sx   |    g }| jD ]}|dd |d D  q| jD ]<}|d D ].}|d D ] }|d urN|d |v rNd|d< qNqBq6d S )Nc                 S   s   g | ]}|d  qS )namer   r   r   r   r   r   p   r   z2ModelGraph.generate_graph_data.<locals>.<listcomp>models	relationstargetF
needs_node)process_appsr=   extend)rB   nodesgraphmodelrelationr   r   r   generate_graph_datak   s    

zModelGraph.generate_graph_dataFc                 C   s   t j  }|d| j| j| j| j| jd}|rdd | jD }|D ]4}|d D ]&}|	d |d D ]}|	d qfqPqD||d	< n
| j|d	< |S )
Nz%Y-%m-%d %H:%M)Z
created_atr)   r*   r+   r?   r<   c                 S   s   g | ]}|  qS r   )flatten)r   contextr   r   r   r      r   z-ModelGraph.get_graph_data.<locals>.<listcomp>rF   rN   fieldsfieldr=   )
datetimenowstrftimer)   r*   r+   r?   r<   r=   pop)rB   Zas_jsonrV   
graph_datar=   rR   Z
model_dataZ
field_datar   r   r   get_graph_datay   s$    
	


zModelGraph.get_graph_datac              	      s   | j r( jr(t j}| r.| }n j}t j}t t	t
frX|d jj7 }  j|| jt fdd|D t t jdS )Nz ({0})c                 3   s   | ]} j |j kV  qd S N)Zcreation_counter)r   Zabstract_fieldrT   r   r   	<genexpr>   s   z,ModelGraph.add_attributes.<locals>.<genexpr>)rT   rE   r:   typeblankabstractrO   primary_key)r/   verbose_namer   islower
capitalizerE   r^   __name__r   r   r   formatremote_field
field_namer_   anyr   ra   )rB   rT   abstract_fieldsr:   tr   r\   r   add_attributes   s&    


zModelGraph.add_attributesr-   Nc                 C   s2  | j r(|jr(t|j}| r.| }n|j}t|drn| }| j r^| r^|dd }d	|t|}| j
rxd}t|jjtr|jjdkr|j}qd|jjv r|jjdd\}}	n|jjj}|jj}	t||	}n|jj}|rd		|dd
 |}| ||||}
|
|d vr.| |
d r.|
S d S )Nrelated_query_name_ z{} ({})r-   rB   .   z[{}, color={}]rG   rH   )r/   rb   r   rc   rd   rE   hasattrrm   replacerf   r7   r   rg   rN   strr%   _meta	app_labelr   Z	get_modelget_relation_context	use_model)rB   rT   rN   extrascolorr:   rm   target_modelrw   
model_name_relr   r   r   add_relation   s4    



zModelGraph.add_relationc                 C   s2   g }|D ]}|dd |j D 7 }qtt|}|S )Nc                 S   s"   g | ]}t |d r|jjr|qS rv   )rs   rv   r`   r   Zabstract_modelr   r   r   r      s   z2ModelGraph.get_abstract_models.<locals>.<listcomp>)	__bases__r   r   )rB   	appmodelsabstract_modelsappmodelr   r   r   get_abstract_models   s    
zModelGraph.get_abstract_modelsc                 C   s,   t d|j d|j d|jdd g dS )Nz"%s"z%sz
cluster_%srp   rn   )rE   app_nameZcluster_app_namerF   )r   rE   rt   )rB   r;   r   r   r   get_app_context   s    zModelGraph.get_app_contextc                 C   s0   | j rdd |jjD }ndd |jjD }|S )Nc                 S   s   g | ]}|qS r   r   r   rT   r   r   r   r      r   z6ModelGraph.get_appmodel_attributes.<locals>.<listcomp>c                 S   s   g | ]}t |ts|qS r   )r   r   r   r   r   r   r      s   
)r1   rv   local_fields)rB   r   
attributesr   r   r   get_appmodel_attributes   s    z"ModelGraph.get_appmodel_attributesc                 C   s   dd |j D S )Nc                 S   s$   g | ]}t |d r|jjr|jqS r   )rs   rv   r`   re   r   r   r   r   r      s   z5ModelGraph.get_appmodel_abstracts.<locals>.<listcomp>)r   )rB   r   r   r   r   get_appmodel_abstracts   s    z!ModelGraph.get_appmodel_abstractsc                 C   sN   ||j dd|j|g g d}| jr>|jjr>t|jj|d< n|d |d< |S )Nrp   rn   )rN   r   rE   Z	abstractsrS   rG   r:   rE   )
__module__rt   re   r/   rv   rb   r   )rB   r   appmodel_abstractsrR   r   r   r   get_appmodel_context   s    	zModelGraph.get_appmodel_contextc                 C   sD   g }|j D ]4}t|dr
|jjr
||jj || | q
|S )Nrv   )r   rs   rv   r`   rK   rS   get_bases_abstract_fields)rB   cZ_abstract_fieldsr   r   r   r   r     s    
z$ModelGraph.get_bases_abstract_fieldsc                 C   sN   d}|j jrd}|j jrd}|d7 }| jr.d}|jdd|jdd|d	d
dS )Nzmulti-tabler`   proxyz\ninheritancer-   rp   rn   r0   z+[arrowhead=empty, arrowtail=none, dir=both]TZ
target_apprH   r^   rE   r:   ZarrowsrI   )rv   r`   r   r7   r   rt   re   )rB   r   parentr:   r   r   r   get_inheritance_context  s     z"ModelGraph.get_inheritance_contextc                 C   s   t | }|S r[   )r   
get_models)rB   r;   r   r   r   r   r   !  s    zModelGraph.get_modelsc                 C   s(   |j dd|jt|j|j||ddS )Nrp   rn   Tr   )r   rt   re   r^   rE   )rB   r|   rT   r:   rz   r   r   r   rx   %  s    zModelGraph.get_relation_contextc                 C   s<   |  }| |s|r"||kr"|S |d | || |S )NrS   )copy
skip_fieldappendrl   )rB   rT   rN   pkrj   newmodelr   r   r   process_attributes0  s
    zModelGraph.process_attributesc              	   C   sh  | j D ]Z}t|}|sq| |}| |}| |}|| }|D ]}| |jjs\qF| 	|}| 
|}| ||}	| |}
|jj}|r|jjs||
v r|	d | || |
D ]}| ||	||}	q| jr| |	}	|jjD ]}| ||	|}	q|jjD ]}| ||	}	q| jr>|jD ]}| |||	}	q(|d |	 qF|d r| j| qd S )NrS   rF   )rA   r   Zget_app_configr   r   r   ry   rv   Zobject_namer   r   r   r   r   r`   r   rl   r   r3   sort_model_fieldsr   process_local_fieldsZlocal_many_to_manyprocess_local_many_to_manyr0   r   process_parentr=   )rB   rw   r;   Z	app_graphZ
app_modelsr   r   r   rj   rN   r   r   rT   r   r   r   r   rJ   7  s@    









zModelGraph.process_appsc                 C   s   |  }|jds&||v s&| |r*|S d }| jrZt|ttfrZt|j	dd }t
|}t|trv| ||d|}n(t|tr| ||d| j|}nd }|d ur|d | |S )NZ_ptr_idZ	on_deletez*[arrowhead=none, arrowtail=none, dir=both]z([arrowhead=none, arrowtail={}, dir=both]rG   )r   Zattnameendswithr   r9   r   r   r   getattrrg   ON_DELETE_COLORSr>   r   rf   r8   r   )rB   rT   rN   rj   r   r{   Zfield_on_deleterO   r   r   r   r   c  s0    


	zModelGraph.process_local_fieldsc                 C   s   |  }| |r|S d }t|trZt|jjdrv|jjjjrv| 	||d
| j| j}nt|trv| 	||td}|d ur|d | |S )Nrv   z%[arrowhead={} arrowtail={}, dir=both]z>[style="dotted", arrowhead=normal, arrowtail=normal, dir=both]rG   )r   r   r   r   rs   rg   Zthroughrv   Zauto_createdr   rf   r8   r
   r   r   )rB   rT   rN   r   rO   r   r   r   r     s$    


z%ModelGraph.process_local_many_to_manyc                 C   sJ   |  }t|drF| ||}||d vrF| |d rF|d | |S )Nrv   rG   rH   )r   rs   r   ry   r   )rB   r   r   rN   r   r~   r   r   r   r     s    
zModelGraph.process_parentc                 C   s$   |  }t|d dd d|d< |S )NrS   c                 S   s   | d  | d  | d fS )Nra   rO   r:   r   r\   r   r   r   <lambda>  r   z.ModelGraph.sort_model_fields.<locals>.<lambda>)key)r   sorted)rB   rN   r   r   r   r   r     s    zModelGraph.sort_model_fieldsc                 C   sp   | j r4| j D ]&}d|dd }t||r dS q| jrh| jD ]&}d|dd }t||r@ dS q@| j  S )z
        Decide whether to use a model, based on the model name and the lists of
        models to exclude and include.
        z^%s$*z.*TF)r,   rt   researchr6   )rB   r}   Zmodel_patternr   r   r   ry     s    

zModelGraph.use_modelc                 C   sV   | j r2| jr"|jr"|j| j v r"dS |j| j v r2dS | jrRt|tttt	t
tfsRdS dS )NTF)r5   r/   rb   rE   r2   r   r   r   r   r   r   r	   )rB   rT   r   r   r   r     s    zModelGraph.skip_field)F)r-   N)re   r   __qualname__rD   rP   rZ   rl   r   r   r   r   r   r   r   r   r   rx   r   rJ   r   r   r   r   ry   r   r   r   r   r   r(   I   s,   !

'
	,	r(   *django_extensions/graph_models/digraph.dotc                 C   sV   t |trt|}t |ts<t|dr4t |jts<tdt| 	 }|
|}|S )NtemplatezyDefault Django template loader isn't used. This can lead to the incorrect template rendering. Please, check the settings.)r   ru   r   Zget_templater   rs   r   	Exceptionr   rQ   render)rY   r   r   dotr   r   r   generate_dot  s    

 
r   c                  O   s   t | i |}|  | S r[   )r(   rP   rZ   )argsrC   	generatorr   r   r   rP     s    rP   c                 C   s   t g ||d}|| S )N)r,   r6   )r(   ry   )rN   r,   r6   r   r   r   r   ry     s    ry   )r   ).__doc__rU   r    r   Zdjango.appsr   Zdjango.db.modelsr   Zdjango.db.models.fields.relatedr   r   r   r   Z'django.db.models.fields.reverse_relatedr   r	   Z"django.contrib.contenttypes.fieldsr
   Zdjango.templater   r   r   Zdjango.utils.encodingr   Zdjango.utils.safestringr   Zdjango.utils.translationr   r@   __version____license__
__author__Z__contributors__ZCASCADEZPROTECTZSET_NULLZSET_DEFAULTZSETZ
DO_NOTHINGZRESTRICTr   r'   r(   r   rP   ry   r   r   r   r   <module>   s@   	
   
