
    Ƿi.                        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          Z G d d e          Z G d! d"e          Z G 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                       e Zd ZdZd ZdS )UnicodeMixinze
    Mixin class to handle defining the proper __str__/__unicode__
    methods in Python 2 or 3.
    c                 *    |                                  S N)__unicode__selfs    m/srv/django_bis/venv311/lib/python3.11/site-packages/django_extensions/management/commands/admin_generator.py__str__zUnicodeMixin.__str__M   s    !!!    N)__name__
__module____qualname____doc__r&    r'   r%   r   r   G   s-         
" " " " "r'   r   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )AdminAppc                 0    || _         || _        || _        d S r!   )
app_config	model_resoptions)r$   r0   r1   r2   s       r%   __init__zAdminApp.__init__R   s    $"r'   c              #      K   | j                                         D ]F}t          |fi | j        }| j        D ]}|                    |j                  r n	| j        rB|V  Gd S r!   )r0   
get_models
AdminModelr2   r1   searchr   )r$   modeladmin_modelmodel_res       r%   __iter__zAdminApp.__iter__W   s      _//11 
	 
	E$U;;dl;;K N  ??;#344 E > 
	 
	r'   c                 P    d                     |                                           S N join_unicode_generatorr#   s    r%   r"   zAdminApp.__unicode__d        wwt..00111r'   c              #      K   d | D             }t           t          d                    |                    z  V  g }| D ]<}t          t          |j        |          z  V  |                    |j                   =d S )Nc                     g | ]	}|j         
S r,   )r   ).0r9   s     r%   
<listcomp>z/AdminApp._unicode_generator.<locals>.<listcomp>h   s    @@@K{'@@@r'   z, r   )r   class_)r   dictr@   r   r   append)r$   models_listadmin_model_namesr9   s       r%   rA   zAdminApp._unicode_generatorg   s      @@4@@@d$))K*@*@AAAAAAA 	7 	7K#d %"' ' '     $$[%56666	7 	7r'   c                 <    d| j         j        d| j        j        dS N<[z]>)	__class__r(   appr   r#   s    r%   __repr__zAdminApp.__repr__s   s'     N###HMMM
 	
r'   N)r(   r)   r*   r3   r;   r"   rA   rR   r,   r'   r%   r.   r.   Q   s_          
  2 2 2
7 
7 
7
 
 
 
 
r'   r.   c                       e Zd ZdZeeeeefdZ	d Z
ed             Zd Zd Zd Zd Zd	 Zd
 ZefdZd Zd Zd Zd ZdS )r6   )list_displaylist_filterraw_id_fieldssearch_fieldsprepopulated_fieldsdate_hierarchyc                     || _         g | _        g | _        g | _        g | _        i | _        d | _        || _        || _        || _	        || _
        || _        d S r!   )r8   rT   rU   rV   rW   rX   rY   search_field_namesraw_id_thresholdlist_filter_thresholddate_hierarchy_namesprepopulated_field_names)r$   r8   r\   r]   r[   r^   r_   r2   s           r%   r3   zAdminModel.__init__   si    
 
#% ""4 0%:"$8!(@%%%r'   c                 2    d| j         j        d| j        dS rM   )rP   r(   r   r#   s    r%   rR   zAdminModel.__repr__   s%     N###III
 	
r'   c                     | j         j        S r!   )r8   r(   r#   s    r%   r   zAdminModel.name   s    z""r'   c              #   0  K   | j         }|j        D ]}t          |d          r!t          |j        d|j        j                  }nt          d          |j                                        }|d |         	                                |k     r	|j
        V  d S )Nremote_fieldrelated_modelz%Unable to process ManyToMany relation)r\   local_many_to_manyhasattrgetattrrc   r8   r   objectsallcountr   )r$   metar\   fieldrd   related_objectss         r%   _process_many_to_manyz AdminModel._process_many_to_many   s      0, 	! 	!Eun-- L '(:OUM_Me f f"#JKKK+37799O0 0017799<LLLj   	! 	!r'   c              #      K   |j                                         }|j        D ]}|                     ||          }|r|V  d S r!   )parentsvaluesfields_process_field)r$   rk   parent_fieldsrl   r   s        r%   _process_fieldszAdminModel._process_fields   s[      ++--[ 	 	E&&um<<D 


	 	r'   c                    | j         }| j        }t          ||          }t          |d          r!t	          |j        d|j        j                  }nt          d          |j        	                                }|d |         
                                }||k    r!| j                            |j                   d S ||k     r!| j                            |j                   d S d S )Nrc   rd   z%Unable to process ForeignKey relation)r\   r]   maxrf   rg   rc   r8   r   rh   ri   rj   rV   rI   r   rU   )r$   rl   r\   r]   	max_countrd   related_counts          r%   _process_foreign_keyzAdminModel._process_foreign_key   s    0 $ :-/?@@	5.)) 	H#E$6I[IabbMMFGGG%-1133%jyj17799,,,%%ej11111222##EJ///// Dr'   c                 |   ||v rd S t          |j                  }| j                            |           t	          |t
                    rJt	          |t          j                  r|                     |           n| j	                            |           |j        | j
        v r| j                            |           |S r!   )strr   rT   rI   
isinstancer   r   
ForeignKeyrz   rU   r[   rW   )r$   rl   rt   
field_names       r%   rs   zAdminModel._process_field   s    M!!F__
  ,,,e[)) 	4%!233 4))%0000 ''
333:000%%j111r'   c                 P    d                     |                                           S r=   r?   r#   s    r%   r"   zAdminModel.__unicode__   rB   r'   c                 z   t          |t          t          t          f          r#|                     |t          |                    S t          |t
                    r|                     ||          S t          |t                    r|                     ||          S t          t          |          d|          )Nz is not supported in )r}   listsettuple_yield_tuplerH   _yield_dictr|   _yield_string	TypeErrortyper$   keyvalues      r%   _yield_valuezAdminModel._yield_value   s    edC/00 	P$$S%,,777t$$ 	P##C///s## 	P%%c51114;;;;NOOOr'   c                 F    t           t          | ||                    z  S )N)r   r   )r   rH   )r$   r   r   	converters       r%   r   zAdminModel._yield_string   s0    #d)E""'
 '
 '
 
 	
r'   c                    g }|                      ||          }t          |          t          k    r|                    |                      |dt                               |                                D ],\  }}|                    dt          z  dz  |d|           -|                    t          dz  dz              d                    |          }|S )N{    z: }
)r   lenr
   rI   r|   itemsr   r@   )r$   r   r   	row_partsrowkvs          r%   r   zAdminModel._yield_dict   s    	  e,,s88n$$T//S#>>??? N N1  q</?#/E/Eqqq!!!LMMMM\C/#5666))I&&C
r'   c                    g }|                      ||          }t          |          t          k    r|                    |                      |dt                               |D ]5}|                    dt
          z  dz  t          |          z   dz              6|                    t
          dz  dz              d                    |          }|S )N(r   r   ,)r   )r   r   r
   rI   r|   r   reprr@   )r$   r   r   r   r   r   s         r%   r   zAdminModel._yield_tuple   s    	  e,,s88n$$T//S#>>??? I I  \!1C!7$q''!AC!GHHHH\C/#5666))I&&C
r'   c              #      K   |                                   | j        D ],}t          | |          }|r|                     ||          V  -d S r!   )_processPRINTABLE_PROPERTIESrg   r   r   s      r%   rA   zAdminModel._unicode_generator  sc      , 	4 	4CD#&&E 4''U33333	4 	4r'   c                    | j         j        }| xj        t          |                     |                    z  c_        t          |                     |                    }| j        d d d         D ]}||v r| j        s	|| _         nt          | j	                  D ]O}|
                    dd          \  }}|
                    d          }||v rd}|D ]
}||vrd} n|s
|| j        |<   Pd| _        d S )N=   r   FT)r8   _metarV   r   rn   ru   r^   rY   sortedr_   splitrX   	processed)r$   rk   field_namesr   r   vs
incompleter   s           r%   r   zAdminModel._process  s)   zd4#=#=d#C#CDDD4//55663DDbD9 	 	J[((1D(&0#566 	5 	5AGGCOOEAr#BK"
  A++%)
 , " 524D,Q/r'   N)r(   r)   r*   r   r   r   r   r   r   r3   rR   propertyr   rn   ru   rz   rs   r"   r   r   r   r   r   rA   r   r,   r'   r%   r6   r6   z   s"        0@'<$6&:*B	A A A A$
 
 
 # # X#	! 	! 	!    (  "2 2 2P P P 37 
 
 
 
    4 4 4    r'   r6   c                   2    e Zd ZdZdZd Zed             ZdS )Commandz5Generate a `admin.py` file for the given app (models)Tc                    |                     d           |                     dd           |                     dddt          d	           |                     d
ddt          d	           |                     dddt          d	           |                     ddt          t
          dd           |                     ddt          t          dd           d S )Napp_name
model_name*)nargsz-sz--search-fieldrI   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])r   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$   parsers     r%   add_argumentszCommand.add_arguments+  s2   J'''L444"8&& 	 	' 	' 	'
 	$X(& 	 	' 	' 	'
 	)(,& 	 	' 	' 	' 	+#)3JG 	 	H 	H 	H
 	&S$.@G 	 	H 	H 	H 	H 	Hr'   c                 R   |d         }	 t          j        |          }n# t          $ r | j                            d           | j                            d           d t          j                    D             }t          |          D ]}| j                            d|z              Y d S w xY wg }|d         D ]4}|                    t          j	        |t          j
                             5| j                            t          ||fi |                                           d S )Nr   z6This command requires an existing app name as argumentzAvailable apps:c                     g | ]	}|j         
S r,   )label)rE   rQ   s     r%   rF   z"Command.handle.<locals>.<listcomp>R  s    FFF#)FFFr'   z    %sr   )r   get_app_configLookupErrorstderrwriteget_app_configsr   rI   recompile
IGNORECASEstdoutr.   r&   )	r$   argsr2   r   rQ   
app_labelsr   r1   args	            r%   handlezCommand.handleI  s>   :&	%h//CC 	 	 	KVWWWK/000FFt/C/E/EFFFJ
++ 4 4!!(U"23333FF	 	<( 	= 	=CRZR];;<<<<(3	==W==EEGGHHHHHs    B
B-,B-N)r(   r)   r*   r   can_import_settingsr   r	   r   r,   r'   r%   r   r   &  sQ        FDH H H< I I ]I I Ir'   r   )!r+   r   django.appsr   django.confr   django.core.management.baser   r   	django.dbr   "django_extensions.management.utilsr	   rg   r
   r   r   r   	DateFieldDateTimeFieldr~   BooleanFieldr   r   r   r   r   r   r   r   r.   r6   r   r,   r'   r%   <module>r      sx     
			                   B B B B B B B B       < < < < < < #3R88wx33*A2FF 78%7== gh



	0   WX'; >   
 wx)? B    #78-G J    / 4   GH&9 <    wx)? B   " " " " " " " "&
 &
 &
 &
 &
| &
 &
 &
Ri i i i i i i iX5I 5I 5I 5I 5Il 5I 5I 5I 5I 5Ir'   