forked from rc/aircox
		
	split website and cms; work on sections
This commit is contained in:
		@ -28,8 +28,8 @@ def add_inline (base_model, post_model, prepend = False):
 | 
			
		||||
    registry[base_model].inlines = inlines
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
add_inline(programs.Program, ProgramPost)
 | 
			
		||||
add_inline(programs.Episode, EpisodePost)
 | 
			
		||||
add_inline(programs.Program, Program)
 | 
			
		||||
add_inline(programs.Episode, Episode)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#class ArticleAdmin (DescriptionAdmin):
 | 
			
		||||
 | 
			
		||||
@ -1,169 +1,21 @@
 | 
			
		||||
from django.db import models
 | 
			
		||||
from django.contrib.auth.models import User
 | 
			
		||||
from django.contrib.contenttypes.fields import GenericForeignKey
 | 
			
		||||
from django.contrib.contenttypes.models import ContentType
 | 
			
		||||
from django.utils.translation import ugettext as _, ugettext_lazy
 | 
			
		||||
from django.utils import timezone
 | 
			
		||||
 | 
			
		||||
from django.db.models.signals import post_save
 | 
			
		||||
from django.dispatch import receiver
 | 
			
		||||
 | 
			
		||||
from cms.models import RelatedPost
 | 
			
		||||
import programs.models as programs
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Thread (models.Model):
 | 
			
		||||
    post_type = models.ForeignKey(ContentType)
 | 
			
		||||
    post_id = models.PositiveIntegerField()
 | 
			
		||||
    post = GenericForeignKey('post_type', 'post_id')
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def get (cl, model, **kwargs):
 | 
			
		||||
        post_type = ContentType.objects.get_for_model(model)
 | 
			
		||||
        return cl.objects.get(post_type__pk = post_type.id,
 | 
			
		||||
                              **kwargs)
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def filter (cl, model, **kwargs):
 | 
			
		||||
        post_type = ContentType.objects.get_for_model(model)
 | 
			
		||||
        return cl.objects.filter(post_type__pk = post_type.id,
 | 
			
		||||
                              **kwargs)
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def exclude (cl, model, **kwargs):
 | 
			
		||||
        post_type = ContentType.objects.get_for_model(model)
 | 
			
		||||
        return cl.objects.exclude(post_type__pk = post_type.id,
 | 
			
		||||
                              **kwargs)
 | 
			
		||||
 | 
			
		||||
    def __str__ (self):
 | 
			
		||||
        return str(self.post)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class BasePost (models.Model):
 | 
			
		||||
    thread = models.ForeignKey(
 | 
			
		||||
        Thread,
 | 
			
		||||
        on_delete=models.SET_NULL,
 | 
			
		||||
        blank = True, null = True,
 | 
			
		||||
        help_text = _('the publication is posted on this thread'),
 | 
			
		||||
    )
 | 
			
		||||
    author = models.ForeignKey(
 | 
			
		||||
        User,
 | 
			
		||||
        verbose_name = _('author'),
 | 
			
		||||
        blank = True, null = True,
 | 
			
		||||
    )
 | 
			
		||||
    date = models.DateTimeField(
 | 
			
		||||
        _('date'),
 | 
			
		||||
        default = timezone.datetime.now
 | 
			
		||||
    )
 | 
			
		||||
    public = models.BooleanField(
 | 
			
		||||
        verbose_name = _('public'),
 | 
			
		||||
        default = True
 | 
			
		||||
    )
 | 
			
		||||
    image = models.ImageField(
 | 
			
		||||
        blank = True, null = True
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    title = ''
 | 
			
		||||
    content = ''
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        abstract = True
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@receiver(post_save)
 | 
			
		||||
def on_new_post (sender, instance, created, *args, **kwargs):
 | 
			
		||||
    """
 | 
			
		||||
    Signal handler to create a thread that is attached to the newly post
 | 
			
		||||
    """
 | 
			
		||||
    if not issubclass(sender, BasePost) or not created:
 | 
			
		||||
        return
 | 
			
		||||
 | 
			
		||||
    thread = Thread(post = instance)
 | 
			
		||||
    thread.save()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Post (BasePost):
 | 
			
		||||
    class Meta:
 | 
			
		||||
        abstract = True
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def create_related_post (model, maps):
 | 
			
		||||
        """
 | 
			
		||||
        Create a subclass of BasePost model, that binds the common-fields
 | 
			
		||||
        using the given maps. The maps' keys are the property to change, and
 | 
			
		||||
        its value is the target model's attribute (or a callable)
 | 
			
		||||
        """
 | 
			
		||||
        class Meta:
 | 
			
		||||
            pass
 | 
			
		||||
 | 
			
		||||
        attrs = {
 | 
			
		||||
            '__module__': BasePost.__module__,
 | 
			
		||||
            'Meta': Meta,
 | 
			
		||||
            'related': models.ForeignKey(model),
 | 
			
		||||
            '__str__': lambda self: str(self.related)
 | 
			
		||||
class Program (RelatedPost):
 | 
			
		||||
    class Relation:
 | 
			
		||||
        related_model = programs.Program
 | 
			
		||||
        mapping = {
 | 
			
		||||
            'title': 'name',
 | 
			
		||||
            'content': 'description',
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        def get_prop (name, related_name):
 | 
			
		||||
            return property(related_name) if callable(related_name) \
 | 
			
		||||
                    else property(lambda self: getattr(self.related, related_name))
 | 
			
		||||
 | 
			
		||||
        attrs.update({
 | 
			
		||||
            name: get_prop(name, related_name)
 | 
			
		||||
                for name, related_name in maps.items()
 | 
			
		||||
        })
 | 
			
		||||
        return type(model.__name__ + 'Post', (BasePost,), attrs)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Article (BasePost):
 | 
			
		||||
    title = models.CharField(
 | 
			
		||||
        _('title'),
 | 
			
		||||
        max_length = 128,
 | 
			
		||||
        blank = False, null = False
 | 
			
		||||
    )
 | 
			
		||||
    content = models.TextField(
 | 
			
		||||
        _('content'),
 | 
			
		||||
        blank = False, null = False
 | 
			
		||||
    )
 | 
			
		||||
    static_page = models.BooleanField(
 | 
			
		||||
        _('static page'),
 | 
			
		||||
        default = False,
 | 
			
		||||
    )
 | 
			
		||||
    focus = models.BooleanField(
 | 
			
		||||
        _('article is focus'),
 | 
			
		||||
        default = False,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        verbose_name = _('Article')
 | 
			
		||||
        verbose_name_plural = _('Articles')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
ProgramPost = Post.create_related_post(programs.Program, {
 | 
			
		||||
                    'title': 'name',
 | 
			
		||||
                    'content': 'description',
 | 
			
		||||
               })
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
EpisodePost = Post.create_related_post(programs.Episode, {
 | 
			
		||||
                    'title': 'name',
 | 
			
		||||
                    'content': 'description',
 | 
			
		||||
               })
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#class MenuItem ():
 | 
			
		||||
#    Menu = {
 | 
			
		||||
#        'top':      0x00,
 | 
			
		||||
#        'sidebar':  0x01,
 | 
			
		||||
#        'bottom':   0x02,
 | 
			
		||||
#    }
 | 
			
		||||
#    for key, value in Type.items():
 | 
			
		||||
#        ugettext_lazy(key)
 | 
			
		||||
#
 | 
			
		||||
#    parent = models.ForeignKey(
 | 
			
		||||
#        'self',
 | 
			
		||||
#        blank = True, null = True
 | 
			
		||||
#    )
 | 
			
		||||
#    menu = models.SmallIntegerField(
 | 
			
		||||
#    )
 | 
			
		||||
 | 
			
		||||
class Episode (RelatedPost):
 | 
			
		||||
    class Relation:
 | 
			
		||||
        related_model = programs.Episode
 | 
			
		||||
        mapping = {
 | 
			
		||||
            'title': 'name',
 | 
			
		||||
            'content': 'description',
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,153 +0,0 @@
 | 
			
		||||
from django.conf.urls import url
 | 
			
		||||
from django.utils import timezone
 | 
			
		||||
 | 
			
		||||
from website.models import *
 | 
			
		||||
from website.views import *
 | 
			
		||||
 | 
			
		||||
class Router:
 | 
			
		||||
    registry = []
 | 
			
		||||
 | 
			
		||||
    def register (self, route):
 | 
			
		||||
        if not route in self.registry:
 | 
			
		||||
            self.registry.append(route)
 | 
			
		||||
 | 
			
		||||
    def register_set (self, view_set):
 | 
			
		||||
        for route in view_set.routes:
 | 
			
		||||
            self.register(route)
 | 
			
		||||
 | 
			
		||||
    def unregister (self, route):
 | 
			
		||||
        self.registry.remove(route)
 | 
			
		||||
 | 
			
		||||
    def get_urlpatterns (self):
 | 
			
		||||
        return [ route.get_url() for route in self.registry ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Route:
 | 
			
		||||
    """
 | 
			
		||||
    Base class for routing. Given a model, we generate url specific for each
 | 
			
		||||
    route type. The generated url takes this form:
 | 
			
		||||
        base_name + '/' + route_name + '/' + '/'.join(route_url_args)
 | 
			
		||||
 | 
			
		||||
    Where base_name by default is the given model's verbose_name (uses plural if
 | 
			
		||||
    Route is for a list).
 | 
			
		||||
 | 
			
		||||
    The given view is considered as a django class view, and has view_
 | 
			
		||||
    """
 | 
			
		||||
    model = None        # model routed here
 | 
			
		||||
    view = None         # view class to call
 | 
			
		||||
    view_kwargs  = None   # arguments passed to view at creation of the urls
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        name = None         # route name
 | 
			
		||||
        is_list = False     # route is for a list
 | 
			
		||||
        url_args = []       # arguments passed from the url [ (name : regex),... ]
 | 
			
		||||
 | 
			
		||||
    def __init__ (self, model, view, view_kwargs = None,
 | 
			
		||||
                  base_name = None):
 | 
			
		||||
        self.model = model
 | 
			
		||||
        self.view = view
 | 
			
		||||
        self.view_kwargs = view_kwargs
 | 
			
		||||
        self.embed = False
 | 
			
		||||
 | 
			
		||||
        _meta = {}
 | 
			
		||||
        _meta.update(Route.Meta.__dict__)
 | 
			
		||||
        _meta.update(self.Meta.__dict__)
 | 
			
		||||
        self._meta = _meta
 | 
			
		||||
 | 
			
		||||
        if not base_name:
 | 
			
		||||
            base_name = model._meta.verbose_name_plural if _meta['is_list'] \
 | 
			
		||||
                    else model._meta.verbose_name
 | 
			
		||||
            base_name = base_name.title().lower()
 | 
			
		||||
        self.base_name = base_name
 | 
			
		||||
 | 
			
		||||
    def get_queryset (self, request, **kwargs):
 | 
			
		||||
        """
 | 
			
		||||
        Called by the view to get the queryset when it is needed
 | 
			
		||||
        """
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def get (self, request, **kwargs):
 | 
			
		||||
        """
 | 
			
		||||
        Called by the view to get the object when it is needed
 | 
			
		||||
        """
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def get_url (self):
 | 
			
		||||
        pattern = '^{}/{}'.format(self.base_name, self.Meta.name)
 | 
			
		||||
        if self._meta['url_args']:
 | 
			
		||||
            url_args = '/'.join([ '(?P<{}>{})'.format(arg, expr) \
 | 
			
		||||
                                    for arg, expr in self._meta['url_args']
 | 
			
		||||
                                ])
 | 
			
		||||
            pattern += '/' + url_args
 | 
			
		||||
        pattern += '/?$'
 | 
			
		||||
 | 
			
		||||
        kwargs = {
 | 
			
		||||
            'route': self,
 | 
			
		||||
        }
 | 
			
		||||
        if self.view_kwargs:
 | 
			
		||||
            kwargs.update(self.view_kwargs)
 | 
			
		||||
 | 
			
		||||
        return url(pattern, self.view, kwargs = kwargs, name = '{}')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class DetailRoute (Route):
 | 
			
		||||
    class Meta:
 | 
			
		||||
        name = 'detail'
 | 
			
		||||
        is_list = False
 | 
			
		||||
        url_args = [
 | 
			
		||||
            ('pk', '[0-9]+'),
 | 
			
		||||
            ('slug', '(\w|-|_)*'),
 | 
			
		||||
        ]
 | 
			
		||||
 | 
			
		||||
    def get (self, request, **kwargs):
 | 
			
		||||
        return self.model.objects.get(pk = int(kwargs['pk']))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ThreadRoute (Route):
 | 
			
		||||
    class Meta:
 | 
			
		||||
        name = 'thread'
 | 
			
		||||
        is_list = True
 | 
			
		||||
        url_args = [
 | 
			
		||||
            ('pk', '[0-9]+')
 | 
			
		||||
        ]
 | 
			
		||||
 | 
			
		||||
    def get_queryset (self, request, **kwargs):
 | 
			
		||||
        return self.model.objects.filter(thread__pk = int(kwargs['pk']))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class DateRoute (Route):
 | 
			
		||||
    class Meta:
 | 
			
		||||
        name = 'date'
 | 
			
		||||
        is_list = True
 | 
			
		||||
        url_args = [
 | 
			
		||||
            ('year', '[0-9]{4}'),
 | 
			
		||||
            ('month', '[0-9]{2}'),
 | 
			
		||||
            ('day', '[0-9]{1,2}'),
 | 
			
		||||
        ]
 | 
			
		||||
 | 
			
		||||
    def get_queryset (self, request, **kwargs):
 | 
			
		||||
        return self.model.objects.filter(
 | 
			
		||||
            date__year = int(kwargs['year']),
 | 
			
		||||
            date__month = int(kwargs['month']),
 | 
			
		||||
            date__day = int(kwargs['day']),
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SearchRoute (Route):
 | 
			
		||||
    class Meta:
 | 
			
		||||
        name = 'search'
 | 
			
		||||
        is_list = True
 | 
			
		||||
 | 
			
		||||
    def get_queryset (self, request, **kwargs):
 | 
			
		||||
        q = request.GET.get('q') or ''
 | 
			
		||||
        qs = self.model.objects
 | 
			
		||||
        for search_field in model.search_fields or []:
 | 
			
		||||
            r = self.model.objects.filter(**{ search_field + '__icontains': q })
 | 
			
		||||
            if qs: qs = qs | r
 | 
			
		||||
            else: qs = r
 | 
			
		||||
 | 
			
		||||
        qs.distinct()
 | 
			
		||||
        return qs
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,47 +0,0 @@
 | 
			
		||||
{# {% extends embed|yesno:"website/single.html,website/base.html" %} #}
 | 
			
		||||
 | 
			
		||||
{% load i18n %}
 | 
			
		||||
{% load thumbnail %}
 | 
			
		||||
{# {% load website_views %} #}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
{% block content %}
 | 
			
		||||
<div class="post_list {{ classes }}">
 | 
			
		||||
{% for post in object_list %}
 | 
			
		||||
    <a class="post_item"
 | 
			
		||||
       href="{{ post.get_detail_url }}">
 | 
			
		||||
 | 
			
		||||
        {% if 'date' in list.fields or 'time' in list.fields %}
 | 
			
		||||
        <time datetime="{{ post.date }}" class="post_datetime">
 | 
			
		||||
            {% if 'date' in list.fields %}
 | 
			
		||||
            <span class="post_date">
 | 
			
		||||
                {{ post.date|date:'D. d F' }},
 | 
			
		||||
            </span>
 | 
			
		||||
            {% endif %}
 | 
			
		||||
            {% if 'time' in list.fields %}
 | 
			
		||||
            <span class="post_time">
 | 
			
		||||
                {{ post.date|date:'H:i' }},
 | 
			
		||||
            </span>
 | 
			
		||||
            {% endif %}
 | 
			
		||||
        </time>
 | 
			
		||||
        {% endif %}
 | 
			
		||||
 | 
			
		||||
        {% if 'image' in list.fields %}
 | 
			
		||||
        <img src="{% thumbnail post.image "64x64" crop %}" class="post_image">
 | 
			
		||||
        {% endif %}
 | 
			
		||||
 | 
			
		||||
        {% if 'title' in list.fields %}
 | 
			
		||||
        <h4 class="post_title">{{ post.title }}</h4>
 | 
			
		||||
        {% endif %}
 | 
			
		||||
 | 
			
		||||
        {% if 'content' in list.fields %}
 | 
			
		||||
        <div class="post_content">
 | 
			
		||||
        {{ post.content|safe|striptags|truncatechars:"64" }}
 | 
			
		||||
        </div>
 | 
			
		||||
        {% endif %}
 | 
			
		||||
    </a>
 | 
			
		||||
{% endfor %}
 | 
			
		||||
</div>
 | 
			
		||||
{% endblock %}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -2,40 +2,42 @@ from django.conf.urls import url, include
 | 
			
		||||
 | 
			
		||||
from website.models import *
 | 
			
		||||
from website.views import *
 | 
			
		||||
from website.routes import *
 | 
			
		||||
 | 
			
		||||
from cms.models import Article
 | 
			
		||||
from cms.views import ViewSet
 | 
			
		||||
from cms.routes import *
 | 
			
		||||
 | 
			
		||||
class ProgramSet (ViewSet):
 | 
			
		||||
    model = ProgramPost
 | 
			
		||||
    name = 'programs'
 | 
			
		||||
 | 
			
		||||
    model = Program
 | 
			
		||||
    list_routes = [
 | 
			
		||||
        AllRoute,
 | 
			
		||||
        ThreadRoute,
 | 
			
		||||
        SearchRoute,
 | 
			
		||||
        DateRoute,
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    detail_sections = [
 | 
			
		||||
        ScheduleSection
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
class EpisodeSet (ViewSet):
 | 
			
		||||
    model = EpisodePost
 | 
			
		||||
    name = 'episodes'
 | 
			
		||||
 | 
			
		||||
    model = Episode
 | 
			
		||||
    list_routes = [
 | 
			
		||||
        AllRoute,
 | 
			
		||||
        ThreadRoute,
 | 
			
		||||
        SearchRoute,
 | 
			
		||||
        DateRoute,
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ArticleSet (ViewSet):
 | 
			
		||||
    model = Article
 | 
			
		||||
    list_routes = [
 | 
			
		||||
        AllRoute,
 | 
			
		||||
        ThreadRoute,
 | 
			
		||||
        SearchRoute,
 | 
			
		||||
        DateRoute,
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
router = Router()
 | 
			
		||||
router.register_set(ProgramSet())
 | 
			
		||||
router.register_set(EpisodeSet())
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										171
									
								
								website/views.py
									
									
									
									
									
								
							
							
						
						
									
										171
									
								
								website/views.py
									
									
									
									
									
								
							@ -5,165 +5,32 @@ from django.views.generic import DetailView
 | 
			
		||||
from django.core import serializers
 | 
			
		||||
from django.utils.translation import ugettext as _, ugettext_lazy
 | 
			
		||||
 | 
			
		||||
from website.models import *
 | 
			
		||||
from website.routes import *
 | 
			
		||||
import programs.models as programs
 | 
			
		||||
from cms.views import ListSection
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class PostListView (ListView):
 | 
			
		||||
    """
 | 
			
		||||
    List view for posts and children
 | 
			
		||||
    """
 | 
			
		||||
    class Query:
 | 
			
		||||
        """
 | 
			
		||||
        Request availables parameters
 | 
			
		||||
        """
 | 
			
		||||
        embed = False
 | 
			
		||||
        exclude = None
 | 
			
		||||
        order = 'desc'
 | 
			
		||||
        reverse = False
 | 
			
		||||
class PlaylistSection (ListSection):
 | 
			
		||||
    title = _('Playlist')
 | 
			
		||||
 | 
			
		||||
        def __init__ (self, query):
 | 
			
		||||
            my_class = self.__class__
 | 
			
		||||
            if type(query) is my_class:
 | 
			
		||||
                self.__dict__.update(query.__dict__)
 | 
			
		||||
                return
 | 
			
		||||
 | 
			
		||||
            if type(query) is not dict:
 | 
			
		||||
                query = query.__dict__
 | 
			
		||||
 | 
			
		||||
            self.__dict__ = { k: v for k,v in query.items() }
 | 
			
		||||
 | 
			
		||||
    template_name = 'website/list.html'
 | 
			
		||||
    allow_empty = True
 | 
			
		||||
 | 
			
		||||
    model = None
 | 
			
		||||
    query = None
 | 
			
		||||
    fields = [ 'date', 'time', 'image', 'title', 'content' ]
 | 
			
		||||
 | 
			
		||||
    def __init__ (self, *args, **kwargs):
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
        if self.query:
 | 
			
		||||
            self.query = Query(self.query)
 | 
			
		||||
 | 
			
		||||
    def get_queryset (self):
 | 
			
		||||
        route = self.kwargs['route']
 | 
			
		||||
        qs = route.get_queryset(self.request, **self.kwargs)
 | 
			
		||||
        qs = qs.filter(public = True)
 | 
			
		||||
 | 
			
		||||
        query = self.query or PostListView.Query(self.request.GET)
 | 
			
		||||
        if query.exclude:
 | 
			
		||||
            qs = qs.exclude(id = int(exclude))
 | 
			
		||||
 | 
			
		||||
        if query.order == 'asc':
 | 
			
		||||
            qs.order_by('date', 'id')
 | 
			
		||||
        else:
 | 
			
		||||
            qs.order_by('-date', '-id')
 | 
			
		||||
        return qs
 | 
			
		||||
    def get_object_list (self):
 | 
			
		||||
        tracks = programs.Track.objects \
 | 
			
		||||
                     .filter(episode = self.object) \
 | 
			
		||||
                     .order_by('position')
 | 
			
		||||
        return [ ListSection.Item(None, track.title, track.artist)
 | 
			
		||||
                    for track in tracks ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def get_context_data (self, **kwargs):
 | 
			
		||||
        context = super().get_context_data(**kwargs)
 | 
			
		||||
        context.update({
 | 
			
		||||
            'list': self
 | 
			
		||||
        })
 | 
			
		||||
class ScheduleSection (ListSection):
 | 
			
		||||
    title = _('Schedule')
 | 
			
		||||
 | 
			
		||||
        return context
 | 
			
		||||
    def get_object_list (self):
 | 
			
		||||
        scheds = programs.Schedule.objects \
 | 
			
		||||
                    .filter(program = self.object.pk)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class PostDetailView (DetailView):
 | 
			
		||||
    """
 | 
			
		||||
    Detail view for posts and children
 | 
			
		||||
    """
 | 
			
		||||
    template_name = 'website/detail.html'
 | 
			
		||||
    sections = None
 | 
			
		||||
 | 
			
		||||
    def __init__ (self, sections = None, *args, **kwargs):
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
 | 
			
		||||
    def get_queryset (self, **kwargs):
 | 
			
		||||
        if self.model:
 | 
			
		||||
            return super().get_queryset(**kwargs).filter(public = True)
 | 
			
		||||
        return []
 | 
			
		||||
 | 
			
		||||
    def get_object (self, **kwargs):
 | 
			
		||||
        if self.model:
 | 
			
		||||
            object = super().get_object(**kwargs)
 | 
			
		||||
            if object.public:
 | 
			
		||||
                return object
 | 
			
		||||
        return None
 | 
			
		||||
 | 
			
		||||
    def get_context_data (self, **kwargs):
 | 
			
		||||
        context = super().get_context_data(**kwargs)
 | 
			
		||||
        context.update({
 | 
			
		||||
            'sections': [
 | 
			
		||||
                section.get(self, self.request, object = self.object)
 | 
			
		||||
                    for section in self.sections
 | 
			
		||||
            ]
 | 
			
		||||
        })
 | 
			
		||||
        return context
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Section (DetailView):
 | 
			
		||||
    """
 | 
			
		||||
    Base class for sections. Sections are view that can be used in detail view
 | 
			
		||||
    in order to have extra content about a post.
 | 
			
		||||
    """
 | 
			
		||||
    model = None
 | 
			
		||||
    template_name = 'website/section.html'
 | 
			
		||||
    classes = ''
 | 
			
		||||
    title = ''
 | 
			
		||||
    header = ''
 | 
			
		||||
    bottom = ''
 | 
			
		||||
 | 
			
		||||
    def get_context_data (self, **kwargs):
 | 
			
		||||
        context = super().get_context_date(**kwargs)
 | 
			
		||||
        context.update({
 | 
			
		||||
            'title': self.title,
 | 
			
		||||
            'header': self.header,
 | 
			
		||||
            'bottom': self.bottom,
 | 
			
		||||
            'classes': self.classes,
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
    def get (self, request, **kwargs):
 | 
			
		||||
        self.object = kwargs.get('object')
 | 
			
		||||
        context = self.get_context_data(**kwargs)
 | 
			
		||||
        return render_to_string(self.template_name, context)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ViewSet:
 | 
			
		||||
    """
 | 
			
		||||
    A ViewSet is a class helper that groups detail and list views that can be
 | 
			
		||||
    used to generate views and routes given a model and a name used for the
 | 
			
		||||
    routing.
 | 
			
		||||
    """
 | 
			
		||||
    model = None
 | 
			
		||||
    name = ''
 | 
			
		||||
 | 
			
		||||
    list_view = PostListView
 | 
			
		||||
    list_routes = []
 | 
			
		||||
 | 
			
		||||
    detail_view = PostDetailView
 | 
			
		||||
    detail_sections = []
 | 
			
		||||
 | 
			
		||||
    def __init__ (self):
 | 
			
		||||
        if not self.name:
 | 
			
		||||
            self.name = self.model._meta.verbose_name_plural
 | 
			
		||||
 | 
			
		||||
        self.detail_sections = [
 | 
			
		||||
            section.as_view(model = self.model)
 | 
			
		||||
                for section in self.detail_sections
 | 
			
		||||
        return [
 | 
			
		||||
            ListSection.Item(None, sched.get_frequency_display(),
 | 
			
		||||
                             _('rerun') if sched.rerun else None)
 | 
			
		||||
            for sched in scheds
 | 
			
		||||
        ]
 | 
			
		||||
        self.detail_view = self.detail_view.as_view(
 | 
			
		||||
            model = self.model,
 | 
			
		||||
            sections = self.detail_sections
 | 
			
		||||
        )
 | 
			
		||||
        self.list_view = self.list_view.as_view(
 | 
			
		||||
            model = self.model
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        self.routes = [ route(self.model, self.list_view, base_name = self.name)
 | 
			
		||||
                            for route in self.list_routes ] + \
 | 
			
		||||
                      [ DetailRoute(self.model, self.detail_view,
 | 
			
		||||
                                    base_name = self.name) ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user