2020-07-31 14:55:26 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
''' Django views. '''
|
2020-07-29 21:28:12 +02:00
|
|
|
from django.shortcuts import render, redirect, get_object_or_404
|
2020-07-29 00:15:12 +02:00
|
|
|
from django.urls import reverse_lazy
|
2020-08-01 19:05:13 +02:00
|
|
|
from django.views.generic.edit import CreateView
|
2020-07-29 20:22:04 +02:00
|
|
|
from django.views import View
|
2020-07-31 00:27:01 +02:00
|
|
|
from django.http import Http404
|
2020-08-01 19:05:13 +02:00
|
|
|
from django.views.generic.edit import UpdateView
|
2020-07-31 00:27:01 +02:00
|
|
|
|
2020-08-06 16:57:43 +02:00
|
|
|
from .forms import (
|
|
|
|
ExplorerCreationForm,
|
|
|
|
PlaceForm,
|
|
|
|
PlaceImageCreateForm,
|
|
|
|
PlaceDeleteForm
|
|
|
|
)
|
2020-08-01 13:11:07 +02:00
|
|
|
from .models import Place, PlaceImage, Voucher
|
2020-07-26 22:19:37 +02:00
|
|
|
|
|
|
|
# Create your views here.
|
|
|
|
|
2020-07-29 00:15:12 +02:00
|
|
|
class SignUpView(CreateView):
|
|
|
|
form_class = ExplorerCreationForm
|
|
|
|
success_url = reverse_lazy('login')
|
|
|
|
template_name = 'signup.html'
|
|
|
|
|
2020-07-26 23:25:07 +02:00
|
|
|
def place_list_view(request,):
|
2020-07-26 23:34:03 +02:00
|
|
|
return render(request, 'placeList.html', {'place_list':Place.objects.all()})
|
2020-07-26 23:25:07 +02:00
|
|
|
|
2020-07-26 22:19:37 +02:00
|
|
|
def place_detail_view(request, pk):
|
2020-07-26 23:25:07 +02:00
|
|
|
return render(request, 'placeOverview.html', {'place':Place.objects.get(pk=pk)})
|
2020-07-26 22:19:37 +02:00
|
|
|
|
|
|
|
def hello_world(request):
|
|
|
|
return render(request, 'hello_world.html', {'text':'Hello World!'})
|
2020-07-29 20:22:04 +02:00
|
|
|
|
2020-08-03 19:14:37 +02:00
|
|
|
class HomeView(View):
|
2020-08-05 18:57:09 +02:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
place_list = Place.objects.all().order_by('submitted_when')[:10]
|
|
|
|
context = {
|
|
|
|
'place_list': place_list
|
|
|
|
}
|
|
|
|
return render(request, 'home.html', context)
|
2020-08-03 19:14:37 +02:00
|
|
|
|
2020-07-31 00:27:01 +02:00
|
|
|
class PlaceUpdateView(UpdateView):
|
|
|
|
template_name = 'update_place.html'
|
|
|
|
model = Place
|
|
|
|
form_class = PlaceForm
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy('place_detail', kwargs={'pk':self.get_object().pk})
|
|
|
|
|
|
|
|
class PlaceCreateView(View):
|
2020-08-01 19:08:29 +02:00
|
|
|
|
2020-07-29 20:22:04 +02:00
|
|
|
def get(self, request, *args, **kwargs):
|
2020-07-30 13:09:00 +02:00
|
|
|
place_image_form = PlaceImageCreateForm()
|
2020-07-31 00:27:01 +02:00
|
|
|
place_form = PlaceForm()
|
2020-07-30 14:27:17 +02:00
|
|
|
|
2020-07-30 10:51:05 +02:00
|
|
|
context = {
|
2020-07-30 13:09:00 +02:00
|
|
|
'place_form': place_form,
|
|
|
|
'place_image_form': place_image_form
|
2020-07-30 10:51:05 +02:00
|
|
|
}
|
|
|
|
return render(request, 'create_place.html', context)
|
|
|
|
|
2020-07-29 20:22:04 +02:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
place_form = PlaceForm(request.POST)
|
2020-07-30 13:09:00 +02:00
|
|
|
|
2020-07-30 14:15:28 +02:00
|
|
|
if place_form.is_valid():
|
|
|
|
submitter = request.user
|
2020-07-30 14:26:52 +02:00
|
|
|
place = place_form.save(commit=False)
|
2020-07-29 20:22:04 +02:00
|
|
|
# Save logged in user as "submitted_by"
|
2020-07-30 14:26:52 +02:00
|
|
|
place.submitted_by = submitter
|
|
|
|
place.save()
|
2020-07-30 14:15:28 +02:00
|
|
|
|
|
|
|
if request.FILES:
|
2020-07-30 14:26:52 +02:00
|
|
|
self._apply_multipart_image_upload(
|
2020-07-30 22:33:41 +02:00
|
|
|
files=request.FILES.getlist('filename'),
|
|
|
|
place=place,
|
|
|
|
submitter=submitter
|
2020-07-30 14:26:52 +02:00
|
|
|
)
|
2020-07-30 14:15:28 +02:00
|
|
|
|
2020-07-30 10:51:05 +02:00
|
|
|
kwargs_to_pass = {
|
2020-07-30 14:26:52 +02:00
|
|
|
'pk': place.pk
|
2020-07-30 10:51:05 +02:00
|
|
|
}
|
|
|
|
return redirect(reverse_lazy('place_detail', kwargs=kwargs_to_pass))
|
2020-07-29 20:22:04 +02:00
|
|
|
else:
|
2020-07-30 10:51:05 +02:00
|
|
|
context = {
|
|
|
|
'form': form_place
|
|
|
|
}
|
|
|
|
return render(request, 'create_place.html', context)
|
2020-07-30 14:26:52 +02:00
|
|
|
|
2020-08-04 22:10:31 +02:00
|
|
|
def _apply_multipart_image_upload(self, files, place, submitter):
|
2020-07-30 14:27:17 +02:00
|
|
|
place_image = PlaceImage.objects.create(
|
|
|
|
filename=image,
|
|
|
|
place=place,
|
|
|
|
submitted_by=submitter
|
|
|
|
)
|
|
|
|
place_image.save()
|
2020-08-06 16:57:43 +02:00
|
|
|
|
|
|
|
class PlaceDeleteView(View):
|
|
|
|
template_name = 'delete_place.html'
|
|
|
|
model = Place
|
|
|
|
form_class = PlaceDeleteForm
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
place_delete_form = PlaceDeleteForm()
|
|
|
|
|
|
|
|
context = {'place_form': place_delete_form}
|
|
|
|
return render(request, 'create_place.html', context)
|