work on pagination

This commit is contained in:
bkfox 2016-05-30 13:23:46 +02:00
parent 94b9aab006
commit e0a268505e
5 changed files with 75 additions and 16 deletions

View File

@ -7,10 +7,10 @@ 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 Signal, post_save
from django.dispatch import receiver
import bleach
from taggit.managers import TaggableManager
from aircox.cms import routes
@ -52,6 +52,19 @@ class Comment(models.Model):
_('comment'),
)
def make_safe(self):
self.author = bleach.clean(self.author, tags=[])
if self.email:
self.email = bleach.clean(self.email, tags=[])
if self.url:
self.url = bleach.clean(self.url, tags=[])
self.content = bleach.clean(
self.content,
tags=settings.AIRCOX_CMS_BLEACH_COMMENT_TAGS,
attributes=settings.AIRCOX_CMS_BLEACH_COMMENT_ATTRS
)
class Post (models.Model):
"""
@ -150,6 +163,19 @@ class Post (models.Model):
name = route.get_view_name(name)
return reverse(name, kwargs = kwargs)
def make_safe(self):
self.title = bleach.clean(
self.title,
tags=settings.AIRCOX_CMS_BLEACH_TITLE_TAGS,
attributes=settings.AIRCOX_CMS_BLEACH_TITLE_ATTRS
)
self.content = bleach.clean(
self.content,
tags=settings.AIRCOX_CMS_BLEACH_CONTENT_TAGS,
attributes=settings.AIRCOX_CMS_BLEACH_CONTENT_ATTRS
)
self.tags = [ bleach.clean(tag, tags=[]) for tag in self.tags.all() ]
class Meta:
abstract = True

View File

@ -57,7 +57,11 @@ class Section(View):
def __init__ (self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.css_class = 'section' if not self.css_class else ' section'
self.css_class += 'section' if not self.css_class else ' section'
if type(self) != Section:
self.css_class += ' section_' + type(self).__name__.lower()
if not self.attrs:
self.attrs = {}
if self.name:
@ -186,7 +190,7 @@ class List(Section):
fields = [ 'date', 'time', 'image', 'title', 'content' ]
image_size = '64x64'
truncate = 64
truncate = 16
def __init__ (self, items = None, *args, **kwargs):
"""
@ -222,6 +226,7 @@ class Comments(List):
truncate = 0
fields = [ 'date', 'time', 'author', 'content' ]
comment_form = None
success_message = ( _('Your message is awaiting for approval'),
_('Your message has been published') )
error_message = _('There was an error while saving your post. '
@ -254,6 +259,7 @@ class Comments(List):
"""
Forward data to this view
"""
# TODO: comment satanize
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
comment = comment_form.save(commit=False)

View File

@ -3,6 +3,8 @@
{% load i18n %}
{% load thumbnail %}
{% load aircox_cms %}
{% block content %}
<ul>
@ -72,18 +74,39 @@
{% if not page_obj or embed %}
<a href="{{list.url}}" title={% trans "More elements" %}>&#8690;</a>
{% else %}
{# FIXME: page numbers #}
{% with page_obj.paginator.num_pages as num_pages %}
{% 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.number > 3 %}
<a href="?page=1">1</a>
{% if page_obj.number > 4 %}
&#8230;
{% endif %}
{% endif %}
{% for i in page_obj.number|around:2 %}
{% if i == page_obj.number %}
{{ page_obj.number }}
{% elif i > 0 and i <= num_pages %}
<a href="?page={{ i }}">{{ i }}</a>
{% endif %}
{% endfor %}
{% with page_obj.number|add:"2" as max %}
{% if max < num_pages %}
{% if max|add:"1" < num_pages %}
&#8230;
{% endif %}
<a href="?page={{ num_pages }}">{{ num_pages }}</a>
{% endif %}
{% endwith %}
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">next</a>
{% endif %}
{% endwith %}
{% endif %}
</nav>
{% endif %}

View File

@ -5,7 +5,7 @@ register = template.Library()
@register.filter(name='threads')
def threads (post, sep = '/'):
def threads(post, sep = '/'):
"""
print a list of all parents, from top to bottom
"""
@ -20,4 +20,8 @@ def threads (post, sep = '/'):
for post in posts if post.published
])
@register.filter(name='around')
def around(page_num, n):
return range(page_num-n, page_num+n+1)

View File

@ -49,7 +49,7 @@ class PostListView(PostBaseView, ListView):
"""
template_name = 'aircox/cms/list.html'
allow_empty = True
paginate_by = 25
paginate_by = 3
model = None
route = None
@ -102,7 +102,7 @@ class PostListView(PostBaseView, ListView):
if not self.list:
import aircox.cms.sections as sections
self.list = sections.List(
truncate = 64,
truncate = 32,
fields = [ 'date', 'time', 'image', 'title', 'content' ],
)