upd
This commit is contained in:
commit
1af80897ac
10
docker/Dockerfile
Normal file
10
docker/Dockerfile
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
FROM alpine
|
||||||
|
|
||||||
|
RUN apk update && apk add python py2-pip py-gunicorn py-pillow py2-futures
|
||||||
|
RUN pip install django==1.11.6
|
||||||
|
|
||||||
|
COPY . /app
|
||||||
|
|
||||||
|
RUN cd /app && python manage.py migrate
|
||||||
|
|
||||||
|
ENTRYPOINT cd /app && gunicorn --threads 2 imagehost.wsgi:application --bind 0.0.0.0:8080 -t 60 --max-requests 1000
|
|
@ -2,10 +2,10 @@ from PIL import Image
|
||||||
|
|
||||||
def resize_image(fh, img_prop):
|
def resize_image(fh, img_prop):
|
||||||
im = Image.open(fh)
|
im = Image.open(fh)
|
||||||
# size = 100, 100
|
# size = 100, 100
|
||||||
im.thumbnail(img_prop['size'])
|
im.thumbnail(img_prop['size'])
|
||||||
print img_prop['size']
|
print(img_prop['size'])
|
||||||
# im.thumbnail(size)
|
# im.thumbnail(size)
|
||||||
thumb_name = img_prop['dest'] + '/thumb_' + img_prop['name']
|
thumb_name = img_prop['dest'] + '/thumb_' + img_prop['name']
|
||||||
im.save(thumb_name)
|
im.save(thumb_name)
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,6 @@ import os
|
||||||
|
|
||||||
from django.core.wsgi import get_wsgi_application
|
from django.core.wsgi import get_wsgi_application
|
||||||
|
|
||||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "imagehost.settings")
|
||||||
|
|
||||||
application = get_wsgi_application()
|
application = get_wsgi_application()
|
||||||
|
|
|
@ -2,6 +2,6 @@ from django.contrib import admin
|
||||||
|
|
||||||
from .models import Post
|
from .models import Post
|
||||||
|
|
||||||
admin.site.register(Post
|
admin.site.register(Post)
|
||||||
)
|
|
||||||
# Register your models here.
|
# Register your models here.
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from .models import Post
|
from .models import Post
|
||||||
|
|
||||||
|
|
||||||
class PostForm(forms.ModelForm):
|
class PostForm(forms.ModelForm):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Post
|
model = Post
|
||||||
fields = ('name', 'file')
|
fields = ('name', 'file')
|
||||||
|
|
||||||
|
|
||||||
class DeleteForm(forms.ModelForm):
|
class DeleteForm(forms.ModelForm):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
|
@ -2,13 +2,14 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
#from ih import images
|
# from ih import images
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
class Post(models.Model):
|
class Post(models.Model):
|
||||||
name = models.CharField(max_length=30, blank=True)
|
name = models.CharField(max_length=30, blank=True)
|
||||||
file = models.FileField(upload_to='images')
|
file = models.FileField(upload_to='images')
|
||||||
# thumb = models.CharField(max_length=32, null=True, blank=True)
|
# thumb = models.CharField(max_length=32, null=True, blank=True)
|
||||||
|
|
||||||
def publish(self):
|
def publish(self):
|
||||||
self.published_date = timezone.now()
|
self.published_date = timezone.now()
|
||||||
|
@ -17,24 +18,23 @@ class Post(models.Model):
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
def thumb_name(self):
|
|
||||||
|
|
||||||
x = os.path.split(self.file.name)[-1]
|
def thumb_name(self):
|
||||||
return '/thumb_' + x
|
x = os.path.split(self.file.name)[-1]
|
||||||
|
return '/thumb_' + x
|
||||||
|
|
||||||
thumb_name = property(thumb_name)
|
thumb_name = property(thumb_name)
|
||||||
|
|
||||||
def orig_name(self):
|
def orig_name(self):
|
||||||
|
|
||||||
x = os.path.split(self.file.name)[-1]
|
x = os.path.split(self.file.name)[-1]
|
||||||
return x
|
return x
|
||||||
|
|
||||||
orig_name = property(orig_name)
|
orig_name = property(orig_name)
|
||||||
|
|
||||||
'''def resize_image(self.file):
|
'''def resize_image(self.file):
|
||||||
path, file_long = os.path.split(self.file)
|
path, file_long = os.path.split(self.file)
|
||||||
|
|
||||||
im=image.open(file_long)
|
im=image.open(file_long)
|
||||||
size = 128, 128
|
size = 128, 128
|
||||||
im.thumbnail(size)
|
im.thumbnail(size)
|
||||||
|
|
|
@ -7,6 +7,8 @@ from django.shortcuts import redirect
|
||||||
from ih import files, images
|
from ih import files, images
|
||||||
#from django.views.decorators.csrf import requires_csrf_token
|
#from django.views.decorators.csrf import requires_csrf_token
|
||||||
from django.contrib.auth.decorators import login_required
|
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):
|
'''def create_thumbnail(request):
|
||||||
# Get file from <InMemoryUploadedFile>
|
# Get file from <InMemoryUploadedFile>
|
||||||
|
@ -32,12 +34,12 @@ from django.contrib.auth.decorators import login_required
|
||||||
|
|
||||||
|
|
||||||
def create_thumb_from_file(filename):
|
def create_thumb_from_file(filename):
|
||||||
orig_file = 'imagehost/media/images/' + filename
|
orig_file = 'imagehost/media/images/' + filename
|
||||||
#f = str(filename)
|
# f = str(filename)
|
||||||
#print(filename)
|
# print(filename)
|
||||||
#print f
|
# print f
|
||||||
# Create thumbnail
|
# Create thumbnail
|
||||||
print("DEBUG: Resizing image " + orig_file)
|
print("DEBUG: Resizing image " + orig_file)
|
||||||
thumb_name = images.resize_image(orig_file, {
|
thumb_name = images.resize_image(orig_file, {
|
||||||
'name': filename,
|
'name': filename,
|
||||||
'size': [300, 300],
|
'size': [300, 300],
|
||||||
|
@ -45,21 +47,20 @@ def create_thumb_from_file(filename):
|
||||||
}
|
}
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return thumb_name
|
|
||||||
|
|
||||||
|
return thumb_name
|
||||||
|
|
||||||
@login_required(login_url="/login")
|
@login_required(login_url="/login")
|
||||||
def image_new(request):
|
def image_new(request):
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
form = PostForm(request.POST, request.FILES)
|
form = PostForm(request.POST, request.FILES)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
# thumb_name = create_thumbnail(request)
|
# thumb_name = create_thumbnail(request)
|
||||||
post = form.save(commit=False)
|
post = form.save(commit=False)
|
||||||
post.save()
|
post.save()
|
||||||
#print("FILE: ", post.file)
|
# print("FILE: ", post.file)
|
||||||
print post.orig_name
|
print(post.orig_name)
|
||||||
create_thumb_from_file(post.orig_name)
|
create_thumb_from_file(post.orig_name)
|
||||||
return redirect('imagehosting.views.image_detail', pk=post.id)
|
return redirect('imagehosting.views.image_detail', pk=post.id)
|
||||||
else:
|
else:
|
||||||
form = PostForm()
|
form = PostForm()
|
||||||
|
@ -74,9 +75,10 @@ def image_detail(request, pk):
|
||||||
|
|
||||||
def all_posts(request):
|
def all_posts(request):
|
||||||
all = Post.objects.order_by('-pk')
|
all = Post.objects.order_by('-pk')
|
||||||
page = request.POST.get("page")
|
# page = request.POST.get("page")
|
||||||
return render(request, 'imagehosting/all.html', {'all': all})
|
return render(request, 'imagehosting/all.html', {'all': all})
|
||||||
|
|
||||||
|
|
||||||
def carousel(request):
|
def carousel(request):
|
||||||
return render(request, 'imagehosting/carousel.html', {'all': Post.objects.all()})
|
return render(request, 'imagehosting/carousel.html', {'all': Post.objects.all()})
|
||||||
|
|
||||||
|
@ -86,10 +88,11 @@ def delete_post(request, pk):
|
||||||
post_to_delete = get_object_or_404(Post, pk=pk)
|
post_to_delete = get_object_or_404(Post, pk=pk)
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
form = DeleteForm(request.POST, instance=post_to_delete)
|
form = DeleteForm(request.POST, instance=post_to_delete)
|
||||||
if form.is_valid(): # checks CSRF
|
if form.is_valid(): # checks CSRF
|
||||||
if not request.user.is_authenticated():
|
if not request.user.is_authenticated():
|
||||||
return HttpResponseRedirect('/admin')
|
return HttpResponseRedirect('/admin')
|
||||||
post_to_delete.delete()
|
post_to_delete.delete()
|
||||||
return redirect('imagehosting.views.all_posts')
|
return redirect('imagehosting.views.all_posts')
|
||||||
|
|
||||||
#return render(request, 'imagehosting/delete.html', {'form': form})
|
#return render(request, 'imagehosting/delete.html', {'form': form})
|
||||||
|
return render(request, 'imagehosting/delete.html', {'form': form})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user