From 9957fcc02907d81aea1aaa4f6a043278b1c9411d Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 22 May 2015 13:07:54 +0400 Subject: [PATCH 01/15] Fix project structure to Django 1.8 --- .gitignore | 3 +- .../__init__.py | 0 .../settings.py | 1 + urls.py => django-jquery-file-upload/urls.py | 0 manage.py | 30 ++++++++++++------- requirements.txt | 2 +- 6 files changed, 24 insertions(+), 12 deletions(-) rename __init__.py => django-jquery-file-upload/__init__.py (100%) rename settings.py => django-jquery-file-upload/settings.py (98%) rename urls.py => django-jquery-file-upload/urls.py (100%) diff --git a/.gitignore b/.gitignore index b0d4e48..55e6249 100644 --- a/.gitignore +++ b/.gitignore @@ -7,8 +7,9 @@ db /share /env /venv -/django-jquery-file-upload + *.log *.pot *.pyc local_settings.py +.idea \ No newline at end of file diff --git a/__init__.py b/django-jquery-file-upload/__init__.py similarity index 100% rename from __init__.py rename to django-jquery-file-upload/__init__.py diff --git a/settings.py b/django-jquery-file-upload/settings.py similarity index 98% rename from settings.py rename to django-jquery-file-upload/settings.py index 68744b1..d7e49bd 100644 --- a/settings.py +++ b/django-jquery-file-upload/settings.py @@ -100,6 +100,7 @@ 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', + 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', ) ROOT_URLCONF = 'django-jquery-file-upload.urls' diff --git a/urls.py b/django-jquery-file-upload/urls.py similarity index 100% rename from urls.py rename to django-jquery-file-upload/urls.py diff --git a/manage.py b/manage.py index 3e4eedc..92a4d6e 100755 --- a/manage.py +++ b/manage.py @@ -1,14 +1,24 @@ #!/usr/bin/env python -from django.core.management import execute_manager -import imp -try: - imp.find_module('settings') # Assumed to be in the same directory. -except ImportError: - import sys - sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n" % __file__) - sys.exit(1) +# from django.core.management import execute_manager +# import imp +# try: +# imp.find_module('settings') # Assumed to be in the same directory. +# except ImportError: +# import sys +# sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n" % __file__) +# sys.exit(1) +# +# import settings +# +# if __name__ == "__main__": +# execute_manager(settings) -import settings +#!/usr/bin/env python +import os, sys if __name__ == "__main__": - execute_manager(settings) + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django-jquery-file-upload.settings") + + from django.core.management import execute_from_command_line + + execute_from_command_line(sys.argv) diff --git a/requirements.txt b/requirements.txt index 23cb63a..67a04d6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -django<1.6 +django pillow From fe06a23c191aab7f6b7d0a9de489da240a6b9d3a Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 22 May 2015 13:12:19 +0400 Subject: [PATCH 02/15] Fix using django.utils.simplejson. Now use json. --- fileupload/response.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fileupload/response.py b/fileupload/response.py index 829685a..334eb29 100644 --- a/fileupload/response.py +++ b/fileupload/response.py @@ -1,6 +1,6 @@ # encoding: utf-8 from django.http import HttpResponse -from django.utils import simplejson +import json MIMEANY = '*/*' MIMEJSON = 'application/json' @@ -34,5 +34,5 @@ def a_iew(request): """ def __init__(self, obj='', json_opts=None, mimetype=MIMEJSON, *args, **kwargs): json_opts = json_opts if isinstance(json_opts, dict) else {} - content = simplejson.dumps(obj, **json_opts) + content = json.dumps(obj, **json_opts) super(JSONResponse, self).__init__(content, mimetype, *args, **kwargs) From 3a9ea7fd4594b17f30f8cdd2197c0b91b3faed3a Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 22 May 2015 13:14:54 +0400 Subject: [PATCH 03/15] Add 'fields' to PictureCreateView --- fileupload/views.py | 1 + 1 file changed, 1 insertion(+) diff --git a/fileupload/views.py b/fileupload/views.py index 98b339c..e9f6b01 100644 --- a/fileupload/views.py +++ b/fileupload/views.py @@ -10,6 +10,7 @@ class PictureCreateView(CreateView): model = Picture + fields = "__all__" def form_valid(self, form): self.object = form.save() From 82155f3caad1220eeb2ee718142c5aace8600f87 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 22 May 2015 13:21:07 +0400 Subject: [PATCH 04/15] Fix serve media files path --- django-jquery-file-upload/urls.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django-jquery-file-upload/urls.py b/django-jquery-file-upload/urls.py index 00270ad..0c3e2d4 100644 --- a/django-jquery-file-upload/urls.py +++ b/django-jquery-file-upload/urls.py @@ -19,7 +19,7 @@ url(r'^admin/', include(admin.site.urls)), ) -import os +from os.path import join, abspath, dirname urlpatterns += patterns('', - (r'^media/(.*)$', 'django.views.static.serve', {'document_root': os.path.join(os.path.abspath(os.path.dirname(__file__)), 'media')}), + (r'^media/(.*)$', 'django.views.static.serve', {'document_root': join(abspath(dirname(dirname(__file__))), 'media')}), ) From 8b7bb906df393f213b087553e16ee27710dab8fb Mon Sep 17 00:00:00 2001 From: Sigurd Gartmann Date: Sat, 23 May 2015 18:13:34 +0200 Subject: [PATCH 05/15] Still saving images to /media under django-jquery-file-upload app. --- .gitignore | 4 ++-- django-jquery-file-upload/urls.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 55e6249..8fef53c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,9 +7,9 @@ db /share /env /venv - +/django-jquery-file-upload/media *.log *.pot *.pyc local_settings.py -.idea \ No newline at end of file +.idea diff --git a/django-jquery-file-upload/urls.py b/django-jquery-file-upload/urls.py index 0c3e2d4..7072912 100644 --- a/django-jquery-file-upload/urls.py +++ b/django-jquery-file-upload/urls.py @@ -21,5 +21,5 @@ from os.path import join, abspath, dirname urlpatterns += patterns('', - (r'^media/(.*)$', 'django.views.static.serve', {'document_root': join(abspath(dirname(dirname(__file__))), 'media')}), + (r'^media/(.*)$', 'django.views.static.serve', {'document_root': join(abspath(dirname(__file__)), 'media')}), ) From a9b307c9e2670c044a97f2961206404b0c1d517d Mon Sep 17 00:00:00 2001 From: Sigurd Gartmann Date: Tue, 7 Jun 2016 22:24:48 +0200 Subject: [PATCH 06/15] Upgrade to Django 1.9 --- django-jquery-file-upload/settings.py | 194 +++++++----------- django-jquery-file-upload/urls.py | 22 +- django-jquery-file-upload/wsgi.py | 11 + fileupload/apps.py | 6 + fileupload/migrations/0001_initial.py | 24 +++ fileupload/migrations/__init__.py | 0 fileupload/models.py | 2 +- .../fileupload/picture_angular_form.html | 36 ++-- .../fileupload/picture_basic_form.html | 12 +- .../fileupload/picture_basicplus_form.html | 26 +-- .../templates/fileupload/picture_form.html | 38 ++-- .../fileupload/picture_jquery_form.html | 40 ++-- fileupload/templates/upload_base.html | 8 +- fileupload/urls.py | 6 +- 14 files changed, 202 insertions(+), 223 deletions(-) create mode 100644 django-jquery-file-upload/wsgi.py create mode 100644 fileupload/apps.py create mode 100644 fileupload/migrations/0001_initial.py create mode 100644 fileupload/migrations/__init__.py diff --git a/django-jquery-file-upload/settings.py b/django-jquery-file-upload/settings.py index d7e49bd..d27d9db 100644 --- a/django-jquery-file-upload/settings.py +++ b/django-jquery-file-upload/settings.py @@ -1,149 +1,95 @@ +""" +Django settings. +""" + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = '9%$in^gpdaig@v3or_to&_z(=n)3)$f1mr3hf9e#kespy2ajlo' + +# SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -TEMPLATE_DEBUG = DEBUG -SITE_ROOT = os.path.dirname(os.path.realpath(__file__)) +ALLOWED_HOSTS = [] + +# Application definition -ADMINS = ( - # ('Your Name', 'your_email@example.com'), +INSTALLED_APPS = ( + 'fileupload.apps.FileuploadConfig', + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', +) + +MIDDLEWARE_CLASSES = ( + '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', + 'django.middleware.security.SecurityMiddleware', ) -MANAGERS = ADMINS +ROOT_URLCONF = 'django-jquery-file-upload.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + #'DIRS': ['django-jquery-file-upload/templates'], + '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 = 'django-jquery-file-upload.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/1.8/ref/settings/#databases DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. - 'NAME': os.path.join(SITE_ROOT, 'db'), # Or path to database file if using sqlite3. - 'USER': '', # Not used with sqlite3. - 'PASSWORD': '', # Not used with sqlite3. - 'HOST': '', # Set to empty string for localhost. Not used with sqlite3. - 'PORT': '', # Set to empty string for default. Not used with sqlite3. + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db'), } } -# Local time zone for this installation. Choices can be found here: -# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name -# although not all choices may be available on all operating systems. -# On Unix systems, a value of None will cause Django to use the same -# timezone as the operating system. -# If running in a Windows environment this must be set to the same as your -# system time zone. -TIME_ZONE = 'America/Chicago' - -# Language code for this installation. All choices can be found here: -# http://www.i18nguy.com/unicode/language-identifiers.html + +# Internationalization +# https://docs.djangoproject.com/en/1.8/topics/i18n/ + LANGUAGE_CODE = 'en-us' -SITE_ID = 1 +TIME_ZONE = 'Europe/Oslo' -# If you set this to False, Django will make some optimizations so as not -# to load the internationalization machinery. USE_I18N = True -# If you set this to False, Django will not format dates, numbers and -# calendars according to the current locale USE_L10N = True -# Absolute filesystem path to the directory that will hold user-uploaded files. -# Example: "/home/media/media.lawrence.com/media/" -MEDIA_ROOT = os.path.abspath(os.path.dirname(__file__)) + '/media/' +USE_TZ = True -# URL that handles the media served from MEDIA_ROOT. Make sure to use a -# trailing slash. -# Examples: "http://media.lawrence.com/media/", "http://example.com/media/" -MEDIA_URL = '/media/' - -# Absolute path to the directory static files should be collected to. -# Don't put anything in this directory yourself; store your static files -# in apps' "static/" subdirectories and in STATICFILES_DIRS. -# Example: "/home/media/media.lawrence.com/static/" -STATIC_ROOT = os.path.abspath(os.path.dirname(__file__)) + '/static/' -# URL prefix for static files. -# Example: "http://media.lawrence.com/static/" +# Static files (CSS, JavaScript, Images) STATIC_URL = '/static/' -# URL prefix for admin static files -- CSS, JavaScript and images. -# Make sure to use a trailing slash. -# Examples: "http://foo.com/static/admin/", "/static/admin/". -ADMIN_MEDIA_PREFIX = '/static/admin/' - -# Additional locations of static files -STATICFILES_DIRS = ( - # Put strings here, like "/home/html/static" or "C:/www/django/static". - # Always use forward slashes, even on Windows. - # Don't forget to use absolute paths, not relative paths. -) +STATICFILES_DIRS = [ + 'django-jquery-file-upload/static', +] -# List of finder classes that know how to find static files in -# various locations. -STATICFILES_FINDERS = ( - 'django.contrib.staticfiles.finders.FileSystemFinder', - 'django.contrib.staticfiles.finders.AppDirectoriesFinder', -# 'django.contrib.staticfiles.finders.DefaultStorageFinder', -) - -# Make this unique, and don't share it with anybody. -SECRET_KEY = '9%$in^gpdaig@v3or_to&_z(=n)3)$f1mr3hf9e#kespy2ajlo' - -# List of callables that know how to import templates from various sources. -TEMPLATE_LOADERS = ( - 'django.template.loaders.filesystem.Loader', - 'django.template.loaders.app_directories.Loader', -# 'django.template.loaders.eggs.Loader', -) - -MIDDLEWARE_CLASSES = ( - 'django.middleware.common.CommonMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', -) - -ROOT_URLCONF = 'django-jquery-file-upload.urls' - -TEMPLATE_DIRS = ( - # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". - # Always use forward slashes, even on Windows. - # Don't forget to use absolute paths, not relative paths. -) - -INSTALLED_APPS = ( - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.sites', - 'django.contrib.messages', - 'django.contrib.staticfiles', - 'fileupload', - # Uncomment the next line to enable the admin: - 'django.contrib.admin', - # Uncomment the next line to enable admin documentation: - # 'django.contrib.admindocs', -) - -# A sample logging configuration. The only tangible logging -# performed by this configuration is to send an email to -# the site admins on every HTTP 500 error. -# See http://docs.djangoproject.com/en/dev/topics/logging for -# more details on how to customize your logging configuration. -LOGGING = { - 'version': 1, - 'disable_existing_loggers': False, - 'handlers': { - 'mail_admins': { - 'level': 'ERROR', - 'class': 'django.utils.log.AdminEmailHandler' - } - }, - 'loggers': { - 'django.request': { - 'handlers': ['mail_admins'], - 'level': 'ERROR', - 'propagate': True, - }, - } -} +MEDIA_URL = '/media/' +MEDIA_ROOT = os.path.join(BASE_DIR, 'django-jquery-file-upload', 'media') diff --git a/django-jquery-file-upload/urls.py b/django-jquery-file-upload/urls.py index 7072912..53f314a 100644 --- a/django-jquery-file-upload/urls.py +++ b/django-jquery-file-upload/urls.py @@ -1,25 +1,17 @@ -from django.conf.urls import patterns, include, url +from django.conf.urls import include, url from django.http import HttpResponseRedirect +from django.conf import settings # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() -urlpatterns = patterns('', - # Examples: - # url(r'^$', 'upload.views.home', name='home'), - +urlpatterns = [ url(r'^$', lambda x: HttpResponseRedirect('/upload/new/')), url(r'^upload/', include('fileupload.urls')), - - # Uncomment the admin/doc line below to enable admin documentation: - # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), - - # Uncomment the next line to enable the admin: url(r'^admin/', include(admin.site.urls)), -) +] -from os.path import join, abspath, dirname -urlpatterns += patterns('', - (r'^media/(.*)$', 'django.views.static.serve', {'document_root': join(abspath(dirname(__file__)), 'media')}), -) +if settings.DEBUG: + from django.conf.urls.static import static + urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/django-jquery-file-upload/wsgi.py b/django-jquery-file-upload/wsgi.py new file mode 100644 index 0000000..8e47fba --- /dev/null +++ b/django-jquery-file-upload/wsgi.py @@ -0,0 +1,11 @@ +""" +WSGI config for django-jquery-file-upload project. +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django-jquery-file-upload.settings") + +application = get_wsgi_application() diff --git a/fileupload/apps.py b/fileupload/apps.py new file mode 100644 index 0000000..ff22d3e --- /dev/null +++ b/fileupload/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class FileuploadConfig(AppConfig): + name = 'fileupload' + diff --git a/fileupload/migrations/0001_initial.py b/fileupload/migrations/0001_initial.py new file mode 100644 index 0000000..0c157ef --- /dev/null +++ b/fileupload/migrations/0001_initial.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.6 on 2016-06-07 19:15 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Picture', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('file', models.ImageField(upload_to='pictures')), + ('slug', models.SlugField(blank=True)), + ], + ), + ] diff --git a/fileupload/migrations/__init__.py b/fileupload/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/fileupload/models.py b/fileupload/models.py index f28d242..81a20a3 100644 --- a/fileupload/models.py +++ b/fileupload/models.py @@ -12,7 +12,7 @@ class Picture(models.Model): file = models.ImageField(upload_to="pictures") slug = models.SlugField(max_length=50, blank=True) - def __unicode__(self): + def __str__(self): return self.file.name @models.permalink diff --git a/fileupload/templates/fileupload/picture_angular_form.html b/fileupload/templates/fileupload/picture_angular_form.html index a5ac9c0..a6add16 100644 --- a/fileupload/templates/fileupload/picture_angular_form.html +++ b/fileupload/templates/fileupload/picture_angular_form.html @@ -24,13 +24,13 @@ - + - + - + - + - + - + - +