Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
refactor: Reverts DTZ
Browse files Browse the repository at this point in the history
  • Loading branch information
frgfm committed Dec 6, 2023
1 parent e94ca5c commit c9c0e93
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 17 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ select = [
"ASYNC", # flake8-async
"BLE", # flake8-blind-except
"A", # flake8-builtins
"DTZ", # flake8-datetimez
"ICN", # flake8-import-conventions
"PIE", # flake8-pie
"ARG", # flake8-unused-arguments
Expand Down
10 changes: 3 additions & 7 deletions src/app/api/api_v1/endpoints/guidelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# All rights reserved.
# Copying and/or distributing is strictly prohibited without the express permission of its copyright owner.

from datetime import datetime, timezone
from datetime import datetime
from typing import List, cast

from fastapi import APIRouter, Depends, Path, Security, status
Expand Down Expand Up @@ -72,9 +72,7 @@ async def update_guideline_content(
repos: RepositoryCRUD = Depends(get_repo_crud),
user: User = Security(get_current_user, scopes=[UserScope.USER, UserScope.ADMIN]),
) -> Guideline:
guideline = await guidelines.update(
guideline_id, ContentUpdate(**payload.dict(), updated_at=datetime.now(tz=timezone.utc))
)
guideline = await guidelines.update(guideline_id, ContentUpdate(**payload.dict(), updated_at=datetime.utcnow()))
telemetry_client.capture(user.id, event="guideline-content", properties={"repo_id": guideline.repo_id})
# Check if user is allowed
repo = cast(Repository, await repos.get(guideline.repo_id, strict=True))
Expand All @@ -91,9 +89,7 @@ async def update_guideline_order(
repos: RepositoryCRUD = Depends(get_repo_crud),
user: User = Security(get_current_user, scopes=[UserScope.USER, UserScope.ADMIN]),
) -> Guideline:
guideline = await guidelines.update(
guideline_id, OrderUpdate(order=order_idx, updated_at=datetime.now(tz=timezone.utc))
)
guideline = await guidelines.update(guideline_id, OrderUpdate(order=order_idx, updated_at=datetime.utcnow()))
telemetry_client.capture(user.id, event="guideline-order", properties={"repo_id": guideline.repo_id})
# Check if user is allowed
repo = cast(Repository, await repos.get(guideline.repo_id, strict=True))
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/api_v1/endpoints/repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import logging
from base64 import b64decode
from datetime import datetime, timezone
from datetime import datetime
from typing import List, cast

from fastapi import APIRouter, Depends, HTTPException, Path, Security, status
Expand Down Expand Up @@ -106,7 +106,7 @@ async def reorder_repo_guidelines(
gh_client.check_user_permission(user, repo.full_name, repo.owner_id, payload.github_token, repo.installed_by)
# Update all order
return [
await guidelines.update(guideline_id, OrderUpdate(order=order_idx, updated_at=datetime.now(tz=timezone.utc)))
await guidelines.update(guideline_id, OrderUpdate(order=order_idx, updated_at=datetime.utcnow()))
for order_idx, guideline_id in enumerate(payload.guideline_ids)
]

Expand Down
4 changes: 2 additions & 2 deletions src/app/core/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# All rights reserved.
# Copying and/or distributing is strictly prohibited without the express permission of its copyright owner.

from datetime import datetime, timedelta, timezone
from datetime import datetime, timedelta
from typing import Any, Dict, Optional

from jose import jwt
Expand All @@ -19,7 +19,7 @@
async def create_access_token(content: Dict[str, Any], expires_minutes: Optional[int] = None) -> str:
"""Encode content dict using security algorithm, setting expiration."""
expire_delta = timedelta(minutes=expires_minutes or settings.ACCESS_TOKEN_EXPIRE_MINUTES)
expire = datetime.now(tz=timezone.utc) + expire_delta
expire = datetime.utcnow() + expire_delta
return jwt.encode({**content, "exp": expire}, settings.SECRET_KEY, algorithm=settings.JWT_ENCODING_ALGORITHM)


Expand Down
4 changes: 2 additions & 2 deletions src/app/services/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import json
import logging
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timezone
from datetime import datetime
from enum import Enum
from typing import Any, Dict, List, Union

Expand Down Expand Up @@ -165,7 +165,7 @@ def __init__(
self.temperature = temperature
self.frequency_penalty = frequency_penalty
logger.info(
f"Using OpenAI model: {self.model} (created at {datetime.fromtimestamp(model_card.json()['created'], tz=timezone.utc).isoformat()})",
f"Using OpenAI model: {self.model} (created at {datetime.fromtimestamp(model_card.json()['created']).isoformat()})",
)

@staticmethod
Expand Down
6 changes: 3 additions & 3 deletions src/tests/test_security.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta, timezone
from datetime import datetime, timedelta

import pytest
from jose import jwt
Expand Down Expand Up @@ -37,10 +37,10 @@ async def test_verify_password():
@pytest.mark.asyncio()
async def test_create_access_token(content, expires_minutes, expected_delta):
payload = await create_access_token(content, expires_minutes)
after = datetime.now(tz=timezone.utc)
after = datetime.utcnow()
assert isinstance(payload, str)
decoded_data = jwt.decode(payload, settings.SECRET_KEY)
# Verify data integrity
assert all(v == decoded_data[k] for k, v in content.items())
# Check expiration
assert datetime.fromtimestamp(decoded_data["exp"], tz=timezone.utc) - timedelta(minutes=expected_delta) < after
assert datetime.utcfromtimestamp(decoded_data["exp"]) - timedelta(minutes=expected_delta) < after

0 comments on commit c9c0e93

Please sign in to comment.