diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-04-22 13:00:00 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-06-12 08:43:31 +0200 |
commit | 186460a9db6aa083f55ab46b77a07a7a273b5258 (patch) | |
tree | 5f69cd7f4c04c032bc8c4e49664c485bf80f65d4 | |
parent | 9777cde256561c13eb3e80dbc07e693f162a7b28 (diff) | |
download | Ishtar-186460a9db6aa083f55ab46b77a07a7a273b5258.tar.bz2 Ishtar-186460a9db6aa083f55ab46b77a07a7a273b5258.zip |
Manage own permissions with areas for finds (refs #4060)
-rw-r--r-- | archaeological_context_records/tests.py | 11 | ||||
-rw-r--r-- | archaeological_finds/models_finds.py | 23 | ||||
-rw-r--r-- | archaeological_finds/tests.py | 91 |
3 files changed, 102 insertions, 23 deletions
diff --git a/archaeological_context_records/tests.py b/archaeological_context_records/tests.py index aad8b8b6d..8f7626656 100644 --- a/archaeological_context_records/tests.py +++ b/archaeological_context_records/tests.py @@ -200,10 +200,10 @@ class ContextRecordInit(OperationInitTest): **default)) return self.context_records - def get_default_context_record(self, force=False): + def get_default_context_record(self, force=False, user=None): if force: - return self.create_context_record(force=force)[-1] - return self.create_context_record(force=force)[0] + return self.create_context_record(force=force, user=user)[-1] + return self.create_context_record(force=force, user=user)[0] def tearDown(self): if hasattr(self, 'context_records'): @@ -613,6 +613,8 @@ class ContextRecordPermissionTest(ContextRecordInit, TestCase): area = Area.objects.create(label='Galaxie', txt_idx='galaxie') area.towns.add(town) + profile.areas.add(area) + self.orgas = self.create_orgas(self.user) self.operations = self.create_operation(self.user, self.orgas[0]) self.operations += self.create_operation(self.alt_user, self.orgas[0]) @@ -626,9 +628,6 @@ class ContextRecordPermissionTest(ContextRecordInit, TestCase): self.cr_1 = self.context_records[0] self.cr_2 = self.context_records[1] - - profile.areas.add(area) - def test_own_search(self): # no result when no authentification c = Client() diff --git a/archaeological_finds/models_finds.py b/archaeological_finds/models_finds.py index 35dc6995a..7155f3906 100644 --- a/archaeological_finds/models_finds.py +++ b/archaeological_finds/models_finds.py @@ -1094,14 +1094,21 @@ class Find(BulkUpdatedItem, ValueGetter, BaseHistorizedItem, ImageModel, @classmethod def get_query_owns(cls, ishtaruser): - return (Q(base_finds__context_record__operation__scientist= - ishtaruser.person) | - Q(base_finds__context_record__operation__in_charge= - ishtaruser.person) | - Q(base_finds__context_record__operation__collaborators__pk= - ishtaruser.person.pk) | - Q(history_creator=ishtaruser.user_ptr)) \ - & Q(base_finds__context_record__operation__end_date__isnull=True) + profile = ishtaruser.current_profile + town_ids = [] + if profile: + town_ids = [town['pk'] + for town in profile.query_towns.values('pk').all()] + return ( + Q(base_finds__context_record__operation__scientist= + ishtaruser.person) | + Q(base_finds__context_record__operation__in_charge= + ishtaruser.person) | + Q(base_finds__context_record__operation__collaborators__pk= + ishtaruser.person.pk) | + Q(history_creator=ishtaruser.user_ptr) | + Q(base_finds__context_record__operation__towns__pk__in=town_ids) + ) & Q(base_finds__context_record__operation__end_date__isnull=True) @classmethod def get_owns(cls, user, menu_filtr=None, limit=None, diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py index 3ae669517..b44bc9979 100644 --- a/archaeological_finds/tests.py +++ b/archaeological_finds/tests.py @@ -20,15 +20,16 @@ import json from django.conf import settings -from django.contrib.auth.models import User +from django.contrib.auth.models import User, Permission from django.core.files import File from django.core.files.uploadedfile import SimpleUploadedFile from django.core.urlresolvers import reverse from django.test.client import Client from ishtar_common.models import ImporterType, IshtarUser, ImporterColumn,\ - FormaterType, ImportTarget, IshtarSiteProfile + FormaterType, ImportTarget, IshtarSiteProfile, ProfileType -from ishtar_common.models import Person, get_current_profile +from ishtar_common.models import Person, get_current_profile, UserProfile, \ + Town, Area from archaeological_context_records.models import Period, Dating, ContextRecord from archaeological_finds import models, views from archaeological_warehouse.models import Warehouse, WarehouseType @@ -36,7 +37,7 @@ from archaeological_warehouse.models import Warehouse, WarehouseType from ishtar_common import forms_common from ishtar_common.tests import WizardTest, WizardTestFormData as FormData, \ - TestCase + TestCase, create_user, create_superuser from archaeological_operations.tests import ImportTest, create_operation from archaeological_context_records.tests import ContextRecordInit, \ CONTEXT_RECORD_FIXTURES, CONTEXT_RECORD_TOWNS_FIXTURES @@ -61,21 +62,23 @@ WAREHOUSE_FIXTURES = FIND_FIXTURES + [ class FindInit(ContextRecordInit): test_context_records = False - def create_finds(self, user=None, data_base={}, data={}, force=False): + def create_finds(self, data_base={}, data={}, user=None, force=False): if not getattr(self, 'finds', None): self.finds = [] if not getattr(self, 'base_finds', None): self.base_finds = [] default = {'label': "Base find"} - if not data_base.get('history_modifier') or not data_base[ + if user: + data_base['history_modifier'] = user + elif not data_base.get('history_modifier') or not data_base[ 'history_modifier'].pk: user = self.get_default_user() user.save() data_base['history_modifier'] = user if force or not data_base.get('context_record'): data_base['context_record'] = self.get_default_context_record( - force=force) + force=force, user=user) default.update(data_base) base_find = models.BaseFind.objects.create(**default) self.base_finds.append(base_find) @@ -630,6 +633,76 @@ class FindSearchTest(FindInit, TestCase): self.assertTrue(json.loads(response.content)['recordsTotal'] == 1) +class FindPermissionTest(FindInit, TestCase): + fixtures = FIND_FIXTURES + model = models.Find + + def setUp(self): + self.username, self.password, self.user = create_superuser() + self.alt_username, self.alt_password, self.alt_user = create_user() + self.alt_user.user_permissions.add(Permission.objects.get( + codename='view_own_find')) + self.alt_user.user_permissions.add(Permission.objects.get( + codename='change_own_find')) + self.alt_username2, self.alt_password2, self.alt_user2 = create_user( + username='luke', password='iamyourfather' + ) + profile = UserProfile.objects.create( + profile_type=ProfileType.objects.get(txt_idx='collaborator'), + person=self.alt_user2.ishtaruser.person, + current=True + ) + + town = Town.objects.create(name='Tatouine', numero_insee='66000') + area = Area.objects.create(label='Galaxie', txt_idx='galaxie') + area.towns.add(town) + profile.areas.add(area) + + self.orgas = self.create_orgas(self.user) + self.create_operation(self.user, self.orgas[0]) + self.create_operation(self.alt_user, self.orgas[0]) + + self.create_context_record( + user=self.user, + data={"label": u"CR 1", "operation": self.operations[0]}) + self.create_context_record( + user=self.alt_user, + data={"label": u"CR 2", "operation": self.operations[1]}) + self.cr_1 = self.context_records[-2] + self.cr_2 = self.context_records[-1] + + self.create_finds(data_base={'context_record': self.cr_1}, + user=self.user, force=True) + self.create_finds(data_base={'context_record': self.cr_2}, + user=self.alt_user, force=True) + + self.find_1 = self.finds[-2] + self.find_2 = self.finds[-1] + self.operations[-1].towns.add(town) + + def test_own_search(self): + # no result when no authentification + c = Client() + response = c.get(reverse('get-find')) + self.assertTrue(not json.loads(response.content)) + + # possession + c = Client() + c.login(username=self.alt_username, password=self.alt_password) + response = c.get(reverse('get-find')) + # only one "own" context record available + self.assertTrue(json.loads(response.content)) + self.assertEqual(json.loads(response.content)['recordsTotal'], 1) + + # area filter + c = Client() + c.login(username=self.alt_username2, password=self.alt_password2) + response = c.get(reverse('get-find')) + # only one "own" operation available + self.assertTrue(json.loads(response.content)) + self.assertEqual(json.loads(response.content)['recordsTotal'], 1) + + class PackagingTest(FindInit, TestCase): fixtures = FIND_FIXTURES model = models.Find @@ -638,8 +711,8 @@ class PackagingTest(FindInit, TestCase): img = settings.ROOT_PATH + \ '../ishtar_common/static/media/images/ishtar-bg.jpg' - self.create_finds({"label": u"Find 1"}, force=True) - self.create_finds({"label": u"Find 2"}, force=True) + self.create_finds(data_base={"label": u"Find 1"}, force=True) + self.create_finds(data_base={"label": u"Find 2"}, force=True) self.finds[0].image.save('ishtar-bg.jpg', File(open(img))) self.finds[0].save() |