Start to rework to actually make use of the db_model field of Tags and Attributes. Reapplying this will require a migration down the line.

This commit is contained in:
Griatch 2016-11-03 21:38:28 +01:00
parent ae3af20cd7
commit 82866a7dda
3 changed files with 20 additions and 7 deletions

View file

@ -216,6 +216,7 @@ class AttributeHandler(object):
def _fullcache(self):
"Cache all attributes of this object"
query = {"%s__id" % self._model : self._objid,
"attribute__db_model" : self._model,
"attribute__db_attrtype" : self._attrtype}
attrs = [conn.attribute for conn in getattr(self.obj, self._m2m_fieldname).through.objects.filter(**query)]
self._cache = dict(("%s-%s" % (to_str(attr.db_key).lower(),
@ -259,6 +260,7 @@ class AttributeHandler(object):
return [attr] # return cached entity
else:
query = {"%s__id" % self._model : self._objid,
"attribute__db_model" : self._model,
"attribute__db_attrtype" : self._attrtype,
"attribute__db_key__iexact" : key.lower(),
"attribute__db_category__iexact" : category.lower() if category else None}

View file

@ -179,7 +179,7 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
# Tag manager methods
def get_tag(self, key=None, category=None, obj=None, tagtype=None, global_search=False):
def get_tag(self, key=None, category=None, obj=None, tagtype=None, dbmodel="objectdb", global_search=False):
"""
Return Tag objects by key, by category, by object (it is
stored on) or with a combination of those criteria.
@ -192,6 +192,9 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
search for is.
tagtype (str, optional): One of None (normal tags),
"alias" or "permission"
dbobj (str, optional): A natural_key for which type of
entity this tag attaches to. Example, "objectdb" or
"scriptdb".
global_search (bool, optional): Include all possible tags,
not just tags on this object
@ -204,7 +207,7 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
from evennia.typeclasses.models import Tag as _Tag
if global_search:
# search all tags using the Tag model
query = [("db_tagtype", tagtype)]
query = [("db_tagtype", tagtype), ("db_model", dbmodel)]
if obj:
query.append(("id", obj.id))
if key:
@ -254,7 +257,7 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
return self.get_tag(key=key, category=category, obj=obj, tagtype="alias")
@returns_typeclass_list
def get_by_tag(self, key=None, category=None, tagtype=None):
def get_by_tag(self, key=None, category=None, tagtype=None, dbmodel="objectdb"):
"""
Return objects having tags with a given key or category or
combination of the two.
@ -265,14 +268,17 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
tagtype (str or None, optional): 'type' of Tag, by default
this is either `None` (a normal Tag), `alias` or
`permission`.
dbmodel (str or None, optionsl): The naturalkey of the entity this Tag
attaches to. A string like "objectdb", "scriptdb" etc.
Returns:
objects (list): Objects with matching tag.
"""
query = [("db_tags__db_tagtype", tagtype)]
query = [("db_tags__db_tagtype", tagtype), ("db_tags__db_model", dbmodel)]
if key:
query.append(("db_tags__db_key", key.lower()))
if category:
query.append(("db_tags__db_category", category.lower()))
print "get_by_tag query:", query
return self.filter(**dict(query))
def get_by_permission(self, key=None, category=None):
@ -301,11 +307,11 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
"""
return self.get_by_tag(key=key, category=category, tagtype="alias")
def create_tag(self, key=None, category=None, data=None, tagtype=None):
def create_tag(self, key=None, category=None, data=None, tagtype=None, dbmodel="objectdb"):
"""
Create a new Tag of the base type associated with this
object. This makes sure to create case-insensitive tags.
If the exact same tag configuration (key+category+tagtype)
If the exact same tag configuration (key+category+tagtype+dbmodel)
exists on the model, a new tag will not be created, but an old
one returned.
@ -317,6 +323,8 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
tagtype (str or None, optional): 'type' of Tag, by default
this is either `None` (a normal Tag), `alias` or
`permission`.
dbmodel (str, optional): Which model type this Tag attaches to.
This is a natural_key like "dbobject" or "dbscript".
Notes:
The `data` field is not part of the uniqueness of the tag:
@ -345,6 +353,7 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
db_key=key.strip().lower() if key is not None else None,
db_category=category.strip().lower() if category and key is not None else None,
db_data=data,
db_model=dbmodel,
db_tagtype=tagtype.strip().lower() if tagtype is not None else None)
tag.save()
return make_iter(tag)[0]

View file

@ -105,6 +105,7 @@ class TagHandler(object):
def _fullcache(self):
"Cache all tags of this object"
query = {"%s__id" % self._model : self._objid,
"tag__db_model" : self._model,
"tag__db_tagtype" : self._tagtype}
tags = [conn.tag for conn in getattr(self.obj, self._m2m_fieldname).through.objects.filter(**query)]
self._cache = dict(("%s-%s" % (to_str(tag.db_key).lower(),
@ -148,6 +149,7 @@ class TagHandler(object):
return [tag] # return cached entity
else:
query = {"%s__id" % self._model : self._objid,
"tag__db_model" : self._model,
"tag__db_tagtype" : self._tagtype,
"tag__db_key__iexact" : key.lower(),
"tag__db_category__iexact" : category.lower() if category else None}
@ -254,7 +256,7 @@ class TagHandler(object):
# will overload data on an existing tag since that is not
# considered part of making the tag unique)
tagobj = self.obj.__class__.objects.create_tag(key=tagstr, category=category, data=data,
tagtype=self._tagtype)
tagtype=self._tagtype, dbmodel=self._model)
getattr(self.obj, self._m2m_fieldname).add(tagobj)
self._setcache(tagstr, category, tagobj)