server.py 1.71 KB
Newer Older
Sli's avatar
Sli committed
1
#!/usr/bin/env python3
2 3 4 5
# -*- coding: utf-8 -*-
# @Author: klmp200
# @Date:   2016-07-03 17:57:28
# @Last Modified by:   klmp200
6
# @Last Modified time: 2016-07-04 17:50:18
7

8
from bottle import Bottle, static_file, request
Sli's avatar
Sli committed
9
import os
10

11 12 13
app = Bottle()
plugin = sqlite.Plugin(dbfile='../data/sqliteDB.db')
app.install(plugin)
14

Sli's avatar
Sli committed
15 16 17
@app.route('/')
def GetApp():
    return static_file("app.apk", root="../data/")
18 19

@app.route('/keys')
20
def GetKeys():
21 22
    return static_file("keys.json", root="../data/")

23

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
@app.route('/validate', method='POST')
def Validate(db):

    try:
        send = request.json
        ticket = db.execute('SELECT * from ticket where verifKey=:key',
                            {"key": send['verif']}).fetchone()

        if (ticket is None):
            message = NewEntry(db, send)
        else:
            message = UpdateEntry(db, send, ticket)

    except:
        message = '<p>Error processing data</p>'

    return message


def NewEntry(db, data):
    avaliable = data['nb'] - data['qt']
    response = {
        "avaliable": avaliable,
        "valid": True
    }

    db.execute('INSERT into ticket(verifKey, avaliablePlaces) values (?, ?)',
               (data['verif'], avaliable))

    return dict(response)


def UpdateEntry(db, data, obj):
    avaliableP = obj['avaliablePlaces'] - data['qt']

    if avaliableP >= 0:
        avaliable = True
    else:
        avaliableP = obj['avaliablePlaces']
        avaliable = False

    response = {
        "avaliable": avaliableP,
        "valid": avaliable
    }

    db.execute('UPDATE ticket SET avaliablePlaces=:av WHERE id=:id',
               {"av": avaliableP, "id": obj['id']})

    return dict(response)

75

Sli's avatar
Sli committed
76
app.run(host='0.0.0.0', port=8080, debug=True)
77

78
app.uninstall(plugin)