from django.views import View from django.views.generic.edit import CreateView from django.contrib.messages.views import SuccessMessageMixin from django.contrib import messages from django.urls import reverse_lazy from django.shortcuts import render, redirect from lostplaces_app.forms import ExplorerCreationForm from lostplaces_app.models import Place, PhotoAlbum from lostplaces_app.views import IsAuthenticated from lostplaces_app.views.base_views import ( PlaceAssetCreateView, PlaceAssetDeleteView ) class SignUpView(SuccessMessageMixin, CreateView): form_class = ExplorerCreationForm success_url = reverse_lazy('login') template_name = 'signup.html' success_message = 'User created.' class HomeView(IsAuthenticated, View): def get(self, request, *args, **kwargs): place_list = Place.objects.all().order_by('-submitted_when')[:10] place_map_center = Place.average_latlon(place_list) context = { 'place_list': place_list, 'place_map_center': place_map_center } return render(request, 'home.html', context) def handle_no_permission(self): place_list = Place.objects.all().order_by('-submitted_when')[:10] context = { 'place_list': place_list } return render(self.request, 'home_unauth.html', context) class PhotoAlbumCreateView(PlaceAssetCreateView): model = PhotoAlbum fields = ['url', 'label'] template_name = 'photo_album/photo_album_create.html' success_message = 'Photo Album submitted' class PhotoAlbumDeleteView(PlaceAssetDeleteView): model = PhotoAlbum pk_url_kwarg = 'pk' success_message = 'Photo Album deleted' permission_denied_messsage = 'You do not have permissions to alter this photo album'