summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ishtar_common/models.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index 5c2875fc5..5141ed66d 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -1649,11 +1649,66 @@ class SearchQuery(models.Model):
class ShortMenuItem(object):
+ """
+ Item available in the short menu
+ """
@classmethod
def get_short_menu_class(cls, pk):
return ''
+class QuickAction(object):
+ """
+ Quick action available from tables
+ """
+ def __init__(self, url, icon='', text='', target=None, rights=None):
+ self.url = url
+ self.icon = icon
+ self.text = text
+ self.rights = rights
+ self.target = target
+ assert self.target in ('one', 'many', None)
+
+ def is_available(self, user, session=None, obj=None):
+ if not self.rights: # no restriction
+ return True
+ for right in self.rights:
+ if user.has_perm(right, session=session, obj=obj):
+ return True
+ return False
+
+ def render(self):
+ lbl = self.text
+ if self.icon:
+ lbl = self.icon
+ if self.target is None:
+ url = reverse(self.url)
+ else:
+ # put arbitrary pk for the target
+ url = reverse(self.url, args=[0])
+ url = url[:-1] # all quick action url have to finish with the
+ # pk of the selected item
+ return u'<a href="#" data-url="{}" title="{}">{}</a>'.format(
+ url, self.text, lbl
+ )
+
+
+class MainItem(ShortMenuItem):
+ """
+ Item with quick actions availables from tables
+ """
+ QUICK_ACTIONS = []
+
+ @classmethod
+ def render_quick_actions(cls, user, session=None, obj=None):
+ rendered = []
+ for action in cls.QUICK_ACTIONS:
+ if not action.is_available(user, session=session, obj=obj):
+ continue
+ rendered.append(action.render())
+ return mark_safe(u" ".join(rendered))
+
+
class LightHistorizedItem(BaseHistorizedItem):
history_date = models.DateTimeField(default=datetime.datetime.now)