This commit is contained in:
bkfox 2015-10-02 15:31:44 +02:00
parent 2af9cf8b13
commit 7069ed8918
7 changed files with 224 additions and 105 deletions

View File

@ -7,35 +7,48 @@ from django.utils.text import slugify
from django.utils.translation import ugettext as _, ugettext_lazy from django.utils.translation import ugettext as _, ugettext_lazy
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.db.models.signals import post_save from django.db.models.signals import post_init, post_save, post_delete
from django.dispatch import receiver from django.dispatch import receiver
from taggit.managers import TaggableManager
# 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): class Thread (models.Model):
"""
Object assigned to any Post and children that can be used to have parent and
children relationship between posts of different kind.
We use this system instead of having directly a GenericForeignKey into the
Post because it avoids having to define the relationship with two models for
routing (one for the parent and one for the children).
"""
post_type = models.ForeignKey(ContentType) post_type = models.ForeignKey(ContentType)
post_id = models.PositiveIntegerField() post_id = models.PositiveIntegerField()
post = GenericForeignKey('post_type', 'post_id') post = GenericForeignKey('post_type', 'post_id')
@classmethod @classmethod
def get (cl, model, **kwargs): def __get_query_set (cl, function, model, post, kwargs):
post_type = ContentType.objects.get_for_model(model) if post:
return cl.objects.get(post_type__pk = post_type.id, model = type(post)
**kwargs) kwargs['post_id'] = post.id
kwargs['post_type'] = ContentType.objects.get_for_model(model)
return getattr(cl.objects, function)(**kwargs)
@classmethod @classmethod
def filter (cl, model, **kwargs): def get (cl, model = None, post = None, **kwargs):
post_type = ContentType.objects.get_for_model(model) return cl.__get_query_set('get', model, post, kwargs)
return cl.objects.filter(post_type__pk = post_type.id,
**kwargs)
@classmethod @classmethod
def exclude (cl, model, **kwargs): def filter (cl, model = None, post = None, **kwargs):
post_type = ContentType.objects.get_for_model(model) return self.__get_query_set('filter', model, post, kwargs)
return cl.objects.exclude(post_type__pk = post_type.id,
**kwargs) @classmethod
def exclude (cl, model = None, post = None, **kwargs):
return self.__get_query_set('exclude', model, post, kwargs)
def save (self, *args, **kwargs):
self.post = self.__initial_post
super().save(*args, **kwargs)
def __str__ (self): def __str__ (self):
return self.post_type.name + ': ' + str(self.post) return self.post_type.name + ': ' + str(self.post)
@ -57,16 +70,26 @@ class Post (models.Model):
_('date'), _('date'),
default = timezone.datetime.now default = timezone.datetime.now
) )
public = models.BooleanField( published = models.BooleanField(
verbose_name = _('public'), verbose_name = _('public'),
default = True default = True
) )
title = models.CharField (
_('title'),
max_length = 128,
)
content = models.TextField (
_('description'),
blank = True, null = True
)
image = models.ImageField( image = models.ImageField(
blank = True, null = True blank = True, null = True
) )
tags = TaggableManager(
title = '' _('tags'),
content = '' blank = True,
)
def detail_url (self): def detail_url (self):
return reverse(self._meta.verbose_name_plural.lower() + '_detail', return reverse(self._meta.verbose_name_plural.lower() + '_detail',
@ -77,28 +100,7 @@ class Post (models.Model):
abstract = True 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): 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 = models.BooleanField(
_('static page'), _('static page'),
default = False, default = False,
@ -125,18 +127,6 @@ class RelatedPostBase (models.base.ModelBase):
if related_model: if related_model:
attrs['related'] = models.ForeignKey(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: if not '__str__' in attrs:
attrs['__str__'] = lambda self: str(self.related) attrs['__str__'] = lambda self: str(self.related)
@ -144,11 +134,54 @@ class RelatedPostBase (models.base.ModelBase):
class RelatedPost (Post, metaclass = RelatedPostBase): class RelatedPost (Post, metaclass = RelatedPostBase):
related = None
class Meta: class Meta:
abstract = True abstract = True
class Relation: class Relation:
related_model = None related_model = None
mapping = None mapping = None # dict of related mapping values
bind_mapping = False # update fields of related data on save
def get_attribute (self, attr):
attr = self.Relation.mappings.get(attr)
return self.related.__dict__[attr] if attr else None
def save (self, *args, **kwargs):
if not self.title and self.related:
self.title = self.get_attribute('title')
if self.Relation.bind_mapping:
self.related.__dict__.update({
rel_attr: self.__dict__[attr]
for attr, rel_attr in self.Relation.mapping
})
self.related.save()
super().save(*args, **kwargs)
@receiver(post_init)
def on_thread_init (sender, instance, **kwargs):
if not issubclass(Thread, sender):
return
instance.__initial_post = instance.post
@receiver(post_save)
def on_post_save (sender, instance, created, *args, **kwargs):
if not issubclass(sender, Post) or not created:
return
thread = Thread(post = instance)
thread.save()
@receiver(post_delete)
def on_post_delete (sender, instance, using, *args, **kwargs):
try:
Thread.get(sender, post = instance).delete()
except:
pass

View File

@ -35,10 +35,12 @@ class PostListView (ListView):
template_name = 'cms/list.html' template_name = 'cms/list.html'
allow_empty = True allow_empty = True
model = None website = None
query = None
classes = ''
title = '' title = ''
classes = ''
route = None
query = None
embed = False embed = False
fields = [ 'date', 'time', 'image', 'title', 'content' ] fields = [ 'date', 'time', 'image', 'title', 'content' ]
@ -48,12 +50,11 @@ class PostListView (ListView):
self.query = Query(self.query) self.query = Query(self.query)
def dispatch (self, request, *args, **kwargs): def dispatch (self, request, *args, **kwargs):
self.route = self.kwargs.get('route') self.route = self.kwargs.get('route') or self.route
return super().dispatch(request, *args, **kwargs) return super().dispatch(request, *args, **kwargs)
def get_queryset (self): def get_queryset (self):
qs = self.route.get_queryset(self.model, self.request, **self.kwargs) qs = self.route.get_queryset(self.model, self.request, **self.kwargs)
qs = qs.filter(public = True)
query = self.query or PostListView.Query(self.request.GET) query = self.query or PostListView.Query(self.request.GET)
if query.exclude: if query.exclude:
@ -92,6 +93,9 @@ class PostDetailView (DetailView):
Detail view for posts and children Detail view for posts and children
""" """
template_name = 'cms/detail.html' template_name = 'cms/detail.html'
website = None
embed = False
sections = [] sections = []
def __init__ (self, sections = None, *args, **kwargs): def __init__ (self, sections = None, *args, **kwargs):
@ -100,13 +104,13 @@ class PostDetailView (DetailView):
def get_queryset (self, **kwargs): def get_queryset (self, **kwargs):
if self.model: if self.model:
return super().get_queryset(**kwargs).filter(public = True) return super().get_queryset(**kwargs).filter(published = True)
return [] return []
def get_object (self, **kwargs): def get_object (self, **kwargs):
if self.model: if self.model:
object = super().get_object(**kwargs) object = super().get_object(**kwargs)
if object.public: if object.published:
return object return object
return None return None
@ -134,17 +138,19 @@ class ViewSet:
detail_view = PostDetailView detail_view = PostDetailView
detail_sections = [] detail_sections = []
def __init__ (self): def __init__ (self, website = None):
self.detail_sections = [ self.detail_sections = [
section() section()
for section in self.detail_sections for section in self.detail_sections
] ]
self.detail_view = self.detail_view.as_view( self.detail_view = self.detail_view.as_view(
model = self.model, model = self.model,
sections = self.detail_sections sections = self.detail_sections,
website = website,
) )
self.list_view = self.list_view.as_view( self.list_view = self.list_view.as_view(
model = self.model model = self.model,
website = website,
) )
self.urls = [ route.as_url(self.model, self.list_view) self.urls = [ route.as_url(self.model, self.list_view)
@ -152,6 +158,31 @@ class ViewSet:
[ routes.DetailRoute.as_url(self.model, self.detail_view ) ] [ routes.DetailRoute.as_url(self.model, self.detail_view ) ]
class Menu (DetailView):
template_name = 'cms/menu.html'
name = ''
enabled = True
classes = ''
position = '' # top, left, bottom, right
sections = None
def get_context_data (self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'name': self.name,
'classes': self.classes,
'position': self.position,
'sections': [
section.get(self.request, object = self.object)
for section in self.sections
]
})
def get (self, **kwargs):
context = self.get_context_data(**kwargs)
return render_to_string(self.template_name, context)
class Section (DetailView): class Section (DetailView):
""" """
@ -159,6 +190,7 @@ class Section (DetailView):
in order to have extra content about a post. in order to have extra content about a post.
""" """
template_name = 'cms/section.html' template_name = 'cms/section.html'
require_object = False
classes = '' classes = ''
title = '' title = ''
content = '' content = ''
@ -177,11 +209,12 @@ class Section (DetailView):
return context return context
def get (self, request, **kwargs): def get (self, request, **kwargs):
self.object = kwargs.get('object') self.object = kwargs.get('object') or self.object
context = self.get_context_data(**kwargs) context = self.get_context_data(**kwargs)
return render_to_string(self.template_name, context) return render_to_string(self.template_name, context)
class ListSection (Section): class ListSection (Section):
""" """
Section to render list. The context item 'object_list' is used as list of Section to render list. The context item 'object_list' is used as list of
@ -197,6 +230,8 @@ class ListSection (Section):
self.title = title self.title = title
self.text = text self.text = text
use_icons = True
icon_size = '32x32'
template_name = 'cms/section_list.html' template_name = 'cms/section_list.html'
def get_object_list (self): def get_object_list (self):
@ -204,11 +239,28 @@ class ListSection (Section):
def get_context_data (self, **kwargs): def get_context_data (self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['classes'] += ' section_list' context.update({
context['object_list'] = self.get_object_list() 'classes': context.classes + ' section_list',
'icon_size': self.icon_size,
'object_list': self.get_object_list(),
})
return context return context
class UrlListSection (ListSection):
classes = 'section_urls'
targets = None
def get_object_list (self, request, **kwargs):
return [
ListSection.Item(
target.image or None,
'<a href="{}">{}</a>'.format(target.detail_url(), target.title)
)
for target in self.targets
]
class PostListSection (PostListView): class PostListSection (PostListView):
route = None route = None
model = None model = None
@ -222,8 +274,8 @@ class PostListSection (PostListView):
def dispatch (self, request, *args, **kwargs): def dispatch (self, request, *args, **kwargs):
kwargs = self.get_kwargs(kwargs) kwargs = self.get_kwargs(kwargs)
# TODO: to_string response = super().dispatch(request, *args, **kwargs)
return super().dispatch(request, *args, **kwargs) return str(response.content)
# TODO: # TODO:
# - get_title: pass object / queryset # - get_title: pass object / queryset

24
cms/website.py Normal file
View File

@ -0,0 +1,24 @@
import cms.routes as routes
class Website:
name = ''
logo = None
menus = None
router = None
def __init__ (self, **kwargs):
self.__dict__.update(kwargs)
if not self.router:
self.router = routes.Router()
if not self.sets:
self.sets = []
def register_set (self, view_set):
view_set = view_set(website = self)
self.router.register_set(view_set)
@property
def urls (self):
return self.router.get_urlpatterns()

View File

@ -39,22 +39,19 @@ class TrackInline (SortableTabularInline):
extra = 10 extra = 10
class DescriptionAdmin (admin.ModelAdmin): class NameableAdmin (admin.ModelAdmin):
fields = [ 'name', 'tags', 'description' ] fields = [ 'name' ]
def tags (obj): list_display = ['id', 'name']
return ', '.join(obj.tags.names())
list_display = ['id', 'name', tags]
list_filter = [] list_filter = []
search_fields = ['name',] search_fields = ['name',]
@admin.register(Sound) @admin.register(Sound)
class SoundAdmin (DescriptionAdmin): class SoundAdmin (NameableAdmin):
fields = None fields = None
fieldsets = [ fieldsets = [
(None, { 'fields': DescriptionAdmin.fields + ['path' ] } ), (None, { 'fields': NameableAdmin.fields + ['path' ] } ),
(None, { 'fields': ['duration', 'date', 'fragment' ] } ) (None, { 'fields': ['duration', 'date', 'fragment' ] } )
] ]
@ -66,15 +63,15 @@ class StreamAdmin (SortableModelAdmin):
@admin.register(Program) @admin.register(Program)
class ProgramAdmin (DescriptionAdmin): class ProgramAdmin (NameableAdmin):
fields = DescriptionAdmin.fields + ['stream'] fields = NameableAdmin.fields + ['stream']
inlines = [ ScheduleInline ] inlines = [ ScheduleInline ]
@admin.register(Episode) @admin.register(Episode)
class EpisodeAdmin (DescriptionAdmin): class EpisodeAdmin (NameableAdmin):
list_filter = ['program'] + DescriptionAdmin.list_filter list_filter = ['program'] + NameableAdmin.list_filter
fields = DescriptionAdmin.fields + ['sounds'] fields = NameableAdmin.fields + ['sounds']
inlines = (TrackInline, DiffusionInline) inlines = (TrackInline, DiffusionInline)

View File

@ -24,20 +24,11 @@ def date_or_default (date, date_only = False):
return date return date
class Description (models.Model): class Nameable (models.Model):
name = models.CharField ( name = models.CharField (
_('name'), _('name'),
max_length = 128, max_length = 128,
) )
description = models.TextField (
_('description'),
max_length = 1024,
blank = True, null = True
)
tags = TaggableManager(
_('tags'),
blank = True,
)
def get_slug_name (self): def get_slug_name (self):
return slugify(self.name) return slugify(self.name)
@ -51,7 +42,7 @@ class Description (models.Model):
abstract = True abstract = True
class Track (Description): class Track (Nameable):
# There are no nice solution for M2M relations ship (even without # There are no nice solution for M2M relations ship (even without
# through) in django-admin. So we unfortunately need to make one- # through) in django-admin. So we unfortunately need to make one-
# to-one relations and add a position argument # to-one relations and add a position argument
@ -68,6 +59,10 @@ class Track (Description):
default = 0, default = 0,
help_text=_('position in the playlist'), help_text=_('position in the playlist'),
) )
tags = TaggableManager(
_('tags'),
blank = True,
)
def __str__(self): def __str__(self):
return ' '.join([self.artist, ':', self.name ]) return ' '.join([self.artist, ':', self.name ])
@ -77,7 +72,7 @@ class Track (Description):
verbose_name_plural = _('Tracks') verbose_name_plural = _('Tracks')
class Sound (Description): class Sound (Nameable):
""" """
A Sound is the representation of a sound, that can be: A Sound is the representation of a sound, that can be:
- An episode podcast/complete record - An episode podcast/complete record
@ -134,7 +129,7 @@ class Sound (Description):
if not self.pk: if not self.pk:
self.date = self.get_mtime() self.date = self.get_mtime()
super(Sound, self).save(*args, **kwargs) super().save(*args, **kwargs)
def __str__ (self): def __str__ (self):
return '/'.join(self.path.split('/')[-3:]) return '/'.join(self.path.split('/')[-3:])
@ -393,7 +388,7 @@ class Stream (models.Model):
return '#{} {}'.format(self.priority, self.name) return '#{} {}'.format(self.priority, self.name)
class Program (Description): class Program (Nameable):
stream = models.ForeignKey( stream = models.ForeignKey(
Stream, Stream,
verbose_name = _('stream'), verbose_name = _('stream'),
@ -419,7 +414,7 @@ class Program (Description):
return schedule return schedule
class Episode (Description): class Episode (Nameable):
program = models.ForeignKey( program = models.ForeignKey(
Program, Program,
verbose_name = _('program'), verbose_name = _('program'),

View File

@ -15,6 +15,15 @@ def add_inline (base_model, post_model, prepend = False):
max_num = 1 max_num = 1
verbose_name = _('Post') verbose_name = _('Post')
fieldsets = [
(None, {
'fields': ['title', 'content', 'image', 'tags']
}),
(None, {
'fields': ['date', 'published', 'author', 'thread']
})
]
registry = admin.site._registry registry = admin.site._registry
if not base_model in registry: if not base_model in registry:
raise TypeError(str(base_model) + " not in admin registry") raise TypeError(str(base_model) + " not in admin registry")
@ -28,8 +37,8 @@ def add_inline (base_model, post_model, prepend = False):
registry[base_model].inlines = inlines registry[base_model].inlines = inlines
add_inline(programs.Program, Program) add_inline(programs.Program, Program, True)
add_inline(programs.Episode, Episode) add_inline(programs.Episode, Episode, True)
#class ArticleAdmin (DescriptionAdmin): #class ArticleAdmin (DescriptionAdmin):

View File

@ -4,7 +4,7 @@ from website.models import *
from website.views import * from website.views import *
from cms.models import Article from cms.models import Article
from cms.views import ViewSet from cms.views import ViewSet, Menu
from cms.routes import * from cms.routes import *
class ProgramSet (ViewSet): class ProgramSet (ViewSet):
@ -38,11 +38,20 @@ class ArticleSet (ViewSet):
DateRoute, DateRoute,
] ]
router = Router()
router.register_set(ProgramSet())
router.register_set(EpisodeSet())
router.register_set(ArticleSet())
urlpatterns = router.get_urlpatterns() website = Website(
name = 'RadioCampus',
menus = [
Menu(
position = 'top',
sections = []
)
],
})
website.register_set(ProgramSet)
website.register_set(EpisodeSet)
website.register_set(ArticleSet)
urlpatterns = website.urls