summaryrefslogtreecommitdiff
path: root/ishtar_common
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common')
-rw-r--r--ishtar_common/context_processors.py14
-rw-r--r--ishtar_common/management/commands/update_specific_importers.py7
-rw-r--r--ishtar_common/menu_base.py31
-rw-r--r--ishtar_common/models.py57
-rw-r--r--ishtar_common/tests.py6
-rw-r--r--ishtar_common/views.py36
6 files changed, 99 insertions, 52 deletions
diff --git a/ishtar_common/context_processors.py b/ishtar_common/context_processors.py
index ce303c344..03ba9bc36 100644
--- a/ishtar_common/context_processors.py
+++ b/ishtar_common/context_processors.py
@@ -25,18 +25,20 @@ from ishtar_common.utils import shortify
from menus import Menu
+from ishtar_common.models import get_current_profile
from archaeological_operations.models import Operation
+from archaeological_files.models import File
+from archaeological_context_records.models import ContextRecord
+from archaeological_finds.models import Find
+profile = get_current_profile()
CURRENT_ITEMS = []
-if 'archaeological_files' in settings.INSTALLED_APPS:
- from archaeological_files.models import File
+if profile.files:
CURRENT_ITEMS.append((_(u"Archaeological file"), File))
CURRENT_ITEMS.append((_(u"Operation"), Operation))
-if 'archaeological_context_records' in settings.INSTALLED_APPS:
- from archaeological_context_records.models import ContextRecord
+if profile.context_record:
CURRENT_ITEMS.append((_(u"Context record"), ContextRecord))
-if 'archaeological_finds' in settings.INSTALLED_APPS:
- from archaeological_finds.models import Find
+if profile.find:
CURRENT_ITEMS.append((_(u"Find"), Find))
diff --git a/ishtar_common/management/commands/update_specific_importers.py b/ishtar_common/management/commands/update_specific_importers.py
index c5445eb0b..9a13e3f3e 100644
--- a/ishtar_common/management/commands/update_specific_importers.py
+++ b/ishtar_common/management/commands/update_specific_importers.py
@@ -4,14 +4,11 @@
from optparse import make_option
from django.core.management.base import BaseCommand
-from django.conf import settings
IMPORTERS = []
-
-if 'archaeological_files' in settings.INSTALLED_APPS:
- from archaeological_files.data_importer import FileImporterSraPdL
- IMPORTERS.append(FileImporterSraPdL)
+from archaeological_files.data_importer import FileImporterSraPdL
+IMPORTERS.append(FileImporterSraPdL)
class Command(BaseCommand):
diff --git a/ishtar_common/menu_base.py b/ishtar_common/menu_base.py
index ab0a43d41..eb08d8c78 100644
--- a/ishtar_common/menu_base.py
+++ b/ishtar_common/menu_base.py
@@ -17,16 +17,28 @@
# See the file COPYING for details.
+from ishtar_common.models import get_current_profile
+
class SectionItem:
- def __init__(self, idx, label, childs=[]):
+ def __init__(self, idx, label, childs=[], profile_restriction=None):
self.idx = idx
self.label = label
self.childs = childs
self.available = False
self.items = {}
+ self.profile_restriction = profile_restriction
+
+ def check_profile_restriction(self):
+ if self.profile_restriction:
+ profile = get_current_profile()
+ if not getattr(profile, self.profile_restriction):
+ return False
+ return True
def can_be_available(self, user, session=None):
+ if not self.check_profile_restriction():
+ return False
for child in self.childs:
if child.can_be_available(user, session=session):
return True
@@ -50,14 +62,27 @@ class SectionItem:
class MenuItem:
- def __init__(self, idx, label, model=None, access_controls=[]):
+ def __init__(self, idx, label, model=None, access_controls=[],
+ profile_restriction=None):
self.idx = idx
self.label = label
self.model = model
self.access_controls = access_controls
self.available = False
+ self.profile_restriction = profile_restriction
+ if not self.check_profile_restriction():
+ return False
+
+ def check_profile_restriction(self):
+ if self.profile_restriction:
+ profile = get_current_profile()
+ if not getattr(profile, self.profile_restriction):
+ return False
+ return True
def can_be_available(self, user, session=None):
+ if not self.check_profile_restriction():
+ return False
if not self.access_controls:
return True
prefix = (self.model._meta.app_label + '.') if self.model else ''
@@ -75,6 +100,8 @@ class MenuItem:
return False
def is_available(self, user, obj=None, session=None):
+ if not self.check_profile_restriction():
+ return False
if not self.access_controls:
return True
prefix = (self.model._meta.app_label + '.') if self.model else ''
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index bcd1881d6..f5c6ed223 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -250,13 +250,16 @@ class OwnPerms:
class Cached(object):
+ slug_field = 'txt_idx'
+
@classmethod
def get_cache(cls, slug):
cache_key, value = get_cache(cls, slug)
if value:
return value
try:
- obj = cls.objects.get(txt_idx=slug)
+ k = {cls.slug_field: slug}
+ obj = cls.objects.get(**k)
cache.set(cache_key, obj, settings.CACHE_TIMEOUT)
return obj
except cls.DoesNotExist:
@@ -768,6 +771,7 @@ class LightHistorizedItem(BaseHistorizedItem):
class IshtarSiteProfile(models.Model, Cached):
+ slug_field = 'slug'
label = models.TextField(_(u"Name"))
slug = models.SlugField(_(u"Slug"), unique=True)
description = models.TextField(_(u"Description"), null=True, blank=True)
@@ -817,6 +821,20 @@ class IshtarSiteProfile(models.Model, Cached):
return obj
+def get_current_profile():
+ cache_key, value = get_cache(IshtarSiteProfile, 'is-current-profile')
+ if value:
+ return value
+ q = IshtarSiteProfile.objects.filter(active=True)
+ if not q.count():
+ obj = IshtarSiteProfile.objects.create(
+ label="Default profile", slug='default', active=True)
+ else:
+ obj = q.all()[0]
+ cache.set(cache_key, obj, settings.CACHE_TIMEOUT)
+ return obj
+
+
class GlobalVar(models.Model, Cached):
slug = models.SlugField(_(u"Variable name"), unique=True)
description = models.TextField(_(u"Description of the variable"),
@@ -1214,29 +1232,29 @@ class OrganizationType(GeneralType):
verbose_name_plural = _(u"Organization types")
ordering = ('label',)
-MODELS = [
- ('archaeological_operations.models.Operation', _(u"Operation")),
- ('archaeological_operations.models.ArchaeologicalSite',
- _(u"Archaeological site")),
- ('archaeological_operations.models.Parcel', _(u"Parcels")),
- ('archaeological_operations.models.OperationSource',
- _(u"Operation source")),
-]
-
IMPORTER_CLASSES = {}
-if 'archaeological_files' in settings.INSTALLED_APPS:
- MODELS = [('archaeological_files.models.File', _(u"Archaeological files"))]\
- + MODELS
- IMPORTER_CLASSES.update({
- 'sra-pdl-files':
- 'archaeological_files.data_importer.FileImporterSraPdL'})
-if 'archaeological_context_records' in settings.INSTALLED_APPS:
+IMPORTER_CLASSES.update({
+ 'sra-pdl-files':
+ 'archaeological_files.data_importer.FileImporterSraPdL'})
+
+
+def get_importer_models():
+ MODELS = [
+ ('archaeological_operations.models.Operation', _(u"Operation")),
+ ('archaeological_operations.models.ArchaeologicalSite',
+ _(u"Archaeological site")),
+ ('archaeological_operations.models.Parcel', _(u"Parcels")),
+ ('archaeological_operations.models.OperationSource',
+ _(u"Operation source")),
+ ]
+ MODELS = [('archaeological_files.models.File',
+ _(u"Archaeological files"))] + MODELS
MODELS = [('archaeological_context_records.models.ContextRecord',
_(u"Context records")), ] + MODELS
-if 'archaeological_finds' in settings.INSTALLED_APPS:
MODELS = [('archaeological_finds.models.BaseFind',
_(u"Finds")), ] + MODELS
+ return MODELS
def get_model_fields(model):
@@ -1274,7 +1292,8 @@ class ImporterType(models.Model):
users = models.ManyToManyField('IshtarUser', verbose_name=_(u"Users"),
blank=True, null=True)
associated_models = models.CharField(_(u"Associated model"),
- max_length=200, choices=MODELS)
+ max_length=200,
+ choices=get_importer_models())
is_template = models.BooleanField(_(u"Is template"), default=False)
unicity_keys = models.CharField(_(u"Unicity keys (separator \";\")"),
blank=True, null=True, max_length=500)
diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py
index 4e800b0d5..e2c9b233f 100644
--- a/ishtar_common/tests.py
+++ b/ishtar_common/tests.py
@@ -182,3 +182,9 @@ class IshtarSiteProfileTest(TestCase):
profile2.warehouse = True
profile2 = profile2.save()
self.assertTrue(profile2.context_record and profile2.find)
+
+ def testDefaultProfile(self):
+ self.assertFalse(models.IshtarSiteProfile.objects.count())
+ profile = models.get_current_profile()
+ self.assertTrue(profile)
+ self.assertTrue(models.IshtarSiteProfile.objects.count())
diff --git a/ishtar_common/views.py b/ishtar_common/views.py
index 6f95e070a..1c944441f 100644
--- a/ishtar_common/views.py
+++ b/ishtar_common/views.py
@@ -51,7 +51,12 @@ from xhtml2odt import xhtml2odt
from menus import menu
+from archaeological_files.models import File
+from archaeological_context_records.models import ContextRecord
+from archaeological_finds.models import Find
+
from archaeological_operations.forms import DashboardForm as DashboardFormOpe
+from archaeological_files.forms import DashboardForm as DashboardFormFile
from ishtar_common.forms import FinalForm, FinalDeleteForm
from ishtar_common import forms_common as forms
@@ -927,22 +932,20 @@ def dashboard_main(request, dct, obj_id=None, *args, **kwargs):
Main dashboard
"""
app_list = []
- if 'archaeological_files' in settings.INSTALLED_APPS:
+ profile = models.get_current_profile()
+ if profile.files:
app_list.append((_(u"Archaeological files"), 'files'))
app_list.append((_(u"Operations"), 'operations'))
- if 'archaeological_context_records' in settings.INSTALLED_APPS:
+ if profile.context_record:
app_list.append((_(u"Context records"), 'contextrecords'))
- if 'archaeological_finds' in settings.INSTALLED_APPS:
+ if profile.find:
app_list.append((_(u"Finds"), 'finds'))
dct = {'app_list': app_list}
return render_to_response('ishtar/dashboards/dashboard_main.html', dct,
context_instance=RequestContext(request))
DASHBOARD_FORMS = {}
-if 'archaeological_files' in settings.INSTALLED_APPS:
- from archaeological_files.forms import DashboardForm as DashboardFormFile
- DASHBOARD_FORMS['files'] = DashboardFormFile
-
+DASHBOARD_FORMS['files'] = DashboardFormFile
DASHBOARD_FORMS['operations'] = DashboardFormOpe
@@ -957,8 +960,8 @@ def dashboard_main_detail(request, item_name):
dct, context_instance=RequestContext(request))
form = None
slicing, date_source, fltr, show_detail = 'year', None, {}, False
- if (item_name == 'files' and
- 'archaeological_files' in settings.INSTALLED_APPS) \
+ profile = models.get_current_profile()
+ if (item_name == 'files' and profile.files) \
or item_name == 'operations':
slicing = 'month'
if item_name in DASHBOARD_FORMS:
@@ -974,32 +977,25 @@ def dashboard_main_detail(request, item_name):
else:
form = DASHBOARD_FORMS[item_name]()
lbl, dashboard = None, None
- if (item_name == 'files' and
- 'archaeological_files' in settings.INSTALLED_APPS) \
+ if (item_name == 'files' and profile.files) \
or item_name == 'operations':
dashboard_kwargs = {'slice': slicing, 'fltr': fltr,
'show_detail': show_detail}
# date_source is only relevant when the form has set one
if date_source:
dashboard_kwargs['date_source'] = date_source
- if item_name == 'files' and \
- 'archaeological_files' in settings.INSTALLED_APPS:
- from archaeological_files.models import File
+ if item_name == 'files' and profile.files:
lbl, dashboard = (_(u"Archaeological files"),
models.Dashboard(File, **dashboard_kwargs))
if item_name == 'operations':
from archaeological_operations.models import Operation
lbl, dashboard = (_(u"Operations"),
models.Dashboard(Operation, **dashboard_kwargs))
- if item_name == 'contextrecords' and \
- 'archaeological_context_records' in settings.INSTALLED_APPS:
- from archaeological_context_records.models import ContextRecord
+ if item_name == 'contextrecords' and profile.context_record:
lbl, dashboard = (
_(u"Context records"),
models.Dashboard(ContextRecord, slice=slicing, fltr=fltr))
- if item_name == 'finds' and \
- 'archaeological_finds' in settings.INSTALLED_APPS:
- from archaeological_finds.models import Find
+ if item_name == 'finds' and profile.find:
lbl, dashboard = (_(u"Finds"), models.Dashboard(Find,
slice=slicing,
fltr=fltr))