1from ..exceptions import ImproperlyConfigured
2from .enriched_datetime import ArrowDateTime
3from .enriched_datetime.enriched_datetime_type import EnrichedDateTimeType
4
5arrow = None
6try:
7 import arrow
8except ImportError:
9 pass
10
11
12class ArrowType(EnrichedDateTimeType):
13 """
14 ArrowType provides way of saving Arrow_ objects into database. It
15 automatically changes Arrow_ objects to datetime objects on the way in and
16 datetime objects back to Arrow_ objects on the way out (when querying
17 database). ArrowType needs Arrow_ library installed.
18
19 .. _Arrow: https://github.com/arrow-py/arrow
20
21 ::
22
23 from datetime import datetime
24 from sqlalchemy_utils import ArrowType
25 import arrow
26
27
28 class Article(Base):
29 __tablename__ = 'article'
30 id = sa.Column(sa.Integer, primary_key=True)
31 name = sa.Column(sa.Unicode(255))
32 created_at = sa.Column(ArrowType)
33
34
35
36 article = Article(created_at=arrow.utcnow())
37
38
39 As you may expect all the arrow goodies come available:
40
41 ::
42
43
44 article.created_at = article.created_at.replace(hours=-1)
45
46 article.created_at.humanize()
47 # 'an hour ago'
48
49 """
50
51 cache_ok = True
52
53 def __init__(self, *args, **kwargs):
54 if not arrow:
55 raise ImproperlyConfigured("'arrow' package is required to use 'ArrowType'")
56
57 super().__init__(datetime_processor=ArrowDateTime, *args, **kwargs)