some stuff
This commit is contained in:
parent
81a2e0533f
commit
6dd2036ec9
|
@ -9,11 +9,10 @@ from website.models import *
|
|||
|
||||
|
||||
def add_inline (base_model, post_model, prepend = False):
|
||||
class InlineModel (GenericStackedInline):
|
||||
class InlineModel (admin.StackedInline):
|
||||
model = post_model
|
||||
extra = 1
|
||||
max_num = 1
|
||||
ct_field = 'object_type'
|
||||
verbose_name = _('Post')
|
||||
|
||||
registry = admin.site._registry
|
||||
|
@ -29,8 +28,8 @@ def add_inline (base_model, post_model, prepend = False):
|
|||
registry[base_model].inlines = inlines
|
||||
|
||||
|
||||
add_inline(Program, ObjectDescription)
|
||||
add_inline(Episode, ObjectDescription)
|
||||
add_inline(programs.Program, ProgramPost)
|
||||
add_inline(programs.Episode, EpisodePost)
|
||||
|
||||
|
||||
#class ArticleAdmin (DescriptionAdmin):
|
||||
|
|
|
@ -8,7 +8,7 @@ from django.utils import timezone
|
|||
from django.db.models.signals import post_save
|
||||
from django.dispatch import receiver
|
||||
|
||||
from programs.models import *
|
||||
import programs.models as programs
|
||||
|
||||
|
||||
class Thread (models.Model):
|
||||
|
@ -38,7 +38,7 @@ class Thread (models.Model):
|
|||
return str(self.post)
|
||||
|
||||
|
||||
class Post (models.Model):
|
||||
class BasePost (models.Model):
|
||||
thread = models.ForeignKey(
|
||||
Thread,
|
||||
on_delete=models.SET_NULL,
|
||||
|
@ -62,31 +62,8 @@ class Post (models.Model):
|
|||
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
|
||||
title = ''
|
||||
content = ''
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
@ -97,24 +74,54 @@ 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:
|
||||
if not issubclass(sender, BasePost) 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 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)
|
||||
}
|
||||
|
||||
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 (Post):
|
||||
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'),
|
||||
|
@ -129,6 +136,19 @@ class Article (Post):
|
|||
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,
|
||||
|
|
|
@ -68,9 +68,6 @@ class Route:
|
|||
|
||||
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']
|
||||
|
|
|
@ -14,43 +14,35 @@
|
|||
<div class="post_list {{ classes }}">
|
||||
{% for post in object_list %}
|
||||
<a class="post_item"
|
||||
href="post.get_detail_url">
|
||||
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">
|
||||
<time datetime="{{ post.date }}" class="post_datetime">
|
||||
{% if 'date' in list.fields %}
|
||||
<span class="post_date">
|
||||
{{ post_date|date:'D. d F' }},
|
||||
{{ post.date|date:'D. d F' }},
|
||||
</span>
|
||||
{% endif %}
|
||||
{% if 'time' in list.fields %}
|
||||
<span class="post_time">
|
||||
{{ post_date|date:'H:i' }},
|
||||
{{ 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 %}
|
||||
<img src="{% thumbnail post.image "64x64" crop %}" class="post_image">
|
||||
{% endif %}
|
||||
|
||||
{% if 'title' in list.fields %}
|
||||
{% with post_title=post.get_title %}
|
||||
<h4 class="post_title">post_title</h4>
|
||||
{% endwith %}
|
||||
<h4 class="post_title">{{ post.title }}</h4>
|
||||
{% endif %}
|
||||
|
||||
{% if 'content' in list.fields %}
|
||||
{% with post_content=post.get_content %}
|
||||
<div class="post_content">
|
||||
{{ post_content|safe|striptags|truncatechars:"64" }}
|
||||
{{ post.content|safe|striptags|truncatechars:"64" }}
|
||||
</div>
|
||||
{% endwith %}
|
||||
{% endif %}
|
||||
</a>
|
||||
{% endfor %}
|
||||
|
|
|
@ -8,15 +8,15 @@ from website.routes import *
|
|||
routes = Routes()
|
||||
|
||||
routes.register( SearchRoute(Article, PostListView) )
|
||||
#routes.register( SearchRoute(ProgramPost, PostListView, base_name = 'programs') )
|
||||
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(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(ProgramPost, PostListView, base_name = 'programs') )
|
||||
#routes.register( DateRoute(EpisodePost, PostListView, base_name = 'episodes') )
|
||||
|
||||
urlpatterns = routes.get_urlpatterns()
|
||||
|
|
|
@ -13,10 +13,10 @@ class PostListView (ListView):
|
|||
"""
|
||||
Request availables parameters
|
||||
"""
|
||||
embed = False
|
||||
exclude = None
|
||||
order = 'desc'
|
||||
reverse = False
|
||||
format = 'normal'
|
||||
|
||||
def __init__ (self, query):
|
||||
my_class = self.__class__
|
||||
|
@ -33,19 +33,13 @@ class PostListView (ListView):
|
|||
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)
|
||||
|
||||
|
@ -61,7 +55,6 @@ class PostListView (ListView):
|
|||
qs.order_by('date', 'id')
|
||||
else:
|
||||
qs.order_by('-date', '-id')
|
||||
|
||||
return qs
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user