Created
December 21, 2011 01:32
-
-
Save mhulse/1504127 to your computer and use it in GitHub Desktop.
Having problems using Treebeard with my django homebrew pages app...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import datetime | |
| from django.contrib import admin | |
| from treebeard.admin import TreeAdmin | |
| from dox.models import Page | |
| #-------------------------------------------------------------------------- | |
| # | |
| # Admin models: | |
| # | |
| #-------------------------------------------------------------------------- | |
| class PageAdmin(TreeAdmin): | |
| save_on_top = True | |
| def save_form(self, request, form, change): | |
| if not change: | |
| form.cleaned_data['created'] = datetime.datetime.now() | |
| return form.save(commit=False) | |
| list_display = ('title', 'url', 'template_name', 'parent',) | |
| list_editable = ('url', 'template_name', 'parent',) | |
| search_fields = ( | |
| 'url', | |
| 'title', | |
| 'content', | |
| 'template_name', | |
| 'meta_about', | |
| 'meta_notes', | |
| 'head_title', | |
| 'head_title_extra', | |
| 'head_description', | |
| 'head_keywords', | |
| 'head_ads', | |
| 'head_extra', | |
| 'description_short', | |
| 'description_long', | |
| 'footer_extra', | |
| 'ad_page_peel', | |
| 'ad_expanding_billboard', | |
| 'ad_leaderboard', | |
| 'ad_leaderboard_bottom', | |
| 'ad_medium_rectangle', | |
| 'ad_half_page', | |
| 'ad_skyscraper', | |
| 'ad_column_block', | |
| 'ad_column_block_bottom', | |
| 'ad_site_skin', | |
| ) | |
| fieldsets = [ | |
| ('Page', { | |
| 'fields': [ | |
| 'url', | |
| 'template_name', | |
| 'parent', | |
| ], | |
| }), | |
| ('Head', { | |
| 'fields': [ | |
| 'head_title', | |
| 'head_title_extra', | |
| 'head_description', | |
| 'head_keywords', | |
| 'head_ads', | |
| 'head_extra', | |
| ], 'classes': ['collapse'], | |
| }), | |
| ('Body', { | |
| 'fields': [ | |
| 'title', | |
| 'description_short', | |
| 'description_long', | |
| 'content', | |
| ], | |
| }), | |
| ('Ads', { | |
| 'fields': [ | |
| 'ad_page_peel', | |
| 'ad_expanding_billboard', | |
| 'ad_leaderboard', | |
| 'ad_leaderboard_bottom', | |
| 'ad_medium_rectangle', | |
| 'ad_half_page', | |
| 'ad_skyscraper', | |
| 'ad_column_block', | |
| 'ad_column_block_bottom', | |
| 'ad_site_skin', | |
| ], 'classes': ['collapse'], | |
| }), | |
| ('Meta', { | |
| 'fields': [ | |
| 'meta_about', | |
| 'meta_notes', | |
| ], 'classes': ['collapse'], | |
| }), | |
| ] | |
| #-------------------------------------------------------------------------- | |
| # | |
| # Admin model registers: | |
| # | |
| #-------------------------------------------------------------------------- | |
| admin.site.register(Page, PageAdmin) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import datetime | |
| from django.db import models | |
| from django.utils.translation import ugettext_lazy as _ | |
| from treebeard.ns_tree import NS_Node | |
| # http://docs.djangoproject.com/en/dev/internals/contributing/#model-style | |
| # http://code.tabo.pe/django-treebeard/src/93b579395a9c/tbexample | |
| class Page(NS_Node): | |
| """ | |
| created = models.DateTimeField(_(u'Created'), editable=False) | |
| modified = models.DateTimeField(_(u'Modified'), editable=False) | |
| """ | |
| created = models.DateTimeField(editable=False) | |
| #---------------------------------- | |
| # Treebeard: | |
| #---------------------------------- | |
| parent = models.ForeignKey('self', blank=True, null=True, db_index=True, related_name='children') | |
| #---------------------------------- | |
| # FlatPage fields: | |
| #---------------------------------- | |
| # https://code.djangoproject.com/browser/django/trunk/django/contrib/flatpages/models.py | |
| url = models.CharField(_('URL'), max_length=100, db_index=True) | |
| title = models.CharField(_('title'), max_length=200) | |
| content = models.TextField(_('content'), blank=True) | |
| # enable_comments = models.BooleanField(_('enable comments')) | |
| template_name = models.CharField(_('template name'), max_length=70, blank=True, help_text=_("Example: 'files/contact_page.html'. If this isn't provided, the system will use 'files/default.html'.")) | |
| #---------------------------------- | |
| # Head: | |
| #---------------------------------- | |
| meta_about = models.TextField(_(u'About'), blank=True) | |
| meta_notes = models.TextField(_(u'Notes'), blank=True) | |
| head_title = models.CharField(_(u'Title'), max_length=255, blank=True) | |
| head_title_extra = models.CharField(_(u'Title extra'), max_length=255, blank=True) | |
| head_description = models.CharField(_(u'Description'), max_length=255, blank=True) | |
| head_keywords = models.CharField(_(u'Keywords'), max_length=255, blank=True) | |
| head_ads = models.TextField(_(u'Ads'), blank=True) | |
| head_extra = models.TextField(_(u'Extra'), blank=True) | |
| description_short = models.CharField(_(u'Short description'), max_length=255, blank=True) | |
| description_long = models.TextField(_(u'Long description'), blank=True) | |
| #---------------------------------- | |
| # Body: | |
| #---------------------------------- | |
| footer_extra = models.TextField(_(u'Footer'), blank=True) | |
| ad_page_peel = models.CharField(_(u'Page Peel'), max_length=255, blank=True) | |
| ad_expanding_billboard = models.CharField(_(u'Expanding Billboard'), max_length=255, blank=True) | |
| ad_leaderboard = models.CharField(_(u'Leaderboard'), max_length=255, blank=True) | |
| ad_leaderboard_bottom = models.CharField(_(u'Leaderboard • bottom'), max_length=255, blank=True) | |
| ad_medium_rectangle = models.CharField(_(u'Medium Rectangle'), max_length=255, blank=True) | |
| ad_half_page = models.CharField(_(u'Half Page'), max_length=255, blank=True) | |
| ad_skyscraper = models.CharField(_(u'Skyscraper'), max_length=255, blank=True) | |
| ad_column_block = models.CharField(_(u'Column Block'), max_length=255, blank=True) | |
| ad_column_block_bottom = models.CharField(_(u'Column Block • bottom'), max_length=255, blank=True) | |
| ad_site_skin = models.CharField(_(u'Site Skin'), max_length=255, blank=True) | |
| node_order_by = ['created'] | |
| class Meta: | |
| verbose_name = _('dox') | |
| verbose_name_plural = _('dox\'s') | |
| ordering = ['tree_id', 'lft'] | |
| """ | |
| def save(self, *args, **kwargs): | |
| if not self.pk: | |
| self.created = datetime.datetime.now() | |
| self.modified = datetime.datetime.now() | |
| super(Page, self).save(*args, **kwargs) | |
| @property | |
| def is_modified(self): | |
| if self.modified.date() > self.created.date(): | |
| return True | |
| return False | |
| """ | |
| @models.permalink | |
| def get_absolute_url(self): | |
| return self.url | |
| def __unicode__(self): | |
| return u"%s -- %s" % (self.url, self.title) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.conf.urls.defaults import * | |
| urlpatterns = patterns('dox.views', | |
| (r'^(?P<url>.*)$', 'page'), | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django import shortcuts, template | |
| from django.template import loader, RequestContext | |
| from django.shortcuts import get_object_or_404 | |
| from django.http import Http404, HttpResponse, HttpResponsePermanentRedirect | |
| from django.conf import settings | |
| from django.core.xheaders import populate_xheaders | |
| from django.utils.safestring import mark_safe | |
| from django.views.decorators.csrf import csrf_protect | |
| from dox.models import Page | |
| # Copied from: | |
| # https://code.djangoproject.com/browser/django/trunk/django/contrib/flatpages/views.py | |
| DEFAULT_TEMPLATE = 'files/default.html' | |
| def page(request, url): | |
| if not url.startswith('/'): | |
| url = '/' + url | |
| try: | |
| f = get_object_or_404(Page, url__exact=url) | |
| except Http404: | |
| if not url.endswith('/') and settings.APPEND_SLASH: | |
| url += '/' | |
| f = get_object_or_404(Page, url__exact=url) | |
| return HttpResponsePermanentRedirect('%s/' % request.path) | |
| else: | |
| raise | |
| return render_page(request, f) | |
| @csrf_protect | |
| def render_page(request, f): | |
| if f.template_name: | |
| t = loader.select_template((f.template_name, DEFAULT_TEMPLATE)) | |
| else: | |
| t = loader.get_template(DEFAULT_TEMPLATE) | |
| f.title = mark_safe(f.title) | |
| f.content = mark_safe(f.content) | |
| c = RequestContext(request, { | |
| 'page': f, | |
| }) | |
| response = HttpResponse(t.render(c)) | |
| populate_xheaders(request, response, File, f.id) | |
| return response |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I keep getting:
When I remove "fieldsets" from the admin, everything works great.
What am I missing here?