forked from rc/aircox
		
	schedule section; page view with arbitrary content
This commit is contained in:
		@ -2,7 +2,7 @@ from django.contrib import admin
 | 
			
		||||
 | 
			
		||||
import aircox.cms.models as models
 | 
			
		||||
 | 
			
		||||
admin.site.register(cms.Article)
 | 
			
		||||
admin.site.register(cms.Comment)
 | 
			
		||||
admin.site.register(models.Article)
 | 
			
		||||
admin.site.register(models.Comment)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -38,10 +38,12 @@ class Routable:
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def route_url(cl, route, kwargs = None):
 | 
			
		||||
    def route_url(cl, route, **kwargs):
 | 
			
		||||
        name = cl._website.name_of_model(cl)
 | 
			
		||||
        name = route.get_view_name(name)
 | 
			
		||||
        return reverse(name, kwargs = kwargs)
 | 
			
		||||
        r = reverse(name, kwargs = kwargs)
 | 
			
		||||
        return r
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Comment(models.Model, Routable):
 | 
			
		||||
    thread_type = models.ForeignKey(
 | 
			
		||||
@ -162,8 +164,10 @@ class Post (models.Model, Routable):
 | 
			
		||||
        return qs
 | 
			
		||||
 | 
			
		||||
    def detail_url(self):
 | 
			
		||||
        return self.route_url(routes.DetailRoute,
 | 
			
		||||
                              { 'pk': self.pk, 'slug': slugify(self.title) })
 | 
			
		||||
        return self.route_url(
 | 
			
		||||
            routes.DetailRoute,
 | 
			
		||||
            pk = self.pk, slug = slugify(self.title)
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    def get_object_list(self, request, object, **kwargs):
 | 
			
		||||
        type = ContentType.objects.get_for_model(object)
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,5 @@
 | 
			
		||||
from django.db import models
 | 
			
		||||
from django.utils import timezone as tz
 | 
			
		||||
from django.conf.urls import url
 | 
			
		||||
from django.contrib.contenttypes.models import ContentType
 | 
			
		||||
from django.utils import timezone
 | 
			
		||||
@ -137,8 +138,8 @@ class DateRoute(Route):
 | 
			
		||||
    name = 'date'
 | 
			
		||||
    url_args = [
 | 
			
		||||
        ('year', '[0-9]{4}'),
 | 
			
		||||
        ('month', '[0-9]{2}'),
 | 
			
		||||
        ('day', '[0-9]{1,2}'),
 | 
			
		||||
        ('month', '[0-1]?[0-9]'),
 | 
			
		||||
        ('day', '[0-3]?[0-9]'),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
@ -151,11 +152,10 @@ class DateRoute(Route):
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def get_title(cl, model, request, year, month, day, **kwargs):
 | 
			
		||||
        return _('%(model)s of %(year)/%(month)/%(day)') % {
 | 
			
		||||
        date = tz.datetime(year = int(year), month = int(month), day = int(day))
 | 
			
		||||
        return _('%(model)s of %(date)s') % {
 | 
			
		||||
            'model': model._meta.verbose_name_plural,
 | 
			
		||||
            'year': year,
 | 
			
		||||
            'month': month,
 | 
			
		||||
            'day': day
 | 
			
		||||
            'date': date.strftime('%A %d %B %Y'),
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										60
									
								
								cms/views.py
									
									
									
									
									
								
							
							
						
						
									
										60
									
								
								cms/views.py
									
									
									
									
									
								
							@ -1,7 +1,7 @@
 | 
			
		||||
from django.templatetags.static import static
 | 
			
		||||
from django.template.loader import render_to_string
 | 
			
		||||
from django.views.generic import ListView, DetailView
 | 
			
		||||
from django.views.generic.base import View
 | 
			
		||||
from django.views.generic.base import View, TemplateView
 | 
			
		||||
from django.utils.translation import ugettext as _, ugettext_lazy
 | 
			
		||||
from django.contrib import messages
 | 
			
		||||
from django.http import Http404
 | 
			
		||||
@ -13,7 +13,8 @@ class PostBaseView:
 | 
			
		||||
    website = None  # corresponding website
 | 
			
		||||
    title = ''      # title of the page
 | 
			
		||||
    embed = False   # page is embed (if True, only post content is printed
 | 
			
		||||
    classes = ''    # extra classes for the content
 | 
			
		||||
    attrs = ''      # attr for the HTML element of the content
 | 
			
		||||
    css_classes = ''# css classes for the HTML element of the content
 | 
			
		||||
 | 
			
		||||
    def get_base_context(self, **kwargs):
 | 
			
		||||
        """
 | 
			
		||||
@ -108,11 +109,13 @@ class PostListView(PostBaseView, ListView):
 | 
			
		||||
                    self.route.get_title(self.model, self.request,
 | 
			
		||||
                                         **self.kwargs)
 | 
			
		||||
 | 
			
		||||
        context['title'] = title
 | 
			
		||||
        context['base_template'] = 'aircox/cms/website.html'
 | 
			
		||||
        context['css_class'] = 'list' if not self.css_class else \
 | 
			
		||||
                               'list ' + self.css_class
 | 
			
		||||
        context['list'] = self.list
 | 
			
		||||
        context.update({
 | 
			
		||||
        'title': title,
 | 
			
		||||
        'base_template': 'aircox/cms/website.html',
 | 
			
		||||
        'css_class': 'list' if not self.css_class else \
 | 
			
		||||
                     'list ' + self.css_class,
 | 
			
		||||
        'list': self.list,
 | 
			
		||||
        })
 | 
			
		||||
        # FIXME: list.url = if self.route: self.model(self.route, self.kwargs) else ''
 | 
			
		||||
        return context
 | 
			
		||||
 | 
			
		||||
@ -155,11 +158,13 @@ class PostDetailView(DetailView, PostBaseView):
 | 
			
		||||
        context.update(self.get_base_context())
 | 
			
		||||
 | 
			
		||||
        kwargs['object'] = self.object
 | 
			
		||||
        context['content'] = ''.join([
 | 
			
		||||
            section.get(request = self.request, **kwargs)
 | 
			
		||||
            for section in self.sections
 | 
			
		||||
        ])
 | 
			
		||||
        context['css_class'] = 'detail'
 | 
			
		||||
        context.update({
 | 
			
		||||
            'content': ''.join([
 | 
			
		||||
                section.get(request = self.request, **kwargs)
 | 
			
		||||
                for section in self.sections
 | 
			
		||||
            ]),
 | 
			
		||||
            'css_class': 'detail',
 | 
			
		||||
        })
 | 
			
		||||
        return context
 | 
			
		||||
 | 
			
		||||
    def post(self, request, *args, **kwargs):
 | 
			
		||||
@ -176,3 +181,34 @@ class PostDetailView(DetailView, PostBaseView):
 | 
			
		||||
        return self.get(request, *args, **kwargs)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class PageView(TemplateView, PostBaseView):
 | 
			
		||||
    """
 | 
			
		||||
    A simple page view. Used to render pages that have arbitrary content
 | 
			
		||||
    without linked post object.
 | 
			
		||||
    """
 | 
			
		||||
    template_name = 'aircox/cms/detail.html'
 | 
			
		||||
 | 
			
		||||
    sections = []
 | 
			
		||||
    css_class = 'page'
 | 
			
		||||
 | 
			
		||||
    def __init__(self, *args, **kwargs):
 | 
			
		||||
        css_class = 'css_class' in kwargs and kwargs.pop('css_class')
 | 
			
		||||
        if css_class:
 | 
			
		||||
            self.css_class += ' ' + css_class
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
 | 
			
		||||
    def get_context_data(self, **kwargs):
 | 
			
		||||
        context = super().get_context_data(**kwargs)
 | 
			
		||||
        context.update(self.get_base_context())
 | 
			
		||||
 | 
			
		||||
        context.update({
 | 
			
		||||
            'title': self.title,
 | 
			
		||||
            'content': ''.join([
 | 
			
		||||
                section.get(request = self.request, **kwargs)
 | 
			
		||||
                for section in self.sections
 | 
			
		||||
            ]),
 | 
			
		||||
        })
 | 
			
		||||
        return context
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,6 @@
 | 
			
		||||
from django.utils.text import slugify
 | 
			
		||||
from django.conf.urls import url
 | 
			
		||||
 | 
			
		||||
import aircox.cms.routes as routes
 | 
			
		||||
import aircox.cms.views as views
 | 
			
		||||
 | 
			
		||||
@ -104,6 +107,22 @@ class Website:
 | 
			
		||||
        self.urls += [ route.as_url(name, view) for route in routes ]
 | 
			
		||||
        self.registry[name] = model
 | 
			
		||||
 | 
			
		||||
    def register_page(self, name, view = views.PageView, path = None,
 | 
			
		||||
                      **view_kwargs):
 | 
			
		||||
        """
 | 
			
		||||
        Register a page that is accessible to the given path. If path is None,
 | 
			
		||||
        use a slug of the name.
 | 
			
		||||
        """
 | 
			
		||||
        view = view.as_view(
 | 
			
		||||
            website = self,
 | 
			
		||||
            **view_kwargs
 | 
			
		||||
        )
 | 
			
		||||
        self.urls.append(url(
 | 
			
		||||
            slugify(name) if path is None else path,
 | 
			
		||||
            view = view,
 | 
			
		||||
            name = name,
 | 
			
		||||
        ))
 | 
			
		||||
 | 
			
		||||
    def register(self, name, model, sections = None, routes = None,
 | 
			
		||||
                  list_view = views.PostListView,
 | 
			
		||||
                  detail_view = views.PostDetailView,
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user