imagehost/imagehosting/views.py

60 lines
2.2 KiB
Python
Raw Normal View History

2017-12-12 13:15:45 +03:00
# -*- coding: utf-8 -*-
from django.shortcuts import render, get_object_or_404, HttpResponseRedirect
from .models import Post
from .forms import PostForm, DeleteForm
from django.shortcuts import redirect
from ih import files, images
from django.contrib.auth.decorators import login_required
from ih import images
from django.views.decorators.csrf import requires_csrf_token
def create_thumb_from_file(filename):
orig_file = 'imagehost/media/images/' + filename
# Create thumbnail
thumb_name = images.resize_image(orig_file, {
'name': filename,
'size': [300, 300],
'dest': 'imagehost/media/images'
}
)
return thumb_name
@login_required(login_url="/login")
def image_new(request):
if request.method == "POST":
form = PostForm(request.POST, request.FILES)
if form.is_valid():
post = form.save(commit=False)
post.save()
create_thumb_from_file(post.orig_name)
return redirect('imagehosting.views.image_detail', pk=post.id)
else:
form = PostForm()
return render(request, 'imagehosting/index.html', {'form': form})
def image_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'imagehosting/image.html', {'post': post})
def all_posts(request):
all_posts = Post.objects.order_by('-pk')
return render(request, 'imagehosting/all.html', {'all': all_posts})
@login_required(login_url="/login")
def delete_post(request, pk):
post_to_delete = get_object_or_404(Post, pk=pk)
if request.method == 'GET':
form = DeleteForm(request.POST, instance=post_to_delete)
if form.is_valid(): # checks CSRF
if not request.user.is_authenticated():
return HttpResponseRedirect('/admin')
post_to_delete.delete()
return redirect('imagehosting.views.all_posts')
return render(request, 'imagehosting/delete.html', {'form': form})