camp/content/views.py

295 lines
9.4 KiB
Python
Raw Normal View History

2017-05-20 12:45:26 +00:00
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
2017-12-22 22:22:14 +00:00
2017-12-18 12:54:04 +00:00
from datetime import datetime
2018-03-07 16:25:55 +00:00
import re
2017-12-18 12:54:04 +00:00
2018-02-21 15:01:25 +00:00
from django.conf import settings
2017-12-18 12:54:04 +00:00
from django.core.exceptions import ObjectDoesNotExist
2018-02-21 15:01:25 +00:00
from django.core.mail import EmailMessage
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
2018-08-21 18:13:38 +00:00
from django.db.models import Q, Count
2017-12-18 14:17:06 +00:00
from django.http import HttpResponse, Http404
2017-12-18 12:54:04 +00:00
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
2018-08-21 18:13:38 +00:00
from django.db.models.functions.datetime import TruncYear
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
2018-02-21 15:01:25 +00:00
from . import forms
2017-05-20 12:45:26 +00:00
2018-01-01 13:31:30 +00:00
ITEMS_PER_PAGE = 30
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']
2017-12-18 14:17:06 +00:00
base = Content.objects.filter(type__name__in=display_events).order_by('-datestart')
base = base.filter(published=True)
upcoming_events = base.filter(datestart__gt=now)[:12]
ongoing_events = base.filter(datestart__lt=now, dateend__gte=now)[:12]
2017-12-23 11:06:13 +00:00
past_events = base.filter(Q(dateend__lt=now) | Q(dateend=None, datestart__lt=now))[:12]
2017-12-18 14:17:06 +00:00
2017-12-18 12:54:04 +00:00
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)
2017-07-03 08:14:07 +00:00
2017-12-22 22:22:14 +00:00
SECTION_TYPE = {
'Projects': ['projects'],
'Works': ['works'],
'Texts': ['texts'],
'Events': ['events', 'news'],
}
def section_content(section):
types = SECTION_TYPE.get(section, [section.lower()])
2017-12-18 14:17:06 +00:00
content = Content.objects.filter(type__name__in=types).order_by('-datestart')
2017-12-22 22:22:14 +00:00
content = content.filter(published=True)
return content
2017-12-23 11:22:39 +00:00
def section_index(request, section, max_length=10):
2017-12-22 22:22:14 +00:00
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)
2017-12-18 14:17:06 +00:00
if featured:
content = content.exclude(pk=featured[0].pk)
2017-12-23 11:22:39 +00:00
more_content = content.count() > max_length
content = content[:max_length]
2017-12-18 12:54:04 +00:00
return render(request, 'section_index.html', {
2017-12-18 14:17:06 +00:00
'section': section,
2017-12-18 12:54:04 +00:00
'featured': featured,
2017-12-23 11:22:39 +00:00
'content': content,
'more_content': more_content,
2017-12-18 12:54:04 +00:00
})
2017-08-03 17:44:00 +00:00
2018-08-21 18:13:38 +00:00
def content_years(content):
years = [
year['year'].year
for year in content.annotate(year=TruncYear('datestart')).values('year').annotate(count=Count('shortname')).order_by('-year')
]
return years
def filter_by_years(content, year):
years = content_years(content)
if year.isdigit():
year = int(year)
else:
year = years[0]
content = content.filter(datestart__gte='%s-01-01' % year, datestart__lt='%s-01-01' % (year+1))
return {
'content': content,
'year': year,
'years': years
}
2017-12-22 22:22:14 +00:00
def section_list(request, section):
content = section_content(section)
q = request.GET.get('q')
content = limit_content(content, q)
2018-08-21 18:13:38 +00:00
year = request.GET.get('year', '')
2018-08-22 16:40:36 +00:00
if year or section not in ('Projects', 'Works'):
context = filter_by_years(content, year)
else:
context = {
'content': content
}
2017-12-22 22:22:14 +00:00
2018-08-21 18:13:38 +00:00
'''
2017-12-22 22:22:14 +00:00
page = request.GET.get('page', 1)
2018-01-01 13:31:30 +00:00
paginator = Paginator(content, ITEMS_PER_PAGE)
2017-12-22 22:22:14 +00:00
try:
content = paginator.page(page)
except PageNotAnInteger:
content = paginator.page(1)
except EmptyPage:
content = paginator.page(paginator.num_pages)
2018-08-21 18:13:38 +00:00
'''
2017-12-22 22:22:14 +00:00
base_query = ''
if q:
base_query = 'q=%s&' % q
2018-08-21 18:13:38 +00:00
context.update({
2017-12-22 22:22:14 +00:00
'base_query': base_query,
2017-12-23 11:06:13 +00:00
'query': q,
'section': section,
2017-12-22 22:22:14 +00:00
})
2018-08-21 18:13:38 +00:00
return render(request, 'results.html', context)
2017-12-22 22:22:14 +00:00
2017-08-03 17:44:00 +00:00
def event(request):
2017-12-18 12:54:04 +00:00
now = datetime.now()
2017-12-22 22:22:14 +00:00
display_events = ['events', 'news']
2017-12-18 12:54:04 +00:00
featured = Content.objects.filter(type__name='events', featured=True).order_by('-datestart')[:1]
2017-12-18 14:17:06 +00:00
base = Content.objects.filter(type__name__in=display_events).order_by('-datestart')
base = base.filter(published=True)
if featured:
base = base.exclude(pk=featured[0].pk)
upcoming_events = base.filter(datestart__gt=now).order_by('-datestart')
ongoing_events = base.filter(datestart__lt=now, dateend__gte=now).order_by('-datestart')
2018-01-01 13:31:30 +00:00
past_events = base.filter(Q(dateend__lt=now) | Q(dateend=None, datestart__lt=now)).order_by('-datestart')[:10]
2017-12-18 14:17:06 +00:00
2017-12-18 12:54:04 +00:00
context = {
'upcoming_events': upcoming_events,
'ongoing_events': ongoing_events,
'past_events': past_events,
'featured': featured,
2017-12-23 11:06:13 +00:00
'section': 'Events',
2017-12-18 12:54:04 +00:00
}
2017-08-03 17:44:00 +00:00
return render(request, 'event.html', context)
2017-12-23 11:06:13 +00:00
def get_related_content(types, current=None, max_length=10):
latest_content_list = Content.objects.filter(type__name__in=types).order_by('-datestart')
if current:
latest_content_list = latest_content_list.exclude(pk=current.pk)
latest_content_list = latest_content_list.filter(published=True)
2017-12-23 11:22:39 +00:00
more = latest_content_list.count() > max_length
2017-12-23 11:06:13 +00:00
latest_content_list = latest_content_list[:max_length]
return latest_content_list, more
def render_content(request, shortname, section, template, types):
2017-12-18 12:54:04 +00:00
if not shortname:
2017-12-23 11:06:13 +00:00
if section == 'Events':
return event(request)
else:
return section_index(request, section)
2018-08-21 12:23:09 +00:00
shortname = shortname.replace(' ', '_')
2017-12-23 11:06:13 +00:00
content = get_object_or_404(Content, shortname=shortname, type__name__in=types)
if not content.published and not request.user.is_staff:
2017-12-18 14:17:06 +00:00
raise Http404
2017-12-23 11:06:13 +00:00
latest_content_list, more = get_related_content(types, content)
return render(request, template, {
'content': content,
'latest_content_list': latest_content_list,
2017-12-23 11:06:13 +00:00
'has_more_content': more,
'section': section,
})
2017-07-03 08:14:07 +00:00
2017-12-23 11:06:13 +00:00
def events(request, shortname=None):
return render_content(request, shortname, 'Events', 'content.html', ['events', 'news'])
2017-12-18 14:17:06 +00:00
def projects(request, shortname=None):
2017-12-23 11:06:13 +00:00
return render_content(request, shortname, 'Projects', 'content.html', ['projects'])
2017-08-03 17:44:00 +00:00
2017-12-18 14:37:32 +00:00
def works(request, shortname=None):
2017-12-23 11:06:13 +00:00
return render_content(request, shortname, 'Works', 'content.html', ['works'])
2017-08-03 17:44:00 +00:00
2017-12-18 14:37:32 +00:00
def texts(request, shortname=None):
2017-12-23 11:22:39 +00:00
return render_content(request, shortname, 'Texts', 'content.html', ['texts'])
2017-12-18 12:54:04 +00:00
def page(request, shortname):
content = get_object_or_404(Content, shortname=shortname, type__name='page')
2017-12-18 14:39:24 +00:00
if not content.published and not request.user.is_staff:
2017-12-18 14:17:06 +00:00
raise Http404
2017-12-18 12:54:04 +00:00
return render(request, 'page.html', {'content': content})
2018-02-21 15:01:25 +00:00
def contact(request):
context = {}
if request.method == 'POST':
form = forms.ContactForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
email = form.cleaned_data['email']
message = form.cleaned_data['message']
subject = '{} has left a message on studio.camp'.format(name)
from_ = settings.CONTACT_FROM_EMAIL
to = settings.CONTACT_TO_EMAIL
msg = EmailMessage(subject, message, from_, to, reply_to=[email])
msg.send(fail_silently=True)
#msg.send()
context['sent'] = True
else:
form = forms.ContactForm()
context['form'] = form
return render(request, 'contact.html', context)
2017-12-18 12:54:04 +00:00
2017-12-22 22:22:14 +00:00
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
2017-12-23 11:06:13 +00:00
2017-12-18 12:54:04 +00:00
def search(request):
2017-12-22 22:22:14 +00:00
content = Content.objects.filter(published=True).order_by('-datestart')
2017-12-22 22:24:48 +00:00
content = content.exclude(type__name__in=['page', 'homepage'])
2017-12-18 12:54:04 +00:00
q = request.GET.get('q')
2017-12-22 22:22:14 +00:00
content = limit_content(content, q)
2018-08-21 18:13:38 +00:00
year = request.GET.get('year', '')
context = filter_by_years(content, year)
'''
2017-12-19 09:40:14 +00:00
page = request.GET.get('page', 1)
2018-01-01 13:31:30 +00:00
paginator = Paginator(content, ITEMS_PER_PAGE)
2017-12-19 09:40:14 +00:00
try:
2017-12-22 22:22:14 +00:00
content = paginator.page(page)
2017-12-19 09:40:14 +00:00
except PageNotAnInteger:
2017-12-22 22:22:14 +00:00
content = paginator.page(1)
2017-12-19 09:40:14 +00:00
except EmptyPage:
2017-12-22 22:22:14 +00:00
content = paginator.page(paginator.num_pages)
2018-08-21 18:13:38 +00:00
'''
2017-12-22 22:22:14 +00:00
base_query = ''
if q:
base_query = 'q=%s&' % q
2017-12-19 09:40:14 +00:00
2018-08-21 18:13:38 +00:00
context.update({
2017-12-22 22:22:14 +00:00
'base_query': base_query,
2017-12-19 09:59:37 +00:00
'query': q
})
2017-08-03 17:44:00 +00:00
2018-08-21 18:13:38 +00:00
return render(request, 'results.html', context)
2017-07-03 08:14:07 +00:00
class GalleryListViews(ListView):
queryset = Gallery.objects.on_site().is_public()
2018-01-01 13:31:30 +00:00
paginate_by = ITEMS_PER_PAGE
2017-07-03 08:14:07 +00:00
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):
2018-08-22 16:40:36 +00:00
shortname = request.GET.get('this', '').replace(' ', '_').lower()
2017-12-18 12:54:04 +00:00
if shortname:
2018-08-21 12:23:09 +00:00
content = get_object_or_404(Content, shortname__iexact=shortname)
2017-12-18 14:17:06 +00:00
return redirect(content.get_absolute_url())
2017-12-18 12:54:04 +00:00
id = request.GET.get('id')
2018-06-10 13:44:16 +00:00
if id:
id = re.compile('\d+').findall(id)
2017-12-18 12:54:04 +00:00
if id:
2018-03-07 16:25:55 +00:00
id = id[0]
2017-12-18 12:54:04 +00:00
content = get_object_or_404(Content, id=id)
2017-12-18 14:17:06 +00:00
return redirect(content.get_absolute_url())
2017-12-18 12:54:04 +00:00
return redirect(reverse('events'))
def redirect_images(request, image):
return redirect(image)
2017-07-03 08:14:07 +00:00