99 lines
3.4 KiB
Python
99 lines
3.4 KiB
Python
# -*- 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.views.decorators.csrf import requires_csrf_token
|
|
from django.contrib.auth.decorators import login_required
|
|
from ih import images
|
|
from django.views.decorators.csrf import requires_csrf_token
|
|
|
|
'''def create_thumbnail(request):
|
|
# Get file from <InMemoryUploadedFile>
|
|
file_l, filename = files.get_file_from_request(request, 'file')
|
|
fh = files.write_to_shm(file_l, filename)
|
|
file_l = None
|
|
|
|
# Create thumbnail
|
|
thumb_name = images.resize_image(fh, {
|
|
'name': filename,
|
|
'size': (100, 100),
|
|
'dest': 'mysite/media/images'
|
|
}
|
|
|
|
)
|
|
print(thumb_name)
|
|
# Remove file from shm
|
|
e = files.rm_from_shm(filename)
|
|
if e is not True:
|
|
print "WARNING: " + e
|
|
|
|
return thumb_name'''
|
|
|
|
|
|
def create_thumb_from_file(filename):
|
|
orig_file = 'imagehost/media/images/' + filename
|
|
# f = str(filename)
|
|
# print(filename)
|
|
# print f
|
|
# Create thumbnail
|
|
print("DEBUG: Resizing image " + orig_file)
|
|
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():
|
|
# thumb_name = create_thumbnail(request)
|
|
post = form.save(commit=False)
|
|
post.save()
|
|
# print("FILE: ", post.file)
|
|
print(post.orig_name)
|
|
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)
|
|
print(post)
|
|
return render(request, 'imagehosting/image.html', {'post': post})
|
|
|
|
|
|
def all_posts(request):
|
|
all = Post.objects.order_by('-pk')
|
|
# page = request.POST.get("page")
|
|
return render(request, 'imagehosting/all.html', {'all': all})
|
|
|
|
|
|
def carousel(request):
|
|
return render(request, 'imagehosting/carousel.html', {'all': Post.objects.all()})
|
|
|
|
#@requires_csrf_token
|
|
@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})
|
|
return render(request, 'imagehosting/delete.html', {'form': form})
|