tags url; fix bugs

This commit is contained in:
bkfox
2016-06-20 13:38:34 +02:00
parent 23016a594f
commit a3a9beac6d
10 changed files with 108 additions and 25 deletions

View File

@ -1,3 +1,5 @@
import copy
from django.contrib import admin
from django.utils.translation import ugettext as _, ugettext_lazy
@ -67,6 +69,7 @@ def inject_related_inline(post_model, prepend = False, inline = None):
verbose_name = _('Related post')
inline = inline or InlineModel
inline.fieldsets = copy.deepcopy(inline.fieldsets)
# remove bound attributes
for none, dic in inline.fieldsets:
@ -78,6 +81,12 @@ def inject_related_inline(post_model, prepend = False, inline = None):
inject_inline(post_model._meta.get_field('related').rel.to,
inline, prepend)
def inject(model, name, value):
registry = admin.site._registry
if not model in registry:
return TypeError('{} not in admin registry'.format(model.__name__))
setattr(registry[model], name, value)
def inject_inline(model, inline, prepend = False):
registry = admin.site._registry
if not model in registry:

View File

@ -7,7 +7,7 @@ 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.db.models.signals import Signal, post_save, pre_save
from django.dispatch import receiver
import bleach
@ -265,11 +265,24 @@ class RelatedMeta (models.base.ModelBase):
@classmethod
def make_auto_create(cl, model):
"""
Enable auto_create on the given RelatedPost model if it is available.
"""
if not model._relation.rel_to_post:
return
def handler(sender, instance, created, *args, **kwargs):
def handler_rel(sender, instance, created, *args, **kwargs):
"""
handler for the related object
"""
rel = model._relation
# TODO: make the check happen by overriding inline save function
# this check is done in order to allow creation of multiple
# models when using admin.inlines: related is saved before
# the post, so no post is found, then create an extra post
if hasattr(instance, '__cms_post'):
return
post = model.objects.filter(related = instance)
if post.count():
post = post[0]
@ -280,7 +293,7 @@ class RelatedMeta (models.base.ModelBase):
return
post.rel_to_post()
post.save(avoid_sync = True)
post_save.connect(handler, model._relation.model, False)
post_save.connect(handler_rel, model._relation.model, False)
def __new__ (cl, name, bases, attrs):
# TODO: allow proxy models and better inheritance

View File

@ -135,6 +135,9 @@ class ThreadRoute(Route):
class DateRoute(Route):
"""
Select posts using a date with format yyyy/mm/dd;
"""
name = 'date'
url_args = [
('year', '[0-9]{4}'),
@ -160,6 +163,11 @@ class DateRoute(Route):
class SearchRoute(Route):
"""
Search post using request.GET['q']. It searches in fields designated by
model.search_fields
"""
# TODO: q argument in url_args -> need to allow optional url_args
name = 'search'
@classmethod
@ -183,3 +191,27 @@ class SearchRoute(Route):
}
class TagsRoute(Route):
"""
Select posts that contains the given tags. The tags are separated
by a '+'.
"""
name = 'tags'
url_args = [
('tags', '(\w|-|_|\+)+')
]
@classmethod
def get_queryset(cl, model, request, tags, **kwargs):
tags = tags.split('+')
return model.objects.filter(tags__name__in=tags)
@classmethod
def get_title(cl, model, request, tags, **kwargs):
return _('Tagged %(model)s with %(tags)s') % {
'model': model._meta.verbose_name_plural,
'tags': tags.replace('+', ', ')
}

View File

@ -16,9 +16,8 @@
</time>
{% if object.tags.all %}
{# TODO: url to the tags #}
<div class="tags">
{{ object.tags.all|join:', ' }}
{{ object|post_tags:' - '|safe }}
</div>
{% endif %}
</header>

View File

@ -1,13 +1,33 @@
from django import template
from django.core.urlresolvers import reverse
import aircox.cms.routes as routes
register = template.Library()
@register.filter(name='post_tags')
def post_tags(post, sep = '-'):
"""
print the list of all the tags of the given post, with url if available
"""
tags = post.tags.all()
r = []
for tag in tags:
try:
r.append('<a href="{url}">{name}</a>'.format(
url = post.route_url(routes.TagsRoute, tags = tag),
name = tag,
))
except:
r.push(tag)
return sep.join(r)
@register.filter(name='threads')
def threads(post, sep = '/'):
"""
print a list of all parents, from top to bottom
print the list of all the parents of the given post, from top to bottom
"""
posts = [post]
while posts[0].thread:

View File

@ -60,7 +60,7 @@ class BaseView:
Return a context with all attributes of this classe plus 'view' set
to self.
"""
context = super().get_context_data(**kwargs)
context = {}
# update from sections
if self.sections:
@ -76,6 +76,8 @@ class BaseView:
'content': self.sections.render(self.request, **kwargs)
})
context.update(super().get_context_data(**kwargs))
# then from me
context.update({
'website': self.website,