diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/admin.py b/admin.py new file mode 100644 index 0000000..2d0670b --- /dev/null +++ b/admin.py @@ -0,0 +1,7 @@ +from django.contrib import admin + +from .models import Post + +admin.site.register(Post +) +# Register your models here. diff --git a/apps.py b/apps.py new file mode 100644 index 0000000..9a28b40 --- /dev/null +++ b/apps.py @@ -0,0 +1,7 @@ +from __future__ import unicode_literals + +from django.apps import AppConfig + + +class ImagehostConfig(AppConfig): + name = 'imagehost' diff --git a/forms.py b/forms.py new file mode 100644 index 0000000..34b26a1 --- /dev/null +++ b/forms.py @@ -0,0 +1,15 @@ +# -*- 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/imagehost/__init__.py b/imagehost/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/imagehost/settings.py b/imagehost/settings.py new file mode 100644 index 0000000..cdd8603 --- /dev/null +++ b/imagehost/settings.py @@ -0,0 +1,135 @@ +# -*- 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' +] + +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 = 'mysite.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(BASE_DIR, '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(BASE_DIR, 'media') +MEDIA_URL = '/media/' diff --git a/imagehost/urls.py b/imagehost/urls.py new file mode 100644 index 0000000..0257aa1 --- /dev/null +++ b/imagehost/urls.py @@ -0,0 +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) diff --git a/imagehost/wsgi.py b/imagehost/wsgi.py new file mode 100644 index 0000000..f47ba04 --- /dev/null +++ b/imagehost/wsgi.py @@ -0,0 +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", "mysite.settings") + +application = get_wsgi_application() diff --git a/manage.py b/manage.py new file mode 100644 index 0000000..8a50ec0 --- /dev/null +++ b/manage.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") + + from django.core.management import execute_from_command_line + + execute_from_command_line(sys.argv) diff --git a/models.py b/models.py new file mode 100644 index 0000000..1bf72d1 --- /dev/null +++ b/models.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +from django.db import models +from django.utils import timezone +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) + + def publish(self): + self.published_date = timezone.now() + print(self.__dict__) + self.save() + + def __unicode__(self): + return self.name + + def thumb_name(self): + x = str(self.file) + t = x.split('/') + + return '/thumb_' + t[1] + + thumb_name = property(thumb_name) + + def orig_name(self): + x = str(self.file) + return x.split('/')[1] + 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) + im.save(thumb_name) + property(resize_image)''' diff --git a/templates/imagehost/all.html b/templates/imagehost/all.html new file mode 100644 index 0000000..2ab1f0c --- /dev/null +++ b/templates/imagehost/all.html @@ -0,0 +1,18 @@ +{% extends 'base.html' %} + +{% load pagination_tags %} + +{% block content%} +
+{% for image in all %} +
+ + + {{ image.name }} +
+{% endfor %} +
+ +{% endblock content %} + + diff --git a/templates/imagehost/base.html b/templates/imagehost/base.html new file mode 100644 index 0000000..f705c4b --- /dev/null +++ b/templates/imagehost/base.html @@ -0,0 +1,38 @@ + + + + + + + + + + + + +
+ {% block content %} + {% endblock%} + + diff --git a/templates/imagehost/carousel.html b/templates/imagehost/carousel.html new file mode 100644 index 0000000..0a0dc16 --- /dev/null +++ b/templates/imagehost/carousel.html @@ -0,0 +1,62 @@ + {% load staticfiles %} + + + Gallery + + + + + + + + + + + + + + + +
+ + +
+ +
+
+{% for image in all %} + +{% endfor %} +
+
+ +
1
+ diff --git a/templates/imagehost/delete.html b/templates/imagehost/delete.html new file mode 100644 index 0000000..26e9e4e --- /dev/null +++ b/templates/imagehost/delete.html @@ -0,0 +1,10 @@ +{% extends 'base.html' %} + + {% block content %} +

Delete

+
{% csrf_token %} + {{ form.as_p }} + +
+
+ {% endblock %} diff --git a/templates/imagehost/image.html b/templates/imagehost/image.html new file mode 100644 index 0000000..b1152aa --- /dev/null +++ b/templates/imagehost/image.html @@ -0,0 +1,13 @@ +{% extends 'base.html' %} + +{% block content %} +
+ +

+

{{ post.name }} скачать

+ +удалить +
+{% endblock %} + + diff --git a/templates/imagehost/index.html b/templates/imagehost/index.html new file mode 100644 index 0000000..682821d --- /dev/null +++ b/templates/imagehost/index.html @@ -0,0 +1,10 @@ +{% extends 'base.html' %} + +{% block content %} +
+
{% csrf_token %} + {{ form.as_p }} + +
+
+{% endblock %} diff --git a/tests.py b/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/urls.py b/urls.py new file mode 100644 index 0000000..d13043a --- /dev/null +++ b/urls.py @@ -0,0 +1,13 @@ +# -*- coding: utf-8 -*- + +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'), + url(r'^all/$', views.all_posts, name='all_posts'), + url(r'^carousel/$', views.carousel, name='carousel'), + url(r'^delete/(?P[0-9]+)/$', views.delete_post, name='delete_post') +] diff --git a/views.py b/views.py new file mode 100644 index 0000000..3e68053 --- /dev/null +++ b/views.py @@ -0,0 +1,93 @@ +# -*- 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 + +'''def create_thumbnail(request): + # Get file from + 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 = 'mysite/media/images/' + str(filename) + + print(filename) + # Create thumbnail + print("DEBUG: Resizing image " + orig_file) + thumb_name = images.resize_image(orig_file, { + 'name': filename, + 'size': [300, 300], + 'dest': 'mysite/media/images' + } + + ) + + 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) + post = form.save(commit=False) + post.save() + print("FILE: ", post.file) + create_thumb_from_file(post.orig_name) + return redirect('imagehost.views.image_detail', pk=post.id) + else: + form = PostForm() + return render(request, 'imagehost/index.html', {'form': form}) + + +def image_detail(request, pk): + post = get_object_or_404(Post, pk=pk) + print(post) + return render(request, 'imagehost/image.html', {'post': post}) + + +def all_posts(request): + all = Post.objects.order_by('-pk') + page = request.POST.get("page") + return render(request, 'imagehost/all.html', {'all': all}) + +def carousel(request): + return render(request, 'imagehost/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 not request.user.is_authenticated(): + return HttpResponseRedirect('/admin') + post_to_delete.delete() + return redirect('imagehost.views.all_posts') + else: + form = DeleteForm(instance=post_to_delete) + + return render(request, 'imagehost/delete.html', {'form': form})