tags url; fix bugs
This commit is contained in:
parent
23016a594f
commit
a3a9beac6d
|
@ -1,3 +1,5 @@
|
||||||
|
import copy
|
||||||
|
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.utils.translation import ugettext as _, ugettext_lazy
|
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')
|
verbose_name = _('Related post')
|
||||||
|
|
||||||
inline = inline or InlineModel
|
inline = inline or InlineModel
|
||||||
|
inline.fieldsets = copy.deepcopy(inline.fieldsets)
|
||||||
|
|
||||||
# remove bound attributes
|
# remove bound attributes
|
||||||
for none, dic in inline.fieldsets:
|
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,
|
inject_inline(post_model._meta.get_field('related').rel.to,
|
||||||
inline, prepend)
|
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):
|
def inject_inline(model, inline, prepend = False):
|
||||||
registry = admin.site._registry
|
registry = admin.site._registry
|
||||||
if not model in registry:
|
if not model in registry:
|
||||||
|
|
|
@ -7,7 +7,7 @@ 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 Signal, post_save
|
from django.db.models.signals import Signal, post_save, pre_save
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
|
|
||||||
import bleach
|
import bleach
|
||||||
|
@ -265,11 +265,24 @@ class RelatedMeta (models.base.ModelBase):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def make_auto_create(cl, model):
|
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:
|
if not model._relation.rel_to_post:
|
||||||
return
|
return
|
||||||
|
|
||||||
def handler(sender, instance, created, *args, **kwargs):
|
def handler_rel(sender, instance, created, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
handler for the related object
|
||||||
|
"""
|
||||||
rel = model._relation
|
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)
|
post = model.objects.filter(related = instance)
|
||||||
if post.count():
|
if post.count():
|
||||||
post = post[0]
|
post = post[0]
|
||||||
|
@ -280,7 +293,7 @@ class RelatedMeta (models.base.ModelBase):
|
||||||
return
|
return
|
||||||
post.rel_to_post()
|
post.rel_to_post()
|
||||||
post.save(avoid_sync = True)
|
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):
|
def __new__ (cl, name, bases, attrs):
|
||||||
# TODO: allow proxy models and better inheritance
|
# TODO: allow proxy models and better inheritance
|
||||||
|
|
|
@ -135,6 +135,9 @@ class ThreadRoute(Route):
|
||||||
|
|
||||||
|
|
||||||
class DateRoute(Route):
|
class DateRoute(Route):
|
||||||
|
"""
|
||||||
|
Select posts using a date with format yyyy/mm/dd;
|
||||||
|
"""
|
||||||
name = 'date'
|
name = 'date'
|
||||||
url_args = [
|
url_args = [
|
||||||
('year', '[0-9]{4}'),
|
('year', '[0-9]{4}'),
|
||||||
|
@ -160,6 +163,11 @@ class DateRoute(Route):
|
||||||
|
|
||||||
|
|
||||||
class SearchRoute(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'
|
name = 'search'
|
||||||
|
|
||||||
@classmethod
|
@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('+', ', ')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -16,9 +16,8 @@
|
||||||
</time>
|
</time>
|
||||||
|
|
||||||
{% if object.tags.all %}
|
{% if object.tags.all %}
|
||||||
{# TODO: url to the tags #}
|
|
||||||
<div class="tags">
|
<div class="tags">
|
||||||
{{ object.tags.all|join:', ' }}
|
{{ object|post_tags:' - '|safe }}
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</header>
|
</header>
|
||||||
|
|
|
@ -1,13 +1,33 @@
|
||||||
from django import template
|
from django import template
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
|
|
||||||
|
import aircox.cms.routes as routes
|
||||||
|
|
||||||
|
|
||||||
register = template.Library()
|
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')
|
@register.filter(name='threads')
|
||||||
def threads(post, sep = '/'):
|
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]
|
posts = [post]
|
||||||
while posts[0].thread:
|
while posts[0].thread:
|
||||||
|
|
|
@ -60,7 +60,7 @@ class BaseView:
|
||||||
Return a context with all attributes of this classe plus 'view' set
|
Return a context with all attributes of this classe plus 'view' set
|
||||||
to self.
|
to self.
|
||||||
"""
|
"""
|
||||||
context = super().get_context_data(**kwargs)
|
context = {}
|
||||||
|
|
||||||
# update from sections
|
# update from sections
|
||||||
if self.sections:
|
if self.sections:
|
||||||
|
@ -76,6 +76,8 @@ class BaseView:
|
||||||
'content': self.sections.render(self.request, **kwargs)
|
'content': self.sections.render(self.request, **kwargs)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
context.update(super().get_context_data(**kwargs))
|
||||||
|
|
||||||
# then from me
|
# then from me
|
||||||
context.update({
|
context.update({
|
||||||
'website': self.website,
|
'website': self.website,
|
||||||
|
|
5
notes.md
5
notes.md
|
@ -3,13 +3,15 @@
|
||||||
- debug/prod configuration
|
- debug/prod configuration
|
||||||
|
|
||||||
# TODO:
|
# TODO:
|
||||||
|
- general:
|
||||||
|
- timezone shit
|
||||||
|
|
||||||
- programs:
|
- programs:
|
||||||
- schedule:
|
- schedule:
|
||||||
- (old) schedule.to_string unused? commented
|
- (old) schedule.to_string unused? commented
|
||||||
- check one week on two
|
- check one week on two
|
||||||
- write more tests
|
- write more tests
|
||||||
- sounds:
|
- sounds:
|
||||||
- print sounds of diffusions
|
|
||||||
- inline admin
|
- inline admin
|
||||||
- one sound, one diffusion?
|
- one sound, one diffusion?
|
||||||
|
|
||||||
|
@ -36,6 +38,7 @@
|
||||||
- website:
|
- website:
|
||||||
- diffusions:
|
- diffusions:
|
||||||
- filter sounds for undiffused diffusions
|
- filter sounds for undiffused diffusions
|
||||||
|
- print sounds of diffusions
|
||||||
- player:
|
- player:
|
||||||
- "listen" + "favorite" buttons made easy + automated
|
- "listen" + "favorite" buttons made easy + automated
|
||||||
- single mode / play next auto
|
- single mode / play next auto
|
||||||
|
|
|
@ -69,7 +69,7 @@ class ProgramAdmin(NameableAdmin):
|
||||||
schedule.short_description = _("Schedule")
|
schedule.short_description = _("Schedule")
|
||||||
|
|
||||||
list_display = ('id', 'name', 'active', 'schedule')
|
list_display = ('id', 'name', 'active', 'schedule')
|
||||||
fields = NameableAdmin.fields + [ 'station', 'active' ]
|
fields = NameableAdmin.fields + [ 'active' ]
|
||||||
# TODO list_display
|
# TODO list_display
|
||||||
inlines = [ ScheduleInline, StreamInline ]
|
inlines = [ ScheduleInline, StreamInline ]
|
||||||
|
|
||||||
|
|
|
@ -12,14 +12,14 @@ class TrackInline(SortableTabularInline):
|
||||||
form = forms.TrackForm
|
form = forms.TrackForm
|
||||||
model = programs.Track
|
model = programs.Track
|
||||||
sortable = 'position'
|
sortable = 'position'
|
||||||
extra = 10
|
extra = 4
|
||||||
|
|
||||||
admin.site.register(models.Article, cms.PostAdmin)
|
admin.site.register(models.Article, cms.PostAdmin)
|
||||||
admin.site.register(models.Program, cms.RelatedPostAdmin)
|
admin.site.register(models.Program, cms.RelatedPostAdmin)
|
||||||
admin.site.register(models.Diffusion, cms.RelatedPostAdmin)
|
admin.site.register(models.Diffusion, cms.RelatedPostAdmin)
|
||||||
|
|
||||||
cms.inject_related_inline(models.Program, True)
|
|
||||||
cms.inject_inline(programs.Diffusion, TrackInline, True)
|
cms.inject_inline(programs.Diffusion, TrackInline, True)
|
||||||
|
cms.inject_related_inline(models.Program, True)
|
||||||
cms.inject_related_inline(models.Diffusion, True)
|
cms.inject_related_inline(models.Diffusion, True)
|
||||||
cms.inject_related_inline(models.Sound, True)
|
cms.inject_related_inline(models.Sound, True)
|
||||||
|
|
||||||
|
|
|
@ -77,13 +77,14 @@ class Diffusions(sections.List):
|
||||||
type = programs.Diffusion.Type.normal
|
type = programs.Diffusion.Type.normal
|
||||||
)
|
)
|
||||||
if self.object:
|
if self.object:
|
||||||
object = self.object.related
|
obj = self.object.related
|
||||||
if type(object) == programs.Program:
|
obj_type = type(obj)
|
||||||
qs = qs.filter(program = object)
|
if obj_type == programs.Program:
|
||||||
elif type(object) == programs.Diffusion:
|
qs = qs.filter(program = obj)
|
||||||
if object.initial:
|
elif obj_type == programs.Diffusion:
|
||||||
object = object.initial
|
if obj.initial:
|
||||||
qs = qs.filter(initial = object) | qs.filter(pk = object.pk)
|
obj = obj.initial
|
||||||
|
qs = qs.filter(initial = obj) | qs.filter(pk = obj.pk)
|
||||||
if filter_args:
|
if filter_args:
|
||||||
qs = qs.filter(**filter_args).order_by('start')
|
qs = qs.filter(**filter_args).order_by('start')
|
||||||
|
|
||||||
|
@ -212,13 +213,17 @@ class Schedule(Diffusions):
|
||||||
|
|
||||||
def get_object_list(self):
|
def get_object_list(self):
|
||||||
date = self.date_or_default()
|
date = self.date_or_default()
|
||||||
qs = routes.DateRoute.get_queryset(
|
year, month, day = date.year, date.month, date.day
|
||||||
models.Diffusion, self.request,
|
|
||||||
year = date.year,
|
diffs = [d.initial if d.initial else d
|
||||||
month = date.month,
|
for d in programs.Diffusion.objects.filter(
|
||||||
day = date.day
|
start__year = year,
|
||||||
).order_by('date')
|
start__month = month,
|
||||||
return qs
|
start__day = day,
|
||||||
|
)
|
||||||
|
]
|
||||||
|
return models.Diffusion.objects.filter(related__in = diffs). \
|
||||||
|
order_by('date')
|
||||||
|
|
||||||
def get_context_data(self, *args, **kwargs):
|
def get_context_data(self, *args, **kwargs):
|
||||||
context = super().get_context_data(*args, **kwargs)
|
context = super().get_context_data(*args, **kwargs)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user