1 class MenuItem(object):
4 def __init__(self, url, title, html_id=None, more_urls=None):
8 self.more_urls = more_urls or set()
10 def is_active(self, request, value):
11 url = request.get_full_path()
12 return url == str(self.url) or url in set(str(url) for url in self.more_urls)
14 def check_active(self, request, value):
15 self.active = self.is_active(request, value)
24 class ObjectMenuItem(MenuItem):
25 """Menu item corresponding to an object.
27 If no url or title is provided, get_absolute_url and __unicode__ are used.
28 You can also provide a reverse lookup dictionary, as in {model: field_name}.
30 def __init__(self, obj, url=None, rev_lookups=None, title=None, html_id=None):
31 super(ObjectMenuItem, self).__init__(url=url, title=title, html_id=html_id)
33 self.rev_lookups = rev_lookups
36 return self.title or unicode(self.obj)
40 return self.url or self.obj.get_absolute_url()
42 def is_active(self, request, value):
46 for model, manager in self.rev_lookups.items():
47 if (isinstance(value, model) and
48 self.obj in getattr(value, manager).all()):
53 class ModelMenuItem(MenuItem):
54 """Menu item corresponding to a model, optionally filtered by some fields."""
56 def __init__(self, model, url, field_lookups=None, title=None, html_id=None):
58 title = unicode(model)
59 super(ModelMenuItem, self).__init__(title=title, url=url, html_id=html_id)
61 self.field_lookups = field_lookups
63 def is_active(self, request, value):
64 if value == self.model and not self.field_lookups:
66 if str(self.url) == request.get_full_path():
68 if isinstance(value, self.model) and self.field_lookups:
70 for field, lookup in self.field_lookups.items():
71 if getattr(value, field) != lookup:
78 return self.title or unicode(self.model)