
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
U
    SW                     @   sx   d Z ddlmZ ddlZddlT edZG dd deZG dd	 d	e	Z
zddlZe
 jZW n ek
rr   Y nX dS )
zE
Simple translation of Django model classes to peewee model classes.
    )partialN)*zpeewee.playhouse.djpeeweec                   @   s   e Zd Zdd ZdS )AttrDictc                 C   s   | | S N )selfattrr   r   E/opt/alt/python38/lib64/python3.8/site-packages/playhouse/djpeewee.py__getattr__   s    zAttrDict.__getattr__N)__name__
__module____qualname__r
   r   r   r   r	   r      s   r   c                   @   s6   e Zd Zdd Zdd Zdd Zdd	d
Zdd ZdS )DjangoTranslatorc                 C   s   |   | _d S r   )get_django_field_map
_field_map)r   r   r   r	   __init__   s    zDjangoTranslator.__init__c                 C   s   ddl m} |jtf|jtf|jtf|jtf|jtf|jtf|j	t	f|j
tf|jtf|jtf|jttddf|jtf|jtf|jjtfgS )Nr   fieldsTnull)django.db.modelsr   Z	AutoFieldPrimaryKeyFieldBigIntegerFieldBooleanField	CharFieldDateTimeField	DateFieldDecimalFieldZFilePathField
FloatFieldIntegerFieldZNullBooleanFieldr   	TextField	TimeFieldrelatedZ
ForeignKeyForeignKeyField)r   djfr   r   r	   r      s     
z%DjangoTranslator.get_django_field_mapc                 C   s*   d }| j D ]\}}t||r
|} q&q
|S r   )r   
isinstance)r   field	convertedZdjango_fieldZpeewee_fieldr   r   r	   convert_field'   s    
zDjangoTranslator.convert_fieldNFc                 C   sT  |r||krd S |d krd}ddl m} |j}||jr>d S d ||j< i }dd }	t|j|	dD ]}
| |
}|tkrP|dkr<|
jj	}|jj}||krd ||< | j
|||d ||d || d krtd	|
j| t|
jd
||
j< nL|
jjp|
 }|dr d||
j|df }t|| ||
jd||
j< nt|
jd
||
j< qb|rb||
jd
||
j< qbt|jtf|}|j|j_d|jj_|||j< |rzdd | D }W n( tk
r   dd | D }Y nX |D ]4\}}|jj|krq| j
|||d ||d q|jD ]2}t||jj s4q| j
|jj!||||d qd S )Nr   r   c                 S   s   | j r
dpd| fS )N   r   r   )r&   r   r   r	   <lambda>C       z3DjangoTranslator._translate_model.<locals>.<lambda>)keyr*   	max_depthbackrefsexcludezCycle detected: %s: %s)	db_column+z
__%s:%s:%s)related_namer2   z%sc                 S   s   g | ]}||j fqS r   )model.0fr   r   r	   
<listcomp>{   s     z5DjangoTranslator._translate_model.<locals>.<listcomp>c                 S   s0   g | ](}|j s|jr|jr|js||jjfqS r   )Zone_to_manyZ
one_to_oneZauto_createdZconcreter&   r5   r6   r   r   r	   r9   ~   s      )"r   r   _metagetZobject_namesortedr(   r#   relto_translate_modelloggerwarnnamer   columnr4   Zrelated_query_nameendswithstripr2   typeModeldb_tabledatabaseinterpolationZget_all_related_objectsAttributeErrorZ
get_fieldsmany_to_manyr%   r"   ZManyToManyFieldZthrough)r   r5   mappingr/   r0   r1   r$   optionsattrsZ	field_keyZmodel_fieldr'   Zrelated_modelZ
model_namer4   klassZall_relatedZrel_objrL   r   r   r	   r?   /   s    



 





z!DjangoTranslator._translate_modelc           	      O   s   t  }|dd}|dd}|dd}|dd}|sH|rHtdn|sRd	}n|rb|dkrbd
}|D ]}| j|||||d qf|S )av  
        Generate a group of peewee models analagous to the provided Django
        models for the purposes of creating queries.

        :param model: A Django model class.
        :param options: A dictionary of options, see note below.
        :returns: A dictionary mapping model names to peewee model classes.
        :rtype: dict

        Recognized options:
            `recurse`: Follow foreign keys (default: True)
            `max_depth`: Max depth to recurse (default: None, unlimited)
            `backrefs`: Follow backrefs (default: False)
            `exclude`: A list of models to exclude

        Example::

            # Map Django models to peewee models. Foreign keys and M2M will be
            # traversed as well.
            peewee = translate(Account)

            # Generate query using peewee.
            PUser = peewee['User']
            PAccount = peewee['Account']
            query = (PUser
                     .select()
                     .join(PAccount)
                     .where(PAccount.acct_type == 'foo'))

            # Django raw query.
            users = User.objects.raw(*query.sql())
        recurseTr/   Nr0   Fr1   z9Error, you cannot specify a max_depth when recurse=False.r   r)   r.   )r   r;   
ValueErrorr?   )	r   modelsrN   rM   rQ   r/   r0   r1   r5   r   r   r	   translate_models   s(    !
z!DjangoTranslator.translate_models)NFN)r   r   r   r   r   r(   r?   rT   r   r   r   r	   r      s      
ir   )__doc__	functoolsr   loggingpeewee	getLoggerr@   dictr   objectr   ZdjangorT   	translateImportErrorr   r   r   r	   <module>   s   
 A