diff options
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r-- | ishtar_common/models.py | 61 |
1 files changed, 20 insertions, 41 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index de34a2b9c..d13def4c7 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -88,41 +88,6 @@ def post_save_user(sender, **kwargs): post_save.connect(post_save_user, sender=User) -def check_model_access_control(request, model, available_perms=None): - """ - Check access control to a model for a specific request - - :param request: the current request - :param model: the concerned model - :param available_perms: specific permissions to check if not specified - "view" and "view_own" will be checked - :return: (allowed, own) tuple - """ - own = True # more restrictive by default - allowed = False - if not request.user.is_authenticated(): - return allowed, own - - if not available_perms: - available_perms = ['view_' + model.__name__.lower(), - 'view_own_' + model.__name__.lower()] - if request.user.ishtaruser.has_right('administrator', - session=request.session): - allowed = True - own = False - return allowed, own - for perm, lbl in model._meta.permissions: - if perm not in available_perms: - continue - if request.user.ishtaruser.person.has_right( - perm, session=request.session): - allowed = True - if "_own_" not in perm: - own = False - break # max right reach - return allowed, own - - class ValueGetter(object): _prefix = "" GET_VALUES_EXTRA = [] @@ -3128,7 +3093,7 @@ class Document(OwnPerms, ImageModel, FullSearch): scale = models.CharField(_(u"Scale"), max_length=30, null=True, blank=True) authors = models.ManyToManyField(Author, verbose_name=_(u"Authors"), - related_name="%(class)s_related") + related_name="documents") authors_raw = models.CharField(verbose_name=_(u"Authors (raw)"), blank=True, null=True, max_length=250) associated_url = models.URLField( @@ -3182,6 +3147,19 @@ class Document(OwnPerms, ImageModel, FullSearch): self.index) """ + @property + def images(self): + # mimic a queryset pointing to himself + return Document.objects.filter(pk=self.pk, + image__isnull=False).exclude(image='') + + @property + def has_related(self): + for rel in self.RELATED_MODELS: + if getattr(self, rel).count(): + return True + return False + @classmethod def get_query_owns(cls, ishtaruser): Operation = cls.operations.rel.related_model @@ -3204,11 +3182,12 @@ class Document(OwnPerms, ImageModel, FullSearch): return slugify(u"-".join(values)) def _get_base_image_paths(self): - for related_model in self.RELATED_MODELS: - q = getattr(self, related_model).all() - if q.count(): - item = q.all()[0] - yield item._get_base_image_path() + if self.pk: # m2m not available if not created... + for related_model in self.RELATED_MODELS: + q = getattr(self, related_model).all() + if q.count(): + item = q.all()[0] + yield item._get_base_image_path() def _get_base_image_path(self): for path in self._get_base_image_paths(): |