Your IP : 216.73.216.213


Current Path : /opt/alt/python35/lib64/python3.5/site-packages/playhouse/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib64/python3.5/site-packages/playhouse/__pycache__/gfk.cpython-35.opt-1.pyc



R6�W��@s�dZddlTddlmZddlmZddlmZddlmZddlmZe	�Z
iZGdd	�d	e�ZGd
d�deee��Zdd
�ZGdd�de
�ZGdd�de
�ZGdd�de
�ZdS)a
Provide a "Generic ForeignKey", similar to Django.  A "GFK" is composed of two
columns: an object ID and an object type identifier.  The object types are
collected in a global registry (all_models), so all you need to do is subclass
``gfk.Model`` and your model will be added to the registry.

Example:

class Tag(Model):
    tag = CharField()
    object_type = CharField(null=True)
    object_id = IntegerField(null=True)
    object = GFKField('object_type', 'object_id')

class Blog(Model):
    tags = ReverseGFK(Tag, 'object_type', 'object_id')

class Photo(Model):
    tags = ReverseGFK(Tag, 'object_type', 'object_id')

tag.object -> a blog or photo
blog.tags -> select query of tags for ``blog`` instance
Blog.tags -> select query of all tags for Blog instances
�)�*)�	BaseModel)�Model)�SelectQuery)�UpdateQuery)�with_metaclasscs"eZdZ�fdd�Z�S)rcs>tt|�j||||�}|dkr:tj|�|S)N�_metaclass_helper_r)rr)�superr�__new__�
all_models�add)�cls�name�bases�attrs)�	__class__��@/opt/alt/python35/lib64/python3.5/site-packages/playhouse/gfk.pyr
's!
zBaseModel.__new__)�__name__�
__module__�__qualname__r
rr)rrr&src@seZdZdS)rN)rrrrrrrr-srcCsG|tkr:x+tD]#}|jj|kr|t|<PqWtj|�S)N)�table_cacher�_meta�db_table�get)�tbl_name�modelrrr�	get_model0s

rc@s^eZdZdZdd�Zedd��Zedd��Zd	d
�Zdd�Z	d
S)�
BoundGFKField�model_class�	gfk_fieldcCs||_||_dS)N)rr )�selfrr rrr�__init__;s	zBoundGFKField.__init__cCs\|jjj}t|jj|jjf�}x(|D] \}}|t|�s4dSq4WdS)NTF)rr�indexes�setr �model_type_field�model_id_field)r!r#�fieldsZindexed_columnsZ	is_uniquerrr�unique?szBoundGFKField.uniquecCsW|jjj}t|t�rSt|jj|jjf�}|t|j	�sSdSdS)NTF)
rr�primary_key�
isinstanceZCompositeKeyr$r r%r&�field_names)r!Zpkr'rrrr)IszBoundGFKField.primary_keycCsR|jj}|j|jj}|j|jj}||jjk||j�k@S)N)rrr'r r%r&r�
_get_pk_value)r!�other�meta�
type_field�id_fieldrrr�__eq__Ss
zBoundGFKField.__eq__cCsXt|�}|jj|jj}|jj|jj}||jjk||j�k@S)N)�typerr'r r%r&rr,)r!r-Z	other_clsr/r0rrr�__ne__[s
zBoundGFKField.__ne__N)rr )
rrr�	__slots__r"�propertyr(r)r1r3rrrrr8s

rc@sIeZdZdddd�Zdd�Zddd	�Zd
d�ZdS)�GFKField�object_type�	object_idcCs4||_||_dj|j|jf�|_dS)N�.)r%r&�join�att_name)r!r%r&rrrr"es		zGFKField.__init__cCs�|j}|j|j�r�|j|j�r�||j}t|�}|s\td|��|j�j|jj	||jk�}|j�SdS)Nz-Model for table "%s" not found in GFK lookup.)
�_datarr%r&r�AttributeError�select�whererr))r!�instance�datarrZqueryrrr�get_objks	$

zGFKField.get_objNcCs]|rP|j|jkr=|j|�}|r=||j|j<|jj|j�St||�S)N)r;�
_obj_cacherBrr)r!r@�
instance_typeZrel_objrrr�__get__wszGFKField.__get__cCs@||j|j<|jj|j|j<|j�|j|j<dS)N)rCr;rrr<r%r,r&)r!r@�valuerrr�__set__�szGFKField.__set__)rrrr"rBrErGrrrrr6ds
	r6c@s=eZdZdddd�Zddd�Zdd	�ZdS)
�
ReverseGFKr7r8cCs3||_|jj||_|jj||_dS)N)rrr'r%r&)r!rr%r&rrrr"�s	zReverseGFK.__init__NcCsg|r>|jj�j|j|jjk|j|j�k@�S|jj�j|j|jjk�SdS)N)rr>r?r%rrr&r,)r!r@rDrrrrE�szReverseGFK.__get__cs�|jj}|j�}t|t�rp|j�jkrpt�j�j|�j|i�j	|j
�j�n{tt
�fdd�|��r�xW|D]<}t|�jj|�t|�jj|�|j�q�Wntd|��dS)Ncst|�j�S)N)r*r)�i)r!rr�<lambda>�sz$ReverseGFK.__set__.<locals>.<lambda>z&ReverseGFK field unable to handle "%s")rrr,r*rrrr%r&r?Z_where�execute�all�map�setattrrZsave�
ValueError)r!r@rFZmtvZmiv�objr)r!rrG�s		!
zReverseGFK.__set__)rrrr"rErGrrrrrH�srHN)�__doc__ZpeeweerZ
_BaseModelrZ_Modelrrrr$rrr�objectrr6rHrrrr�<module>s
	,"