diff options
Diffstat (limited to 'ishtar/furnitures/models.py')
-rw-r--r-- | ishtar/furnitures/models.py | 61 |
1 files changed, 52 insertions, 9 deletions
diff --git a/ishtar/furnitures/models.py b/ishtar/furnitures/models.py index 7687cb6f3..64c76822d 100644 --- a/ishtar/furnitures/models.py +++ b/ishtar/furnitures/models.py @@ -26,6 +26,7 @@ from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.core.validators import validate_slug from django.utils.translation import ugettext_lazy as _, ugettext from django.db.utils import DatabaseError +from django.db.models import Q from django.contrib.auth.models import User from django.contrib.gis.db import models @@ -58,8 +59,47 @@ class OwnPerms: """ Manage special permissions for object's owner """ + @classmethod + def get_query_owns(cls, user): + """ + Query object to get own items + """ + return None # implement for each object + def is_own(self, user): - return False + """ + Check if the current object is owned by the user + """ + query = self.get_query_owns(user) + if not query: + return False + query = query & Q(pk=self.pk) + return cls.objects.filter(query).count() + + + @classmethod + def has_item_of(cls, user): + """ + Check if the user own some items + """ + query = cls.get_query_owns(user) + if not query: + return False + return cls.objects.filter(query).count() + + @classmethod + def get_owns(cls, user): + """ + Get Own items + """ + if isinstance(user, User): + user = IshtarUser.objects.get(user_ptr=user) + if user.is_anonymous(): + return [] + query = cls.get_query_owns(user) + if not query: + return [] + return cls.objects.filter(query).order_by(*cls._meta.ordering).all() class GeneralType(models.Model): """ @@ -266,6 +306,8 @@ class File(BaseHistorizedItem, OwnPerms): ("change_own_file", ugettext(u"Can change own Archaelogical file")), ("delete_own_file", ugettext(u"Can delete own Archaelogical file")), ) + ordering = ['-year', '-numeric_reference'] + def __unicode__(self): items = [unicode(_('Intercommunal'))] if self.towns.count() == 1: @@ -275,13 +317,6 @@ class File(BaseHistorizedItem, OwnPerms): if getattr(self, k)] return u" - ".join(items) - @classmethod - def get_owns(cls, user, order_by=['-year', '-numeric_reference']): - if user.is_anonymous(): - return [] - return cls.objects.filter(history_modifier=user).order_by(*order_by - ).all() - class OperationType(GeneralType): class Meta: verbose_name = _(u"Operation type") @@ -332,13 +367,21 @@ class Operation(BaseHistorizedItem, OwnPerms): if getattr(self, k)] return u" - ".join(items) + def is_own(self, person): + return False + + @classmethod + def get_query_owns(cls, user): + return Q(in_charge=user.person)|Q(history_modifier=user) + + """ @classmethod def get_owns(cls, user, order_by=['-year', '-operation_code']): if user.is_anonymous(): return [] return cls.objects.filter(history_modifier=user).order_by(*order_by ).all() - +""" class Parcel(LightHistorizedItem): associated_file = models.ForeignKey(File, related_name='parcels', blank=True, null=True, verbose_name=_(u"File")) |