o
    i/                     @   s"  d Z ddl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mZ eedd	Zeed
dZeeddZeeddZeede
je
je
je
jfZeeddZeeddZeeddZeeddZeeddZeeddZG dd dZG dd  d eZG d!d" d"eZG d#d$ d$eZ dS )%as  
The Django Admin Generator is a project which can automatically generate
(scaffold) a Django Admin for you. By doing this it will introspect your
models and automatically generate an Admin with properties like:

 - `list_display` for all local fields
 - `list_filter` for foreign keys with few items
 - `raw_id_fields` for foreign keys with a lot of items
 - `search_fields` for name and `slug` fields
 - `prepopulated_fields` for `slug` fields
 - `date_hierarchy` for `created_at`, `updated_at` or `joined_at` fields

The original source and latest version can be found here:
https://github.com/WoLpH/django-admin-generator/
    N)apps)settings)LabelCommandCommandErrormodels)signalcommandMAX_LINE_WIDTHN   INDENT_WIDTH   LIST_FILTER_THRESHOLD   RAW_ID_THRESHOLDd   LIST_FILTERSEARCH_FIELD_NAMES)nameslugDATE_HIERARCHY_NAMES)	joined_at
updated_at
created_atPREPOPULATED_FIELD_NAMES)z	slug=namePRINT_IMPORTSzY# -*- coding: utf-8 -*-
from django.contrib import admin

from .models import %(models)s
PRINT_ADMIN_CLASSzM

@admin.register(%(name)s)
class %(name)sAdmin(admin.ModelAdmin):%(class_)s
PRINT_ADMIN_PROPERTYz
    %(key)s = %(value)sc                   @   s   e Zd ZdZdd ZdS )UnicodeMixinze
    Mixin class to handle defining the proper __str__/__unicode__
    methods in Python 2 or 3.
    c                 C   s   |   S N)__unicode__self r"   u/var/www/html/formularioweb/env/lib/python3.10/site-packages/django_extensions/management/commands/admin_generator.py__str__c   s   zUnicodeMixin.__str__N)__name__
__module____qualname____doc__r$   r"   r"   r"   r#   r   ]   s    r   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )AdminAppc                 K   s   || _ || _|| _d S r   )
app_config	model_resoptions)r!   r*   r+   r,   r"   r"   r#   __init__h   s   
zAdminApp.__init__c                 c   sR    | j  D ] }t|fi | j}| jD ]
}||jr nq| jr#q|V  qd S r   )r*   
get_models
AdminModelr,   r+   searchr   )r!   modeladmin_modelmodel_rer"   r"   r#   __iter__m   s   
zAdminApp.__iter__c                 C      d |  S N join_unicode_generatorr    r"   r"   r#   r   z      zAdminApp.__unicode__c                 c   sX    dd | D }t td|d V  g }| D ]}tt|j|d V  ||j qd S )Nc                 S      g | ]}|j qS r"   )r   ).0r2   r"   r"   r#   
<listcomp>~       z/AdminApp._unicode_generator.<locals>.<listcomp>z, r   )r   class_)r   dictr9   r   r   append)r!   models_listadmin_model_namesr2   r"   r"   r#   r:   }   s   
zAdminApp._unicode_generatorc                 C   s   d| j j| jjf S Nz<%s[%s]>)	__class__r%   appr   r    r"   r"   r#   __repr__   s   zAdminApp.__repr__N)r%   r&   r'   r-   r4   r   r:   rH   r"   r"   r"   r#   r)   g   s    r)   c                   @   s   e Zd ZdZeeeeefddZ	dd Z
edd Zdd	 Zd
d Zdd Zdd Zdd Zdd ZefddZdd Zdd Zdd Zdd ZdS )r/   )list_displaylist_filterraw_id_fieldssearch_fieldsprepopulated_fieldsdate_hierarchyc                 K   sL   || _ g | _g | _g | _g | _i | _d | _|| _|| _|| _	|| _
|| _d S r   )r1   rI   rJ   rK   rL   rM   rN   search_field_namesraw_id_thresholdlist_filter_thresholddate_hierarchy_namesprepopulated_field_names)r!   r1   rP   rQ   rO   rR   rS   r,   r"   r"   r#   r-      s   

zAdminModel.__init__c                 C   s   d| j j| jf S rE   )rF   r%   r   r    r"   r"   r#   rH      s   zAdminModel.__repr__c                 C   s   | j jS r   )r1   r%   r    r"   r"   r#   r      s   zAdminModel.namec                 c   sd    | j }|jD ](}t|drt|jd|jj}ntd|j }|d | 	 |k r/|j
V  qd S )Nremote_fieldrelated_modelz%Unable to process ManyToMany relation)rP   local_many_to_manyhasattrgetattrrT   r1   r   objectsallcountr   )r!   metarP   fieldrU   related_objectsr"   r"   r#   _process_many_to_many   s   


z AdminModel._process_many_to_manyc                 c   s2    |j  }|jD ]}| ||}|r|V  q	d S r   )parentsvaluesfields_process_field)r!   r\   parent_fieldsr]   r   r"   r"   r#   _process_fields   s   

zAdminModel._process_fieldsc                 C   s   | j }| j}t||}t|drt|jd|jj}ntd|j	 }|d | 
 }||kr8| j|j d S ||k rE| j|j d S 	 d S )NrT   rU   z%Unable to process ForeignKey relation)rP   rQ   maxrW   rX   rT   r1   r   rY   rZ   r[   rK   rB   r   rJ   )r!   r]   rP   rQ   	max_countrU   related_countr"   r"   r#   _process_foreign_key   s   


zAdminModel._process_foreign_keyc                 C   sl   ||v rd S t |j}| j| t|tr(t|tjr"| | n| j	| |j| j
v r4| j| |S r   )strr   rI   rB   
isinstancer   r   
ForeignKeyri   rJ   rO   rL   )r!   r]   rd   
field_namer"   r"   r#   rc      s   

zAdminModel._process_fieldc                 C   r5   r6   r8   r    r"   r"   r#   r      r;   zAdminModel.__unicode__c                 C   s`   t |tttfr| |t|S t |tr| ||S t |tr&| ||S t	dt
||f )Nz%s is not supported in %r)rk   listsettuple_yield_tuplerA   _yield_dictrj   _yield_string	TypeErrortyper!   keyvaluer"   r"   r#   _yield_value   s   

zAdminModel._yield_valuec                 C   s   t t|||d S )N)rw   rx   )r   rA   )r!   rw   rx   	converterr"   r"   r#   rs     s   zAdminModel._yield_stringc                 C   s~   g }|  ||}t|tkr=||  |dt | D ]\}}|ddt d ||f  q|td d  d|}|S )N{z%s%r: %r    }
)rs   lenr	   rB   rj   itemsr   r9   )r!   rw   rx   	row_partsrowkvr"   r"   r#   rr   	  s   
zAdminModel._yield_dictc                 C   sx   g }|  ||}t|tkr:||  |dt |D ]}|dt d t| d  q|td d  d|}|S )N(r|   r}   ,)r   )rs   r   r	   rB   rj   r   reprr9   )r!   rw   rx   r   r   r   r"   r"   r#   rq     s    
zAdminModel._yield_tuplec                 c   s6    |    | jD ]}t| |}|r| ||V  qd S r   )_processPRINTABLE_PROPERTIESrX   ry   rv   r"   r"   r#   r:   #  s   

zAdminModel._unicode_generatorc                 C   s   | j j}|  jt| |7  _t| |}| jd d d D ]}||v r-| js-|| _ nqt| j	D ])}|
dd\}}|
d}||v r\d}|D ]
}||vrTd} nqJ|s\|| j|< q3d| _d S )N=   r   FT)r1   _metarK   rn   r_   re   rR   rN   sortedrS   splitrM   	processed)r!   r\   field_namesrm   r   vs
incompleter   r"   r"   r#   r   *  s,   


zAdminModel._processN)r%   r&   r'   r   r   r   r   r   r   r-   rH   propertyr   r_   re   ri   rc   r   ry   r   rs   rr   rq   r:   r   r"   r"   r"   r#   r/      s,    


r/   c                   @   s(   e Zd ZdZdZdd Zedd ZdS )Commandz5Generate a `admin.py` file for the given app (models)Tc                 C   s   | d |j ddd |j dddtdd	 |j d
ddtdd	 |j dddtdd	 |j ddttddd |j ddttddd d S )Napp_name
model_name*)nargsz-sz--search-fieldrB   zNFields named like this will be added to `search_fields` [default: %(default)s])actiondefaulthelpz-dz--date-hierarchyzNA field named like this will be set as `date_hierarchy` [default: %(default)s]z-pz--prepopulated-fieldszThese fields will be prepopulated by the other field.The field names can be specified like `spam=eggA,eggB,eggC` [default: %(default)s]z-lz--list-filter-thresholdr   zsIf a foreign key has less than LIST_FILTER_THRESHOLD items it will be added to `list_filter` [default: %(default)s])ru   r   metavarr   z-rz--raw-id-thresholdr   znIf a foreign key has more than RAW_ID_THRESHOLD items it will be added to `list_filter` [default: %(default)s])add_argumentr   r   r   intr   r   )r!   parserr"   r"   r#   add_argumentsJ  sN   
		
zCommand.add_argumentsc           	      O   s   |d }zt |}W n. ty9   | jd | jd dd t  D }t|D ]
}| jd|  q+Y d S w g }|d D ]}|t	|tj
 q@| jt||fi |  d S )Nr   z6This command requires an existing app name as argumentzAvailable apps:c                 S   r<   r"   )label)r=   rG   r"   r"   r#   r>     r?   z"Command.handle.<locals>.<listcomp>z    %sr   )r   get_app_configLookupErrorstderrwriteget_app_configsr   rB   recompile
IGNORECASEstdoutr)   r$   )	r!   argsr,   r   rG   
app_labelsr   r+   argr"   r"   r#   handley  s   "zCommand.handleN)r%   r&   r'   r   can_import_settingsr   r   r   r"   r"   r"   r#   r   E  s    /r   )!r(   r   django.appsr   django.confr   django.core.management.baser   r   	django.dbr   "django_extensions.management.utilsr   rX   r	   r   r   r   	DateFieldDateTimeFieldrl   BooleanFieldr   r   r   r   r   r   r   r   r)   r/   r   r"   r"   r"   r#   <module>   sf   	



) 6