summaryrefslogtreecommitdiff
path: root/ishtar_common/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r--ishtar_common/models.py43
1 files changed, 37 insertions, 6 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index 6fb7852ae..7015c70a5 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -226,7 +226,7 @@ class OwnPerms:
query = self.get_query_owns(user)
if not query:
return False
- query = query & Q(pk=self.pk)
+ query &= Q(pk=self.pk)
return self.__class__.objects.filter(query).count()
@classmethod
@@ -240,28 +240,57 @@ class OwnPerms:
return cls.objects.filter(query).count()
@classmethod
- def get_owns(cls, user, replace_query={}, limit=None):
+ def _return_get_owns(cls, owns, values, get_short_menu_class,
+ label_key='cached_label'):
+ if not values:
+ if not get_short_menu_class:
+ return sorted(owns, key=lambda x: getattr(x, label_key))
+ return sorted(owns, key=lambda x: getattr(x[0], label_key))
+ if not get_short_menu_class:
+ return sorted(owns, key=lambda x: x[label_key])
+ return sorted(owns, key=lambda x: x[0][label_key])
+
+ @classmethod
+ def get_owns(cls, user, replace_query={}, limit=None, values=None,
+ get_short_menu_class=False):
"""
Get Own items
"""
if isinstance(user, User):
user = IshtarUser.objects.get(user_ptr=user)
if user.is_anonymous():
- return cls.objects.filter(pk__isnull=True)
+ returned = cls.objects.filter(pk__isnull=True)
+ if values:
+ returned = []
+ return returned
items = []
if hasattr(cls, 'BASKET_MODEL'):
items = list(cls.BASKET_MODEL.objects.filter(user=user).all())
query = cls.get_query_owns(user)
if not query and not replace_query:
- return cls.objects.filter(pk__isnull=True)
+ returned = cls.objects.filter(pk__isnull=True)
+ if values:
+ returned = []
+ return returned
if query:
q = cls.objects.filter(query)
if replace_query:
q = cls.objects.filter(replace_query)
+ if values:
+ q = q.values(*values)
if limit:
items += list(q.order_by('-pk')[:limit])
else:
items += list(q.order_by(*cls._meta.ordering).all())
+ if get_short_menu_class:
+ if values:
+ if 'id' not in values:
+ raise NotImplementedError(
+ "Call of get_owns with get_short_menu_class option and"
+ " no 'id' in values is not implemented")
+ items = [(i, cls.get_short_menu_class(i['id'])) for i in items]
+ else:
+ items = [(i, cls.get_short_menu_class(i.pk)) for i in items]
return items
@@ -643,7 +672,8 @@ class Basket(models.Model):
def __unicode__(self):
return self.label
- def get_short_menu_class(self):
+ @classmethod
+ def get_short_menu_class(cls, pk):
return 'basket'
@property
@@ -968,7 +998,8 @@ def post_delete_record_relation(sender, instance, **kwargs):
class ShortMenuItem(object):
- def get_short_menu_class(self):
+ @classmethod
+ def get_short_menu_class(cls, pk):
return ''