1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# |
3
|
|
|
# Copyright (c) 2016 dotzero <[email protected]> |
4
|
|
|
# |
5
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
6
|
|
|
# of this software and associated documentation files (the "Software"), to deal |
7
|
|
|
# in the Software without restriction, including without limitation the rights |
8
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9
|
|
|
# copies of the Software, and to permit persons to whom the Software is |
10
|
|
|
# furnished to do so, subject to the following conditions: |
11
|
|
|
# |
12
|
|
|
# The above copyright notice and this permission notice shall be included |
13
|
|
|
# in all copies or substantial portions of the Software. |
14
|
|
|
# |
15
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
21
|
|
|
# SOFTWARE. |
22
|
|
|
|
23
|
1 |
|
import json |
24
|
1 |
|
from datetime import datetime |
25
|
|
|
|
26
|
|
|
|
27
|
1 |
|
class TildaBase(object): |
28
|
1 |
|
@staticmethod |
29
|
|
|
def _fromdatestring(string_date): |
30
|
|
|
""" |
31
|
|
|
Args: |
32
|
|
|
string_date (string): |
33
|
|
|
Returns: |
34
|
|
|
datetime.datetime: |
35
|
|
|
""" |
36
|
1 |
|
if not string_date: |
37
|
1 |
|
return None |
38
|
|
|
|
39
|
1 |
|
return datetime.strptime(string_date, '%Y-%m-%d %H:%M:%S') |
40
|
|
|
|
41
|
1 |
|
@staticmethod |
42
|
|
|
def _fromtimestamp(unixtime): |
43
|
|
|
""" |
44
|
|
|
Args: |
45
|
|
|
unixtime (string): |
46
|
|
|
Returns: |
47
|
|
|
datetime.datetime: |
48
|
|
|
""" |
49
|
1 |
|
if not unixtime: |
50
|
1 |
|
return None |
51
|
|
|
|
52
|
1 |
|
return datetime.fromtimestamp(int(unixtime)) |
53
|
|
|
|
54
|
1 |
|
def to_json(self): |
55
|
|
|
""" |
56
|
|
|
Returns: |
57
|
|
|
str: |
58
|
|
|
""" |
59
|
1 |
|
data = dict() |
60
|
|
|
|
61
|
1 |
|
for key, value in self.__dict__.items(): |
62
|
1 |
|
if value: |
63
|
1 |
|
if hasattr(value, 'to_dict'): |
64
|
|
|
data[key] = value.to_dict() |
65
|
1 |
|
elif isinstance(value, datetime): |
66
|
1 |
|
data[key] = value.strftime('%Y-%m-%d %H:%M:%S') |
67
|
|
|
else: |
68
|
1 |
|
data[key] = value |
69
|
|
|
|
70
|
1 |
|
return json.dumps(data) |
71
|
|
|
|
72
|
1 |
|
def to_dict(self): |
73
|
|
|
""" |
74
|
|
|
Returns: |
75
|
|
|
dict: |
76
|
|
|
""" |
77
|
1 |
|
data = dict() |
78
|
|
|
|
79
|
1 |
|
for key, value in self.__dict__.items(): |
80
|
1 |
|
if value: |
81
|
1 |
|
if hasattr(value, 'to_dict'): |
82
|
|
|
data[key] = value.to_dict() |
83
|
|
|
else: |
84
|
1 |
|
data[key] = value |
85
|
|
|
|
86
|
|
|
return data |
87
|
|
|
|