Commit 64c12b64 authored by Aljaž Srebrnič's avatar Aljaž Srebrnič

Migrate project do Django

parent 419aacb6
[flake8]
max-line-length = 100
\ No newline at end of file
__pycache__/
\ No newline at end of file
/bower_components/
# -*- coding: utf-8 -*-
import os
import types
import cherrypy
import jinja2
import psycopg2
import config
class TemplateTool(cherrypy.Tool):
_engine = None
'''Jinja environment instance'''
def __init__(self):
viewLoader = jinja2.FileSystemLoader(os.path.join(config.path, 'app', 'view'))
self._engine = jinja2.Environment(loader=viewLoader)
cherrypy.Tool.__init__(self, 'before_handler', self.render)
def __call__(self, *args, **kwargs):
if args and isinstance(args[0], (types.FunctionType, types.MethodType)):
# @template
args[0].exposed = True
return cherrypy.Tool.__call__(self, **kwargs)(args[0])
else:
# @template()
def wrap(f):
f.exposed = True
return cherrypy.Tool.__call__(self, *args, **kwargs)(f)
return wrap
def render(self, name=None):
cherrypy.request.config['template'] = name
handler = cherrypy.serving.request.handler
def wrap(*args, **kwargs):
return self._render(handler, *args, **kwargs)
cherrypy.serving.request.handler = wrap
def _render(self, handler, *args, **kwargs):
template = cherrypy.request.config['template']
if not template:
parts = []
if hasattr(handler.callable, '__self__'):
parts.append(handler.callable.__self__.__class__.__name__.lower())
if hasattr(handler.callable, '__name__'):
parts.append(handler.callable.__name__.lower())
template = '/'.join(parts)
data = handler(*args, **kwargs) or {}
renderer = self._engine.get_template('page/{0}.html'.format(template))
return renderer.render(**data) if template and isinstance(data, dict) else data
def db_connect(thread_index):
cherrypy.thread_data.db = psycopg2.connect("dbname=inventory user=mittelab password=mittelab")
def bootstrap():
cherrypy.tools.template = TemplateTool()
cherrypy.config.update(config.config)
cherrypy.engine.subscribe('start_thread', db_connect)
from . import controller
cherrypy.config.update({'error_page.default': controller.errorPage})
cherrypy.tree.mount(controller.Index(), '/', config.config)
# -*- coding: utf-8 -*-
import datetime
import cherrypy
class Index:
news = None
user = None
@cherrypy.tools.template
def index(self):
pass
@cherrypy.expose
def broken(self):
raise RuntimeError('Pretend something has broken')
@cherrypy.expose
def item(self, **kwargs):
from psycopg2.extensions import AsIs
data = kwargs
if 'cameraInput' in data.keys():
del data['cameraInput']
for k in data.keys():
if len(data[k]) == 0:
data[k] = None
cur = cherrypy.thread_data.db.cursor()
columns = data.keys()
values = [data[column] for column in columns]
insert_statement = 'INSERT INTO object (%s) VALUES %s'
cur.execute(insert_statement, (AsIs(",".join(columns)), tuple(values)))
cherrypy.thread_data.db.commit()
return
def errorPage(status, message, **kwargs):
return cherrypy.tools.template._engine.get_template('page/error.html').render()
{% extends 'layout/main.html' %}
{% block content %}
<h1>Error!</h1>
{% endblock %}
#!/usr/bin/env python
import os
import cherrypy
path = os.path.abspath(os.path.dirname(__file__))
config = {
'global': {
'server.socket_host': '0.0.0.0',
'server.socket_port': 8080,
},
'/': {
},
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.dir': './public',
'tools.staticdir.root': os.path.abspath(os.getcwd())
}
}
This diff is collapsed.
"""
Django settings for invman project.
Generated by 'django-admin startproject' using Django 1.11.1.
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 = '_z)vyvdvsz1$^8@_f$x7blf=h0sdx_mgb23p)^$!h8upe3q$y='
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'things.apps.ThingsConfig',
'locations.apps.LocationsConfig',
'loans.apps.LoansConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
'djangobower',
]
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 = 'invman.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
'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 = 'invman.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'invman',
'USER': 'invman',
'PASSWORD': 'InVmAn',
'HOST': 'localhost'
}
}
# 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 = 'Europe/Rome'
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_URL = '/static/'
STATICFILES_FINDERS = (
'djangobower.finders.BowerFinder',
)
BOWER_COMPONENTS_ROOT = BASE_DIR
BOWER_INSTALLED_APPS = (
'jquery',
'bootstrap',
'qcode-decoder',
)
"""invman 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.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^things/', include('things.urls'))
]
"""
WSGI config for invman 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", "invman.settings")
application = get_wsgi_application()
from django.contrib import admin
from .models import Loan
admin.site.register(Loan)
from django.apps import AppConfig
class LoansConfig(AppConfig):
name = 'loans'
# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2017-05-28 21:17
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
('things', '0002_auto_20170528_2316'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Loan',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('date_out', models.DateTimeField()),
('date_in', models.DateTimeField()),
('borrower', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
('thing', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='things.Thing')),
],
),
]
from django.db import models
from django.contrib.auth.models import User
from things.models import Thing
class Loan(models.Model):
borrower = models.ForeignKey(User, null=False)
thing = models.ForeignKey(Thing, null=False)
date_out = models.DateTimeField(null=False)
date_in = models.DateTimeField()
from django.test import TestCase
# Create your tests here.
from django.shortcuts import render
# Create your views here.
from django.contrib import admin
from .models import Location, Map
admin.site.register([Location, Map])
from django.apps import AppConfig
class LocationsConfig(AppConfig):
name = 'locations'
# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2017-05-28 21:17
from __future__ import unicode_literals
import django.contrib.gis.db.models.fields
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
('things', '0002_auto_20170528_2316'),
]
operations = [
migrations.CreateModel(
name='Location',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('coord', django.contrib.gis.db.models.fields.PointField(srid=4326)),
],
),
migrations.CreateModel(
name='Map',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=80)),
('drawing', models.BinaryField()),
],
),
migrations.CreateModel(
name='Thing',
fields=[
('thing_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='things.Thing')),
('location', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='locations.Location')),
],
bases=('things.thing',),
),
migrations.AddField(
model_name='location',
name='map',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='locations.Map'),
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2017-05-28 21:31
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('locations', '0001_initial'),
]
operations = [
migrations.RemoveField(
model_name='thing',
name='location',
),
migrations.RemoveField(
model_name='thing',
name='thing_ptr',
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2017-05-28 21:31
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('locations', '0002_auto_20170528_2331'),
('things', '0003_auto_20170528_2331'),
('loans', '0001_initial'),
]
operations = [
migrations.DeleteModel(
name='Thing',
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2017-05-28 21:36
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('locations', '0003_delete_thing'),
]
operations = [
migrations.AlterField(
model_name='map',
name='drawing',
field=models.ImageField(upload_to=''),
),
]
from django.contrib.gis.db import models
class Map(models.Model):
name = models.CharField(max_length=80)
drawing = models.ImageField()
class Location(models.Model):
map = models.ForeignKey(Map, null=False)
coord = models.PointField(null=False)
from django.test import TestCase
# Create your tests here.
from django.shortcuts import render
# Create your views here.
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "invman.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)
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
$(function () {
var qr = new QCodeDecoder();
var resultHandler = function (err, result) {
if (err) {
console.log(err.message);
return;
}
$('input#id')[0].value=result;
$('#qr-scan').modal("hide");
};
$('#cameraProcess').click(function(e) {
var input = $('#cameraInput')[0];
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
qr.decodeFromImage(e.target.result, resultHandler);
};
reader.readAsDataURL(input.files[0]);
}
});
$('#qr-scan').on('show.bs.modal', function(){
if (!(qr.isCanvasSupported() && qr.hasGetUserMedia())) {
alert('Your browser doesn\'t match the required specs.');
throw new Error('Canvas and getUserMedia are required');
}
var video = document.querySelector('video');
var reset = document.querySelector('#reset');
// prepare a canvas element that will receive the image to decode, sets
// the callback for the result and then prepares the videoElement to
// send its source to the decoder.
qr.decodeFromCamera(video, resultHandler);
// attach some event handlers to reset and stop whenever we want.
reset.onclick = function () {
qr.decodeFromCamera(video, resultHandler);
};
}).on('hide.bs.modal', function() {
qr.stop();
});
});
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
// This file is autogenerated via the `commonjs` Grunt task. You can require() this file in a CommonJS environment.
require('../../js/transition.js')
require('../../js/alert.js')
require('../../js/button.js')
require('../../js/carousel.js')
require('../../js/collapse.js')
require('../../js/dropdown.js')
require('../../js/modal.js')
require('../../js/tooltip.js')
require('../../js/popover.js')
require('../../js/scrollspy.js')
require('../../js/tab.js')
require('../../js/affix.js')
\ No newline at end of file
This diff is collapsed.
appdirs==1.4.2
cheroot==5.2.0
CherryPy==10.1.1
Jinja2==2.9.5
MarkupSafe==0.23
packaging==16.8
portend==1.8
psycopg2==2.7
pyparsing==2.1.10
pytz==2016.10
six==1.10.0
tempora==1.6.1
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from app import bootstrap
bootstrap()
# debugging purpose, e.g. run with PyDev debugger
if __name__ == '__main__':
import cherrypy
cherrypy.engine.signals.subscribe()
cherrypy.engine.start()
cherrypy.engine.block()
{% load static %}
<!doctype html>
<html class="no-js" lang="">
<head>
......@@ -7,21 +8,19 @@
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="static/css/bootstrap.min.css">
<link rel="stylesheet" href="{% static 'bootstrap/dist/css/bootstrap.min.css' %}">
<style>
body {
padding-top: 50px;
padding-bottom: 20px;
}
</style>
<link rel="stylesheet" href="static/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="static/css/main.css">
<link rel="stylesheet" href="{% static 'css/main.css' %}">
<script src="static/js/vendor/modernizr-2.8.3-respond-1.4.2.min.js"></script>
<script src="static/js/vendor/qcode-decoder.min.js"></script>
<script src="{% static 'qcode-decoder/build/qcode-decoder.min.js' %}"></script>
</head>
<body>
{% include 'part/menu.html' %}
{% include 'menu.html' %}
{% block modal %}