Add the Collection class to the common app.

This commit is contained in:
Roberto Rosario
2016-11-16 22:53:20 -04:00
parent b5c5602301
commit a94745ab9e
2 changed files with 43 additions and 1 deletions

View File

@@ -1,10 +1,48 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.apps import apps from django.apps import apps
from django.core.urlresolvers import reverse
from django.db import models from django.db import models
from django.utils.translation import ugettext from django.utils.translation import ugettext
class Collection(object):
_registry = []
@classmethod
def get_all(cls):
return sorted(cls._registry, key=lambda entry: entry._order)
def __init__(self, label, icon=None, link=None, queryset=None, model=None, order=None):
self._label = label
self._icon = icon
self._link = link
self._queryset = queryset
self._model = model
self._order = order or 99
self.__class__._registry.append(self)
def __unicode__(self):
return unicode(self.label)
def resolve(self):
self.children = self._get_children()
self.icon = self._icon
self.label = self._label
self.url = None
if self._link:
self.icon = getattr(self._link, 'icon', self._icon)
self.url = reverse(viewname=self._link.view, args=self._link.args)
return ''
def _get_children(self):
if self._queryset:
return self._queryset
else:
if self._model:
return self._model.objects.all()
class DashboardWidget(object): class DashboardWidget(object):
_registry = [] _registry = []

View File

@@ -10,7 +10,7 @@ from django.template.loader import get_template
import mayan import mayan
from ..classes import DashboardWidget from ..classes import Collection, DashboardWidget
from ..utils import return_attrib from ..utils import return_attrib
register = Library() register = Library()
@@ -23,6 +23,10 @@ except sh.CommandNotFound:
DATE = None DATE = None
def get_collections():
return Collection.get_all()
@register.assignment_tag @register.assignment_tag
def get_dashboard_widgets(): def get_dashboard_widgets():
return DashboardWidget.get_all() return DashboardWidget.get_all()