forked from moppermonster/kiosky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
64 lines (55 loc) · 1.92 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
'''
server
'''
from flask import Flask, request, jsonify, make_response, send_from_directory, redirect
import utils
from page import Channels
CHANNELS = Channels()
APP = Flask(__name__)
@APP.route('/favicon.ico')
def favicon():
'''Return favicon.ico'''
return send_from_directory('/static', 'favicon.ico', mimetype='image/vnd.microsoft.icon')
@APP.route('/', methods=['GET'])
def index():
'''root home page'''
return utils.home(CHANNELS.channels)
@APP.route('/channels/<channel>')
def channels(channel):
'''channels'''
if not channel in CHANNELS.channels:
return f'Unknown channel: {channel}'
CHANNELS.position_update(channel)
return utils.channel_page(channel, CHANNELS.channels)
@APP.route('/config/<channel>', methods=['GET', 'POST'])
def config(channel):
'''config'''
if request.method == 'GET':
if not channel in CHANNELS.channels:
return f'Unable to display config: Unknown channel: {channel}'
return utils.config_page(channel, CHANNELS.channels)
elif request.method == 'POST':
name = request.form['channel-name']
time = request.form['channel-time']
pages_string = request.form['channel-pages']
pages = pages_string.split(',')
CHANNELS.remove(name)
CHANNELS.add(name, pages, time)
return redirect('/')
@APP.route('/new', methods=['GET', 'POST'])
def new():
'''new channel'''
if request.method == 'POST':
channel = request.form['channel-name']
if channel in CHANNELS.channels:
return f'Unable to make new channel: name {channel} already exists'
CHANNELS.add(channel, [], 60)
return redirect('/')
elif request.method == 'GET':
return utils.make_new()
@APP.errorhandler(404)
def not_found(error):
'''Return a generic 404 message'''
_path = request.full_path
_ip = request.remote_addr
return make_response(jsonify({'error': 'Not found'}), 404)