forked from rc/aircox
221 lines
6.0 KiB
Python
221 lines
6.0 KiB
Python
from django.db import models
|
|
from django.utils import timezone as tz
|
|
from django.conf.urls import url
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.utils import timezone
|
|
from django.utils.translation import ugettext as _, ugettext_lazy
|
|
|
|
class Route:
|
|
"""
|
|
Base class for routing. Given a model, we generate url specific for each
|
|
type of route.
|
|
|
|
The generated url takes this form:
|
|
name + '/' + route_name + '/' + '/'.join(route_url_args)
|
|
|
|
And their name (to use for reverse:
|
|
name + '_' + route_name
|
|
|
|
By default name is the verbose name of the model. It is always in
|
|
singular form.
|
|
"""
|
|
name = None # route name
|
|
url_args = [] # arguments passed from the url [ (name : regex),... ]
|
|
|
|
@classmethod
|
|
def get_queryset(cl, model, request, **kwargs):
|
|
"""
|
|
Called by the view to get the queryset when it is needed
|
|
"""
|
|
pass
|
|
|
|
@classmethod
|
|
def get_object(cl, model, request, **kwargs):
|
|
"""
|
|
Called by the view to get the object when it is needed
|
|
"""
|
|
pass
|
|
|
|
@classmethod
|
|
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, view, view_kwargs = None):
|
|
pattern = '^{}/{}'.format(name, cl.name)
|
|
if cl.url_args:
|
|
url_args = '/'.join([
|
|
'(?P<{}>{}){}'.format(
|
|
arg, expr,
|
|
(optional and optional[0] and '?') or ''
|
|
)
|
|
for arg, expr, *optional in cl.url_args
|
|
])
|
|
pattern += '/' + url_args
|
|
pattern += '/?$'
|
|
|
|
kwargs = {
|
|
'route': cl,
|
|
}
|
|
if view_kwargs:
|
|
kwargs.update(view_kwargs)
|
|
|
|
return url(pattern, view, kwargs = kwargs,
|
|
name = cl.get_view_name(name))
|
|
|
|
|
|
class DetailRoute(Route):
|
|
name = 'detail'
|
|
url_args = [
|
|
('pk', '[0-9]+'),
|
|
('slug', '(\w|-|_)+', True),
|
|
]
|
|
|
|
@classmethod
|
|
def get_object(cl, model, request, pk, **kwargs):
|
|
return model.objects.get(pk = int(pk))
|
|
|
|
|
|
class AllRoute(Route):
|
|
name = 'all'
|
|
|
|
@classmethod
|
|
def get_queryset(cl, model, request, **kwargs):
|
|
return model.objects.all()
|
|
|
|
@classmethod
|
|
def get_title(cl, model, request, **kwargs):
|
|
return _('All %(model)s') % {
|
|
'model': model._meta.verbose_name_plural
|
|
}
|
|
|
|
|
|
class ThreadRoute(Route):
|
|
"""
|
|
Select posts using by their assigned thread.
|
|
|
|
- "thread_model" can be a string with the name of a registered item on
|
|
website or a model.
|
|
- "pk" is the pk of the thread item.
|
|
"""
|
|
name = 'thread'
|
|
url_args = [
|
|
('thread_model', '(\w|_|-)+'),
|
|
('pk', '[0-9]+'),
|
|
]
|
|
|
|
@classmethod
|
|
def get_thread(cl, model, thread_model, pk=None):
|
|
"""
|
|
Return a model if not pk, otherwise the model element of given id
|
|
"""
|
|
if type(thread_model) is str:
|
|
thread_model = model._website.registry.get(thread_model)
|
|
if not thread_model or not pk:
|
|
return thread_model
|
|
return thread_model.objects.get(pk=pk)
|
|
|
|
|
|
@classmethod
|
|
def get_queryset(cl, model, request, thread_model, pk, **kwargs):
|
|
thread = cl.get_thread(model, thread_model, pk)
|
|
return model.get_with_thread(thread_model = thread, thread_id = pk)
|
|
|
|
@classmethod
|
|
def get_title(cl, model, request, thread_model, pk, **kwargs):
|
|
thread = cl.get_thread(model, thread_model, pk)
|
|
return '<a href="{url}">{title}</a>'.format(
|
|
url = thread.url(),
|
|
title = _('%(name)s: %(model)s') % {
|
|
'model': model._meta.verbose_name_plural,
|
|
'name': thread.title,
|
|
}
|
|
)
|
|
|
|
|
|
class DateRoute(Route):
|
|
"""
|
|
Select posts using a date with format yyyy/mm/dd;
|
|
"""
|
|
name = 'date'
|
|
url_args = [
|
|
('year', '[0-9]{4}'),
|
|
('month', '[0-1]?[0-9]'),
|
|
('day', '[0-3]?[0-9]'),
|
|
]
|
|
|
|
@classmethod
|
|
def get_queryset(cl, model, request, year, month, day, **kwargs):
|
|
return model.objects.filter(
|
|
date__year = int(year),
|
|
date__month = int(month),
|
|
date__day = int(day),
|
|
)
|
|
|
|
@classmethod
|
|
def get_title(cl, model, request, year, month, day, **kwargs):
|
|
date = tz.datetime(year = int(year), month = int(month), day = int(day))
|
|
return _('%(model)s of %(date)s') % {
|
|
'model': model._meta.verbose_name_plural,
|
|
'date': date.strftime('%A %d %B %Y'),
|
|
}
|
|
|
|
|
|
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
|
|
def get_queryset(cl, model, request, **kwargs):
|
|
q = request.GET.get('q') or ''
|
|
qs = None
|
|
|
|
## TODO: by tag
|
|
for search_field in model.search_fields or []:
|
|
r = models.Q(**{ search_field + '__icontains': q })
|
|
if qs: qs = qs | r
|
|
else: qs = r
|
|
|
|
return model.objects.filter(qs).distinct()
|
|
|
|
@classmethod
|
|
def get_title(cl, model, request, **kwargs):
|
|
return _('Search "%(search)s" in %(model)s') % {
|
|
'model': model._meta.verbose_name_plural,
|
|
'search': request.GET.get('q') or '',
|
|
}
|
|
|
|
|
|
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('+', ', ')
|
|
}
|
|
|
|
|
|
|