1 class MenuItem(object):
4 def __init__(self, url, title, html_id=None):
9 def is_active(self, request, value):
10 return request.get_full_path() == str(self.url)
12 def check_active(self, request, value):
13 self.active = self.is_active(request, value)
22 class ObjectMenuItem(MenuItem):
23 """Menu item corresponding to an object.
25 If no url or title is provided, get_absolute_url and __unicode__ are used.
26 You can also provide a reverse lookup dictionary, as in {model: field_name}.
28 def __init__(self, obj, url=None, rev_lookups=None, title=None, html_id=None):
29 super(ObjectMenuItem, self).__init__(url=url, title=title, html_id=html_id)
31 self.rev_lookups = rev_lookups
34 return self.title or unicode(self.obj)
38 return self.url or self.obj.get_absolute_url()
40 def is_active(self, request, value):
44 for model, manager in self.rev_lookups.items():
45 if (isinstance(value, model) and
46 self.obj in getattr(value, manager).all()):
51 class ModelMenuItem(MenuItem):
52 """Menu item corresponding to a model, optionally filtered by some fields."""
54 def __init__(self, model, url, field_lookups=None, title=None, html_id=None):
56 title = unicode(model)
57 super(ModelMenuItem, self).__init__(title=title, url=url, html_id=html_id)
59 self.field_lookups = field_lookups
61 def is_active(self, request, value):
62 if value == self.model and not self.field_lookups:
64 if isinstance(value, self.model) and self.field_lookups:
66 for field, lookup in self.field_lookups.items():
67 if getattr(value, field) != lookup:
74 return self.title or unicode(self.model)