forked from rc/aircox
split website and cms; work on sections
This commit is contained in:
3
cms/admin.py
Normal file
3
cms/admin.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
154
cms/models.py
Normal file
154
cms/models.py
Normal file
@ -0,0 +1,154 @@
|
||||
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 import timezone
|
||||
from django.utils.text import slugify
|
||||
from django.utils.translation import ugettext as _, ugettext_lazy
|
||||
from django.core.urlresolvers import reverse
|
||||
|
||||
from django.db.models.signals import post_save
|
||||
from django.dispatch import receiver
|
||||
|
||||
|
||||
# Using a separate thread helps for routing, by avoiding to specify an
|
||||
# additional argument to get the second model that implies to find it by
|
||||
# the name that can be non user-friendly, like /thread/relatedpost/id
|
||||
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 self.post_type.name + ': ' + str(self.post)
|
||||
|
||||
|
||||
class Post (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 = ''
|
||||
|
||||
def detail_url (self):
|
||||
return reverse(self._meta.verbose_name_plural.lower() + '_detail',
|
||||
kwargs = { 'pk': self.pk,
|
||||
'slug': slugify(self.title) })
|
||||
|
||||
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, Post) or not created:
|
||||
return
|
||||
|
||||
thread = Thread(post = instance)
|
||||
thread.save()
|
||||
|
||||
|
||||
class Article (Post):
|
||||
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')
|
||||
|
||||
|
||||
class RelatedPostBase (models.base.ModelBase):
|
||||
"""
|
||||
Metaclass for RelatedPost children.
|
||||
"""
|
||||
def __new__ (cls, name, bases, attrs):
|
||||
rel = attrs.get('Relation')
|
||||
rel = (rel and rel.__dict__) or {}
|
||||
|
||||
related_model = rel.get('related_model')
|
||||
if related_model:
|
||||
attrs['related'] = models.ForeignKey(related_model)
|
||||
|
||||
mapping = rel.get('mapping')
|
||||
if mapping:
|
||||
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 mapping.items()
|
||||
})
|
||||
|
||||
if not '__str__' in attrs:
|
||||
attrs['__str__'] = lambda self: str(self.related)
|
||||
|
||||
return super().__new__(cls, name, bases, attrs)
|
||||
|
||||
|
||||
class RelatedPost (Post, metaclass = RelatedPostBase):
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
class Relation:
|
||||
related_model = None
|
||||
mapping = None
|
||||
|
||||
|
162
cms/routes.py
Normal file
162
cms/routes.py
Normal file
@ -0,0 +1,162 @@
|
||||
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):
|
||||
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
|
||||
|
||||
self.base_name = model._meta.verbose_name_plural.lower()
|
||||
|
||||
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,
|
||||
(optional and optional[0] and '?') or ''
|
||||
)
|
||||
for arg, expr, *optional 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 = self.base_name + '_' + self.Meta.name)
|
||||
|
||||
|
||||
class DetailRoute (Route):
|
||||
class Meta:
|
||||
name = 'detail'
|
||||
is_list = False
|
||||
url_args = [
|
||||
('pk', '[0-9]+'),
|
||||
('slug', '(\w|-|_)+', True),
|
||||
]
|
||||
|
||||
def get (self, request, **kwargs):
|
||||
return self.model.objects.get(pk = int(kwargs['pk']))
|
||||
|
||||
|
||||
class AllRoute (Route):
|
||||
class Meta:
|
||||
name = 'all'
|
||||
is_list = True
|
||||
|
||||
def get_queryset (self, request, **kwargs):
|
||||
return self.model.objects.all()
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
|
46
cms/templates/cms/list.html
Normal file
46
cms/templates/cms/list.html
Normal file
@ -0,0 +1,46 @@
|
||||
{# {% 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.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 %}
|
||||
|
||||
|
193
cms/views.py
Normal file
193
cms/views.py
Normal file
@ -0,0 +1,193 @@
|
||||
from django.shortcuts import render
|
||||
from django.template.loader import render_to_string
|
||||
from django.views.generic import ListView
|
||||
from django.views.generic import DetailView
|
||||
from django.core import serializers
|
||||
from django.utils.translation import ugettext as _, ugettext_lazy
|
||||
|
||||
import cms.routes as routes
|
||||
|
||||
|
||||
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.
|
||||
"""
|
||||
template_name = 'cms/section.html'
|
||||
classes = ''
|
||||
title = ''
|
||||
content = ''
|
||||
header = ''
|
||||
bottom = ''
|
||||
|
||||
def get_context_data (self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context.update({
|
||||
'title': self.title,
|
||||
'header': self.header,
|
||||
'content': self.content,
|
||||
'bottom': self.bottom,
|
||||
'classes': self.classes,
|
||||
})
|
||||
return context
|
||||
|
||||
def get (self, parent, request, object = None, **kwargs):
|
||||
print(type(object))
|
||||
self.object = object
|
||||
context = self.get_context_data(**kwargs)
|
||||
return render_to_string(self.template_name, context)
|
||||
|
||||
|
||||
class ListSection (Section):
|
||||
"""
|
||||
Section to render list. The context item 'object_list' is used as list of
|
||||
items to render.
|
||||
"""
|
||||
class Item:
|
||||
icon = None
|
||||
title = None
|
||||
text = None
|
||||
|
||||
def __init__ (self, icon, title = None, text = None):
|
||||
self.icon = icon
|
||||
self.title = title
|
||||
self.text = text
|
||||
|
||||
template_name = 'cms/section_list.html'
|
||||
|
||||
def get_object_list (self):
|
||||
return []
|
||||
|
||||
def get_context_data (self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['classes'] += ' section_list'
|
||||
context['object_list'] = self.get_object_list()
|
||||
return context
|
||||
|
||||
|
||||
class PostListView (ListView):
|
||||
"""
|
||||
List view for posts and children
|
||||
"""
|
||||
class Query:
|
||||
"""
|
||||
Request availables parameters
|
||||
"""
|
||||
embed = False
|
||||
exclude = None
|
||||
order = 'desc'
|
||||
reverse = False
|
||||
|
||||
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 = 'cms/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_context_data (self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context.update({
|
||||
'list': self
|
||||
})
|
||||
|
||||
return context
|
||||
|
||||
|
||||
class PostDetailView (DetailView):
|
||||
"""
|
||||
Detail view for posts and children
|
||||
"""
|
||||
template_name = 'cms/detail.html'
|
||||
sections = []
|
||||
|
||||
def __init__ (self, sections = None, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.sections = sections or []
|
||||
|
||||
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 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
|
||||
list_view = PostListView
|
||||
list_routes = []
|
||||
|
||||
detail_view = PostDetailView
|
||||
detail_sections = []
|
||||
|
||||
def __init__ (self):
|
||||
self.detail_sections = [
|
||||
section()
|
||||
for section in self.detail_sections
|
||||
]
|
||||
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)
|
||||
for route in self.list_routes ] + \
|
||||
[ routes.DetailRoute(self.model, self.detail_view ) ]
|
||||
|
||||
|
Reference in New Issue
Block a user