def index(request): cat_list = [] if request.method == "POST": form = ImageForm(data=request.POST, files=request.FILES) if form.is_valid(): form.save() obj = form.instance return render(request, "index.html", {"obj": obj}) else: form = ImageForm() all_img = ImageModel.objects.all() print(request.GET.get('category')) img = ImageModel.objects.filter( category=request.GET.get('category')).order_by('date').reverse() for i in all_img: if i is not cat_list: cat_list.append(i.category) # print(cat_list) cat_list = set(cat_list) if request.GET.get('category') is None or request.GET.get( 'category') == "all": return render(request, "index.html", { "all_img": all_img, "form": form, "cat_list": cat_list }) else: return render(request, "index.html", { "img": img, "form": form, "cat_list": cat_list })
def image_edit(request, image_id): image = Image.objects.get(pk=image_id) old_path = image.image if image.user.id == request.user.id: form = ImageForm(request.POST or None, request.FILES or None, instance=image) if request.method == 'POST': data = {'result': False} if form.is_valid(): img = form.save() History.objects.create( image=img, old_path=old_path, ) user_email = request.user.email msg = '{};{};{};'.format(user_email, img.date_time, img.image) send_mail( 'Изображение было изменено', msg, '*****@*****.**', [user_email], fail_silently=False, ) data['result'] = True else: data['errors'] = form.errors return JsonResponse(data) else: ctx = { 'form': form, } return render(request, 'gallery/form.html', ctx) else: raise Http404
def edit_image(request, image_id): image = get_object_or_404(Image, id=image_id) form = ImageForm(instance=image) if request.user != image.user: messages.add_message( request, messages.ERROR, '<div class="error">You may only edit images that belong to ' "you!</div>" ) return render(request, "permission_denied.html", {}) if request.method == "POST": form = ImageForm(request.POST, request.FILES, instance=image) if form.is_valid(): form.save() si = StreamItem( action_type="U", user=request.user, content_type=ContentType.objects.get_for_model(Image), object_id=image_id, ) si.save() messages.add_message(request, messages.SUCCESS, '<div class="success">Image updated</div>') return HttpResponseRedirect(image.get_absolute_url()) return render(request, "gallery/image/edit.html", {"form": form})
def edit_image(request, image_id): image = get_object_or_404(Image, id=image_id) form = ImageForm(instance=image) if (request.user != image.user): messages.add_message( request, messages.ERROR, '<div class="error">You may only edit images that belong to ' 'you!</div>') return render(request, 'permission_denied.html', {}) if (request.method == "POST"): form = ImageForm(request.POST, request.FILES, instance=image) if (form.is_valid()): form.save() si = StreamItem( action_type='U', user=request.user, content_type=ContentType.objects.get_for_model(Image), object_id=image_id) si.save() messages.add_message(request, messages.SUCCESS, '<div class="success">Image updated</div>') return HttpResponseRedirect(image.get_absolute_url()) return render(request, 'gallery/image/edit.html', {'form': form})
def list(request): # handle file upload form = ImageForm(request.POST or None, request.FILES or None) if form.is_valid(): form.save() return redirect('list') # load images for the list page images = Image.objects.all() # paging paginator = Paginator(images, 8) page = request.GET.get('page') try: image_list = paginator.page(page) except InvalidPage: image_list = paginator.page(1) # render list page with the images and the form return render(request, 'gallery/list.html', { 'image_list': image_list, 'form': form, })
def image_add(request): album_lst = Album.objects.all() if request.method == 'POST': form = ImageForm(request.POST, request.FILES, ) if form.is_valid(): image_instance = form.save() create_thumbnail(image_instance) image_instance.save() return redirect('gallery.views.album_detail', image_instance.album.slug) else: album_slug = request.GET.get('album_slug', '__') if album_slug == '__': print("Classic form") form = ImageForm() else: album = Album.objects.get(slug=album_slug) # print("album : %s // album slug : %s" % (album_slug, album_slug)) form = ImageForm(initial={'album': album}) return render(request, 'gallery/image_add.html', locals())
def create_image(request): form = ImageForm() if (request.method == "POST"): form = ImageForm(request.POST, request.FILES) if (form.is_valid()): image = form.save(commit=False) image.user = request.user image.save() form.save_m2m() si = StreamItem( action_type='C', user=request.user, content_type=ContentType.objects.get_for_model(Image), object_id=image.id) si.save() messages.add_message( request, messages.SUCCESS, '<div class="success">Image created - <a ' 'href="/image/create/">Add another!</a></div>') return HttpResponseRedirect(image.get_absolute_url()) return render(request, 'gallery/image/edit.html', {'form': form})
def create_image(request): form = ImageForm() if request.method == "POST": form = ImageForm(request.POST, request.FILES) if form.is_valid(): image = form.save(commit=False) image.user = request.user image.save() form.save_m2m() si = StreamItem( action_type="C", user=request.user, content_type=ContentType.objects.get_for_model(Image), object_id=image.id, ) si.save() messages.add_message( request, messages.SUCCESS, '<div class="success">Image created - <a ' 'href="/image/create/">Add another!</a></div>', ) return HttpResponseRedirect(image.get_absolute_url()) return render(request, "gallery/image/edit.html", {"form": form})