mirror of
https://github.com/evennia/evennia.git
synced 2026-03-28 02:36:32 +01:00
Merge from DBenoy's branch.
This commit is contained in:
commit
b0bf60cda4
138 changed files with 5295 additions and 1803 deletions
|
|
@ -5,11 +5,13 @@ all Attributes and TypedObjects).
|
|||
"""
|
||||
from functools import update_wrapper
|
||||
from django.db import models
|
||||
from django.db.models import Q
|
||||
from src.utils import idmapper
|
||||
from src.utils.utils import make_iter
|
||||
from src.utils.dbserialize import to_pickle
|
||||
|
||||
__all__ = ("AttributeManager", "TypedObjectManager")
|
||||
_GA = object.__getattribute__
|
||||
|
||||
# Managers
|
||||
|
||||
|
|
@ -48,28 +50,168 @@ class AttributeManager(models.Manager):
|
|||
def exists(self,*args, **kwargs):
|
||||
return super(AttributeManager, self).exists(*args, **kwargs)
|
||||
|
||||
def attr_namesearch(self, searchstr, obj, exact_match=True):
|
||||
def get_attrs_on_obj(self, searchstr, obj, exact_match=True):
|
||||
"""
|
||||
Searches the object's attributes for name matches.
|
||||
Searches the object's attributes for attribute key matches.
|
||||
|
||||
searchstr: (str) A string to search for.
|
||||
"""
|
||||
# Retrieve the list of attributes for this object.
|
||||
if exact_match:
|
||||
return self.filter(db_obj=obj).filter(
|
||||
db_key__iexact=searchstr)
|
||||
else:
|
||||
return self.filter(db_obj=obj).filter(
|
||||
db_key__icontains=searchstr)
|
||||
|
||||
def attr_valuesearch(self, searchstr, obj=None):
|
||||
if exact_match:
|
||||
return _GA("obj", "db_attributes").filter(db_key__iexact=searchstr)
|
||||
else:
|
||||
return _GA("obj", "db_attributes").filter(db_key__icontains=searchstr)
|
||||
|
||||
def attr_namesearch(self, *args, **kwargs):
|
||||
"alias wrapper for backwards compatability"
|
||||
return self.get_attrs_on_obj(*args, **kwargs)
|
||||
|
||||
def get_attr_by_value(self, searchstr, obj=None):
|
||||
"""
|
||||
Searches for Attributes with a given value on obj
|
||||
Searches obj for Attributes with a given value.
|
||||
searchstr - value to search for. This may be any suitable object.
|
||||
obj - limit to a given object instance
|
||||
|
||||
If no restraint is given, all Attributes on all types of objects
|
||||
will be searched. It's highly recommended to at least
|
||||
supply the objclass argument (DBObject, DBScript or DBPlayer)
|
||||
to restrict this lookup.
|
||||
"""
|
||||
if obj:
|
||||
return self.filter(db_obj=obj, db_value=searchstr)
|
||||
return _GA(obj, "db_attributes").filter(db_value=searchstr)
|
||||
return self.filter(db_value=searchstr)
|
||||
|
||||
def attr_valuesearch(self, *args, **kwargs):
|
||||
"alias wrapper for backwards compatability"
|
||||
return self.get_attr_by_value(self, *args, **kwargs)
|
||||
|
||||
#
|
||||
# LiteAttributeManager
|
||||
#
|
||||
|
||||
class LiteAttributeManager(models.Manager):
|
||||
"""
|
||||
Manager methods for LiteAttributes
|
||||
"""
|
||||
def get_lattrs_on_obj(self, obj, search_key=None, category=None):
|
||||
"""
|
||||
Get all lattrs on obj, optionally limited by key and/or category
|
||||
"""
|
||||
if search_key or category:
|
||||
key_cands = Q(db_key__iexact=search_key.lower().strip()) if search_key!=None else Q()
|
||||
cat_cands = Q(db_category__iexact=category.lower.strip()) if search_key!=None else Q()
|
||||
return _GA(obj, "db_liteattributes").filter(cat_cands & key_cands)
|
||||
else:
|
||||
return list(_GA(obj, "db_liteattributes").all())
|
||||
|
||||
def get_lattr(self, search_key=None, category=None):
|
||||
"""
|
||||
Search and return all liteattrs matching any combination of
|
||||
the search criteria.
|
||||
search_key (string) - the lattr identifier
|
||||
category (string) - the lattr category
|
||||
"""
|
||||
key_cands = Q(db_key__iexact=search_key.lower().strip()) if search_key!=None else Q()
|
||||
cat_cands = Q(db_category__iexact=category.lower.strip()) if search_key!=None else Q()
|
||||
return list(self.filter(key_cands & cat_cands))
|
||||
|
||||
def get_lattr_data(self, obj=None, search_key=None, category=None):
|
||||
"""
|
||||
Retrieve data from found lattrs in an efficient way. Returns a list of data
|
||||
matching the search criterions
|
||||
"""
|
||||
key_cands = Q(db_key__iexact=search_key.lower().strip()) if search_key!=None else Q()
|
||||
cat_cands = Q(db_category__iexact=category.lower.strip()) if search_key!=None else Q()
|
||||
if obj:
|
||||
query = _GA(obj, "db_liteattributes").filter(key_cands & cat_cands).prefetch_related("db_data")
|
||||
else:
|
||||
query = self.filter(key_cands & cat_cands).prefetch_related("db_data")
|
||||
return [q.db_data for q in query]
|
||||
|
||||
def create_lattr(self, key, category=None, data=None, obj=None):
|
||||
"""
|
||||
Create a LiteAttribute. This makes sure the create case-insensitive keys.
|
||||
"""
|
||||
|
||||
lattr = self.objects.create(db_key=key.lower().strip(),
|
||||
db_category=category.lower().strip() if category!=None else None,
|
||||
db_data=str(data) if data!=None else None)
|
||||
lattr.save()
|
||||
if obj:
|
||||
obj.db_liteattributes.add(lattr)
|
||||
return lattr
|
||||
|
||||
#
|
||||
# TagManager
|
||||
#
|
||||
|
||||
class TagManager(models.Manager):
|
||||
"""
|
||||
Extra manager methods for Tags
|
||||
"""
|
||||
def get_tags_on_obj(self, obj, key=None, category=None):
|
||||
"""
|
||||
Get all tags on obj, optionally limited by key and/or category
|
||||
"""
|
||||
if key or category:
|
||||
key_cands = Q(db_key__iexact=key.lower().strip()) if key!=None else Q()
|
||||
cat_cands = Q(db_category__iexact=category.lower.strip()) if key!=None else Q()
|
||||
return _GA(obj, "db_tags").filter(cat_cands & key_cands)
|
||||
else:
|
||||
return list(_GA(obj, "db_tags").all())
|
||||
|
||||
def get_tag(self, key=None, category=None):
|
||||
"""
|
||||
Search and return all tags matching any combination of
|
||||
the search criteria.
|
||||
search_key (string) - the tag identifier
|
||||
category (string) - the tag category
|
||||
|
||||
Returns a single Tag (or None) if both key and category is given, otherwise
|
||||
it will return a list.
|
||||
"""
|
||||
key_cands = Q(db_key__iexact=key.lower().strip()) if key!=None else Q()
|
||||
cat_cands = Q(db_category__iexact=category.lower().strip()) if category!=None else Q()
|
||||
tags = self.filter(key_cands & cat_cands)
|
||||
if key and category:
|
||||
return tags[0] if tags else None
|
||||
else:
|
||||
return list(tags)
|
||||
|
||||
def get_objs_with_tag(self, objclass, key=None, category=None):
|
||||
"""
|
||||
Search and return all objects of objclass that has tags matching
|
||||
the given search criteria.
|
||||
objclass (dbmodel) - the object class to search
|
||||
key (string) - the tag identifier
|
||||
category (string) - the tag category
|
||||
"""
|
||||
key_cands = Q(db_tags__db_key__iexact=key.lower().strip()) if search_key!=None else Q()
|
||||
cat_cands = Q(db_tags__db_category__iexact=category.lower().strip()) if category!=None else Q()
|
||||
return objclass.objects.filter(key_cands & cat_cands)
|
||||
|
||||
def create_tag(self, key=None, category=None, data=None):
|
||||
"""
|
||||
Create a tag. This makes sure the create case-insensitive tags.
|
||||
Note that if the exact same tag configuration (key+category)
|
||||
exists, it will be re-used. A data keyword will overwrite existing
|
||||
data on a tag (it is not part of what makes the tag unique).
|
||||
|
||||
"""
|
||||
data = str(data) if data!=None else None
|
||||
|
||||
tag = self.get_tag(key=key, category=category)
|
||||
if tag and data != None:
|
||||
tag.db_data = data
|
||||
tag.save()
|
||||
elif not tag:
|
||||
tag = self.create(db_key=key.lower().strip() if key!=None else None,
|
||||
db_category=category.lower().strip() if key!=None else None,
|
||||
db_data=str(data) if data!=None else None)
|
||||
tag.save()
|
||||
return tag
|
||||
|
||||
#
|
||||
# helper functions for the TypedObjectManager.
|
||||
#
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue