-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy patharrow.py
More file actions
62 lines (40 loc) · 1.52 KB
/
Copy patharrow.py
File metadata and controls
62 lines (40 loc) · 1.52 KB
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
from arrow import Arrow
from ..exceptions import ImproperlyConfigured
from .enriched_datetime import ArrowDateTime
from .enriched_datetime.enriched_datetime_type import EnrichedDateTimeType
arrow = None
try:
import arrow
except ImportError:
pass
class ArrowType(EnrichedDateTimeType):
"""
ArrowType provides way of saving Arrow_ objects into database. It
automatically changes Arrow_ objects to datetime objects on the way in and
datetime objects back to Arrow_ objects on the way out (when querying
database). ArrowType needs Arrow_ library installed.
.. _Arrow: https://github.com/arrow-py/arrow
::
from datetime import datetime
from sqlalchemy_utils import ArrowType
import arrow
class Article(Base):
__tablename__ = 'article'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.Unicode(255))
created_at = sa.Column(ArrowType)
article = Article(created_at=arrow.utcnow())
As you may expect all the arrow goodies come available:
::
article.created_at = article.created_at.replace(hours=-1)
article.created_at.humanize()
# 'an hour ago'
"""
cache_ok = True
def __init__(self, *args, **kwargs):
if not arrow:
raise ImproperlyConfigured("'arrow' package is required to use 'ArrowType'")
super().__init__(datetime_processor=ArrowDateTime, *args, **kwargs)
@property
def python_type(self):
return Arrow