Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending MENU configuration key functionality #430

Merged
merged 4 commits into from
Sep 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ Following keys are available for each app and model level links:

``permissions`` are verified using `user.has_perms() <https://docs.djangoproject.com/en/dev/ref/contrib/auth/#django.contrib.auth.models.User.has_perm>`_ method.

A custom application can contain a ``models`` list (or tuple) to customize the application models
list. The ``models`` list can contain model references and model definitions. The model reference
is a string referencing to the model through the application label and model name. The model
name may be globbed to reference all models in the application like 'auth.*'.

Here is full example of ``MENU`` from simple existing app reorder to defining custom menu items::

SUIT_CONFIG = {
Expand Down
36 changes: 32 additions & 4 deletions suit/templatetags/suit_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# For Django < 1.4.2
string_types = basestring,

import re

import warnings
from suit.config import get_config

Expand Down Expand Up @@ -58,6 +60,7 @@ def get_admin_site(current_app):

class Menu(object):
app_activated = False
MULTIPLE_MODELS_RE = re.compile(r'([^*]*)[*]')

def __init__(self, context, request, app_list):
self.request = request
Expand Down Expand Up @@ -251,12 +254,31 @@ def process_models(self, app):
models = []
models_def = app.get('models', [])
for model_def in models_def:
model = self.make_model(model_def, app['name'])
if model:
models.append(model)
# multiple models may be returned
models += self.make_models(model_def, app['name'])

app['models'] = models

def make_models(self, model_def, app_name):
if not isinstance(model_def, string_types):
model = self.make_model(model_def, app_name)
return [model] if model else []
match = self.MULTIPLE_MODELS_RE.match(model_def)
if not match:
model = self.make_model(model_def, app_name)
return [model] if model else []
prefix = match.group(1)
prefix = self.get_model_name(app_name,prefix)
return [
m
for m in [
self.convert_native_model(native_model,app_name)
for native_model in self.all_models
if self.get_native_model_name(native_model).startswith(prefix)
]
if m
]

def make_model(self, model_def, app_name):
if isinstance(model_def, dict):
model = model_def.copy()
Expand All @@ -283,6 +305,10 @@ def model_is_excluded(self, model_name):
return self.conf_exclude and model_name in self.conf_exclude

def get_model_name(self, app_name, model_name):
if app_name:
app_name = app_name.lower()
if model_name:
model_name = model_name.lower()
if '.' not in model_name:
model_name = '%s.%s' % (app_name, model_name)
return model_name
Expand All @@ -300,7 +326,9 @@ def convert_native_model(self, model, app_name):
'label': model['name'],
'url': self.get_native_model_url(model),
'name': self.get_native_model_name(model),
'app': app_name
'app': app_name,
'perms': model.get('perms',None),
'add_url': model.get('add_url',None),
}

def get_native_model_url(self, model):
Expand Down