-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
97 lines (69 loc) · 2.43 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from datetime import datetime
from flask import Flask, jsonify, request
app = Flask(__name__)
from service import get_task, get_all_tasks, create_task, update_task_by_id
class APIException(Exception):
status_code = 404
def __init__(self, message, status_code=None, payload=None):
Exception.__init__(self)
self.message = message
if status_code is not None:
self.status_code = status_code
self.payload = payload
def to_dict(self):
rv = dict(self.payload or ())
rv['message'] = self.message
return rv
@app.errorhandler(APIException)
def handle_invalid_usage(error):
response = jsonify(error.to_dict())
response.status_code = error.status_code
return response
@app.route("/tasks", methods=['GET'])
def tasks_list():
tasks = get_all_tasks()
return jsonify(tasks=tasks)
@app.route("/tasks/<int:task_id>", methods=['GET'])
def task_by_id(task_id):
task = get_task(task_id)
if not task:
raise APIException("Task doesn't exist")
return jsonify(task=task)
@app.route("/tasks/<int:task_id>", methods=['PUT'])
def task_update_by_id(task_id):
text = request.args.get('text')
update_task_by_id(task_id, text)
if not task_id:
raise APIException("Task doesn't exist")
return jsonify({'task_id': task_id})
from uuid import uuid4
from collections import namedtuple
TASKS = {}
Task = namedtuple('Task', ['date', 'text'])
def get_parameters_for_task_creation(args):
text = args.get('text')
date = args.get('date')
return text, date
def format_date(date):
try:
date_format = datetime.strptime(date, '%Y-%m-%d %H:%M')
if date_format > datetime.now():
return date_format
except ValueError:
return None
@app.route("/tasks", methods=['POST'])
def task_post():
text, date = get_parameters_for_task_creation(request.args)
if not (text and date):
APIException(status_code=412, message='Text and date should not be empty')
date_format = format_date(date)
if not date_format:
raise APIException(
status_code=412, message='Date should be in the future in format %Y-%m-%d %H:%M')
task_id = str(uuid4())
TASKS[task_id] = Task(date=date_format, text=text)
if task_id:
return jsonify({'task_id': task_id})
raise APIException(status_code=412, message='Date should be in the future')
if __name__ == '__main__':
app.run()