mirror of
https://github.com/jazzband/django-eav2.git
synced 2026-03-16 22:40:26 +00:00
First filter attempt
This commit is contained in:
parent
b765aa9c51
commit
de61e49622
2 changed files with 59 additions and 1 deletions
56
managers.py
56
managers.py
|
|
@ -1,4 +1,58 @@
|
|||
from django.db import models
|
||||
|
||||
class EntityManager(models.Manager):
|
||||
pass
|
||||
def filter(self, *args, **kwargs):
|
||||
qs = self.get_query_set().filter(*args)
|
||||
for lookup, value in kwargs.items():
|
||||
lookups = self._filter_by_lookup(qs, lookup, value)
|
||||
qs = qs.filter(**lookups)
|
||||
return qs
|
||||
|
||||
def _filter_by_lookup(self, qs, lookup, value):
|
||||
eav_entity = self._get_eav_entity()
|
||||
slugs = eav_entity.get_all_attribute_slugs()
|
||||
cache = eav_entity.get_attr_cache()
|
||||
fields = self.model._meta.get_all_field_names()
|
||||
attributes = eav_entity.get_all_attributes()
|
||||
|
||||
config_cls = self._get_config_cls()
|
||||
eav_prefix = config_cls.proxy_field_name
|
||||
gr_name = config_cls.generic_relation_field_name
|
||||
|
||||
if not lookup.startswith("%s__" % eav_prefix):
|
||||
return {lookup: value}
|
||||
|
||||
lookup = lookup.lstrip("%s__" % eav_prefix)
|
||||
|
||||
# Sublookup will be None if there is no __ in the lookup
|
||||
name, sublookup = (lookup.split('__', 1) + [None])[:2]
|
||||
|
||||
if name in slugs:
|
||||
# EAV attribute (Attr instance linked to entity)
|
||||
attribute = eav_entity.get_attribute_by_slug(name)
|
||||
return self._filter_by_simple_schema(qs, lookup, sublookup, value, attribute)
|
||||
else:
|
||||
raise NameError('Cannot filter items by attributes: unknown '
|
||||
'attribute "%s". Available fields: %s. '
|
||||
'Available attribute: %s.' % (name,
|
||||
', '.join(fields), ', '.join(slugs)))
|
||||
|
||||
def _filter_by_simple_schema(self, qs, lookup, sublookup, value, attribute):
|
||||
config_cls = self._get_config_cls()
|
||||
eav_prefix = config_cls.proxy_field_name
|
||||
gr_name = config_cls.generic_relation_field_name
|
||||
|
||||
|
||||
value_lookup = '%s__value_%s' % (gr_name, attribute.datatype)
|
||||
if sublookup:
|
||||
value_lookup = '%s__%s' % (value_lookup, sublookup)
|
||||
return { value_lookup: value }
|
||||
|
||||
def _get_config_cls(self):
|
||||
from .utils import EavRegistry
|
||||
return EavRegistry.get_config_cls_for_model(self.model)
|
||||
|
||||
def _get_eav_entity(self):
|
||||
cls = self._get_config_cls()
|
||||
eav_entity_name = cls.proxy_field_name
|
||||
return getattr(self.model, eav_entity_name)
|
||||
|
|
|
|||
4
utils.py
4
utils.py
|
|
@ -24,6 +24,10 @@ class EavRegistry(object):
|
|||
"""
|
||||
cache = {}
|
||||
|
||||
@staticmethod
|
||||
def get_config_cls_for_model(model_cls):
|
||||
return EavRegistry.cache[model_cls.__name__]['config_cls']
|
||||
|
||||
@staticmethod
|
||||
def attach(sender, *args, **kwargs):
|
||||
"""
|
||||
|
|
|
|||
Loading…
Reference in a new issue