From af73b3774202eb83b517d8cfb58b6cebd8b8c821 Mon Sep 17 00:00:00 2001 From: Alejandro Arroyo <41422777+arroyoAle@users.noreply.github.com> Date: Fri, 13 Dec 2024 09:59:52 +0000 Subject: [PATCH] RAS-1378 Fix datetime deprecation warning on party service (#448) * updating datetime.utcnnow to datetime.now(utc) * auto patch increment * making sqlalchemy get the time rather than datetime * adding CODEOWNERS and removing tz info from created on columns --------- Co-authored-by: ras-rm-pr-bot --- .github/CODEOWNERS | 1 + _infra/helm/party/Chart.yaml | 4 ++-- .../controllers/pending_survey_controller.py | 6 +++--- ras_party/models/models.py | 18 +++++++++--------- 4 files changed, 15 insertions(+), 14 deletions(-) create mode 100644 .github/CODEOWNERS diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 00000000..abe8a906 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +* @ONSdigital/sdc-rmras \ No newline at end of file diff --git a/_infra/helm/party/Chart.yaml b/_infra/helm/party/Chart.yaml index b3998d30..b62cf0b8 100644 --- a/_infra/helm/party/Chart.yaml +++ b/_infra/helm/party/Chart.yaml @@ -14,8 +14,8 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. -version: 2.5.12 +version: 2.5.13 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. -appVersion: 2.5.12 +appVersion: 2.5.13 diff --git a/ras_party/controllers/pending_survey_controller.py b/ras_party/controllers/pending_survey_controller.py index 6fa6c794..185cd7ae 100644 --- a/ras_party/controllers/pending_survey_controller.py +++ b/ras_party/controllers/pending_survey_controller.py @@ -1,6 +1,6 @@ import logging import uuid -from datetime import datetime, timedelta +from datetime import UTC, datetime, timedelta from urllib.error import HTTPError import structlog @@ -129,7 +129,7 @@ def delete_pending_surveys(session): Deletes all the existing pending surveys which have expired :param session A db session """ - _expired_hrs = datetime.utcnow() - timedelta(seconds=float(current_app.config["EMAIL_TOKEN_EXPIRY"])) + _expired_hrs = datetime.now(UTC) - timedelta(seconds=float(current_app.config["EMAIL_TOKEN_EXPIRY"])) pending_shares = session.query(PendingSurveys).filter(PendingSurveys.time_shared < _expired_hrs) pending_shares.delete() logger.info("Deletion complete") @@ -143,7 +143,7 @@ def get_unique_pending_surveys(is_transfer, session): :type is_transfer: bool :param session A db session """ - _expired_hrs = datetime.utcnow() - timedelta(seconds=float(current_app.config["EMAIL_TOKEN_EXPIRY"])) + _expired_hrs = datetime.now(UTC) - timedelta(seconds=float(current_app.config["EMAIL_TOKEN_EXPIRY"])) pending_shares_ready_for_deletion = ( session.query(PendingSurveys) .filter(PendingSurveys.time_shared < _expired_hrs) diff --git a/ras_party/models/models.py b/ras_party/models/models.py index 1430d6bd..bbae2939 100644 --- a/ras_party/models/models.py +++ b/ras_party/models/models.py @@ -1,4 +1,3 @@ -import datetime import enum import logging import uuid @@ -18,6 +17,7 @@ ) from sqlalchemy.dialects.postgresql import JSONB, UUID from sqlalchemy.orm import declarative_base, relationship +from sqlalchemy.sql import func from sqlalchemy.types import Enum from werkzeug.exceptions import BadRequest @@ -39,7 +39,7 @@ class Business(Base): attributes = relationship( "BusinessAttributes", backref="business", order_by="desc(BusinessAttributes.created_on)", lazy="joined" ) - created_on = Column(DateTime, default=datetime.datetime.utcnow) + created_on = Column(DateTime, default=func.now()) @staticmethod def validate(json_packet, schema): @@ -188,7 +188,7 @@ class BusinessAttributes(Base): sample_summary_id = Column(Text) collection_exercise = Column(Text) attributes = Column(JSONB) - created_on = Column(DateTime, default=datetime.datetime.utcnow) + created_on = Column(DateTime, default=func.now()) name = Column(Text) # New columns placed at end of list in case code uses positional rather than named references trading_as = Column(Text) Index("attributes_name_idx", name) @@ -232,9 +232,9 @@ class BusinessRespondent(Base): business_id = Column(UUID, ForeignKey("business.party_uuid"), primary_key=True) respondent_id = Column(Integer, ForeignKey("respondent.id"), primary_key=True) status = Column("status", Enum(BusinessRespondentStatus), default=BusinessRespondentStatus.ACTIVE) - effective_from = Column(DateTime, default=datetime.datetime.utcnow) + effective_from = Column(DateTime, default=func.now()) effective_to = Column(DateTime) - created_on = Column(DateTime, default=datetime.datetime.utcnow) + created_on = Column(DateTime, default=func.now()) business = relationship("Business", back_populates="respondents", lazy="joined") respondent = relationship("Respondent", back_populates="businesses", lazy="joined") @@ -257,7 +257,7 @@ class PendingEnrolment(Base): business_id = Column(UUID) survey_id = Column(UUID) - created_on = Column(DateTime, default=datetime.datetime.utcnow) + created_on = Column(DateTime, default=func.now()) respondent = relationship("Respondent") Index("pending_enrolment_case_idx", case_id) @@ -279,7 +279,7 @@ class Respondent(Base): last_name = Column(Text) telephone = Column(Text) mark_for_deletion = Column(Boolean, default=False) - created_on = Column(DateTime, default=datetime.datetime.utcnow) + created_on = Column(DateTime, default=func.now()) password_verification_token = Column(Text) password_reset_counter = Column(Integer, default=0) pending_enrolment = relationship("PendingEnrolment", back_populates="respondent") @@ -360,7 +360,7 @@ class Enrolment(Base): respondent_id = Column(Integer, primary_key=True) survey_id = Column(Text, primary_key=True) status = Column("status", Enum(EnrolmentStatus), default=EnrolmentStatus.PENDING) - created_on = Column(DateTime, default=datetime.datetime.utcnow) + created_on = Column(DateTime, default=func.now()) business_respondent = relationship("BusinessRespondent", back_populates="enrolment", lazy="joined") Index("enrolment_business_idx", business_id) @@ -387,7 +387,7 @@ class PendingSurveys(Base): email_address = Column(Text, primary_key=True) business_id = Column(UUID, primary_key=True) survey_id = Column(Text, primary_key=True) - time_shared = Column(DateTime, default=datetime.datetime.utcnow) + time_shared = Column(DateTime, default=func.now()) shared_by = Column(UUID) batch_no = Column(UUID, default=uuid.uuid4) is_transfer = Column(Boolean, default=False)