diff --git a/aurweb/auth/__init__.py b/aurweb/auth/__init__.py index 8e7faed1..c4f433c0 100644 --- a/aurweb/auth/__init__.py +++ b/aurweb/auth/__init__.py @@ -1,6 +1,5 @@ import functools -from datetime import datetime from http import HTTPStatus from typing import Callable @@ -13,7 +12,7 @@ from starlette.requests import HTTPConnection import aurweb.config -from aurweb import db, filters, l10n, util +from aurweb import db, filters, l10n, time, util from aurweb.models import Session, User from aurweb.models.account_type import ACCOUNT_TYPE_ID @@ -110,7 +109,7 @@ class BasicAuthBackend(AuthenticationBackend): "persistent_cookie_timeout") # If no session with sid and a LastUpdateTS now or later exists. - now_ts = int(datetime.utcnow().timestamp()) + now_ts = time.utcnow() record = db.query(Session).filter(Session.SessionID == sid).first() if not record: return unauthenticated diff --git a/aurweb/models/package_base.py b/aurweb/models/package_base.py index 8c88b7b5..37ad63ce 100644 --- a/aurweb/models/package_base.py +++ b/aurweb/models/package_base.py @@ -1,9 +1,7 @@ -from datetime import datetime - from sqlalchemy.exc import IntegrityError from sqlalchemy.orm import backref, relationship -from aurweb import schema +from aurweb import schema, time from aurweb.models.declarative import Base from aurweb.models.user import User as _User @@ -43,7 +41,7 @@ class PackageBase(Base): # If no SubmittedTS/ModifiedTS is provided on creation, set them # here to the current utc timestamp. - now = datetime.utcnow().timestamp() + now = time.utcnow() if not self.SubmittedTS: self.SubmittedTS = now if not self.ModifiedTS: diff --git a/aurweb/models/tu_voteinfo.py b/aurweb/models/tu_voteinfo.py index 64551998..7934a772 100644 --- a/aurweb/models/tu_voteinfo.py +++ b/aurweb/models/tu_voteinfo.py @@ -1,11 +1,9 @@ import typing -from datetime import datetime - from sqlalchemy.exc import IntegrityError from sqlalchemy.orm import backref, relationship -from aurweb import schema +from aurweb import schema, time from aurweb.models.declarative import Base from aurweb.models.user import User as _User @@ -71,7 +69,7 @@ class TUVoteInfo(Base): return attr def is_running(self): - return self.End > int(datetime.utcnow().timestamp()) + return self.End > time.utcnow() def total_votes(self): return self.Yes + self.No + self.Abstain diff --git a/aurweb/models/user.py b/aurweb/models/user.py index fcf4a9b3..871ff209 100644 --- a/aurweb/models/user.py +++ b/aurweb/models/user.py @@ -1,6 +1,5 @@ import hashlib -from datetime import datetime from typing import List, Set import bcrypt @@ -14,7 +13,7 @@ import aurweb.config import aurweb.models.account_type import aurweb.schema -from aurweb import db, logging, schema, util +from aurweb import db, logging, schema, time, util from aurweb.models.account_type import AccountType as _AccountType from aurweb.models.ban import is_banned from aurweb.models.declarative import Base @@ -121,7 +120,7 @@ class User(Base): exc = None for i in range(tries): exc = None - now_ts = datetime.utcnow().timestamp() + now_ts = time.utcnow() try: with db.begin(): self.LastLogin = now_ts diff --git a/aurweb/packages/requests.py b/aurweb/packages/requests.py index 9ee672c7..724249d4 100644 --- a/aurweb/packages/requests.py +++ b/aurweb/packages/requests.py @@ -1,10 +1,9 @@ -from datetime import datetime from typing import List, Optional, Set from fastapi import Request from sqlalchemy import and_, orm -from aurweb import config, db, l10n, util +from aurweb import config, db, l10n, time, util from aurweb.exceptions import InvariantError from aurweb.models import PackageBase, PackageRequest, User from aurweb.models.package_request import ACCEPTED_ID, PENDING_ID, REJECTED_ID @@ -106,7 +105,7 @@ def verify_orphan_request(user: User, pkgbase: PackageBase): PackageRequest.ReqTypeID == ORPHAN_ID) for pkgreq in requests: idle_time = config.getint("options", "request_idle_time") - time_delta = int(datetime.utcnow().timestamp()) - pkgreq.RequestTS + time_delta = time.utcnow() - pkgreq.RequestTS is_due = pkgreq.Status == PENDING_ID and time_delta > idle_time if is_due: # If the requester is the pkgbase maintainer or the @@ -128,7 +127,7 @@ def close_pkgreq(pkgreq: PackageRequest, closer: User, :param target: Optional PackageBase instance to merge into :param status: `pkgreq`.Status value to update to """ - now = int(datetime.utcnow().timestamp()) + now = time.utcnow() pkgreq.Status = status pkgreq.Closer = closer pkgreq.ClosureComment = ( diff --git a/aurweb/ratelimit.py b/aurweb/ratelimit.py index a71cb1cc..659ab6b8 100644 --- a/aurweb/ratelimit.py +++ b/aurweb/ratelimit.py @@ -1,9 +1,7 @@ -from datetime import datetime - from fastapi import Request from redis.client import Pipeline -from aurweb import config, db, logging +from aurweb import config, db, logging, time from aurweb.models import ApiRateLimit from aurweb.redis import redis_connection @@ -12,7 +10,7 @@ logger = logging.get_logger(__name__) def _update_ratelimit_redis(request: Request, pipeline: Pipeline): window_length = config.getint("ratelimit", "window_length") - now = int(datetime.utcnow().timestamp()) + now = time.utcnow() time_to_delete = now - window_length host = request.client.host @@ -37,7 +35,7 @@ def _update_ratelimit_redis(request: Request, pipeline: Pipeline): def _update_ratelimit_db(request: Request): window_length = config.getint("ratelimit", "window_length") - now = int(datetime.utcnow().timestamp()) + now = time.utcnow() time_to_delete = now - window_length records = db.query(ApiRateLimit).filter( diff --git a/aurweb/routers/auth.py b/aurweb/routers/auth.py index 0b68dac3..bae9a599 100644 --- a/aurweb/routers/auth.py +++ b/aurweb/routers/auth.py @@ -1,4 +1,3 @@ -from datetime import datetime from http import HTTPStatus from fastapi import APIRouter, Form, HTTPException, Request @@ -6,7 +5,7 @@ from fastapi.responses import HTMLResponse, RedirectResponse import aurweb.config -from aurweb import cookies, db +from aurweb import cookies, db, time from aurweb.auth import requires_auth, requires_guest from aurweb.l10n import get_translator_for_request from aurweb.models import User @@ -57,8 +56,7 @@ async def login_post(request: Request, login_timeout = aurweb.config.getint("options", "login_timeout") - expires_at = int(datetime.utcnow().timestamp() - + max(cookie_timeout, login_timeout)) + expires_at = int(time.utcnow() + max(cookie_timeout, login_timeout)) response = RedirectResponse(url=next, status_code=HTTPStatus.SEE_OTHER) diff --git a/aurweb/routers/html.py b/aurweb/routers/html.py index 337acce6..74901762 100644 --- a/aurweb/routers/html.py +++ b/aurweb/routers/html.py @@ -3,7 +3,6 @@ decorators in some way; more complex routes should be defined in their own modules and imported here. """ import os -from datetime import datetime from http import HTTPStatus from fastapi import APIRouter, Form, HTTPException, Request, Response @@ -14,7 +13,7 @@ from sqlalchemy import and_, case, or_ import aurweb.config import aurweb.models.package_request -from aurweb import cookies, db, models, util +from aurweb import cookies, db, models, time, util from aurweb.cache import db_count_cache from aurweb.models.account_type import TRUSTED_USER_AND_DEV_ID, TRUSTED_USER_ID from aurweb.models.package_request import PENDING_ID @@ -97,7 +96,7 @@ async def index(request: Request): redis, "trusted_user_count", query, expire=cache_expire) # Current timestamp. - now = int(datetime.utcnow().timestamp()) + now = time.utcnow() seven_days = 86400 * 7 # Seven days worth of seconds. seven_days_ago = now - seven_days diff --git a/aurweb/routers/pkgbase.py b/aurweb/routers/pkgbase.py index 221a9e51..510ca467 100644 --- a/aurweb/routers/pkgbase.py +++ b/aurweb/routers/pkgbase.py @@ -1,11 +1,10 @@ -from datetime import datetime from http import HTTPStatus from fastapi import APIRouter, Form, HTTPException, Query, Request, Response from fastapi.responses import JSONResponse, RedirectResponse from sqlalchemy import and_ -from aurweb import config, db, l10n, logging, templates, util +from aurweb import config, db, l10n, logging, templates, time, util from aurweb.auth import creds, requires_auth from aurweb.exceptions import InvariantError, ValidationError from aurweb.models import PackageBase @@ -146,7 +145,7 @@ async def pkgbase_flag_post(request: Request, name: str, has_cred = request.user.has_credential(creds.PKGBASE_FLAG) if has_cred and not pkgbase.Flagger: - now = int(datetime.utcnow().timestamp()) + now = time.utcnow() with db.begin(): pkgbase.OutOfDateTS = now pkgbase.Flagger = request.user @@ -170,7 +169,7 @@ async def pkgbase_comments_post( # If the provided comment is different than the record's version, # update the db record. - now = int(datetime.utcnow().timestamp()) + now = time.utcnow() with db.begin(): comment = db.create(PackageComment, User=request.user, PackageBase=pkgbase, @@ -268,7 +267,7 @@ async def pkgbase_comment_post( # If the provided comment is different than the record's version, # update the db record. - now = int(datetime.utcnow().timestamp()) + now = time.utcnow() if db_comment.Comments != comment: with db.begin(): db_comment.Comments = comment @@ -316,7 +315,7 @@ async def pkgbase_comment_pin(request: Request, name: str, id: int, status_code=HTTPStatus.UNAUTHORIZED, detail=_("You are not allowed to pin this comment.")) - now = int(datetime.utcnow().timestamp()) + now = time.utcnow() with db.begin(): comment.PinnedTS = now @@ -387,7 +386,7 @@ async def pkgbase_comment_delete(request: Request, name: str, id: int, status_code=HTTPStatus.UNAUTHORIZED, detail=_("You are not allowed to delete this comment.")) - now = int(datetime.utcnow().timestamp()) + now = time.utcnow() with db.begin(): comment.Deleter = request.user comment.DelTS = now @@ -446,7 +445,7 @@ async def pkgbase_vote(request: Request, name: str): ).first() has_cred = request.user.has_credential(creds.PKGBASE_VOTE) if has_cred and not vote: - now = int(datetime.utcnow().timestamp()) + now = time.utcnow() with db.begin(): db.create(PackageVote, User=request.user, @@ -683,7 +682,7 @@ async def pkgbase_request_post(request: Request, name: str, return render_template(request, "pkgbase/request.html", context) # All good. Create a new PackageRequest based on the given type. - now = int(datetime.utcnow().timestamp()) + now = time.utcnow() with db.begin(): pkgreq = db.create(PackageRequest, ReqTypeID=types.get(type), diff --git a/aurweb/routers/requests.py b/aurweb/routers/requests.py index 4c976655..ac69f079 100644 --- a/aurweb/routers/requests.py +++ b/aurweb/routers/requests.py @@ -1,11 +1,10 @@ -from datetime import datetime from http import HTTPStatus from fastapi import APIRouter, Form, Query, Request from fastapi.responses import RedirectResponse from sqlalchemy import case -from aurweb import db, defaults, util +from aurweb import db, defaults, time, util from aurweb.auth import creds, requires_auth from aurweb.models import PackageRequest, User from aurweb.models.package_request import PENDING_ID, REJECTED_ID @@ -78,7 +77,7 @@ async def request_close_post(request: Request, id: int, context = make_context(request, "Close Request") context["pkgreq"] = pkgreq - now = int(datetime.utcnow().timestamp()) + now = time.utcnow() with db.begin(): pkgreq.Closer = request.user pkgreq.ClosureComment = comments diff --git a/aurweb/routers/trusted_user.py b/aurweb/routers/trusted_user.py index 595ee88e..ff1ebfa1 100644 --- a/aurweb/routers/trusted_user.py +++ b/aurweb/routers/trusted_user.py @@ -2,14 +2,13 @@ import html import re import typing -from datetime import datetime from http import HTTPStatus from fastapi import APIRouter, Form, HTTPException, Request from fastapi.responses import RedirectResponse, Response from sqlalchemy import and_, or_ -from aurweb import db, l10n, logging, models +from aurweb import db, l10n, logging, models, time from aurweb.auth import creds, requires_auth from aurweb.models import User from aurweb.models.account_type import TRUSTED_USER_AND_DEV_ID, TRUSTED_USER_ID @@ -52,7 +51,7 @@ async def trusted_user(request: Request, context["pp"] = pp = ITEMS_PER_PAGE context["prev_len"] = MAX_AGENDA_LENGTH - ts = int(datetime.utcnow().timestamp()) + ts = time.utcnow() if current_by not in {"asc", "desc"}: # If a malicious by was given, default to desc. @@ -290,7 +289,7 @@ async def trusted_user_addvote_post(request: Request, # Gather some mapped constants and the current timestamp. duration, quorum = ADDVOTE_SPECIFICS.get(type) - timestamp = int(datetime.utcnow().timestamp()) + timestamp = time.utcnow() # TODO: Review this. Is this even necessary? # Remove