This repository has been archived by the owner on Oct 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
78 lines (58 loc) · 2.08 KB
/
app.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from typing import Dict, Tuple, Union
from flask import Flask, request
from flask_restful import Api, Resource
from photo_dash import config, image, utils
APP = Flask(__name__)
API = Api(APP)
class PhotoDash(Resource):
"""The photo-dash endpoint.
Attributes:
MODULE_PREFIX (str): the prefix for all module names
"""
MODULE_PREFIX = 'photo-dash-'
def put(self) -> Tuple[Dict, int]:
"""Put new data from a request into an image.
Returns:
Tuple[Dict, int]:
Dict: if 201, the resource requested returned; otherwise,
an error code
int: HTTP status codes
201: no errors occurred
401: too many sections were requested
503: request was sent during quiet hours
"""
config.LOGGER.info('Got a request')
try:
if utils.in_quiet_hours():
e = 'Request was sent during quiet hours'
config.LOGGER.info(e)
return {'error': e}, 503
except AttributeError:
pass
r = request.get_json()
module = r['module']
if not module.startswith(self.MODULE_PREFIX):
module = f'{self.MODULE_PREFIX}{module}'
title = r['title']
sections = r['sections']
img = image.DashImage(module, title, sections)
try:
config.LOGGER.info('Attempting image creation')
img.create()
except image.TooManySections as e:
return {'error': e}, 401
return {}, 201
class QuietHours(Resource):
"""Quiet hours for when photo-dash should not receive input."""
def get(self) -> Union[Dict[str, int], None]:
"""Get quiet hours: quiet_start and quiet_end.
Returns:
Dict[str, int]: if quiet hours were set
None: quiet hours weren't set
"""
try:
return config.QUIET_HOURS
except AttributeError:
return None
API.add_resource(PhotoDash, '/')
API.add_resource(QuietHours, '/quiet', '/quiet-hours')