Agigunte definizioni tabelle

This commit is contained in:
subnixr 2017-09-30 02:45:43 +02:00
parent c0be47764e
commit c89a4bc537
8 changed files with 173 additions and 30 deletions

6
.gitignore vendored
View File

@ -93,10 +93,10 @@ ENV/
# Rope project settings # Rope project settings
.ropeproject .ropeproject
# macao-pos files # macao-autogestionale files
conf/ conf/
pos.db autogestionale.db
pos.log* autogestionale.log*
*.swp *.swp
.idea .idea

View File

@ -3,9 +3,9 @@ from configparser import ConfigParser
import yaml import yaml
APP_NAME = 'pos' APP_NAME = 'autogestionale'
CONFIG_PATHS = ['conf/', '~/.config/pos', '/usr/local/etc/pos', '/etc/pos'] CONFIG_PATHS = ['conf/', '~/.config/autogestionale', '/usr/local/etc/autogestionale', '/etc/autogestionale']
CONFIG_FILES = { CONFIG_FILES = {
'core': 'core.ini', 'core': 'core.ini',
'logging': 'logging.yaml' 'logging': 'logging.yaml'

View File

@ -5,7 +5,7 @@ import sqlalchemy as sa
import sqlalchemy.ext.declarative import sqlalchemy.ext.declarative
from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy import Table, Column, ForeignKey from sqlalchemy import Table, Column, ForeignKey
from sqlalchemy import Integer, String, Boolean, DateTime from sqlalchemy import Integer, String, Boolean, DateTime, Numeric
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from sqlalchemy_utils import force_auto_coercion from sqlalchemy_utils import force_auto_coercion
@ -101,15 +101,35 @@ class User(Base):
return u'{}'.format(self.uid) return u'{}'.format(self.uid)
class Event(Base): class Entry(Base):
__tablename__ = 'events' __tablename__ = 'events'
uid = Column(Integer, primary_key=True) uid = Column(Integer, primary_key=True)
amount = Column(Numeric(precision=3))
description = Column(String, nullable=False)
starts_at = Column(DateTime, nullable=False, default=datetime.now)
ends_at = Column(DateTime)
created_at = Column(DateTime, nullable=False, default=datetime.now)
class Event(Base):
__tablename__ = 'event'
uid = Column(Integer, primary_key=True)
name = Column(String, nullable=False) name = Column(String, nullable=False)
starts_at = Column(DateTime, nullable=False, default=datetime.now) starts_at = Column(DateTime, nullable=False, default=datetime.now)
ends_at = Column(DateTime) ends_at = Column(DateTime)
created_at = Column(DateTime, nullable=False, default=datetime.now) created_at = Column(DateTime, nullable=False, default=datetime.now)
transactions = relationship('Transaction', lazy='joined') entries = relationship('Entry', lazy='joined')
class Group(Base):
__tablename__ = 'group'
uid = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
description = Column(String, nullable=False)
created_at = Column(DateTime, nullable=False, default=datetime.now)
products = relationship('Event', lazy='joined')
class ProductCategory(Base): class ProductCategory(Base):
@ -145,12 +165,14 @@ class Transaction(Base):
event = relationship('Event', lazy='joined') event = relationship('Event', lazy='joined')
orders = relationship('Order', lazy='joined') orders = relationship('Order', lazy='joined')
class Order(Base): class Order(Base):
__tablename__ = 'orders' __tablename__ = 'orders'
uid = Column(Integer, primary_key=True) uid = Column(Integer, primary_key=True)
product_uid = Column(Integer, ForeignKey('products.uid'), nullable=False) product_uid = Column(Integer, ForeignKey('products.uid'), nullable=False)
quantity = Column(Integer, nullable=False) quantity = Column(Integer, nullable=False)
transaction_uid = Column(Integer, ForeignKey('transactions.uid'), nullable=False) transaction_uid = Column(Integer, ForeignKey(
'transactions.uid'), nullable=False)
product = relationship('Product', lazy='joined') product = relationship('Product', lazy='joined')
transaction = relationship('Transaction', lazy='joined') transaction = relationship('Transaction', lazy='joined')

View File

@ -1,7 +1,7 @@
import logging import logging
import logging.config import logging.config
from pos.config import APP_NAME from autogestionale.config import APP_NAME
root = logging.getLogger(APP_NAME) root = logging.getLogger(APP_NAME)

View File

@ -1,17 +1,134 @@
from functools import wraps
from aiohttp.web import json_response from aiohttp.web import json_response
from autogestionale.database import User, ProductCategory, Event
async def playlist_add(request):
uuid = request.match_info['uuid']
try: def needs(*needed):
request.app['playlist'].put(uuid) def decorator(func):
@wraps(func)
except DuplicateTrackError: async def wrapper(request):
request_json = await request.json()
if not all(k in request_json.keys() for k in needed):
return json_response({ return json_response({
'err': 'duplicate', 'err': 'malformed_request',
'msg': 'The track is already present in the playlist.' 'msg': 'Missing one or more keys: {}.'.format(
", ".join(needed))
}, status=400) }, status=400)
else: else:
return func(request)
return wrapper
return decorator
def auth_required(func):
@wraps(func)
async def wrapper(request):
db = request.app['db']
headers = request.headers
if 'Authorization' not in headers.keys():
return json_response({'err': 'malformed_request',
'msg': 'Missing Authorization header.'},
status=400)
else:
remote_token = headers['Authorization']
with db.get_session() as session:
token = session.query(AccessToken) \
.filter_by(token=remote_token) \
.one_or_none()
if not token:
return json_response({'err': 'unauthorized',
'msg': 'The token is not valid.'},
status=401)
elif not token.is_valid():
return json_response({'err': 'forbidden',
'msg': 'The token has expired.'},
status=403)
else:
return await func(request)
return wrapper
@needs('username', 'password')
async def token_create(request):
db = request.app['db']
request_json = await request.json()
username = request_json['username']
password = request_json['password']
with db.get_session() as session:
user = session.query(User) \
.filter_by(username=username) \
.one_or_none()
if not user or user.password != password:
return json_response({'err': 'invalid_credentials'},
status=400)
with db.get_session() as session:
token = AccessToken(user=user)
session.add(token)
return json_response({
'token': token.token,
'created_at': token.created_at.isoformat(),
'expires_at': token.created_at.isoformat()
})
@auth_required
async def token_destroy(request):
db = request.app['db']
remote_token = request.headers['Authorization']
with db.get_session() as session:
token = session.query(AccessToken) \
.filter_by(token=remote_token) \
.one_or_none()
token.is_active = False
session.add(token)
return json_response({}, status=200) return json_response({}, status=200)
@auth_required
async def product_list(request):
db = request.app['db']
with db.get_session() as session:
categories = session.query(ProductCategory).all()
return json_response({
'categories': [{
'uid': c.uid,
'name': c.name,
'products': [{
'uid': p.uid,
'name': p.name,
'price': p.price
} for p in c.products]
} for c in categories]
})
async def event_list(request):
db = request.app['db']
with db.get_session() as session:
events = session.query(Event).all()
return json_response({
'events': [{
'uid': evt.uid,
'name': evt.name,
'entries': [{
'uid': entr.uid,
'amount': entr.amount
} for entr in evt.entries]
} for evt in events]
})

View File

@ -1,7 +1,12 @@
from autogestionale.rest import event_list
def setup_routes(app): def setup_routes(app):
app.router.add_route('GET','/', info) app.router.add_route('GET', '/', info)
app.router.add_route('GET', '/login', info) app.router.add_route('GET', '/login', info)
app.router.add_route('POST', '/login', info) app.router.add_route('POST', '/login', info)
app.router.add_route('GET', '/events', event_list)
async def info(request): async def info(request):
return json_response({ return json_response({

6
cli.py
View File

@ -3,9 +3,9 @@ import click
from tabulate import tabulate from tabulate import tabulate
from datetime import datetime from datetime import datetime
from pos.config import Config from autogestionale.config import Config
from pos.logging import init_logging, get_logger from autogestionale.logging import init_logging, get_logger
from pos.database import Database, User, Event, ProductCategory, Product,\ from autogestionale.database import Database, User, Event, ProductCategory, Product,\
Transaction Transaction
config = Config() config = Config()

9
web.py
View File

@ -1,9 +1,9 @@
#! /usr/bin/env python3 #! /usr/bin/env python3
from pos.config import Config from autogestionale.config import Config
from pos.logging import init_logging, get_logger from autogestionale.logging import init_logging, get_logger
from pos.database import Database from autogestionale.database import Database
from pos.routes import setup_routes from autogestionale.routes import setup_routes
import asyncio import asyncio
from aiohttp import web from aiohttp import web
@ -30,4 +30,3 @@ if __name__ == '__main__':
web.run_app(app, web.run_app(app,
host=config.core.get('GENERAL', 'Address'), host=config.core.get('GENERAL', 'Address'),
port=config.core.getint('GENERAL', 'Port')) port=config.core.getint('GENERAL', 'Port'))