camp/content/views.py

162 lines
6.2 KiB
Python
Raw Normal View History

2017-05-20 12:45:26 +00:00
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
2017-12-18 12:54:04 +00:00
from datetime import datetime
from django.core.exceptions import ObjectDoesNotExist
2017-07-03 08:14:07 +00:00
from django.db.models import Q
2017-12-18 12:54:04 +00:00
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render, redirect
from django.urls import reverse
2017-07-03 08:14:07 +00:00
from django.views.generic.list import ListView
2017-12-18 12:54:04 +00:00
2017-07-03 08:14:07 +00:00
from photologue.views import GalleryListView
from photologue.models import Photo, Gallery
2017-12-18 12:54:04 +00:00
from .models import Content, ContentContent
2017-05-20 12:45:26 +00:00
# Create your views here.
2017-06-10 09:59:00 +00:00
def index(request):
2017-12-18 12:54:04 +00:00
now = datetime.now()
display_events = ['news', 'events']
upcoming_events = Content.objects.filter(datestart__gt=now).filter(type__name__in=display_events).order_by('-datestart')[:12]
ongoing_events = Content.objects.filter(datestart__lt=now, dateend__gte=now).filter(type__name__in=display_events).order_by('-datestart')[:12]
past_events = Content.objects.filter(dateend__lt=now).filter(type__name__in=display_events).order_by('-datestart')[:12]
homepage = Content.objects.filter(type__name='homepage').order_by('-datestart')[:1]
context = {
'upcoming_events': upcoming_events,
'ongoing_events': ongoing_events,
'past_events': past_events,
'homepage': homepage}
2017-06-10 09:59:00 +00:00
return render(request, 'index.html', context)
def project(request):
2017-12-18 12:54:04 +00:00
type = 'projects'
featured = Content.objects.filter(type__name=type, featured=True).order_by('-datestart')[:1]
content = Content.objects.filter(type__name=type).order_by('-datestart')
return render(request, 'section_index.html', {
'section': 'Projects',
'featured': featured,
'content': content
})
2017-07-03 08:14:07 +00:00
2017-08-03 17:44:00 +00:00
def work(request):
2017-12-18 12:54:04 +00:00
type = 'works'
featured = Content.objects.filter(type__name=type, featured=True).order_by('-datestart')[:1]
content = Content.objects.filter(type__name=type).order_by('-datestart')
return render(request, 'section_index.html', {
'section': 'Works',
'featured': featured,
'content': content
})
2017-08-03 17:44:00 +00:00
def event(request):
2017-12-18 12:54:04 +00:00
now = datetime.now()
display_events = ['events']
upcoming_events = Content.objects.filter(datestart__gt=now).filter(type__name__in=display_events).order_by('-datestart')
ongoing_events = Content.objects.filter(datestart__lt=now, dateend__gte=now).filter(type__name__in=display_events).order_by('-datestart')
past_events = Content.objects.filter(Q(dateend__lt=now)|Q(dateend=None, datestart__lt=now)).filter(type__name__in=display_events).order_by('-datestart')
featured = Content.objects.filter(type__name='events', featured=True).order_by('-datestart')[:1]
context = {
'upcoming_events': upcoming_events,
'ongoing_events': ongoing_events,
'past_events': past_events,
'featured': featured,
}
2017-08-03 17:44:00 +00:00
return render(request, 'event.html', context)
def text(request):
2017-12-18 12:54:04 +00:00
type = 'texts'
featured = Content.objects.filter(type__name=type, featured=True).order_by('-datestart')[:1]
content = Content.objects.filter(type__name=type).order_by('-datestart')
return render(request, 'section_index.html', {
'section': 'Texts',
'featured': featured,
'content': content
})
2017-08-03 17:44:00 +00:00
2017-07-03 08:14:07 +00:00
def events(request, shortname):
2017-12-18 12:54:04 +00:00
if not shortname:
return event(request)
events = get_object_or_404(Content, shortname=shortname, type__name__in=['news', 'events'])
2017-08-03 17:44:00 +00:00
gallery = get_or_none(Gallery, slug=shortname)
2017-12-18 12:54:04 +00:00
latest_content_list = Content.objects.filter(type__name='events').order_by('-datestart')
2017-08-03 17:44:00 +00:00
return render(request, 'events.html', {'events': events, 'latest_content_list': latest_content_list, 'gallery': gallery})
2017-07-03 08:14:07 +00:00
def projects(request, shortname):
2017-12-18 12:54:04 +00:00
if not shortname:
return project(request)
projects = get_object_or_404(Content, shortname=shortname, type__name='projects')
2017-08-03 17:44:00 +00:00
gallery = get_or_none(Gallery, slug=shortname)
2017-12-18 12:54:04 +00:00
latest_content_list = Content.objects.filter(type__name='projects')
2017-08-03 17:44:00 +00:00
return render(request, 'projects.html', {'projects': projects, 'latest_content_list': latest_content_list, 'gallery':gallery})
def works(request, shortname):
2017-12-18 12:54:04 +00:00
if not shortname:
return work(request)
works = get_object_or_404(Content, shortname=shortname, type__name='works')
2017-08-03 17:44:00 +00:00
gallery = get_or_none(Gallery, slug=shortname)
2017-12-18 12:54:04 +00:00
latest_content_list = Content.objects.filter(type__name='works')
2017-08-03 17:44:00 +00:00
return render(request, 'works.html', {'works': works, 'latest_content_list': latest_content_list, 'gallery':gallery})
def texts(request, shortname):
2017-12-18 12:54:04 +00:00
if not shortname:
return text(request)
2017-08-03 17:44:00 +00:00
texts = get_object_or_404(Content, shortname=shortname)
gallery = get_or_none(Gallery, slug=shortname)
2017-12-18 12:54:04 +00:00
latest_content_list = Content.objects.filter(type__name='texts')
return render(request, 'texts.html', {
'texts': texts,
'latest_content_list': latest_content_list,
'gallery':gallery
})
def page(request, shortname):
content = get_object_or_404(Content, shortname=shortname, type__name='page')
return render(request, 'page.html', {'content': content})
def search(request):
q = request.GET.get('q')
results = Content.objects.filter(body__contains=q).order_by('-datestart')
return render(request, 'results.html', {'results': results})
2017-08-03 17:44:00 +00:00
2017-07-03 08:14:07 +00:00
class GalleryListViews(ListView):
queryset = Gallery.objects.on_site().is_public()
paginate_by = 20
template_name = 'gallery_list.html'
def get_or_none(classmodel, **kwargs):
try:
return classmodel.objects.get(**kwargs)
except classmodel.DoesNotExist:
return None
2017-12-18 12:54:04 +00:00
def redirect_index(request):
segment = request.GET.get('show')
if segment == 'pastevents':
return redirect(reverse('works'))
elif segment == 'contact':
return redirect(reverse('contact'))
return redirect(reverse('index'))
def redirect_event(request):
shortname = request.GET.get('this')
if shortname:
return redirect(reverse('content', kwargs={'shortname': shortname}))
id = request.GET.get('id')
if id:
content = get_object_or_404(Content, id=id)
return redirect(reverse('content', kwargs={'shortname': content.shortname}))
return redirect(reverse('events'))
def redirect_images(request, image):
return redirect(image)
2017-07-03 08:14:07 +00:00