per section index

This commit is contained in:
j 2017-12-22 23:22:14 +01:00
parent 394580ed51
commit 9f87135c5a
4 changed files with 101 additions and 39 deletions

View File

@ -36,6 +36,11 @@ urlpatterns = [
url(r'directions.html', RedirectView.as_view(url='/directions/')), url(r'directions.html', RedirectView.as_view(url='/directions/')),
url(r'campstudio.html', RedirectView.as_view(url='/directions/')), url(r'campstudio.html', RedirectView.as_view(url='/directions/')),
url(r'^texts/index/$', views.section_list, {'section': 'Texts'}, name='texts_list'),
url(r'^events/index/$', views.section_list, {'section': 'Events'}, name='events_list'),
url(r'^projects/index/$', views.section_list, {'section': 'Projects'}, name='projects_list'),
url(r'^works/index/$', views.section_list, {'section': 'Works'}, name='works_list'),
url(r'^texts/(?P<shortname>.+)/$', views.texts, name='texts'), url(r'^texts/(?P<shortname>.+)/$', views.texts, name='texts'),
url(r'^events/(?P<shortname>.+)/$', views.events, name='events'), url(r'^events/(?P<shortname>.+)/$', views.events, name='events'),
url(r'^projects/(?P<shortname>.+)/$', views.projects, name='projects'), url(r'^projects/(?P<shortname>.+)/$', views.projects, name='projects'),

View File

@ -36,7 +36,8 @@
</div> </div>
</div> </div>
<form method="get" action='/search/'> <form method="get" action='/search/'>
<input type="search" name="q" class="search-form"> <input type="submit" style="visibility: hidden;" /> <input type="search" name="q" class="search-form" value="{{query|default:""}}">
<input type="submit" style="visibility: hidden;" />
</form> </form>
{% block content %} {% block content %}
{% endblock %} {% endblock %}

View File

@ -5,61 +5,66 @@
<div class="row"> <div class="row">
<div class="row"> <div class="row">
<div class="small-4 small-offset-2 columns results-title"> <div class="small-4 small-offset-2 columns results-title">
{% if section %}
<h3> {{section }}</h3>
{% else %}
<h3> Search results </h3> <h3> Search results </h3>
{% endif %}
</div> </div>
</div> </div>
{% for content in results %} {% for row in content %}
{% if content.type.name == 'news' %} {% if row.type.name == 'news' %}
<div class="row"> <div class="row">
<div class= "small-4 small-offset-2 columns"> <div class= "small-4 small-offset-2 columns">
<h6 class="sidebar-date"> <font color="#ef4e5c"> <b> {{content.datestart}} </b> <h6 class="sidebar-date"> <font color="#ef4e5c"> <b> {{row.datestart}} </b>
{{ content.title }} <br /> {{ row.title }} <br />
{{ content.formatted_header|striptags|safe }} </font> {{ row.formatted_header|striptags|safe }} </font>
</h6> </h6>
</div> </div>
</div> </div>
{% elif content.type.name == 'events' %} {% elif row.type.name == 'events' %}
<div class="row results"> <div class="row results">
<div class="small-4 small-offset-2 columns"> <div class="small-4 small-offset-2 columns">
<img src="{{ content.image_url }}"> <img src="{{ row.image_url }}">
</div> </div>
<div class="small-4 end columns"> <div class="small-4 end columns">
<a href="{{ content.get_absolute_url }}" class="sidebar-title">{{ content.title }}</a> <a href="{{ row.get_absolute_url }}" class="sidebar-title">{{ row.title }}</a>
<h6 class="sidebar-date"> <font color="#ef4e5c"> <b> {{ content.datestart }} {% if content.dateend %} - {{content.dateend}} {% endif %} </b></font> <h6 class="sidebar-date"> <font color="#ef4e5c"> <b> {{ row.datestart }} {% if row.dateend %} - {{row.dateend}} {% endif %} </b></font>
{% if content.place %}<br/> {{content.place}} {% endif%}</h6> {% if row.place %}<br/> {{row.place}} {% endif%}</h6>
<p> {{content.header|striptags|safe}} </p> <p> {{row.header|striptags|safe}} </p>
<a href="{{ content.get_absolute_url }}">read more</a> </p> <a href="{{ row.get_absolute_url }}">read more</a> </p>
</div> </div>
</div> </div>
{% else %} {% else %}
{% if request.user.is_staff %}
<div class="row"> <div class="row">
<div class="small-4 small-offset-2 columns"> <div class="small-4 small-offset-2 columns">
Add view for content type: {{content.type}} Add view for content type: {{row.type}}
</div> </div>
</div> </div>
{% endif %}
{% endif %} {% endif %}
{% endfor %} {% endfor %}
<br> <br>
<br> <br>
{% if results.has_other_pages %} {% if content.has_other_pages %}
<div class="row"> <div class="row">
<div class="small-4 small-offset-4 columns"> <div class="small-4 small-offset-4 columns">
<ul class="pagination"> <ul class="pagination">
{% if results.has_previous %} {% if content.has_previous %}
<li><a href="?q={{ query }}&page={{ results.previous_page_number }}">&laquo;</a></li> <li><a href="?{{ base_query }}&page={{ content.previous_page_number }}">&laquo;</a></li>
{% else %} {% else %}
<li class="disabled"><span>&laquo;</span></li> <li class="disabled"><span>&laquo;</span></li>
{% endif %} {% endif %}
{% for i in results.paginator.page_range %} {% for i in content.paginator.page_range %}
{% if results.number == i %} {% if content.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li> <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %} {% else %}
<li><a href="?q={{ query }}&page={{ i }}">{{ i }}</a></li> <li><a href="?{{ base_query }}page={{ i }}">{{ i }}</a></li>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
{% if results.has_next %} {% if content.has_next %}
<li><a href="?q={{ query }}&page={{ results.next_page_number }}">&raquo;</a></li> <li><a href="?{{ base_query }}page={{ content.next_page_number }}">&raquo;</a></li>
{% else %} {% else %}
<li class="disabled"><span>&raquo;</span></li> <li class="disabled"><span>&raquo;</span></li>
{% endif %} {% endif %}

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
from datetime import datetime from datetime import datetime
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
@ -35,21 +36,62 @@ def index(request):
return render(request, 'index.html', context) return render(request, 'index.html', context)
def section_index(request, types, section): SECTION_TYPE = {
featured = Content.objects.filter(type__name__in=types, featured=True).order_by('-datestart')[:1] 'Projects': ['projects'],
'Works': ['works'],
'Texts': ['texts'],
'Events': ['events', 'news'],
}
def section_content(section):
types = SECTION_TYPE.get(section, [section.lower()])
content = Content.objects.filter(type__name__in=types).order_by('-datestart') content = Content.objects.filter(type__name__in=types).order_by('-datestart')
content = content.filter(published=True)
return content
def section_index(request, section):
types = SECTION_TYPE.get(section, [section.lower()])
featured = Content.objects.filter(type__name__in=types, featured=True).order_by('-datestart')[:1]
content = section_content(section)
if featured: if featured:
content = content.exclude(pk=featured[0].pk) content = content.exclude(pk=featured[0].pk)
content = content.filter(published=True)
return render(request, 'section_index.html', { return render(request, 'section_index.html', {
'section': section, 'section': section,
'featured': featured, 'featured': featured,
'content': content 'content': content
}) })
def section_list(request, section):
content = section_content(section)
q = request.GET.get('q')
content = limit_content(content, q)
page = request.GET.get('page', 1)
paginator = Paginator(content, 5)
try:
content = paginator.page(page)
except PageNotAnInteger:
content = paginator.page(1)
except EmptyPage:
content = paginator.page(paginator.num_pages)
base_query = ''
if q:
base_query = 'q=%s&' % q
return render(request, 'results.html', {
'base_query': base_query,
'section': section,
'content': content,
'query': q
})
def event(request): def event(request):
now = datetime.now() now = datetime.now()
display_events = ['events'] display_events = ['events', 'news']
featured = Content.objects.filter(type__name='events', featured=True).order_by('-datestart')[:1] featured = Content.objects.filter(type__name='events', featured=True).order_by('-datestart')[:1]
base = Content.objects.filter(type__name__in=display_events).order_by('-datestart') base = Content.objects.filter(type__name__in=display_events).order_by('-datestart')
base = base.filter(published=True) base = base.filter(published=True)
@ -58,7 +100,7 @@ def event(request):
upcoming_events = base.filter(datestart__gt=now).order_by('-datestart') upcoming_events = base.filter(datestart__gt=now).order_by('-datestart')
ongoing_events = base.filter(datestart__lt=now, dateend__gte=now).order_by('-datestart') ongoing_events = base.filter(datestart__lt=now, dateend__gte=now).order_by('-datestart')
past_events = base.filter(Q(dateend__lt=now)|Q(dateend=None, datestart__lt=now))[:10] past_events = base.filter(Q(dateend__lt=now) | Q(dateend=None, datestart__lt=now))[:10]
context = { context = {
'upcoming_events': upcoming_events, 'upcoming_events': upcoming_events,
@ -81,7 +123,7 @@ def events(request, shortname=None):
def projects(request, shortname=None): def projects(request, shortname=None):
if not shortname: if not shortname:
return section_index(request, ['projects'], 'Projects') return section_index(request, 'Projects')
projects = get_object_or_404(Content, shortname=shortname, type__name='projects') projects = get_object_or_404(Content, shortname=shortname, type__name='projects')
if not projects.published and not request.user.is_staff: if not projects.published and not request.user.is_staff:
raise Http404 raise Http404
@ -95,7 +137,7 @@ def projects(request, shortname=None):
def works(request, shortname=None): def works(request, shortname=None):
if not shortname: if not shortname:
return section_index(request, ['works'], 'Works') return section_index(request, 'Works')
works = get_object_or_404(Content, shortname=shortname, type__name='works') works = get_object_or_404(Content, shortname=shortname, type__name='works')
if not works.published and not request.user.is_staff: if not works.published and not request.user.is_staff:
raise Http404 raise Http404
@ -105,7 +147,7 @@ def works(request, shortname=None):
def texts(request, shortname=None): def texts(request, shortname=None):
if not shortname: if not shortname:
return section_index(request, ['texts'], 'Texts') return section_index(request, 'Texts')
texts = get_object_or_404(Content, shortname=shortname, type__name='texts') texts = get_object_or_404(Content, shortname=shortname, type__name='texts')
gallery = get_or_none(Gallery, slug=shortname) gallery = get_or_none(Gallery, slug=shortname)
latest_content_list = Content.objects.filter(type__name='texts') latest_content_list = Content.objects.filter(type__name='texts')
@ -123,22 +165,31 @@ def page(request, shortname):
return render(request, 'page.html', {'content': content}) return render(request, 'page.html', {'content': content})
def limit_content(content, q):
if q:
content = content.filter(Q(body__icontains=q) | Q(title__icontains=q) | Q(header__icontains=q)).distinct()
return content
def search(request): def search(request):
content = Content.objects.filter(published=True).order_by('-datestart')
q = request.GET.get('q') q = request.GET.get('q')
results = Content.objects.filter(Q(body__icontains=q) | Q(title__icontains=q) | Q(header__icontains=q)).distinct() content = limit_content(content, q)
results = results.filter(published=True)
results = results.order_by('-datestart')
page = request.GET.get('page', 1) page = request.GET.get('page', 1)
paginator = Paginator(results, 5) paginator = Paginator(content, 5)
try: try:
results = paginator.page(page) content = paginator.page(page)
except PageNotAnInteger: except PageNotAnInteger:
results = paginator.page(1) content = paginator.page(1)
except EmptyPage: except EmptyPage:
results = paginator.page(paginator.num_pages) content = paginator.page(paginator.num_pages)
base_query = ''
if q:
base_query = 'q=%s&' % q
return render(request, 'results.html', { return render(request, 'results.html', {
'results': results, 'base_query': base_query,
'content': content,
'query': q 'query': q
}) })