-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.py
40 lines (33 loc) · 1.2 KB
/
action.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
import datetime
from errors import *
class Action:
def __init__(self, id, name, description, start_time, days_counter):
self.id = id
self.name = name
self.description = description
self.start_time = start_time
self.days_counter = days_counter
def to_json(self):
return {
'id': self.id,
'name': self.name,
'description': self.description,
'start_time': self.start_time.strftime("%Y-%m-%d"),
'days_counter': self.days_counter
}
@staticmethod
def from_json(json):
if "id" in json:
oid = json["id"]
else:
oid = None
try:
name = json["name"]
description = json.get("description", "")
start_time = datetime.datetime.strptime(json["start_time"], "%Y-%m-%d").date()
days_counter = json["days_counter"]
return Action(oid, name, description, start_time, days_counter)
except ValueError as err:
raise InvalidJson("ValueError: {}".format(err))
except KeyError as err:
raise InvalidJson("KeyError: {}".format(err))