111 lines
3.6 KiB
Python
111 lines
3.6 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
''' Django views. '''
|
|
from django.shortcuts import render, redirect, get_object_or_404
|
|
from django.urls import reverse_lazy
|
|
from django.views.generic.edit import CreateView, UpdateView
|
|
from django.views import View
|
|
from django.http import Http404
|
|
|
|
from .forms import ExplorerCreationForm, PlaceForm, PlaceImageCreateForm
|
|
from .models import Place, PlaceImage, Voucher
|
|
|
|
# Create your views here.
|
|
|
|
'''
|
|
class VoucherVerifyView(View):
|
|
formclass = VoucherVerifyForm
|
|
fields = ['code']
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
voucher_form = VoucherVerifyForm()
|
|
return render(request, 'voucher-verify.html', {'voucher_form': voucher_form})
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
voucher_form = VoucherVerifyForm(request.POST)
|
|
print(voucher_form.values('code'))
|
|
if voucher_form.is_valid():
|
|
# Compare voucher from form with available vouchers in Voucher.
|
|
for voucher in Voucher.objects.all():
|
|
if voucher.code == voucher_form.cleaned_data.get('code'):
|
|
kwargs_to_pass = {
|
|
'voucher_pk': voucher.pk
|
|
}
|
|
return redirect(reverse_lazy('signup'), kwargs=kwargs_to_pass)
|
|
else:
|
|
return redirect(reverse_lazy('enter_voucher'))
|
|
else:
|
|
return redirect(reverse_lazy('enter_voucher'))
|
|
'''
|
|
|
|
class SignUpView(CreateView):
|
|
form_class = ExplorerCreationForm
|
|
success_url = reverse_lazy('login')
|
|
template_name = 'signup.html'
|
|
|
|
def place_list_view(request,):
|
|
return render(request, 'placeList.html', {'place_list':Place.objects.all()})
|
|
|
|
def place_detail_view(request, pk):
|
|
return render(request, 'placeOverview.html', {'place':Place.objects.get(pk=pk)})
|
|
|
|
def hello_world(request):
|
|
return render(request, 'hello_world.html', {'text':'Hello World!'})
|
|
|
|
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):
|
|
def get(self, request, *args, **kwargs):
|
|
place_image_form = PlaceImageCreateForm()
|
|
place_form = PlaceForm()
|
|
|
|
context = {
|
|
'place_form': place_form,
|
|
'place_image_form': place_image_form
|
|
}
|
|
return render(request, 'create_place.html', context)
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
place_form = PlaceForm(request.POST)
|
|
|
|
if place_form.is_valid():
|
|
submitter = request.user
|
|
place = place_form.save(commit=False)
|
|
# Save logged in user as "submitted_by"
|
|
place.submitted_by = submitter
|
|
place.save()
|
|
|
|
if request.FILES:
|
|
self._apply_multipart_image_upload(
|
|
files=request.FILES.getlist('filename'),
|
|
place=place,
|
|
submitter=submitter
|
|
)
|
|
|
|
kwargs_to_pass = {
|
|
'pk': place.pk
|
|
}
|
|
return redirect(reverse_lazy('place_detail', kwargs=kwargs_to_pass))
|
|
else:
|
|
context = {
|
|
'form': form_place
|
|
}
|
|
return render(request, 'create_place.html', context)
|
|
|
|
def _apply_multipart_image_upload(self, files, place, submitter):
|
|
print(files)
|
|
for image in files:
|
|
place_image = PlaceImage.objects.create(
|
|
filename=image,
|
|
place=place,
|
|
submitted_by=submitter
|
|
)
|
|
place_image.save()
|