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:
graham-dds
2020-02-07 10:38:59 -05:00
parent 4afdc62329
commit 108f65f928
30 changed files with 108 additions and 126 deletions

View File

@@ -3,11 +3,11 @@ from urllib.parse import urlparse
import pytest
from datetime import datetime
import pendulum
from flask import session, url_for
from cryptography.hazmat.primitives.serialization import Encoding
from atst.domain.users import Users
from atst.domain.permission_sets import PermissionSets
from atst.domain.exceptions import NotFoundError
from atst.domain.authnid.crl import CRLInvalidException
from atst.domain.auth import UNPROTECTED_ROUTES
@@ -262,7 +262,7 @@ def test_error_on_invalid_crl(client, monkeypatch):
def test_last_login_set_when_user_logs_in(client, monkeypatch):
last_login = datetime.now()
last_login = pendulum.now(tz="utc")
user = UserFactory.create(last_login=last_login)
monkeypatch.setattr(
"atst.domain.authnid.AuthenticationContext.authenticate", lambda *args: True
@@ -270,7 +270,7 @@ def test_last_login_set_when_user_logs_in(client, monkeypatch):
monkeypatch.setattr(
"atst.domain.authnid.AuthenticationContext.get_user", lambda *args: user
)
response = _login(client)
_login(client)
assert session["last_login"]
assert user.last_login > session["last_login"]
assert isinstance(session["last_login"], datetime)