bunch of work; separate publications from programs, start to work on website

This commit is contained in:
bkfox
2015-09-23 17:27:01 +02:00
parent ef4c098d2e
commit 81a2e0533f
12 changed files with 518 additions and 178 deletions

View File

@ -1,15 +1,42 @@
import copy
from django.contrib import admin
from django.utils.translation import ugettext as _, ugettext_lazy
from django.contrib.contenttypes.admin import GenericStackedInline
from programs.admin import PublicationAdmin
import programs.models as programs
from website.models import *
@admin.register(Article)
class ArticleAdmin (PublicationAdmin):
fieldsets = copy.deepcopy(PublicationAdmin.fieldsets)
fieldsets[1][1]['fields'] += ['static_page']
def add_inline (base_model, post_model, prepend = False):
class InlineModel (GenericStackedInline):
model = post_model
extra = 1
max_num = 1
ct_field = 'object_type'
verbose_name = _('Post')
registry = admin.site._registry
if not base_model in registry:
raise TypeError(str(base_model) + " not in admin registry")
inlines = list(registry[base_model].inlines) or []
if prepend:
inlines.insert(0, InlineModel)
else:
inlines.append(InlineModel)
registry[base_model].inlines = inlines
add_inline(Program, ObjectDescription)
add_inline(Episode, ObjectDescription)
#class ArticleAdmin (DescriptionAdmin):
# fieldsets = copy.deepcopy(DescriptionAdmin.fieldsets)
#
# fieldsets[1][1]['fields'] += ['static_page']

View File

@ -1,15 +1,120 @@
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 programs.models import Publication
from django.db.models.signals import post_save
from django.dispatch import receiver
from programs.models import *
class Article (Publication):
parent = models.ForeignKey(
'self',
verbose_name = _('parent'),
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 Post (models.Model):
thread = models.ForeignKey(
Thread,
on_delete=models.SET_NULL,
blank = True, null = True,
help_text = _('parent article'),
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
)
def as_dict (self):
d = {}
d.update(self.__dict__)
d.update({
'title': self.get_title(),
'image': self.get_image(),
'date': self.get_date(),
'content': self.get_content()
})
def get_detail_url (self):
pass
def get_image (self):
return self.image
def get_date (self):
return self.date
def get_title (self):
pass
def get_content (self):
pass
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 ObjectDescription (Post):
object_type = models.ForeignKey(ContentType, blank = True, null = True)
object_id = models.PositiveIntegerField(blank = True, null = True)
object = GenericForeignKey('object_type', 'object_id')
class Article (Post):
title = models.CharField(
_('title'),
max_length = 128,
)
static_page = models.BooleanField(
_('static page'),
@ -19,17 +124,25 @@ class Article (Publication):
_('article is focus'),
default = False,
)
referring_tag = models.CharField(
_('referring tag'),
max_length = 32,
blank = True, null = True,
help_text = _('tag used by other to refers to this article'),
)
class Meta:
verbose_name = _('Article')
verbose_name_plural = _('Articles')
#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(
# )

139
website/routes.py Normal file
View File

@ -0,0 +1,139 @@
from django.conf.urls import url
from django.utils import timezone
from website.models import *
class Routes:
registry = []
def register (self, route):
if not route in self.registry:
self.registry.append(route)
def unregister (self, route):
self.registry.remove(route)
def get_urlpatterns (self):
patterns = []
for route in self.registry:
patterns += route.get_urlpatterns() or []
return patterns
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
"""
def get_urlpatterns (self):
view_kwargs = self.view_kwargs or {}
pattern = '^{}/{}'.format(self.base_name, self.Meta.name)
if self.view.Meta.formats:
pattern += '(/(?P<format>{}))?'.format('|'.join(self.view.Meta.formats))
if self._meta['url_args']:
url_args = '/'.join([ '(?P<{}>{})'.format(arg, expr) \
for arg, expr in self._meta['url_args']
])
pattern += '/' + url_args
pattern += '/?$'
return [ url(
pattern,
self.view and self.view.as_view(
route = self,
model = self.model,
**view_kwargs
),
name = '{}'
) ]
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
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__id = 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']),
)

View File

@ -0,0 +1,59 @@
{# Parameters are: #}
{# * pub: publication itself; pub.meta must have been eval() #}
{# * threads: list of parent, from top to bottom, including itself #}
{# #}
{# * views: a view object used to know which view to use for links #}
{# #}
{# {% extends embed|yesno:"website/single.html,website/base.html" %} #}
{% load i18n %}
{% load thumbnail %}
{# {% load website_views %} #}
<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 %}
{% with post_date=post.get_date %}
<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>
{% endwith %}
{% endif %}
{% if 'image' in list.fields %}
{% with post_image=post.get_image %}
<img src="{% thumbnail post_image "64x64" crop %}" class="post_image">
{% endwith %}
{% endif %}
{% if 'title' in list.fields %}
{% with post_title=post.get_title %}
<h4 class="post_title">post_title</h4>
{% endwith %}
{% endif %}
{% if 'content' in list.fields %}
{% with post_content=post.get_content %}
<div class="post_content">
{{ post_content|safe|striptags|truncatechars:"64" }}
</div>
{% endwith %}
{% endif %}
</a>
{% endfor %}
</div>

24
website/urls.py Normal file
View File

@ -0,0 +1,24 @@
from django.conf.urls import url, include
from website.models import *
from website.views import *
from website.routes import *
routes = Routes()
routes.register( SearchRoute(Article, PostListView) )
#routes.register( SearchRoute(ProgramPost, PostListView, base_name = 'programs') )
#routes.register( SearchRoute(EpisodePost, PostListView, base_name = 'episodes') )
routes.register( ThreadRoute(Article, PostListView) )
#routes.register( ThreadRoute(ProgramPost, PostListView, base_name = 'programs') )
#routes.register( ThreadRoute(EpisodePost, PostListView, base_name = 'episodes') )
routes.register( DateRoute(Article, PostListView) )
#routes.register( DateRoute(ProgramPost, PostListView, base_name = 'programs') )
#routes.register( DateRoute(EpisodePost, PostListView, base_name = 'episodes') )
urlpatterns = routes.get_urlpatterns()

View File

@ -36,7 +36,7 @@ class ListQueries:
if not q:
q = timezone.datetime.today()
if type(q) is str:
q = timezone.datetime.strptime(q, '%Y/%m/%d').date()
q = timezone.datetime.strptime(q, '%Y%m%d').date()
return qs.filter(date__startswith = q)

View File

@ -1,3 +1,77 @@
from django.shortcuts import render
from django.utils import timezone
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
from website.models import *
class PostListView (ListView):
class Query:
"""
Request availables parameters
"""
exclude = None
order = 'desc'
reverse = False
format = 'normal'
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
query = None
format = None
fields = [ 'date', 'time', 'image', 'title', 'content' ]
route = None
model = None
class Meta:
# FIXME
formats = ['normal', 'embed', 'json', 'yaml', 'xml']
def __init__ (self, *args, **kwargs):
super(PostListView, self).__init__(*args, **kwargs)
if self.query:
self.query = Query(self.query)
def get_queryset (self):
qs = self.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(PostListView, self).get_context_data(**kwargs)
context.update({
'list': self
})
return context
# Create your views here.