master
clemens 2017-11-30 12:37:58 +01:00
commit 351863b382
20 changed files with 345 additions and 0 deletions

12
docker-compose.yml Normal file
View File

@ -0,0 +1,12 @@
version: "2"
services:
db:
image: postgres:10-alpine
volumes:
- ./pgdata/:/var/lib/postgresql/data/
environment:
- POSTGRES_PASSWORD=secret
- POSTGRES_USER=imageHost
ports:
- "127.0.0.1:5432:5432"

0
imageHost/__init__.py Normal file
View File

136
imageHost/settings.py Normal file
View File

@ -0,0 +1,136 @@
"""
Django settings for imageHost project.
Generated by 'django-admin startproject' using Django 1.11.7.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/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.11/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'ox%di3+hzr@#p_!=l68m3ms(_en)ixd38m8u=p2*(p-*slume-'
# 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',
'images.apps.ImagesConfig',
'rest_framework'
]
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES':[
'rest_framework.permissions.IsAdminUser',
]
}
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'imageHost.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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',
'django.template.context_processors.media',
],
},
},
]
WSGI_APPLICATION = 'imageHost.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'imageHost',
'USER': 'imageHost',
'PASSWORD': 'secret',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
# Password validation
# https://docs.djangoproject.com/en/1.11/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.11/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
MEDIA_ROOT = "media/"
MEDIA_URL = "media/"

36
imageHost/urls.py Normal file
View File

@ -0,0 +1,36 @@
"""imageHost URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/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 import settings
from django.conf.urls import url, include
from django.conf.urls.static import static
from django.contrib import admin
from images.views import index,upload, ImageViewSet
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'images', ImageViewSet)
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^upload$', upload),
url(r'^$', index),
#url(r'^api', router.urls),
url(r'^api-auth/', include('rest_framework.urls', namespace="rest_framework"))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
print(urlpatterns)

16
imageHost/wsgi.py Normal file
View File

@ -0,0 +1,16 @@
"""
WSGI config for imageHost 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.11/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()

0
images/__init__.py Normal file
View File

5
images/admin.py Normal file
View File

@ -0,0 +1,5 @@
from django.contrib import admin
from .models import Image
admin.site.register(Image)

5
images/apps.py Normal file
View File

@ -0,0 +1,5 @@
from django.apps import AppConfig
class ImagesConfig(AppConfig):
name = 'images'

8
images/forms.py Normal file
View File

@ -0,0 +1,8 @@
from django import forms
from .models import Image
class UploadImageForm(forms.ModelForm):
class Meta:
model = Image
fields = ['image', 'name']

View File

@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.7 on 2017-11-30 10:17
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Image',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('image', models.FileField(blank=True, null=True, upload_to='sketches/')),
('name', models.CharField(max_length=256)),
],
),
]

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.7 on 2017-11-30 11:02
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('images', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='image',
name='image',
field=models.ImageField(blank=True, null=True, upload_to='sketches/'),
),
]

View File

6
images/models.py Normal file
View File

@ -0,0 +1,6 @@
from django.db import models
class Image(models.Model):
image = models.ImageField(upload_to="image_uploads/", blank=True, null=True)
name = models.CharField(max_length=256)

8
images/serializers.py Normal file
View File

@ -0,0 +1,8 @@
from .models import Image
from rest_framework import serializers
class ImageSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Image
fields = ('image', 'name')

View File

@ -0,0 +1,5 @@
<ul>
{% for image in images %}
<li>{{ image.name }}<img src="media/{{ image.image }}"></li>
{% endfor %}
</ul>

View File

@ -0,0 +1,5 @@
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<input type="submit"/>
</form>

3
images/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

31
images/views.py Normal file
View File

@ -0,0 +1,31 @@
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .models import Image
from .forms import UploadImageForm
def index(request):
context = {'images': Image.objects.all()}
return render(request, "images/index.html", context)
def upload(request):
print("asdf")
if request.method == 'POST':
print("POST")
form = UploadImageForm(request.POST, request.FILES)
print(form.is_valid())
if form.is_valid():
form.save()
return HttpResponseRedirect('/')
else:
form = UploadImageForm()
return render(request, "images/upload.html", {"form":form})
from rest_framework import viewsets
from .serializers import ImageSerializer
class ImageViewSet(viewsets.ModelViewSet):
queryset = Image.objects.all()
serializer_class = ImageSerializer

22
manage.py Executable file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "imageHost.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
django
psycopg2
djangorestframework