import secrets from django.views import View from django.views.generic.list import ListView from django.shortcuts import render, redirect from django.urls import reverse from .forms import ImageUploadForm from .models import Image class ImageUploadView(View): def get(self, request): form = ImageUploadForm() return render( request, 'upload_image/upload.html', { 'form': form } ) def post(self, request): form = ImageUploadForm( request.POST, request.FILES ) if form.is_valid(): image = form.save(commit=False) if image.private: image.access_code = secrets.token_hex(32) image.save() return redirect( reverse('home') ) else: form = ImageUploadForm() return render( request, 'upload_image/upload.html', { 'form': form } ) class PublicImageListView(ListView): model = Image paginate_by = 20 context_object_name = 'images' template_name = 'list_images/image_list.html' def get_queryset(self): return Image.objects.all().filter(private=False)