fixed menu
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Thu, 10 Jan 2013 14:21:33 +0000 (15:21 +0100)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Thu, 10 Jan 2013 14:21:33 +0000 (15:21 +0100)
menu/helpers.py
menu/templates/menu/menu.html
prawokultury/menu_items.py
requirements.txt

index e94cbd8..15d8646 100755 (executable)
@@ -1,18 +1,27 @@
 class MenuItem(object):
     html_id = None
 
 class MenuItem(object):
     html_id = None
 
+    class InvalidMenuItem(BaseException):
+        pass
+
     def __init__(self, url, title, html_id=None, more_urls=None):
         self.url = url
         self.title = title
         self.html_id = html_id
         self.more_urls = more_urls or set()
 
     def __init__(self, url, title, html_id=None, more_urls=None):
         self.url = url
         self.title = title
         self.html_id = html_id
         self.more_urls = more_urls or set()
 
+    def is_valid(self):
+        return True
+
     def is_active(self, request, value):
         url = request.get_full_path()
         return url == str(self.url) or url in set(str(url) for url in self.more_urls)
 
     def check_active(self, request, value):
     def is_active(self, request, value):
         url = request.get_full_path()
         return url == str(self.url) or url in set(str(url) for url in self.more_urls)
 
     def check_active(self, request, value):
-        self.active = self.is_active(request, value)
+        try:
+            self.active = self.is_active(request, value)
+        except self.InvalidMenuItem, e:
+            pass
 
     def get_url(self):
         return self.url
 
     def get_url(self):
         return self.url
@@ -27,25 +36,40 @@ class ObjectMenuItem(MenuItem):
     If no url or title is provided, get_absolute_url and __unicode__ are used.
     You can also provide a reverse lookup dictionary, as in {model: field_name}.
     """
     If no url or title is provided, get_absolute_url and __unicode__ are used.
     You can also provide a reverse lookup dictionary, as in {model: field_name}.
     """
-    def __init__(self, obj, url=None, rev_lookups=None, title=None, html_id=None):
+    def __init__(self,
+            obj=None, obj_get=None,
+            url=None, rev_lookups=None, title=None, html_id=None):
         super(ObjectMenuItem, self).__init__(url=url, title=title, html_id=html_id)
         self.obj = obj
         super(ObjectMenuItem, self).__init__(url=url, title=title, html_id=html_id)
         self.obj = obj
+        self.obj_get = obj_get
         self.rev_lookups = rev_lookups
 
         self.rev_lookups = rev_lookups
 
+    def get_object(self):
+        try:
+            return self.obj or self.obj_get()
+        except BaseException, e:
+            raise self.InvalidMenuItem(e)
+
     def get_title(self):
     def get_title(self):
-        return self.title or unicode(self.obj)
+        return self.title or unicode(self.get_object())
 
 
-    @property
     def get_url(self):
     def get_url(self):
-        return self.url or self.obj.get_absolute_url()
+        return self.url or self.get_object().get_absolute_url()
+
+    def is_valid(self):
+        try:
+            obj = self.get_object()
+        except self.InvalidMenuItem, e:
+            return False
+        return True
 
     def is_active(self, request, value):
 
     def is_active(self, request, value):
-        if value == self.obj:
+        if value == self.get_object():
             return True
         if self.rev_lookups:
             for model, manager in self.rev_lookups.items():
                 if (isinstance(value, model) and 
             return True
         if self.rev_lookups:
             for model, manager in self.rev_lookups.items():
                 if (isinstance(value, model) and 
-                        self.obj in getattr(value, manager).all()):
+                        self.get_object() in getattr(value, manager).all()):
                     return True
         return False
 
                     return True
         return False
 
index 56debe5..3de88d2 100755 (executable)
@@ -1,7 +1,9 @@
 {% load i18n %}
 {% for item in items %}
 {% load i18n %}
 {% for item in items %}
-    <li{% if item.active %} 
-        class="active"{%endif %} {% if item.html_id %}
-        id="{{ item.html_id}}"{% endif %}>
-        <a href="{{ item.get_url }}">{{ item.get_title }}</a></li>
+    {% if item.is_valid %}
+        <li{% if item.active %} 
+            class="active"{%endif %} {% if item.html_id %}
+            id="{{ item.html_id}}"{% endif %}>
+            <a href="{{ item.get_url }}">{{ item.get_title }}</a></li>
+    {% endif %}
 {% endfor %}
 {% endfor %}
index f68b005..f780c48 100644 (file)
@@ -7,16 +7,9 @@ from django.utils.translation import ugettext_lazy as _
 
 ITEMS = []
 
 
 ITEMS = []
 
-def add_entry(**qs):
-    try:
-        entry = Entry.objects.get(**qs)
-    except Entry.DoesNotExist:
-        return
-    if not entry.published:
-        return
-    ITEMS.append(ObjectMenuItem(entry))
-
-add_entry(slug_pl='o-nas')
+ITEMS.append(ObjectMenuItem(
+    obj_get=lambda:Entry.published_objects.get(slug_pl='o-nas')
+))
 
 ITEMS.append(ModelMenuItem(Entry, reverse_lazy('migdal_entry_list_publications'),
         field_lookups={'type': 'publications'}, title=_('Publications')))
 
 ITEMS.append(ModelMenuItem(Entry, reverse_lazy('migdal_entry_list_publications'),
         field_lookups={'type': 'publications'}, title=_('Publications')))
@@ -24,11 +17,13 @@ ITEMS.append(ModelMenuItem(Entry, reverse_lazy('migdal_entry_list_publications')
 ITEMS.append(MenuItem(reverse_lazy('events'), _('Events'),
         more_urls=(reverse_lazy('events_past'),)))
 
 ITEMS.append(MenuItem(reverse_lazy('events'), _('Events'),
         more_urls=(reverse_lazy('events_past'),)))
 
-ITEMS.append(ObjectMenuItem(Category.objects.get(slug_pl='stanowisko'),
-    rev_lookups={Entry: 'categories'}, title=_('Positions')))
-#CategoryMenuItem(Category.objects.get(slug_pl='stanowisko'),
-#            title=_('Positions')),
-
-ITEMS.append(ObjectMenuItem(Entry.objects.get(slug_pl='pierwsza-pomoc'),
-    title=_('First aid in copyright')))
+ITEMS.append(ObjectMenuItem(
+    obj_get=lambda:Category.objects.get(slug_pl='stanowisko'),
+    rev_lookups={Entry: 'categories'},
+    title=_('Positions')
+))
 
 
+ITEMS.append(ObjectMenuItem(
+    obj_get=lambda:Entry.published_objects.get(slug_pl='pierwsza-pomoc'),
+    title=_('First aid in copyright')
+))
index 9a4bc1a..6341005 100644 (file)
@@ -8,8 +8,8 @@ PIL
 sorl-thumbnail>=11.09,<12
 django-pagination
 
 sorl-thumbnail>=11.09,<12
 django-pagination
 
--e git+git://github.com/fnp/django-migdal.git@a3983327291548497d9bdd3db5976b895675b25e#egg=django-migdal
-fnpdjango>=0.1.3,<0.2
+django-migdal>=0.3,<0.4
+fnpdjango>=0.1.4,<0.2
 
 textile
 django-markupfield
 
 textile
 django-markupfield