Use pendulum for datetime operations when possible
Currently, we use both Python's built-in datetime library and Pendulum to do datetime operations. For the sake of consistency, we should try to stick to one library for datetimes. We could have used either, but Pendulum has a more ergonomic API, so I decided to go with it when possible. The places where were we didn't / couldn't replace datetime are: - checking instances of datetimes. Pendulum's objects are subclasses of python native datetime objects, so it's still useful to import datetime in those cases of using is_instance() - WTForms date validators expect datetime style string formats -- Pendulum has its own format for formatting/ parsing strings. As such, our custom validator DateRange needs to use datetime.stptime() to account for this format.
This commit is contained in:
@@ -3,7 +3,7 @@ import random
|
||||
import string
|
||||
import factory
|
||||
from uuid import uuid4
|
||||
import datetime
|
||||
import pendulum
|
||||
|
||||
from atst.forms import data
|
||||
from atst.models import *
|
||||
@@ -56,8 +56,8 @@ def _random_date(year_min, year_max, operation):
|
||||
else:
|
||||
inc = random.randrange(year_min, year_max)
|
||||
|
||||
return datetime.date(
|
||||
operation(datetime.date.today().year, inc),
|
||||
return pendulum.date(
|
||||
operation(pendulum.today().year, inc),
|
||||
random.randrange(1, 12),
|
||||
random.randrange(1, 28),
|
||||
)
|
||||
@@ -99,8 +99,7 @@ class UserFactory(Base):
|
||||
citizenship = "United States"
|
||||
designation = "military"
|
||||
date_latest_training = factory.LazyFunction(
|
||||
lambda: datetime.date.today()
|
||||
+ datetime.timedelta(days=-(random.randrange(1, 365)))
|
||||
lambda: pendulum.today().add(days=-(random.randrange(1, 365)))
|
||||
)
|
||||
|
||||
@classmethod
|
||||
@@ -341,7 +340,7 @@ class CLINFactory(Base):
|
||||
|
||||
task_order = factory.SubFactory(TaskOrderFactory)
|
||||
number = factory.LazyFunction(random_clin_number)
|
||||
start_date = datetime.date.today()
|
||||
start_date = pendulum.today()
|
||||
end_date = factory.LazyFunction(random_future_date)
|
||||
total_amount = factory.LazyFunction(lambda *args: random.randint(50000, 999999))
|
||||
obligated_amount = factory.LazyFunction(lambda *args: random.randint(100, 50000))
|
||||
|
Reference in New Issue
Block a user