U SW@sdZddlTddlmZddlmZddlmZddlmZddlmZe Z iZ Gdd d eZGd d d eeeZd 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_metaclasscseZdZfddZZS)rcs,tt|||||}|dkr(t||S)N)_metaclass_helper_r)superr__new__ all_modelsadd)clsnamebasesattrs __class__@/opt/alt/python38/lib64/python3.8/site-packages/playhouse/gfk.pyr 's zBaseModel.__new__)__name__ __module__ __qualname__r __classcell__rrrrr&src@s eZdZdS)rN)rrrrrrrr-srcCs4|tkr*tD]}|jj|kr |t|<q*q t|SN) table_cacher _metadb_tableget)tbl_namemodelrrr get_model0s  r c@s@eZdZdZddZeddZeddZdd Zd d Z d S) BoundGFKField model_class gfk_fieldcCs||_||_dSrr")selfr#r$rrr__init__;szBoundGFKField.__init__cCsB|jjj}t|jj|jjf}|D]\}}|t|s"dSq"dSNTF)r#rindexessetr$model_type_fieldmodel_id_field)r%r(fieldsZindexed_columns is_uniquerrrunique?s   zBoundGFKField.uniquecCs>|jjj}t|tr:t|jj|jjf}|t|j s:dSdSr') r#r primary_key isinstance CompositeKeyr)r$r*r+ field_names)r%pkr,rrrr/Is  zBoundGFKField.primary_keycCs<|jj}|j|jj}|j|jj}||jjk||k@Sr)r#rr,r$r*r+r _get_pk_value)r%othermeta type_fieldid_fieldrrr__eq__Ss  zBoundGFKField.__eq__cCs@t|}|jj|jj}|jj|jj}||jjk||k@Sr)typerr,r$r*r+rr4)r%r5Z other_clsr7r8rrr__ne__[s  zBoundGFKField.__ne__N) rrr __slots__r&propertyr.r/r9r;rrrrr!8s  r!c@s0eZdZd ddZddZd dd Zd d ZdS)GFKField object_type object_idcCs$||_||_d|j|jf|_dS)N.)r*r+joinatt_name)r%r*r+rrrr&eszGFKField.__init__cCsh|j}||jrd||jrd||j}t|}|s@td|||jj ||jk}|SdS)Nz-Model for table "%s" not found in GFK lookup.) _datarr*r+r AttributeErrorselectwhererr/)r%instancedatarr#queryrrrget_objks zGFKField.get_objNcCsB|r8|j|jkr*||}|r*||j|j<|j|jSt||Sr)rC _obj_cacherKrr!)r%rH instance_typeZrel_objrrr__get__ws   zGFKField.__get__cCs0||j|j<|jj|j|j<||j|j<dSr)rLrCrrrDr*r4r+)r%rHvaluerrr__set__s zGFKField.__set__)r?r@)N)rrrr&rKrNrPrrrrr>ds   r>c@s(eZdZd ddZd ddZdd ZdS) ReverseGFKr?r@cCs&||_|jj||_|jj||_dSr)r#rr,r*r+)r%rr*r+rrrr&szReverseGFK.__init__NcCsJ|r,|j|j|jjk|j|k@S|j|j|jjkSdSr)r#rFrGr*rrr+r4)r%rHrMrrrrNs     zReverseGFK.__get__cs|jj}|}t|trL|jjkrLtjj|j|i |j  nVt t fdd|r|D],}t|jj|t|jj||qfn td|dS)Ncs t|jSr)r0r#)ir%rrz$ReverseGFK.__set__..z&ReverseGFK field unable to handle "%s")rrr4r0rr#rr*r+rG_whereexecuteallmapsetattrrsave ValueError)r%rHrOZmtvZmivobjrrSrrPs&    zReverseGFK.__set__)r?r@)N)rrrr&rNrPrrrrrQs   rQN)__doc__peeweerZ _BaseModelrZ_Modelrrrr)r rr objectr!r>rQrrrrs     ,"