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



R6W@sdZddlTddlmZddlmZddlmZddlmZddlmZe	Z
iZGdd	d	eZGd
ddeeeZdd
ZGddde
ZGddde
ZGddde
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"eZdZfddZS)rcs>tt|j||||}|dkr:tj||S)N_metaclass_helper_r)rr)superr__new__
all_modelsadd)clsnamebasesZattrs)	__class__/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_metadb_tableget)tbl_namemodelrrr	get_model0s

rc@s^eZdZdZddZeddZeddZd	d
ZddZ	d
S)
BoundGFKFieldmodel_class	gfk_fieldcCs||_||_dS)N)rr)selfrrrrr__init__;s	zBoundGFKField.__init__cCs\|jjj}t|jj|jjf}x(|D] \}}|t|s4dSq4WdS)NTF)rrindexessetrmodel_type_fieldmodel_id_field)r r"fieldsZindexed_columnsZ	is_uniquerrrunique?szBoundGFKField.uniquecCsW|jjj}t|trSt|jj|jjf}|t|j	sSdSdS)NTF)
rrprimary_key
isinstanceZCompositeKeyr#rr$r%field_names)r Zpkr&rrrr(IszBoundGFKField.primary_keycCsR|jj}|j|jj}|j|jj}||jjk||jk@S)N)rrr&rr$r%r
_get_pk_value)r othermeta
type_fieldid_fieldrrr__eq__Ss
zBoundGFKField.__eq__cCsXt|}|jj|jj}|jj|jj}||jjk||jk@S)N)typerr&rr$r%rr+)r r,Z	other_clsr.r/rrr__ne__[s
zBoundGFKField.__ne__N)rr)
rrr	__slots__r!propertyr'r(r0r2rrrrr8s

rc@sIeZdZddddZddZddd	Zd
dZdS)GFKFieldobject_type	object_idcCs4||_||_dj|j|jf|_dS)N.)r$r%joinatt_name)r r$r%rrrr!es		zGFKField.__init__cCs|j}|j|jr|j|jr||j}t|}|s\td||jj|jj	||jk}|jSdS)Nz-Model for table "%s" not found in GFK lookup.)
_datarr$r%rAttributeErrorselectwhererr()r instancedatarrZqueryrrrget_objks	$

zGFKField.get_objNcCs]|rP|j|jkr=|j|}|r=||j|j<|jj|jSt||S)N)r:
_obj_cacherArr)r r?
instance_typeZrel_objrrr__get__wszGFKField.__get__cCs@||j|j<|jj|j|j<|j|j|j<dS)N)rBr:rrr;r$r+r%)r r?valuerrr__set__szGFKField.__set__)rrrr!rArDrFrrrrr5ds
	r5c@s=eZdZddddZdddZdd	ZdS)

ReverseGFKr6r7cCs3||_|jj||_|jj||_dS)N)rrr&r$r%)r rr$r%rrrr!s	zReverseGFK.__init__NcCsg|r>|jjj|j|jjk|j|jk@S|jjj|j|jjkSdS)N)rr=r>r$rrr%r+)r r?rCrrrrDszReverseGFK.__get__cs|jj}|j}t|trp|jjkrptjj|j|ij	|j
jn{tt
fdd|rxW|D]<}t|jj|t|jj||jqWntd|dS)Ncst|jS)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_whereZexecuteallmapsetattrrsave
ValueError)r r?rEZmtvZmivobjr)r rrFs		!
zReverseGFK.__set__)rrrr!rDrFrrrrrGsrGN)__doc__ZpeeweerZ
_BaseModelrZ_Modelrrrr#rrrobjectrr5rGrrrr<module>s
	,"
Back to Directory File Manager