
    ]j.                        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 y)%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y)UnicodeMixinze
    Mixin class to handle defining the proper __str__/__unicode__
    methods in Python 2 or 3.
    c                 "    | j                         S N)__unicode__selfs    _/root/env/lib/python3.12/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y)AdminAppc                 .    || _         || _        || _        y r!   )
app_config	model_resoptions)r$   r0   r1   r2   s       r%   __init__zAdminApp.__init__R   s    $"r'   c              #      K   | j                   j                         D ]W  }t        |fi | j                  }| j                  D ]  }|j                  |j                        s n | j                  rT| Y y wr!   )r0   
get_models
AdminModelr2   r1   searchr   )r$   modeladmin_modelmodel_res       r%   __iter__zAdminApp.__iter__W   so     __//1 
	E$U;dll;K NN ??;#3#34 >>
	s   AA8!A8c                 @    dj                  | j                               S N join_unicode_generatorr#   s    r%   r"   zAdminApp.__unicode__d       wwt..011r'   c              #     K   | D cg c]  }|j                    }}t        t        dj                  |            z   g }| D ]=  }t        t        |j                   |      z   |j                  |j                          ? y c c}w w)Nz, r   )r   class_)r   r   dictr@   r   append)r$   r9   models_listadmin_model_namess       r%   rA   zAdminApp._unicode_generatorg   s     ;?@K{''@@d$))K*@AAA 	7K#d %%"'   $$[%5%56	7	 As   B
BA0B
c                 d    d| j                   j                  d| j                  j                  dS N<[z]>)	__class__r(   appr   r#   s    r%   __repr__zAdminApp.__repr__s   s$    NN##HHMM
 	
r'   N)r(   r)   r*   r3   r;   r"   rA   rO   r,   r'   r%   r.   r.   Q   s    
2
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y)r6   )list_displaylist_filterraw_id_fieldssearch_fieldsprepopulated_fieldsdate_hierarchyc                     || _         g | _        g | _        g | _        g | _        i | _        d | _        || _        || _        || _	        || _
        || _        y r!   )r8   rQ   rR   rS   rT   rU   rV   search_field_namesraw_id_thresholdlist_filter_thresholddate_hierarchy_namesprepopulated_field_names)r$   r8   rY   rZ   rX   r[   r\   r2   s           r%   r3   zAdminModel.__init__   sc    
 
#% ""4 0%:"$8!(@%r'   c                 P    d| j                   j                  d| j                  dS rJ   )rM   r(   r   r#   s    r%   rO   zAdminModel.__repr__   s     NN##II
 	
r'   c                 .    | j                   j                  S r!   )r8   r(   r#   s    r%   r   zAdminModel.name   s    zz"""r'   c              #   J  K   | j                   }|j                  D ]  }t        |d      r,t        |j                  d|j                  j
                        }nt        d      |j                  j                         }|d | j                         |k  sw|j                    y w)Nremote_fieldrelated_modelz%Unable to process ManyToMany relation)rY   local_many_to_manyhasattrgetattrr`   r8   r   objectsallcountr   )r$   metarY   fieldra   related_objectss         r%   _process_many_to_manyz AdminModel._process_many_to_many   s     00,, 	!Eun- '(:(:OUM_M_MeMe f"#JKK+33779O0 01779<LLjj 	!s   BB#B#c              #      K   |j                   j                         }|j                  D ]  }| j                  ||      }|s|  y wr!   )parentsvaluesfields_process_field)r$   rh   parent_fieldsri   r   s        r%   _process_fieldszAdminModel._process_fields   sE     ++-[[ 	E&&um<D
	s   >AAc                    | j                   }| j                  }t        ||      }t        |d      r,t	        |j
                  d|j
                  j                        }nt        d      |j                  j                         }|d | j                         }||k\  r&| j                  j                  |j                         y ||k  r&| j                  j                  |j                         y y )Nr`   ra   z%Unable to process ForeignKey relation)rY   rZ   maxrc   rd   r`   r8   r   re   rf   rg   rS   rF   r   rR   )r$   ri   rY   rZ   	max_countra   related_counts          r%   _process_foreign_keyzAdminModel._process_foreign_key   s    00 $ : :-/?@	5.)#E$6$6I[I[IaIabMFGG%--113%jy1779,,%%ejj122##EJJ/ r'   c                    ||v ry t        |j                        }| j                  j                  |       t	        |t
              rGt	        |t        j                        r| j                  |       n| j                  j                  |       |j                  | j                  v r| j                  j                  |       |S r!   )strr   rQ   rF   
isinstancer   r   
ForeignKeyrw   rR   rX   rT   )r$   ri   rq   
field_names       r%   rp   zAdminModel._process_field   s    M!_
  ,e[)%!2!23))%0  ''
3::000%%j1r'   c                 @    dj                  | j                               S r=   r?   r#   s    r%   r"   zAdminModel.__unicode__   rB   r'   c                 (   t        |t        t        t        f      r| j	                  |t        |            S t        |t
              r| j                  ||      S t        |t              r| j                  ||      S t        t        |      d|      )Nz is not supported in )rz   listsettuple_yield_tuplerE   _yield_dictry   _yield_string	TypeErrortyper$   keyvalues      r%   _yield_valuezAdminModel._yield_value   sx    edC/0$$S%,77t$##C//s#%%c5114;NOOr'   c                 6    t         t        | ||            z  S )N)r   r   )r   rE   )r$   r   r   	converters       r%   r   zAdminModel._yield_string   s!    #dE"'
 
 	
r'   c                 j   g }| j                  ||      }t        |      t        kD  r|j                  | j                  |dt                     |j                         D ]'  \  }}|j                  dt        z  dz  |d|       ) |j                  t        dz  dz          dj                  |      }|S )N{    z: }
)r   lenr
   rF   ry   itemsr   r@   )r$   r   r   	row_partsrowkvs          r%   r   zAdminModel._yield_dict   s    	  e,s8n$T//S#>? N1  q</?#/Eq!!LMN \C/#56))I&C
r'   c                 X   g }| j                  ||      }t        |      t        kD  r|j                  | j                  |dt                     |D ],  }|j                  dt
        z  dz  t        |      z   dz          . |j                  t
        dz  dz          dj                  |      }|S )N(r   r   ,)r   )r   r   r
   rF   ry   r   reprr@   )r$   r   r   r   r   r   s         r%   r   zAdminModel._yield_tuple   s    	  e,s8n$T//S#>? I  \!1C!7$q'!AC!GHI \C/#56))I&C
r'   c              #      K   | j                          | j                  D ]%  }t        | |      }|s| j                  ||       ' y wr!   )_processPRINTABLE_PROPERTIESrd   r   r   s      r%   rA   zAdminModel._unicode_generator  sF     ,, 	4CD#&E''U33	4s
   .AAc                    | j                   j                  }| xj                  t        | j	                  |            z  c_        t        | j                  |            }| j                  d d d   D ]  }||v s| j                  r|| _         n t        | j                        D ]P  }|j                  dd      \  }}|j                  d      }||v s.d}|D ]
  }||vsd} n |rB|| j                  |<   R d| _        y )N=   r   FT)r8   _metarS   r   rk   rr   r[   rV   sortedr\   splitrU   	processed)r$   rh   field_namesr|   r   vs
incompleter   s           r%   r   zAdminModel._process  s   zzd4#=#=d#CDD4//5633DbD9 	J[(1D1D&0#	
 556 	5AGGCOEAr#BK"
 A+%)

 "24D,,Q/	5 r'   N)r(   r)   r*   r   r   r   r   r   r   r3   rO   propertyr   rk   rr   rw   rp   r"   r   r   r   r   r   rA   r   r,   r'   r%   r6   r6   z   sx     0@'<$6&:*B	A$
 # #	!("2P 37 
4r'   r6   c                   *    e Zd ZdZdZd Zed        Zy)Commandz5Generate a `admin.py` file for the given app (models)Tc                 d   |j                  d       |j                  dd       |j                  dddt        d	       |j                  d
ddt        d	       |j                  dddt        d	       |j                  ddt        t
        dd       |j                  ddt        t        dd       y )Napp_name
model_name*)nargsz-sz--search-fieldrF   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+  s    J'L4"8&& 	 	'
 	$X(& 	 	'
 	)(,& 	 	' 	+#)3JG 	 	H
 	&S$.@G 	 	Hr'   c                 h   |d   }	 t        j                  |      }g }|d   D ]5  }|j                  t        j                  |t        j                               7 | j                  j	                  t        ||fi |j                                y # t        $ r | j                  j	                  d       | j                  j	                  d       t        j
                         D cg c]  }|j                   nc c}w }}t        |      D ]   }| j                  j	                  d|z         " Y y w xY w)Nr   z6This command requires an existing app name as argumentzAvailable apps:z    %sr   )r   get_app_configLookupErrorstderrwriteget_app_configslabelr   rF   recompile
IGNORECASEstdoutr.   r&   )	r$   argsr2   r   rN   
app_labelsr   r1   args	            r%   handlezCommand.handleI  s
   :&	%%h/C 	<( 	=CRZZR]];<	= 	(3	=W=EEGH  	KKVWKK/0/3/C/C/EF#))FFJF
+ 4!!(U"234	s   B AD1%C986D10D1N)r(   r)   r*   r   can_import_settingsr   r	   r   r,   r'   r%   r   r   &  s)    FDH< I Ir'   r   )!r+   r   django.appsr   django.confr   django.core.management.baser   r   	django.dbr   "django_extensions.management.utilsr	   rd   r
   r   r   r   	DateFieldDateTimeFieldr{   BooleanFieldr   r   r   r   r   r   r   r   r.   r6   r   r,   r'   r%   <module>r      sS    
    B  < #3R8x3*A2F 8%7= h



	0  X'; >  
 x)? B   #8-G J   / 4  H&9 <   x)? B  " "&
| &
Ri iX5Il 5Ir'   