From de60ca084af6a7244935f50d01edaebc890c8d99 Mon Sep 17 00:00:00 2001 From: Denis Zheleztsov Date: Tue, 10 Oct 2017 14:19:27 +0300 Subject: [PATCH 1/3] PEP8 --- imagehosting/admin.py | 4 ++-- imagehosting/forms.py | 5 ++++- imagehosting/models.py | 18 +++++++++--------- imagehosting/urls.py | 1 + imagehosting/views.py | 33 +++++++++++++++++---------------- 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/imagehosting/admin.py b/imagehosting/admin.py index 2d0670b..f884720 100644 --- a/imagehosting/admin.py +++ b/imagehosting/admin.py @@ -2,6 +2,6 @@ from django.contrib import admin from .models import Post -admin.site.register(Post -) +admin.site.register(Post) + # Register your models here. diff --git a/imagehosting/forms.py b/imagehosting/forms.py index 34b26a1..1172811 100644 --- a/imagehosting/forms.py +++ b/imagehosting/forms.py @@ -1,13 +1,16 @@ -# -*- coding: utf-8 -*- +# -*- coding: utf-8 -*- from django import forms from .models import Post + class PostForm(forms.ModelForm): class Meta: model = Post fields = ('name', 'file') + + class DeleteForm(forms.ModelForm): class Meta: diff --git a/imagehosting/models.py b/imagehosting/models.py index 707a96e..8da68c2 100644 --- a/imagehosting/models.py +++ b/imagehosting/models.py @@ -2,13 +2,14 @@ from __future__ import unicode_literals from django.db import models from django.utils import timezone -#from ih import images +# from ih import images import os + class Post(models.Model): name = models.CharField(max_length=30, blank=True) 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): self.published_date = timezone.now() @@ -17,24 +18,23 @@ class Post(models.Model): def __unicode__(self): return self.name - - def thumb_name(self): - x = os.path.split(self.file.name)[-1] - return '/thumb_' + x + def thumb_name(self): + x = os.path.split(self.file.name)[-1] + return '/thumb_' + x thumb_name = property(thumb_name) - + def orig_name(self): - x = os.path.split(self.file.name)[-1] + x = os.path.split(self.file.name)[-1] return x orig_name = property(orig_name) '''def resize_image(self.file): path, file_long = os.path.split(self.file) - + im=image.open(file_long) size = 128, 128 im.thumbnail(size) diff --git a/imagehosting/urls.py b/imagehosting/urls.py index d13043a..2c617af 100644 --- a/imagehosting/urls.py +++ b/imagehosting/urls.py @@ -4,6 +4,7 @@ from django.conf.urls import url from . import views from django.conf import settings from django.conf.urls.static import static + urlpatterns = [ url(r'^$', views.image_new, name='image_new'), url(r'^post/(?P[0-9]+)/$', views.image_detail, name='image_detail'), diff --git a/imagehosting/views.py b/imagehosting/views.py index bd13986..11d1291 100644 --- a/imagehosting/views.py +++ b/imagehosting/views.py @@ -4,7 +4,7 @@ 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 ih import images from django.views.decorators.csrf import requires_csrf_token '''def create_thumbnail(request): @@ -31,12 +31,12 @@ from django.views.decorators.csrf import requires_csrf_token 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) + 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], @@ -44,21 +44,20 @@ def create_thumb_from_file(filename): } ) - - return thumb_name + return thumb_name def image_new(request): if request.method == "POST": form = PostForm(request.POST, request.FILES) if form.is_valid(): - # thumb_name = create_thumbnail(request) + # 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) + # 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() @@ -73,18 +72,20 @@ def image_detail(request, pk): def all_posts(request): all = Post.objects.order_by('-pk') - page = request.POST.get("page") + # 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 def delete_post(request, pk): post_to_delete = get_object_or_404(Post, pk=pk) if request.method == 'POST': 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(): return HttpResponseRedirect('/admin') post_to_delete.delete() @@ -92,4 +93,4 @@ def delete_post(request, pk): else: form = DeleteForm(instance=post_to_delete) - return render(request, 'imagehosting/delete.html', {'form': form}) + return render(request, 'imagehosting/delete.html', {'form': form}) From 5d4d9d3e3398d8c24f08947f7b29ec5b56f15615 Mon Sep 17 00:00:00 2001 From: Denis Zheleztsov Date: Tue, 10 Oct 2017 14:49:33 +0300 Subject: [PATCH 2/3] Change module name and Dockerfile --- ih/__init__.py | 0 ih/images.py | 13 +++++++++++++ imagehost/settings.py | 2 +- imagehost/wsgi.py | 2 +- manage.py | 2 +- 5 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 ih/__init__.py create mode 100644 ih/images.py diff --git a/ih/__init__.py b/ih/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ih/images.py b/ih/images.py new file mode 100644 index 0000000..70b9a37 --- /dev/null +++ b/ih/images.py @@ -0,0 +1,13 @@ +from PIL import Image + + +def resize_image(fh, img_prop): + im = Image.open(fh) + # size = 100, 100 + im.thumbnail(img_prop['size']) + print(img_prop['size']) + # im.thumbnail(size) + thumb_name = img_prop['dest'] + '/thumb_' + img_prop['name'] + im.save(thumb_name) + + return thumb_name diff --git a/imagehost/settings.py b/imagehost/settings.py index cdd8603..e535b85 100644 --- a/imagehost/settings.py +++ b/imagehost/settings.py @@ -71,7 +71,7 @@ TEMPLATES = [ }, ] -WSGI_APPLICATION = 'mysite.wsgi.application' +WSGI_APPLICATION = 'imagehost.wsgi.application' # Database diff --git a/imagehost/wsgi.py b/imagehost/wsgi.py index f47ba04..62bb1ed 100644 --- a/imagehost/wsgi.py +++ b/imagehost/wsgi.py @@ -12,6 +12,6 @@ import os 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() diff --git a/manage.py b/manage.py index 8a50ec0..dc5541d 100644 --- a/manage.py +++ b/manage.py @@ -3,7 +3,7 @@ import os import sys if __name__ == "__main__": - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "imagehost.settings") from django.core.management import execute_from_command_line From 753a470f0d1e7aede37eee48ad8fe014fbe892c4 Mon Sep 17 00:00:00 2001 From: Denis Zheleztsov Date: Tue, 10 Oct 2017 17:57:15 +0300 Subject: [PATCH 3/3] Dockerfile --- docker/Dockerfile | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 docker/Dockerfile diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..7fd08bb --- /dev/null +++ b/docker/Dockerfile @@ -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