Permalink
Browse files

sampleapp refactor for new menu structure

  • Loading branch information...
1 parent 27b30c2 commit aeccf5bf0570f16d3acd33bb3eb39b6c983b20c4 @digi604 digi604 committed Jan 18, 2010
View
0 example/categories/__init__.py
No changes.
View
9 example/categories/admin.py
@@ -1,9 +0,0 @@
-from django.contrib import admin
-from categories.models import Category
-from reversion.admin import VersionAdmin
-
-
-class CategoryAdmin(VersionAdmin):
- pass
-
-admin.site.register(Category, CategoryAdmin)
View
27 example/categories/models.py
@@ -1,27 +0,0 @@
-from django.db import models
-from django.core.urlresolvers import reverse
-import mptt
-
-class Category(models.Model):
- parent = models.ForeignKey('self', blank=True, null=True)
- name = models.CharField(max_length=20)
-
- def __unicode__(self):
- return self.name
-
- def get_title(self):
- return self.name
-
- def get_menu_title(self):
- return self.name
-
- def get_absolute_url(self):
- return reverse('category_view', args=[self.pk])
-
- class Meta:
- verbose_name_plural = 'categories'
-
-try:
- mptt.register(Category)
-except mptt.AlreadyRegistered:
- pass
View
18 example/categories/navigation.py
@@ -1,18 +0,0 @@
-from categories.models import Category
-
-def get_nodes(request):
- cats = list(Category.objects.all())
- res = []
- all_cats = cats[:]
- childs = []
- for cat in cats:
- if cat.parent_id:
- childs.append(cat)
- else:
- res.append(cat)
- for cat in all_cats:
- cat.childrens = []
- for child in childs:
- if child.parent_id == cat.pk:
- cat.childrens.append(child)
- return res
View
5 example/categories/templates/categories/category_view.html
@@ -1,5 +0,0 @@
-{% extends 'index.html' %}
-
-{% block content %}
-<h1>{{ category.get_title }}</h1>
-{% endblock %}
View
5 example/categories/urls.py
@@ -1,5 +0,0 @@
-from django.conf.urls.defaults import *
-
-urlpatterns = patterns('categories.views',
- url(r'(?P<id>[0-9]+)/$', 'category_view', name='category_view'),
-)
View
7 example/categories/views.py
@@ -1,7 +0,0 @@
-from django.shortcuts import render_to_response
-from categories.models import Category
-from django.template.context import RequestContext
-
-
-def category_view(request, id):
- return render_to_response('categories/category_view.html', RequestContext(request, {'category':Category.objects.get(pk=id)}))
View
10 example/sampleapp/admin.py
@@ -0,0 +1,10 @@
+from django.contrib import admin
+from example.sampleapp.models import Picture, Category
+
+class PictureInline(admin.StackedInline):
+ model = Picture
+
+class CategoryAdmin(admin.ModelAdmin):
+ inlines = [PictureInline]
+
+admin.site.register(Category, CategoryAdmin)
View
36 example/sampleapp/menu.py
@@ -0,0 +1,36 @@
+from menus.base import Menu, NavigationNode
+from example.sampleapp.models import Category
+from django.core.urlresolvers import reverse
+from menus.menu_pool import menu_pool
+
+
+class CategoryMenu(Menu):
+ def get_nodes(self, request):
+ nodes = []
+ for cat in Category.objects.all():
+ n = NavigationNode(cat.name, cat.get_absolute_url(), "sampleapp", cat.pk, cat.parent_id, "sampleapp")
+ nodes.append(n)
+ return nodes
+
+menu_pool.register_menu(CategoryMenu)
+
+
+class StaticMenu(Menu):
+ def get_nodes(self, request):
+ nodes = []
+ n = NavigationNode(_('sample root page'), reverse('sample-root'), 1, "static")
+ n2 = NavigationNode(_('sample settings page'), reverse('sample-settings'), 2, "static")
+ n3 = NavigationNode(_('sample account page'), reverse('sample-account'), 3, "static")
+ n4 = NavigationNode(_('sample my profile page'), reverse('sample-profile'), 4, "static", 3, "static")
+ nodes.append(n)
+ nodes.append(n2)
+ nodes.append(n3)
+ nodes.append(n4)
+ return nodes
+
+menu_pool.register_menu(StaticMenu)
+
+class SampleApp(CMSApp):
+ name = _("Sample App")
+ urls = "sampleapp.urls"
+ menus = [CategoryMenu, StaticMenu]
View
20 example/sampleapp/menu_extender.py
@@ -12,3 +12,23 @@ def get_nodes(request):
n = NavigationNode(_('Sublevel 2'), reverse('%s:sample-app-sublevel2' % lang))
res.append(n)
return res
+
+
+from categories.models import Category
+
+def get_nodes(request):
+ cats = list(Category.objects.all())
+ res = []
+ all_cats = cats[:]
+ childs = []
+ for cat in cats:
+ if cat.parent_id:
+ childs.append(cat)
+ else:
+ res.append(cat)
+ for cat in all_cats:
+ cat.childrens = []
+ for child in childs:
+ if child.parent_id == cat.pk:
+ cat.childrens.append(child)
+ return res
View
24 example/sampleapp/models.py
@@ -1,3 +1,25 @@
from django.db import models
+from django.core.urlresolvers import reverse
+import mptt
-# Create your models here.
+class Category(models.Model):
+ parent = models.ForeignKey('self', blank=True, null=True)
+ name = models.CharField(max_length=20)
+
+ def __unicode__(self):
+ return self.name
+
+ def get_absolute_url(self):
+ return reverse('category_view', args=[self.pk])
+
+ class Meta:
+ verbose_name_plural = 'categories'
+
+try:
+ mptt.register(Category)
+except mptt.AlreadyRegistered:
+ pass
+
+class Picture(models.Model):
+ image = models.ImageField(upload_to="pictures")
+ category = models.ForeignKey(Category)
View
6 example/sampleapp/templates/sampleapp/home.html
@@ -7,13 +7,13 @@
{{ block.super }}
<ul>
<li>
- <a href="{% url sample-app-sublevel %}">Sample app sublevel reverse test</a>
+ <a href="{% url sample-root %}">Sample app root reverse test</a>
</li>
<li>
- <a href="{% url de:sample-app-sublevel %}">Sample app sublevel reverse test de</a>
+ <a href="{% url de:sample-account %}">Sample app account reverse test german</a>
</li>
<li>
- <a href="{% url en:sample-app-sublevel %}">Sample app sublevel reverse test en</a>
+ <a href="{% url en:sample-account %}">Sample app account reverse test english</a>
</li>
</ul>
<h4>Sample image - appmedia works?</h4>
View
7 example/sampleapp/urls.py
@@ -1,6 +1,9 @@
from django.conf.urls.defaults import *
urlpatterns = patterns('sampleapp.views',
- (r'^$', 'sample_view', {'message': 'urls.py => root',}),
- url(r'^sublevel/$', 'sample_view', kwargs={'message': 'urls.py => sublevel'}, name='sample-app-sublevel'),
+ url(r'^$', 'sample_view', {'message': 'sample root page',}, name='sample-root'),
+ url(r'^settings/$', 'sample_view', kwargs={'message': 'sample settings page'}, name='sample-settings'),
+ url(r'^account/$', 'sample_view', {'message': 'sample account page'}, name='sample-account'),
+ url(r'^account/my_profile/$', 'sample_view', {'message': 'sample my profile page'}, name='sample-profile'),
+ url(r'(?P<id>[0-9]+)/$', 'category_view', name='category_view'),
)
View
8 example/sampleapp/urlstwo.py
@@ -1,8 +0,0 @@
-from django.conf.urls.defaults import *
-
-urlpatterns = patterns('sampleapp.views',
- (r'^$', 'sample_view', {'message': 'urlstwo.py => root'}),
- url(r'^sublevel/$', 'sample_view', {'message': 'urlstwo.py => sublevel1'}, name='sample-app-sublevel'),
- url(r'^sublevel2/$', 'sample_view', {'message': 'urlstwo.py => sublevel2'}, name='sample-app-sublevel2'),
- url(r'^sublevel/sublevel3/$', 'sample_view', {'message': 'urlstwo.py => sublevel3'}, name='sample-app-sublevel3'),
-)
View
4 example/sampleapp/views.py
@@ -1,7 +1,11 @@
# Create your views here.
from django.shortcuts import render_to_response
from django.template.context import RequestContext
+from example.sampleapp.models import Category
def sample_view(request, **kw):
context = RequestContext(request, kw)
return render_to_response("sampleapp/home.html", context)
+
+def category_view(request, id):
+ return render_to_response('sampleapp/category_view.html', RequestContext(request, {'category':Category.objects.get(pk=id)}))
View
2 example/store/__init__.py
@@ -1,2 +0,0 @@
-"""Sample application for testing admin inlines.
-"""
View
26 example/store/cms_plugins.py
@@ -1,26 +0,0 @@
-from django.contrib import admin
-from cms.plugin_pool import plugin_pool
-from cms.plugin_base import CMSPluginBase
-from django.utils.translation import ugettext_lazy as _
-from example.store.models import Store, StoreItem
-
-from django.contrib import admin
-
-class StoreItemInlineAdmin(admin.TabularInline):
- model = StoreItem
-
-
-class StorePlugin(CMSPluginBase):
- model = Store
- name = _("Store")
-
- inlines = [
- StoreItemInlineAdmin,
- ]
-
- render_template = "store/plugins/store.html"
-
- def render(self, context, instance, placeholder):
- return {}
-
-plugin_pool.register_plugin(StorePlugin)
View
43 example/store/models.py
@@ -1,43 +0,0 @@
-from django.conf import settings
-from django.db import models
-from django.utils.translation import ugettext_lazy as _
-from cms.models import CMSPlugin
-from publisher import Publisher
-
-if 'reversion' in settings.INSTALLED_APPS:
- import reversion
-
-class Store(CMSPlugin):
- """Simple store example - for testing admin inlines
- """
- name = models.CharField(_('name'), max_length=32)
-
- class Meta:
- verbose_name=_('Store')
- verbose_name_plural=_('Store')
-
- __unicode__ = lambda self: self.name
-
-
-class StoreItem(Publisher):
- """Store item examle
- """
- store = models.ForeignKey(Store, verbose_name=_('store item'))
- name = models.CharField(_('name'), max_length=32)
- price = models.DecimalField(_('price'), max_digits=10, decimal_places=4)
-
- class Meta:
- verbose_name=_('Store item')
- verbose_name_plural=_('Store items')
-
- __unicode__ = lambda self: self.name
-
-
-if 'reversion' in settings.INSTALLED_APPS:
- try:
- # seems there is some issue with reversion, probably this get imported
- # more than once, so avoid multiple registrations
- reversion.register(Store, follow=["cmsplugin_ptr", "storeitem_set"])
- reversion.register(StoreItem)
- except:
- pass
View
1 example/store/templates/store/plugins/store.html
@@ -1 +0,0 @@
-<h3>MY SAMPLE STORE</h3>
View
8 menus/base.py
@@ -4,12 +4,12 @@ class Menu(object):
nodes = []
def get_nodes(self, request):
+ """
+ should return a list of NavigationNode instances
+ """
raise NotImplementedError
- def get_node_by_id(self, id):
- for node in self.nodes:
- if node.id == id:
- return node
+
class Modifier(object):
pre_cut = True
View
12 menus/templatetags/menu_tags.py
@@ -39,9 +39,9 @@ def cut_levels(nodes, from_level, to_level, extra_inactive, extra_active):
if node in removed:
final.remove(node)
return final
-
register = template.Library()
+
def show_menu(context, from_level=0, to_level=100, extra_inactive=0, extra_active=100, template="menu/menu.html", namespace=None, root_id=None, next_page=None, ):
"""
render a nested list of all children of the pages
@@ -86,6 +86,7 @@ def show_menu(context, from_level=0, to_level=100, extra_inactive=0, extra_activ
return context
show_menu = register.inclusion_tag('cms/dummy.html', takes_context=True)(show_menu)
+
def show_menu_below_id(context, root_id=None, from_level=0, to_level=100, extra_inactive=100, extra_active=100, template_file="cms/menu.html", namespace=None, next_page=None):
"""
displays a menu below a node that has an uid
@@ -94,7 +95,6 @@ def show_menu_below_id(context, root_id=None, from_level=0, to_level=100, extra_
register.inclusion_tag('cms/dummy.html', takes_context=True)(show_menu_below_id)
-
def show_sub_menu(context, levels=100, template="menu/sub_menu.html"):
"""
show the sub menu of the current nav-node.
@@ -167,9 +167,7 @@ def show_breadcrumb(context, start_level=0, template="menu/breadcrumb.html"):
return context
show_breadcrumb = register.inclusion_tag('cms/dummy.html',
takes_context=True)(show_breadcrumb)
-
-
-
+
def language_chooser(context, template="menu/language_chooser.html"):
"""
@@ -195,6 +193,7 @@ def language_chooser(context, template="menu/language_chooser.html"):
return context
language_chooser = register.inclusion_tag('cms/dummy.html', takes_context=True)(language_chooser)
+
def page_language_url(context, lang):
"""
Displays the url of the current page in the defined language.
@@ -219,6 +218,3 @@ def page_language_url(context, lang):
return {'content':''}
page_language_url = register.inclusion_tag('cms/content.html', takes_context=True)(page_language_url)
-
-
-

0 comments on commit aeccf5b

Please sign in to comment.