From 596d9e03583d2bd8656cd043c4b17796a5a1d85b Mon Sep 17 00:00:00 2001 From: Date: Tue, 12 Dec 2017 13:15:45 +0300 Subject: [PATCH] heroku --- Procfile | 1 + ih/files.py | 66 ++-- ih/images.py | 24 +- imagehost/settings.py | 289 +++++++++--------- imagehost/urls.py | 56 ++-- imagehost/wsgi.py | 34 +-- imagehosting/admin.py | 14 +- imagehosting/apps.py | 14 +- imagehosting/forms.py | 36 +-- imagehosting/models.py | 54 ++-- imagehosting/templates/imagehosting/all.html | 70 ++--- imagehosting/templates/imagehosting/base.html | 228 +++++++------- .../templates/imagehosting/image.html | 30 +- .../templates/imagehosting/index.html | 20 +- .../templates/imagehosting/login.html | 22 +- imagehosting/tests.py | 6 +- imagehosting/urls.py | 44 +-- imagehosting/views.py | 118 +++---- requirements.txt | 4 + 19 files changed, 573 insertions(+), 557 deletions(-) create mode 100644 Procfile create mode 100644 requirements.txt diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..a72b87f --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: gunicorn mysite.wsgi --log-file - diff --git a/ih/files.py b/ih/files.py index 3ef42c3..b1bc257 100644 --- a/ih/files.py +++ b/ih/files.py @@ -1,33 +1,33 @@ -import os - - -# Return file string and filename -def get_file_from_request(request, fieldname): - file_l = '' - for i in request.FILES[fieldname]: - file_l = file_l + i - - return file_l, str(request.FILES[fieldname]) - - -# Write file to /dev/shm and return ar handler -def write_to_shm(file, name): - f = open('/dev/shm/' + name, 'w') - f.write(file) - f.close() - - return '/dev/shm/' + name - - -# Remove file from /dev/shm -def rm_from_shm(name): - try: - os.remove('/dev/shm/' + name) - return True - except Exception as e: - return str(e) - - -# Split filename -def split_file(filename): - pass +import os + + +# Return file string and filename +def get_file_from_request(request, fieldname): + file_l = '' + for i in request.FILES[fieldname]: + file_l = file_l + i + + return file_l, str(request.FILES[fieldname]) + + +# Write file to /dev/shm and return ar handler +def write_to_shm(file, name): + f = open('/dev/shm/' + name, 'w') + f.write(file) + f.close() + + return '/dev/shm/' + name + + +# Remove file from /dev/shm +def rm_from_shm(name): + try: + os.remove('/dev/shm/' + name) + return True + except Exception as e: + return str(e) + + +# Split filename +def split_file(filename): + pass diff --git a/ih/images.py b/ih/images.py index cdfd1e7..c6877dd 100644 --- a/ih/images.py +++ b/ih/images.py @@ -1,12 +1,12 @@ -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 +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 4509c83..f570009 100644 --- a/imagehost/settings.py +++ b/imagehost/settings.py @@ -1,139 +1,150 @@ -# -*- coding: utf-8 -*- - -""" -Django settings for mysite project. - -Generated by 'django-admin startproject' using Django 1.9.7. - -For more information on this file, see -https://docs.djangoproject.com/en/1.9/topics/settings/ - -For the full list of settings and their values, see -https://docs.djangoproject.com/en/1.9/ref/settings/ -""" - -import os - -# Build paths inside the project like this: os.path.join(BASE_DIR, ...) -BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - - -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/ - -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = '1a+3jmf#g!q_gj66$^v9_&8uoev=%k49=j(%lp%!kvv$6x3bn^' - -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True - -ALLOWED_HOSTS = [] - - -# Application definition - -INSTALLED_APPS = [ - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', - 'imagehosting', - 'taggit' -] - -MIDDLEWARE_CLASSES = [ - 'django.middleware.security.SecurityMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', -] - -ROOT_URLCONF = 'imagehosting.urls' - -TEMPLATES = [ - { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', - ], - }, - }, -] - -WSGI_APPLICATION = 'imagehost.wsgi.application' - - -# Database -# https://docs.djangoproject.com/en/1.9/ref/settings/#databases - -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, '../db.sqlite3'), - } -} - - -# Password validation -# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators - -AUTH_PASSWORD_VALIDATORS = [ - { - 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', - }, -] - - -# Internationalization -# https://docs.djangoproject.com/en/1.9/topics/i18n/ - -LANGUAGE_CODE = 'ru-RU' -USE_I18N = True - -TIME_ZONE = 'Europe/Moscow' - - -USE_I18N = True - -USE_L10N = True - -USE_TZ = True - - -# Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/1.9/howto/static-files/ - -PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) -STATIC_URL = '/static/' -STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static') - -ADMIN_TOOLS_MENU = 'myproject.menu.CustomMenu' -ADMIN_TOOLS_INDEX_DASHBOARD = 'myproject.dashboard.CustomIndexDashboard' -ADMIN_TOOLS_APP_INDEX_DASHBOARD = 'myproject.dashboard.CustomAppIndexDashboard' - -MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media') -MEDIA_URL = '/media/' - -LOGIN_REDIRECT_URL = '/' -LOGOUT_REDIRECT_URL = '/' +# -*- coding: utf-8 -*- + +""" +Django settings for mysite project. + +Generated by 'django-admin startproject' using Django 1.9.7. + +For more information on this file, see +https://docs.djangoproject.com/en/1.9/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/1.9/ref/settings/ +""" + +import os + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = '1a+3jmf#g!q_gj66$^v9_&8uoev=%k49=j(%lp%!kvv$6x3bn^' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = ["imgcollection.herokuapp.com",] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'imagehosting', + #'taggit' +] + +MIDDLEWARE_CLASSES = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'imagehosting.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'imagehost.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/1.9/ref/settings/#databases +''' +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, '../db.sqlite3'), + } +} +''' + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': 'ddg4djbee66kal', + 'USER': 'jrowflkieuofex', + 'PASSWORD': 'ec999abac6adefb9876d38458e3aad38c009ebeb78be01783c5a48268e10f04a', + 'HOST': 'ec2-54-247-82-87.eu-west-1.compute.amazonaws.com', + 'PORT': '5432', + } +} + +# Password validation +# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/1.9/topics/i18n/ + +LANGUAGE_CODE = 'ru-RU' +USE_I18N = True + +TIME_ZONE = 'Europe/Moscow' + + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/1.9/howto/static-files/ + +PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) +STATIC_URL = '/static/' +STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static') + +ADMIN_TOOLS_MENU = 'myproject.menu.CustomMenu' +ADMIN_TOOLS_INDEX_DASHBOARD = 'myproject.dashboard.CustomIndexDashboard' +ADMIN_TOOLS_APP_INDEX_DASHBOARD = 'myproject.dashboard.CustomAppIndexDashboard' + +MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media') +MEDIA_URL = '/media/' + +LOGIN_REDIRECT_URL = '/' +LOGOUT_REDIRECT_URL = '/' diff --git a/imagehost/urls.py b/imagehost/urls.py index 0257aa1..8827858 100644 --- a/imagehost/urls.py +++ b/imagehost/urls.py @@ -1,28 +1,28 @@ -"""mysite URL Configuration - -The `urlpatterns` list routes URLs to views. For more information please see: - https://docs.djangoproject.com/en/1.9/topics/http/urls/ -Examples: -Function views - 1. Add an import: from my_app import views - 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') -Class-based views - 1. Add an import: from other_app.views import Home - 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') -Including another URLconf - 1. Import the include() function: from django.conf.urls import url, include - 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) -""" -from django.conf.urls import url, include, patterns -from django.contrib import admin -from django.conf import settings -from django.conf.urls.static import static -from django.views.generic.base import TemplateView -urlpatterns = patterns('', - url(r'^admin/', admin.site.urls), - url(r'^$', TemplateView.as_view(template_name='main.html')), - url(r'^blog/', include('blog.urls')), - url(r'^polls/', include('polls.urls', namespace="polls")), - url(r'^imagehost/', include('imagehost.urls')), - url(r'^pfm/', include('pfm.urls')), -) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) +"""mysite URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/1.9/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.conf.urls import url, include + 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) +""" +from django.conf.urls import url, include +from django.contrib import admin +from django.conf import settings +from django.conf.urls.static import static +from django.views.generic.base import TemplateView +urlpatterns = [ + url(r'^admin/', admin.site.urls), + url(r'^$', TemplateView.as_view(template_name='main.html')), + url(r'^blog/', include('blog.urls')), + url(r'^polls/', include('polls.urls', namespace="polls")), + url(r'^imagehost/', include('imagehost.urls')), + url(r'^pfm/', include('pfm.urls')), +] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/imagehost/wsgi.py b/imagehost/wsgi.py index 62bb1ed..432b2be 100644 --- a/imagehost/wsgi.py +++ b/imagehost/wsgi.py @@ -1,17 +1,17 @@ -# -*- coding: utf-8 -*- -""" -WSGI config for mysite project. - -It exposes the WSGI callable as a module-level variable named ``application``. - -For more information on this file, see -https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/ -""" - -import os - -from django.core.wsgi import get_wsgi_application - -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "imagehost.settings") - -application = get_wsgi_application() +# -*- coding: utf-8 -*- +""" +WSGI config for mysite project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "imagehost.settings") + +application = get_wsgi_application() diff --git a/imagehosting/admin.py b/imagehosting/admin.py index f884720..94778ff 100644 --- a/imagehosting/admin.py +++ b/imagehosting/admin.py @@ -1,7 +1,7 @@ -from django.contrib import admin - -from .models import Post - -admin.site.register(Post) - -# Register your models here. +from django.contrib import admin + +from .models import Post + +admin.site.register(Post) + +# Register your models here. diff --git a/imagehosting/apps.py b/imagehosting/apps.py index 9a28b40..1f07c27 100644 --- a/imagehosting/apps.py +++ b/imagehosting/apps.py @@ -1,7 +1,7 @@ -from __future__ import unicode_literals - -from django.apps import AppConfig - - -class ImagehostConfig(AppConfig): - name = 'imagehost' +from __future__ import unicode_literals + +from django.apps import AppConfig + + +class ImagehostConfig(AppConfig): + name = 'imagehost' diff --git a/imagehosting/forms.py b/imagehosting/forms.py index 1172811..66310f5 100644 --- a/imagehosting/forms.py +++ b/imagehosting/forms.py @@ -1,18 +1,18 @@ -# -*- 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: - model = Post - fields = [] +# -*- 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: + model = Post + fields = [] diff --git a/imagehosting/models.py b/imagehosting/models.py index 382bb56..7573700 100644 --- a/imagehosting/models.py +++ b/imagehosting/models.py @@ -1,28 +1,28 @@ -# -*- coding: utf-8 -*- - -from __future__ import unicode_literals -from django.db import models -from django.utils import timezone -import os - - -class Post(models.Model): - name = models.CharField(max_length=30, blank=True) - file = models.FileField(upload_to='images') - thumb_name = property(thumb_name) - orig_name = property(orig_name) - - def publish(self): - self.published_date = timezone.now() - self.save() - - def __unicode__(self): - return self.name - - def thumb_name(self): - x = os.path.split(self.file.name)[-1] - return '/thumb_' + x - - def orig_name(self): - x = os.path.split(self.file.name)[-1] +# -*- coding: utf-8 -*- + +from __future__ import unicode_literals +from django.db import models +from django.utils import timezone +import os + + +class Post(models.Model): + name = models.CharField(max_length=30, blank=True) + file = models.FileField(upload_to='images') + thumb_name = property(thumb_name) + orig_name = property(orig_name) + + def publish(self): + self.published_date = timezone.now() + self.save() + + def __unicode__(self): + return self.name + + def thumb_name(self): + x = os.path.split(self.file.name)[-1] + return '/thumb_' + x + + def orig_name(self): + x = os.path.split(self.file.name)[-1] return x \ No newline at end of file diff --git a/imagehosting/templates/imagehosting/all.html b/imagehosting/templates/imagehosting/all.html index 53596bb..6b83c19 100644 --- a/imagehosting/templates/imagehosting/all.html +++ b/imagehosting/templates/imagehosting/all.html @@ -1,35 +1,35 @@ -{% extends 'imagehosting/base.html' %} - -{% load static %} - -{% block content%} -
- {% for image in all %}{% endfor %} -
- - - -{% endblock content %} - - +{% extends 'imagehosting/base.html' %} + +{% load static %} + +{% block content%} +
+ {% for image in all %}{% endfor %} +
+ + + +{% endblock content %} + + diff --git a/imagehosting/templates/imagehosting/base.html b/imagehosting/templates/imagehosting/base.html index d99a272..501bddf 100644 --- a/imagehosting/templates/imagehosting/base.html +++ b/imagehosting/templates/imagehosting/base.html @@ -1,114 +1,114 @@ - - - - - - - - - - - - -
- new image - all images - -
- {% block content %} - {% endblock%} - - - + + + + + + + + + + + + +
+ new image + all images + +
+ {% block content %} + {% endblock%} + + + diff --git a/imagehosting/templates/imagehosting/image.html b/imagehosting/templates/imagehosting/image.html index 3a3ad33..261d18c 100644 --- a/imagehosting/templates/imagehosting/image.html +++ b/imagehosting/templates/imagehosting/image.html @@ -1,15 +1,15 @@ -{% extends 'imagehosting/base.html' %} - -{% block content %} -
- -
- {% if user.is_authenticated %} - удалить - {% endif %} - {{ post.name }} скачать -
-
-{% endblock %} - - +{% extends 'imagehosting/base.html' %} + +{% block content %} +
+ +
+ {% if user.is_authenticated %} + удалить + {% endif %} + {{ post.name }} скачать +
+
+{% endblock %} + + diff --git a/imagehosting/templates/imagehosting/index.html b/imagehosting/templates/imagehosting/index.html index 48a0592..23b1376 100644 --- a/imagehosting/templates/imagehosting/index.html +++ b/imagehosting/templates/imagehosting/index.html @@ -1,10 +1,10 @@ -{% extends 'imagehosting/base.html' %} - -{% block content %} -
-
{% csrf_token %} - {{ form.as_p }} - -
-
-{% endblock %} +{% extends 'imagehosting/base.html' %} + +{% block content %} +
+
{% csrf_token %} + {{ form.as_p }} + +
+
+{% endblock %} diff --git a/imagehosting/templates/imagehosting/login.html b/imagehosting/templates/imagehosting/login.html index d1dae58..c0f14f0 100644 --- a/imagehosting/templates/imagehosting/login.html +++ b/imagehosting/templates/imagehosting/login.html @@ -1,12 +1,12 @@ -{% extends 'imagehosting/base.html' %} - -{% block content %} -
- - {% csrf_token %} - {{form.as_table}} - -
- -
+{% extends 'imagehosting/base.html' %} + +{% block content %} +
+ + {% csrf_token %} + {{form.as_table}} + +
+ +
{% endblock %} \ No newline at end of file diff --git a/imagehosting/tests.py b/imagehosting/tests.py index 7ce503c..de8bdc0 100644 --- a/imagehosting/tests.py +++ b/imagehosting/tests.py @@ -1,3 +1,3 @@ -from django.test import TestCase - -# Create your tests here. +from django.test import TestCase + +# Create your tests here. diff --git a/imagehosting/urls.py b/imagehosting/urls.py index 0698c51..71e79b7 100644 --- a/imagehosting/urls.py +++ b/imagehosting/urls.py @@ -1,22 +1,22 @@ -# -*- coding: utf-8 -*- - -from django.conf.urls import url -from . import views -from django.conf import settings -from django.conf.urls.static import static -from django.contrib.staticfiles.urls import staticfiles_urlpatterns -from django.contrib import admin -from django.contrib.auth.views import login, logout - - -urlpatterns = [ - url(r'^$', views.image_new, name='image_new'), - url(r'^post/(?P[0-9]+)/$', views.image_detail, name='image_detail'), - url(r'^all/$', views.all_posts, name='all_posts'), - url(r'^delete/(?P[0-9]+)/$', views.delete_post, name='delete_post'), - url(r'^admin/', admin.site.urls), - url(r'^login$', login, {'template_name':'imagehosting/login.html'}, name='login'), - url(r'^logout/$', logout) -] -urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) -urlpatterns += staticfiles_urlpatterns() +# -*- coding: utf-8 -*- + +from django.conf.urls import url +from . import views +from django.conf import settings +from django.conf.urls.static import static +from django.contrib.staticfiles.urls import staticfiles_urlpatterns +from django.contrib import admin +from django.contrib.auth.views import login, logout + + +urlpatterns = [ + url(r'^$', views.image_new, name='image_new'), + url(r'^post/(?P[0-9]+)/$', views.image_detail, name='image_detail'), + url(r'^all/$', views.all_posts, name='all_posts'), + url(r'^delete/(?P[0-9]+)/$', views.delete_post, name='delete_post'), + url(r'^admin/', admin.site.urls), + url(r'^login$', login, {'template_name':'imagehosting/login.html'}, name='login'), + url(r'^logout/$', logout) +] +urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) +urlpatterns += staticfiles_urlpatterns() diff --git a/imagehosting/views.py b/imagehosting/views.py index 37f3036..0f026d5 100644 --- a/imagehosting/views.py +++ b/imagehosting/views.py @@ -1,59 +1,59 @@ -# -*- 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 = Post.objects.order_by('-pk') - return render(request, 'imagehosting/all.html', {'all': all}) - - -@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}) \ No newline at end of file +# -*- 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}) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..644bac6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ + +Django==1.11.1 +psycopg2 +gunicorn==19.6.0