ó
¸9—^c           @   s®  d  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	 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 d  e f d! „  ƒ  YZ d" e f d# „  ƒ  YZ  d$ e f d% „  ƒ  YZ! d& e f d' „  ƒ  YZ" d S(+   ss  
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/
iÿÿÿÿN(   t   apps(   t   settings(   t   LabelCommandt   CommandError(   t   models(   t   signalcommandt   MAX_LINE_WIDTHiN   t   INDENT_WIDTHi   t   LIST_FILTER_THRESHOLDi   t   RAW_ID_THRESHOLDid   t   LIST_FILTERt   SEARCH_FIELD_NAMESt   namet   slugt   DATE_HIERARCHY_NAMESt	   joined_att
   updated_att
   created_att   PREPOPULATED_FIELD_NAMESs	   slug=namet   PRINT_IMPORTSsY   # -*- coding: utf-8 -*-
from django.contrib import admin

from .models import %(models)s
t   PRINT_ADMIN_CLASSsM   

@admin.register(%(name)s)
class %(name)sAdmin(admin.ModelAdmin):%(class_)s
t   PRINT_ADMIN_PROPERTYs   
    %(key)s = %(value)st   UnicodeMixinc           B   s,   e  Z d  Z e j r! d „  Z n	 d „  Z RS(   se   
    Mixin class to handle defining the proper __str__/__unicode__
    methods in Python 2 or 3.
    c         C   s
   |  j  ƒ  S(   N(   t   __unicode__(   t   self(    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyt   __str__O   s    c         C   s   |  j  ƒ  j d ƒ S(   Nt   utf8(   R   t   encode(   R   (    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyR   R   s    (   t   __name__t
   __module__t   __doc__t   sixt   PY3R   (    (    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyR   H   s   	t   AdminAppc           B   s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         K   s   | |  _  | |  _ | |  _ d  S(   N(   t
   app_configt	   model_rest   options(   R   R"   R#   R$   (    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyt   __init__W   s    		c         c   sn   xg |  j  j ƒ  D]V } t | |  j  } x6 |  j D] } | j | j ƒ r2 Pq2 q2 W|  j ra q n  | Vq Wd  S(   N(   R"   t
   get_modelst
   AdminModelR$   R#   t   searchR   (   R   t   modelt   admin_modelt   model_re(    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyt   __iter__\   s    	c         C   s   d j  |  j ƒ  ƒ S(   Nt    (   t   joint   _unicode_generator(   R   (    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyR   i   s    c         c   s}   g  |  D] } | j  ^ q } t t d d j | ƒ ƒ Vg  } x9 |  D]1 } t t d | j  d | ƒ V| j | j  ƒ qD Wd  S(   NR   s   , R   t   class_(   R   R   t   dictR.   R   t   append(   R   R*   t   models_listt   admin_model_names(    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyR/   l   s    			c         C   s   d |  j  j |  j j f S(   Ns   <%s[%s]>(   t	   __class__R   t   appR   (   R   (    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyt   __repr__x   s    	(   R   R   R%   R,   R   R/   R7   (    (    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyR!   V   s
   				R'   c           B   s¤   e  Z d Z e e e e e d „ Z d „  Z	 e
 d „  ƒ Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z e d „ Z d „  Z d „  Z d „  Z d „  Z RS(   t   list_displayt   list_filtert   raw_id_fieldst   search_fieldst   prepopulated_fieldst   date_hierarchyc         K   sp   | |  _  g  |  _ g  |  _ g  |  _ g  |  _ i  |  _ d  |  _ | |  _ | |  _	 | |  _
 | |  _ | |  _ d  S(   N(   R)   R8   R9   R:   R;   R<   t   NoneR=   t   search_field_namest   raw_id_thresholdt   list_filter_thresholdt   date_hierarchy_namest   prepopulated_field_names(   R   R)   R@   RA   R?   RB   RC   R$   (    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyR%   ‰   s    											c         C   s   d |  j  j |  j f S(   Ns   <%s[%s]>(   R5   R   R   (   R   (    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyR7   ›   s    	c         C   s
   |  j  j S(   N(   R)   R   (   R   (    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyR   ¡   s    c         c   sŠ   |  j  } xz | j D]o } t | d ƒ rF t | j d | j j ƒ } n t d ƒ ‚ | j j ƒ  } | |  j	 ƒ  | k  r | j
 Vq q Wd  S(   Nt   remote_fieldt   related_models%   Unable to process ManyToMany relation(   R@   t   local_many_to_manyt   hasattrt   getattrRD   R)   R   t   objectst   allt   countR   (   R   t   metaR@   t   fieldRE   t   related_objects(    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyt   _process_many_to_many¥   s    	c         c   sG   | j  j ƒ  } x1 | j D]& } |  j | | ƒ } | r | Vq q Wd  S(   N(   t   parentst   valuest   fieldst   _process_field(   R   RL   t   parent_fieldsRM   R   (    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyt   _process_fields°   s
    c         C   sÁ   |  j  } |  j } t | | ƒ } t | d ƒ rN t | j d | j j ƒ } n t d ƒ ‚ | j j	 ƒ  } | |  j
 ƒ  } | | k r› |  j j | j ƒ n" | | k  r½ |  j j | j ƒ n  d  S(   NRD   RE   s%   Unable to process ForeignKey relation(   R@   RA   t   maxRG   RH   RD   R)   R   RI   RJ   RK   R:   R2   R   R9   (   R   RM   R@   RA   t	   max_countRE   t   related_count(    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyt   _process_foreign_key·   s    		c         C   sŸ   | | k r d  St  j | j ƒ } |  j j | ƒ t | t ƒ rv t | t j ƒ rc |  j	 | ƒ qv |  j
 j | ƒ n  | j |  j k r› |  j j | ƒ n  | S(   N(   R   t	   text_typeR   R8   R2   t
   isinstanceR
   R   t
   ForeignKeyRY   R9   R?   R;   (   R   RM   RT   t
   field_name(    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyRS   Ë   s    c         C   s   d j  |  j ƒ  ƒ S(   NR-   (   R.   R/   (   R   (    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyR   Ü   s    c         C   sŒ   t  | t t t f ƒ r. |  j | t | ƒ ƒ St  | t ƒ rM |  j | | ƒ St  | t ƒ rl |  j | | ƒ St	 d t
 | ƒ | f ƒ ‚ d  S(   Ns   %s is not supported in %r(   R[   t   listt   sett   tuplet   _yield_tupleR1   t   _yield_dictt   strt   _yield_stringt	   TypeErrort   type(   R   t   keyt   value(    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyt   _yield_valueß   s    c         C   s   t  t d | d | | ƒ ƒ S(   NRg   Rh   (   R   R1   (   R   Rg   Rh   t	   converter(    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyRd   é   s    	c         C   s°   g  } |  j  | | ƒ } t | ƒ t k r¬ | j |  j  | d t ƒ ƒ x< | j ƒ  D]. \ } } | j d d t d | | f ƒ qS W| j t d d ƒ d j | ƒ } n  | S(   Nt   {s   %s%r: %ri   t    t   }s   
(   Rd   t   lenR   R2   Rc   t   itemsR   R.   (   R   Rg   Rh   t	   row_partst   rowt   kt   v(    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyRb   ï   s    &c         C   s¥   g  } |  j  | | ƒ } t | ƒ t k r¡ | j |  j  | d t ƒ ƒ x1 | D]) } | j d t d t | ƒ d ƒ qM W| j t d d ƒ d j | ƒ } n  | S(   Nt   (i   Rl   t   ,t   )s   
(   Rd   Rn   R   R2   Rc   R   t   reprR.   (   R   Rg   Rh   Rp   Rq   Rs   (    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyRa   ü   s    'c         c   sK   |  j  ƒ  x: |  j D]/ } t |  | ƒ } | r |  j | | ƒ Vq q Wd  S(   N(   t   _processt   PRINTABLE_PROPERTIESRH   Ri   (   R   Rg   Rh   (    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyR/   	  s
    
c         C   s#  |  j  j } |  j t |  j | ƒ ƒ 7_ t |  j | ƒ ƒ } xA |  j d  d  d … D]) } | | k rV |  j rV | |  _ PqV qV Wx t |  j	 ƒ D] } | j
 d d ƒ \ } } | j
 d ƒ } | | k r“ t } x$ | D] } | | k rÙ t } PqÙ qÙ W| s| |  j | <qq“ q“ Wt |  _ d  S(   Niÿÿÿÿt   =i   Ru   (   R)   t   _metaR:   R^   RO   RU   RB   R=   t   sortedRC   t   splitt   Falset   TrueR<   t	   processed(   R   RL   t   field_namesR]   Rr   t   vst
   incompleteRs   (    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyRx     s&    	(   R8   R9   R:   R;   R<   R=   (   R   R   Ry   R	   R   R   R   R   R%   R7   t   propertyR   RO   RU   RY   RS   R   Ri   Rw   Rd   Rb   Ra   R/   Rx   (    (    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyR'      s0        							
			t   Commandc           B   s,   e  Z d  Z e Z d „  Z e d „  ƒ Z RS(   s5   Generate a `admin.py` file for the given app (models)c         C   sÚ   | j  d ƒ | j  d d d ƒ| j  d d d d d	 t d
 d ƒ| j  d d d d d	 t d
 d ƒ| j  d d d d d	 t d
 d ƒ| j  d d d t d	 t d d d
 d ƒ| j  d d d t d	 t d d d
 d ƒd  S(   Nt   app_namet
   model_namet   nargst   *s   -ss   --search-fieldt   actionR2   t   defaultt   helpsN   Fields named like this will be added to `search_fields` [default: %(default)s]s   -ds   --date-hierarchysN   A field named like this will be set as `date_hierarchy` [default: %(default)s]s   -ps   --prepopulated-fieldss‡   These fields will be prepopulated by the other field.The field names can be specified like `spam=eggA,eggB,eggC` [default: %(default)s]s   -ls   --list-filter-thresholdRf   t   metavarR   ss   If a foreign key has less than LIST_FILTER_THRESHOLD items it will be added to `list_filter` [default: %(default)s]s   -rs   --raw-id-thresholdR	   sn   If a foreign key has more than RAW_ID_THRESHOLD items it will be added to `list_filter` [default: %(default)s](   t   add_argumentR   R   R   t   intR   R	   (   R   t   parser(    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyt   add_arguments0  s,    c   	      O   sü   | d } y t  j | ƒ } Wn t k
 rž |  j j d ƒ |  j j d ƒ g  t  j ƒ  D] } | j ^ qZ } x( t | ƒ D] } |  j j d | ƒ q| Wd  SXg  } x. | d D]" } | j t	 j
 | t	 j ƒ ƒ q° W|  j j t | | |  j ƒ  ƒ d  S(   NR†   s6   This command requires an existing app name as arguments   Available apps:s       %sR‡   (   R    t   get_app_configt   LookupErrort   stderrt   writet   get_app_configst   labelR|   R2   t   ret   compilet
   IGNORECASEt   stdoutR!   R   (	   R   t   argsR$   R†   R6   t
   app_labelsR—   R#   t   arg(    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyt   handleN  s    
" (   R   R   RŒ   R   t   can_import_settingsR‘   R   RŸ   (    (    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyR…   +  s   	(   R   R   (   R   R   R   (   s	   slug=name(#   R   R˜   R   t   django.appsR    t   django.confR   t   django.core.management.baseR   R   t	   django.dbR   t"   django_extensions.management.utilsR   RH   R   R   R   R	   t	   DateFieldt   DateTimeFieldR\   t   BooleanFieldR
   R   R   R   R   R   R   t   objectR   R!   R'   R…   (    (    (    sW   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/admin_generator.pyt   <module>   sF   		 		  									)¬