Added the same extra fields to Attributes so as to not have to pollute category for Nicks in any way.
This commit is contained in:
parent
17123bf7ed
commit
641633a0d4
1 changed files with 36 additions and 33 deletions
|
|
@ -106,6 +106,10 @@ class Attribute(SharedMemoryModel):
|
||||||
db_category = models.CharField('category', max_length=128, db_index=True, blank=True, null=True)
|
db_category = models.CharField('category', max_length=128, db_index=True, blank=True, null=True)
|
||||||
# Lock storage
|
# Lock storage
|
||||||
db_lock_storage = models.TextField('locks', blank=True)
|
db_lock_storage = models.TextField('locks', blank=True)
|
||||||
|
# Which model of object this Attribute is attached to (A natural key like objects.dbobject)
|
||||||
|
db_model = models.CharField('model', max_length=32, db_index=True, blank=True, null=True)
|
||||||
|
# subclass of Attribute (None or nick)
|
||||||
|
db_attrype = models.CharField('attrtype', max_length=16, db_index=True, blank=True, null=True)
|
||||||
# time stamp
|
# time stamp
|
||||||
db_date_created = models.DateTimeField('date_created', editable=False, auto_now_add=True)
|
db_date_created = models.DateTimeField('date_created', editable=False, auto_now_add=True)
|
||||||
|
|
||||||
|
|
@ -204,18 +208,21 @@ class AttributeHandler(object):
|
||||||
_attrcreate = "attrcreate"
|
_attrcreate = "attrcreate"
|
||||||
_attredit = "attredit"
|
_attredit = "attredit"
|
||||||
_attrread = "attrread"
|
_attrread = "attrread"
|
||||||
|
_attrtype = None
|
||||||
|
|
||||||
def __init__(self, obj):
|
def __init__(self, obj):
|
||||||
"Initialize handler"
|
"Initialize handler"
|
||||||
self.obj = obj
|
self.obj = obj
|
||||||
|
self._model = "%s.%s" % ContentType.objects.get_for_model(obj).natural_key()
|
||||||
self._cache = None
|
self._cache = None
|
||||||
|
|
||||||
def _recache(self):
|
def _recache(self):
|
||||||
self._cache = dict(("%s_%s" % (to_str(attr.db_key).lower(),
|
self._cache = dict(("%s-%s" % (to_str(attr.db_key).lower(),
|
||||||
to_str(attr.db_category,
|
to_str(attr.db_category,
|
||||||
force_string=True).lower()), attr)
|
force_string=True).lower()), attr)
|
||||||
for attr in _GA(self.obj, self._m2m_fieldname).all())
|
for attr in _GA(self.obj, self._m2m_fieldname).filter(
|
||||||
set_attr_cache(self.obj, self._cache) # currently only for testing
|
db_model=self._model, db_attrtype=self._attrtype))
|
||||||
|
#set_attr_cache(self.obj, self._cache) # currently only for testing
|
||||||
|
|
||||||
def has(self, key, category=None):
|
def has(self, key, category=None):
|
||||||
"""
|
"""
|
||||||
|
|
@ -226,9 +233,10 @@ class AttributeHandler(object):
|
||||||
"""
|
"""
|
||||||
if self._cache is None or not _TYPECLASS_AGGRESSIVE_CACHE:
|
if self._cache is None or not _TYPECLASS_AGGRESSIVE_CACHE:
|
||||||
self._recache()
|
self._recache()
|
||||||
catkey = to_str(category, force_string=True).lower()
|
key = [key.strip().lower() if key is not None else None for key in make_iter(key)]
|
||||||
searchkeys = ["%s_%s" % (k.lower(), catkey) for k in make_iter(key)]
|
category = category.strip().lower() if category is not None else None
|
||||||
ret = [self._cache[skey] for skey in searchkeys if skey in self._cache]
|
searchkeys = ["%s-%s" % (k, category) for k in make_iter(key)]
|
||||||
|
ret = [self._cache.get(skey) for skey in searchkeys]
|
||||||
return ret[0] if len(ret) == 1 else ret
|
return ret[0] if len(ret) == 1 else ret
|
||||||
|
|
||||||
def get(self, key=None, category=None, default=None, return_obj=False,
|
def get(self, key=None, category=None, default=None, return_obj=False,
|
||||||
|
|
@ -251,14 +259,15 @@ class AttributeHandler(object):
|
||||||
if self._cache is None or not _TYPECLASS_AGGRESSIVE_CACHE:
|
if self._cache is None or not _TYPECLASS_AGGRESSIVE_CACHE:
|
||||||
self._recache()
|
self._recache()
|
||||||
ret = []
|
ret = []
|
||||||
catkey = to_str(category, force_string=True).lower()
|
key = [key.strip().lower() if key is not None else None for key in make_iter(key)]
|
||||||
|
category = category.strip().lower() if category is not None else None
|
||||||
if not key:
|
if not key:
|
||||||
# return all with matching category (or no category)
|
# return all with matching category (or no category)
|
||||||
catkey = "_%s" % catkey
|
catkey = "-%s" % category
|
||||||
ret = [attr for key, attr in self._cache.items() if key.endswith(catkey)]
|
ret = [attr for key, attr in self._cache.items() if key.endswith(catkey)]
|
||||||
else:
|
else:
|
||||||
for keystr in ("%s_%s" % (k.lower(), catkey) for k in make_iter(key)):
|
for searchkey in ("%s-%s" % (k, category) for k in key):
|
||||||
attr_obj = self._cache.get(keystr)
|
attr_obj = self._cache.get(searchkey)
|
||||||
if attr_obj:
|
if attr_obj:
|
||||||
ret.append(attr_obj)
|
ret.append(attr_obj)
|
||||||
else:
|
else:
|
||||||
|
|
@ -293,11 +302,14 @@ class AttributeHandler(object):
|
||||||
return
|
return
|
||||||
if self._cache is None:
|
if self._cache is None:
|
||||||
self._recache()
|
self._recache()
|
||||||
cachekey = "%s_%s" % (key.lower(), to_str(category, force_string=True).lower())
|
key = key.strip().lower() if key is not None else None
|
||||||
|
category = category.strip().lower() if category is not None else None
|
||||||
|
cachekey = "%s-%s" % (key, category)
|
||||||
attr_obj = self._cache.get(cachekey)
|
attr_obj = self._cache.get(cachekey)
|
||||||
if not attr_obj:
|
if not attr_obj:
|
||||||
# no old attr available; create new.
|
# no old attr available; create new.
|
||||||
attr_obj = Attribute(db_key=key, db_category=category)
|
attr_obj = Attribute(db_key=key, db_category=category,
|
||||||
|
db_model=self._model, db_attrtype=self._attrtype)
|
||||||
attr_obj.save() # important
|
attr_obj.save() # important
|
||||||
_GA(self.obj, self._m2m_fieldname).add(attr_obj)
|
_GA(self.obj, self._m2m_fieldname).add(attr_obj)
|
||||||
self._cache[cachekey] = attr_obj
|
self._cache[cachekey] = attr_obj
|
||||||
|
|
@ -323,13 +335,13 @@ class AttributeHandler(object):
|
||||||
"""
|
"""
|
||||||
if self._cache is None or not _TYPECLASS_AGGRESSIVE_CACHE:
|
if self._cache is None or not _TYPECLASS_AGGRESSIVE_CACHE:
|
||||||
self._recache()
|
self._recache()
|
||||||
catkey = to_str(category, force_string=True).lower()
|
key = [key.strip().lower() if key is not None else None for key in make_iter(key)]
|
||||||
for keystr in ("%s_%s" % (k.lower(), catkey) for k in make_iter(key)):
|
category = category.strip().lower() if category is not None else None
|
||||||
attr_obj = self._cache.get(keystr)
|
for searchstr in ("%s-%s" % (k, category) for k in key):
|
||||||
|
attr_obj = self._cache.get(searchstr)
|
||||||
if attr_obj:
|
if attr_obj:
|
||||||
if accessing_obj and not attr_obj.access(accessing_obj,
|
if not (accessing_obj and not attr_obj.access(accessing_obj,
|
||||||
self._attredit, default=default_access):
|
self._attredit, default=default_access)):
|
||||||
continue
|
|
||||||
attr_obj.delete()
|
attr_obj.delete()
|
||||||
elif not attr_obj and raise_exception:
|
elif not attr_obj and raise_exception:
|
||||||
raise AttributeError
|
raise AttributeError
|
||||||
|
|
@ -357,7 +369,7 @@ class AttributeHandler(object):
|
||||||
"""
|
"""
|
||||||
if self._cache is None or not _TYPECLASS_AGGRESSIVE_CACHE:
|
if self._cache is None or not _TYPECLASS_AGGRESSIVE_CACHE:
|
||||||
self._recache()
|
self._recache()
|
||||||
catkey = "_%s" % to_str(category, force_string=True).lower()
|
catkey = "-%s" % category.strip().lower() if category is not None else None
|
||||||
return [attr for key, attr in self._cache.items() if key.endswith(catkey)]
|
return [attr for key, attr in self._cache.items() if key.endswith(catkey)]
|
||||||
|
|
||||||
class NickHandler(AttributeHandler):
|
class NickHandler(AttributeHandler):
|
||||||
|
|
@ -368,32 +380,23 @@ class NickHandler(AttributeHandler):
|
||||||
Nicks are stored as Attributes
|
Nicks are stored as Attributes
|
||||||
with categories nick_<nicktype>
|
with categories nick_<nicktype>
|
||||||
"""
|
"""
|
||||||
|
_attrtype = "nick"
|
||||||
|
|
||||||
def has(self, key, category="inputline"):
|
def has(self, key, category="inputline"):
|
||||||
category = "nick_%s" % category
|
|
||||||
return super(NickHandler, self).has(key, category=category)
|
return super(NickHandler, self).has(key, category=category)
|
||||||
|
|
||||||
def get(self, key=None, category="inputline", **kwargs):
|
def get(self, key=None, category="inputline", **kwargs):
|
||||||
"Get the replacement value matching the given key and category"
|
"Get the replacement value matching the given key and category"
|
||||||
category = "nick_%s" % category
|
|
||||||
return super(NickHandler, self).get(key=key, category=category, strattr=True, **kwargs)
|
return super(NickHandler, self).get(key=key, category=category, strattr=True, **kwargs)
|
||||||
|
|
||||||
def add(self, key, replacement, category="inputline", **kwargs):
|
def add(self, key, replacement, category="inputline", **kwargs):
|
||||||
"Add a new nick"
|
"Add a new nick"
|
||||||
category = "nick_%s" % category
|
|
||||||
super(NickHandler, self).add(key, replacement, category=category, strattr=True, **kwargs)
|
super(NickHandler, self).add(key, replacement, category=category, strattr=True, **kwargs)
|
||||||
|
|
||||||
def remove(self, key, category="inputline", **kwargs):
|
def remove(self, key, category="inputline", **kwargs):
|
||||||
"Remove Nick with matching category"
|
"Remove Nick with matching category"
|
||||||
category = "nick_%s" % category
|
|
||||||
super(NickHandler, self).remove(key, category=category, **kwargs)
|
super(NickHandler, self).remove(key, category=category, **kwargs)
|
||||||
|
|
||||||
def all(self, category=None):
|
|
||||||
"Return all attributes with nick_* category"
|
|
||||||
if category:
|
|
||||||
category = "nick_%s" % category
|
|
||||||
return super(NickHandler, self).all(category=category)
|
|
||||||
return _GA(self.obj, self._m2m_fieldname).filter(db_category__startswith="nick_")
|
|
||||||
|
|
||||||
|
|
||||||
class NAttributeHandler(object):
|
class NAttributeHandler(object):
|
||||||
"""
|
"""
|
||||||
|
|
@ -507,8 +510,8 @@ class TagHandler(object):
|
||||||
|
|
||||||
def _recache(self):
|
def _recache(self):
|
||||||
"Update cache from database field"
|
"Update cache from database field"
|
||||||
self._cache = dict(("%s-%s" % (p.db_key, p.db_category), p)
|
self._cache = dict(("%s-%s" % (tag.db_key, tag.db_category), tag)
|
||||||
for p in _GA(self.obj, self._m2m_fieldname).filter(
|
for tag in _GA(self.obj, self._m2m_fieldname).filter(
|
||||||
db_model=self._model, db_tagtype=self._tagtype))
|
db_model=self._model, db_tagtype=self._tagtype))
|
||||||
|
|
||||||
def add(self, tag, category=None, data=None):
|
def add(self, tag, category=None, data=None):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue