forked from rc/aircox
work on lists
This commit is contained in:
parent
c69ef73a94
commit
474a33cfa5
|
@ -56,6 +56,10 @@ class Route:
|
|||
def get_title (cl, model, request, **kwargs):
|
||||
return ''
|
||||
|
||||
@classmethod
|
||||
def get_view_name (cl, name):
|
||||
return name + '_' + cl.name
|
||||
|
||||
@classmethod
|
||||
def as_url (cl, name, model, view, view_kwargs = None):
|
||||
pattern = '^{}/{}'.format(name, cl.name)
|
||||
|
@ -77,7 +81,7 @@ class Route:
|
|||
kwargs.update(view_kwargs)
|
||||
|
||||
return url(pattern, view, kwargs = kwargs,
|
||||
name = name + '_' + cl.name)
|
||||
name = cl.get_view_name(name))
|
||||
|
||||
|
||||
class DetailRoute (Route):
|
||||
|
@ -123,7 +127,7 @@ class ThreadRoute (Route):
|
|||
@classmethod
|
||||
def get_queryset (cl, website, model, request, thread_model, pk, **kwargs):
|
||||
if type(thread_model) is str:
|
||||
thread_model = website.registry.get(thread_model).model
|
||||
thread_model = website.registry.get(thread_model)
|
||||
|
||||
if not thread_model:
|
||||
return
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
{% load thumbnail %}
|
||||
|
||||
{% block content %}
|
||||
<div class="post_list {{ classes }}">
|
||||
<div class="post_list {{ classes }} {% if embed %}embed{% endif %}">
|
||||
{% for post in object_list %}
|
||||
<a class="post_item"
|
||||
href="{{ post.detail_url }}">
|
||||
|
@ -39,6 +39,27 @@
|
|||
</a>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
<nav>
|
||||
{% if embed %}
|
||||
{% with view.get_url as list_url %}
|
||||
{% if list_url %}
|
||||
<a href="{{list_url}}" title="More elements">⇲</a>
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
{% else %}
|
||||
{% if page_obj.has_previous %}
|
||||
<a href="?page={{ page_obj.previous_page_number }}">previous</a>
|
||||
{% endif %}
|
||||
|
||||
<span class="current">
|
||||
{{ page_obj.number }} / {{ page_obj.paginator.num_pages }}
|
||||
</span>
|
||||
|
||||
{% if page_obj.has_next %}
|
||||
<a href="?page={{ page_obj.next_page_number }}">next</a>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
</nav>
|
||||
{% endblock %}
|
||||
|
||||
|
||||
|
|
|
@ -23,12 +23,12 @@
|
|||
{% endblock %}
|
||||
</div>
|
||||
|
||||
{% if bottom %}
|
||||
<div class="section_bottom">
|
||||
{% block section_bottom %}
|
||||
{{ bottom }}
|
||||
{% if footer %}
|
||||
<footer class="section_footer">
|
||||
{% block section_footer %}
|
||||
{{ footer }}
|
||||
{% endblock %}
|
||||
</div>
|
||||
</footer>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% endblock %}
|
||||
|
|
21
aircox_cms/utils.py
Normal file
21
aircox_cms/utils.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.urlresolvers import reverse
|
||||
|
||||
|
||||
def get_url (website, route, model, kwargs):
|
||||
name = website.name_of_model(model)
|
||||
if not name:
|
||||
return
|
||||
name = route.get_view_name(name)
|
||||
return reverse(name, kwargs = kwargs)
|
||||
|
||||
|
||||
def filter_thread (qs, object):
|
||||
model_type = ContentType.objects.get_for_model(object.__class__)
|
||||
return qs.filter(
|
||||
thread_pk = object.pk,
|
||||
thread_type__pk = model_type.pk
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -5,11 +5,13 @@ from django.shortcuts import render
|
|||
from django.template.loader import render_to_string
|
||||
from django.views.generic import ListView, DetailView
|
||||
from django.views.generic.base import View, TemplateResponseMixin
|
||||
from django.core.paginator import Paginator
|
||||
from django.core import serializers
|
||||
from django.utils.translation import ugettext as _, ugettext_lazy
|
||||
from django.utils.html import escape
|
||||
|
||||
import aircox_cms.routes as routes
|
||||
import aircox_cms.utils as utils
|
||||
|
||||
|
||||
class PostBaseView:
|
||||
|
@ -31,7 +33,7 @@ class PostBaseView:
|
|||
|
||||
if not self.embed:
|
||||
context['menus'] = {
|
||||
k: v.get(self.request, **kwargs)
|
||||
k: v.get(self.request, website = self.website, **kwargs)
|
||||
for k, v in {
|
||||
k: self.website.get_menu(k)
|
||||
for k in self.website.menu_layouts
|
||||
|
@ -55,6 +57,7 @@ class PostListView (PostBaseView, ListView):
|
|||
order = 'desc'
|
||||
reverse = False
|
||||
fields = None
|
||||
page = 1
|
||||
|
||||
def __init__ (self, query):
|
||||
if query:
|
||||
|
@ -69,6 +72,7 @@ class PostListView (PostBaseView, ListView):
|
|||
|
||||
template_name = 'aircox_cms/list.html'
|
||||
allow_empty = True
|
||||
paginate_by = 50
|
||||
model = None
|
||||
|
||||
route = None
|
||||
|
@ -128,6 +132,12 @@ class PostListView (PostBaseView, ListView):
|
|||
**self.kwargs)
|
||||
return title
|
||||
|
||||
def get_url (self):
|
||||
if self.route:
|
||||
return utils.get_urls(self.website, self.route,
|
||||
self.model, self.kwargs)
|
||||
return ''
|
||||
|
||||
|
||||
class PostDetailView (DetailView, PostBaseView):
|
||||
"""
|
||||
|
@ -161,7 +171,7 @@ class PostDetailView (DetailView, PostBaseView):
|
|||
context.update(self.get_base_context())
|
||||
context.update({
|
||||
'sections': [
|
||||
section.get(self.request, **kwargs)
|
||||
section.get(self.request, website = self.website, **kwargs)
|
||||
for section in self.sections
|
||||
]
|
||||
})
|
||||
|
@ -189,13 +199,15 @@ class Menu (View):
|
|||
'classes': self.classes,
|
||||
'position': self.position,
|
||||
'sections': [
|
||||
section.get(self.request, object = None, **kwargs)
|
||||
section.get(self.request, website = self.website,
|
||||
object = None, **kwargs)
|
||||
for section in self.sections
|
||||
]
|
||||
}
|
||||
|
||||
def get (self, request, **kwargs):
|
||||
def get (self, request, website, **kwargs):
|
||||
self.request = request
|
||||
self.website = website
|
||||
context = self.get_context_data(**kwargs)
|
||||
return render_to_string(self.template_name, context)
|
||||
|
||||
|
@ -225,8 +237,9 @@ class BaseSection (View):
|
|||
'content': self.content,
|
||||
}
|
||||
|
||||
def get (self, request, **kwargs):
|
||||
def get (self, request, website, **kwargs):
|
||||
self.request = request
|
||||
self.website = website
|
||||
self.kwargs = kwargs
|
||||
|
||||
context = self.get_context_data()
|
||||
|
@ -245,14 +258,14 @@ class Section (BaseSection):
|
|||
object_required = False
|
||||
title = ''
|
||||
header = ''
|
||||
bottom = ''
|
||||
footer = ''
|
||||
|
||||
def get_context_data (self):
|
||||
context = super().get_context_data()
|
||||
context.update({
|
||||
'title': self.title,
|
||||
'header': self.header,
|
||||
'bottom': self.bottom,
|
||||
'footer': self.footer,
|
||||
})
|
||||
return context
|
||||
|
||||
|
@ -277,7 +290,6 @@ class Sections:
|
|||
static(self.url),
|
||||
)
|
||||
|
||||
|
||||
class PostContent (Section):
|
||||
"""
|
||||
Render the content of the Post (format the text a bit and escape HTML
|
||||
|
@ -290,7 +302,6 @@ class Sections:
|
|||
content = re.sub(r'\n', r'<br>', content)
|
||||
return content
|
||||
|
||||
|
||||
class PostImage (Section):
|
||||
"""
|
||||
Render the image of the Post
|
||||
|
@ -301,7 +312,6 @@ class Sections:
|
|||
self.object.image.url
|
||||
)
|
||||
|
||||
|
||||
class List (Section):
|
||||
"""
|
||||
Section to render list. The context item 'object_list' is used as list of
|
||||
|
@ -342,7 +352,6 @@ class Sections:
|
|||
})
|
||||
return context
|
||||
|
||||
|
||||
class Urls (List):
|
||||
"""
|
||||
Render a list of urls of targets that are Posts
|
||||
|
@ -369,6 +378,9 @@ class Sections:
|
|||
icon_size = '64x64'
|
||||
fields = [ 'date', 'time', 'image', 'title', 'content' ]
|
||||
|
||||
def get_url (self):
|
||||
return ''
|
||||
|
||||
def get_object_list (self):
|
||||
return []
|
||||
|
||||
|
|
|
@ -21,6 +21,11 @@ class Website:
|
|||
self.urls = []
|
||||
self.__dict__.update(kwargs)
|
||||
|
||||
def name_of_model (self, model):
|
||||
for name, _model in self.registry.items():
|
||||
if model is _model:
|
||||
return name
|
||||
|
||||
def register_model (self, name, model):
|
||||
"""
|
||||
Register a model and return the name under which it is registered.
|
||||
|
|
|
@ -81,8 +81,8 @@ a:hover {
|
|||
}
|
||||
|
||||
.page {
|
||||
width: 100%;
|
||||
padding: 1.5em 0em;
|
||||
width: calc(100% - 0.4em);
|
||||
padding: 1.5em 0.2em;
|
||||
background-color: rgba(255, 255, 255, 0.8);
|
||||
}
|
||||
|
||||
|
@ -92,11 +92,20 @@ a:hover {
|
|||
}
|
||||
|
||||
|
||||
main .post_list {
|
||||
background-color: #F2F2F2;
|
||||
.post_list {
|
||||
padding: 0.1em;
|
||||
border: 1px #818181 dotted;
|
||||
}
|
||||
|
||||
.post_list.embed + nav {
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
.post_list:not(.embed) + nav {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
|
||||
main .post_list .post_item {
|
||||
min-height: 64px;
|
||||
padding: 0.2em;
|
||||
|
|
|
@ -72,6 +72,7 @@ website.register (
|
|||
'episode',
|
||||
Episode,
|
||||
sections = base_sections,
|
||||
routes = base_routes,
|
||||
)
|
||||
|
||||
urlpatterns = website.urls
|
||||
|
|
|
@ -7,6 +7,8 @@ from django.utils import timezone as tz
|
|||
from django.utils.translation import ugettext as _, ugettext_lazy
|
||||
|
||||
import aircox_programs.models as programs
|
||||
import aircox_cms.routes as routes
|
||||
import aircox_cms.utils as utils
|
||||
from aircox_cms.views import Sections
|
||||
|
||||
from website.models import *
|
||||
|
@ -39,7 +41,11 @@ class EpisodesSection (Sections.Posts):
|
|||
title = _('Episodes')
|
||||
|
||||
def get_object_list (self):
|
||||
return Episode.objects.filter(related__program = self.object.related.pk)
|
||||
return utils.filter_thread(Episode.objects, self.object)
|
||||
|
||||
def get_url (self):
|
||||
return utils.get_url(self.website, routes.ThreadRoute, Episode,
|
||||
{ 'thread_model': 'program', 'pk': self.object.pk})
|
||||
|
||||
class PreviousDiffusions (Sections.Posts):
|
||||
title = _('Previous Diffusions')
|
||||
|
@ -65,4 +71,3 @@ class PreviousDiffusions (Sections.Posts):
|
|||
|
||||
return episodes
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user