Skip to content

Commit

Permalink
Fix Sonar Issue: don't use datetime object
Browse files Browse the repository at this point in the history
  • Loading branch information
midhunmanoj2024 authored and Rajandeep98 committed Aug 23, 2024
1 parent 7fff110 commit f2f1404
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion api/app/models/bookings/appointments.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def delete_appointments(cls, appointment_ids: list):
def find_expired_drafts(cls):
"""Find all is_draft appointments created over expiration cutoff ago."""
EXPIRATION_CUTOFF = timedelta(minutes=15)
expiry_limit = datetime.utcnow().replace(tzinfo=timezone.utc) - EXPIRATION_CUTOFF
expiry_limit = datetime.now(timezone.utc).replace(tzinfo=timezone.utc) - EXPIRATION_CUTOFF

query = db.session.query(Appointment). \
filter(Appointment.is_draft.is_(True)). \
Expand Down
20 changes: 10 additions & 10 deletions api/app/models/theq/service_req.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from qsystem import db
from app.models.theq import Base, Period, PeriodState
from datetime import datetime
from datetime import datetime, timezone
from app.utilities.snowplow import SnowPlow


Expand Down Expand Up @@ -71,7 +71,7 @@ def invite(self, csr, invite_type, sr_count = 1):
else:
snowplow_event = "invitefromhold"

active_period.time_end = datetime.utcnow()
active_period.time_end = datetime.now(timezone.utc)
# db.session.add(active_period)

period_state_invite = PeriodState.get_state_by_name("Invited")
Expand All @@ -81,7 +81,7 @@ def invite(self, csr, invite_type, sr_count = 1):
csr_id=csr.csr_id,
reception_csr_ind=csr.receptionist_ind,
ps_id=period_state_invite.ps_id,
time_start=datetime.utcnow()
time_start=datetime.now(timezone.utc)
)

self.periods.append(new_period)
Expand All @@ -91,7 +91,7 @@ def invite(self, csr, invite_type, sr_count = 1):
def add_to_queue(self, csr, snowplow_event):

active_period = self.get_active_period()
active_period.time_end = datetime.utcnow()
active_period.time_end = datetime.now(timezone.utc)
#db.session.add(active_period)

period_state_waiting = PeriodState.get_state_by_name("Waiting")
Expand All @@ -101,7 +101,7 @@ def add_to_queue(self, csr, snowplow_event):
csr_id=csr.csr_id,
reception_csr_ind=csr.receptionist_ind,
ps_id=period_state_waiting.ps_id,
time_start=datetime.utcnow()
time_start=datetime.now(timezone.utc)
)
self.periods.append(new_period)

Expand All @@ -117,7 +117,7 @@ def begin_service(self, csr, snowplow_event):
if active_period.ps.ps_name in [self.being_served_const]:
raise TypeError("You cannot begin serving a citizen that is already being served")

active_period.time_end = datetime.utcnow()
active_period.time_end = datetime.now(timezone.utc)
# db.session.add(active_period)

period_state_being_served = PeriodState.get_state_by_name(self.being_served_const)
Expand All @@ -127,7 +127,7 @@ def begin_service(self, csr, snowplow_event):
csr_id=csr.csr_id,
reception_csr_ind=csr.receptionist_ind,
ps_id=period_state_being_served.ps_id,
time_start=datetime.utcnow()
time_start=datetime.now(timezone.utc)
)

self.periods.append(new_period)
Expand All @@ -139,7 +139,7 @@ def begin_service(self, csr, snowplow_event):

def place_on_hold(self, csr):
active_period = self.get_active_period()
active_period.time_end = datetime.utcnow()
active_period.time_end = datetime.now(timezone.utc)
# db.session.add(active_period)

period_state_on_hold = PeriodState.get_state_by_name("On hold")
Expand All @@ -149,7 +149,7 @@ def place_on_hold(self, csr):
csr_id=csr.csr_id,
reception_csr_ind=csr.receptionist_ind,
ps_id=period_state_on_hold.ps_id,
time_start=datetime.utcnow()
time_start=datetime.now(timezone.utc)
)

self.periods.append(new_period)
Expand All @@ -158,7 +158,7 @@ def place_on_hold(self, csr):

def finish_service(self, csr, clear_comments=True):
active_period = self.get_active_period()
active_period.time_end = datetime.utcnow()
active_period.time_end = datetime.now(timezone.utc)
if clear_comments:
self.citizen.citizen_comments = None
# db.session.add(active_period)
10 changes: 5 additions & 5 deletions api/app/resources/bookings/walkin/walkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
See the License for the specific language governing permissions and
limitations under the License.'''
import logging, pytz
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from flask import request, g
from flask_restx import Resource
from qsystem import api, api_call_with_retry, db, socketio, application
Expand Down Expand Up @@ -161,8 +161,8 @@ def get_all_app_from_agenda_panel(self, citizen=False, office=False):
if office:
office_id = office.office_id
if office_id:
past_hour = datetime.utcnow() - timedelta(minutes=15)
future_hour = datetime.utcnow() + timedelta(minutes=15)
past_hour = datetime.now(timezone.utc) - timedelta(minutes=15)
future_hour = datetime.now(timezone.utc) + timedelta(minutes=15)
local_past = pytz.utc.localize(past_hour)
local_future = pytz.utc.localize(future_hour)
# getting agenda panel app
Expand Down Expand Up @@ -304,7 +304,7 @@ def send_sms_reminder(self, citizen, office_obj):
if citizen.reminder_flag == 1:
flag_value = 2
citizen.reminder_flag = flag_value
citizen.notification_sent_time = datetime.utcnow()
citizen.notification_sent_time = datetime.now(timezone.utc)
return citizen


Expand All @@ -323,7 +323,7 @@ def send_email_reminder(self, citizen, office_obj):
if citizen.reminder_flag == 1:
flag_value = 2
citizen.reminder_flag = flag_value
citizen.notification_sent_time = datetime.utcnow()
citizen.notification_sent_time = datetime.now(timezone.utc)
return citizen

@api.route("/smardboard/Q-details/waiting/<string:id>", methods=["GET"])
Expand Down
4 changes: 2 additions & 2 deletions api/app/resources/theq/citizen/citizen_add_to_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
limitations under the License.'''

import logging
from datetime import datetime
from datetime import datetime, timezone
from flask import request
from pprint import pprint
from flask_restx import Resource
Expand Down Expand Up @@ -99,7 +99,7 @@ def post(self, id):
update_table = False
if update_table:
citizen.reminder_flag = 0
citizen.notification_sent_time = datetime.utcnow()
citizen.notification_sent_time = datetime.now(timezone.utc)
except Exception as err:
logging.error('{}'.format(str(err)))
logging.exception(err)
Expand Down
10 changes: 5 additions & 5 deletions api/app/resources/theq/citizen/citizen_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
See the License for the specific language governing permissions and
limitations under the License.'''
import logging
from datetime import datetime
from datetime import datetime, timezone
from flask import request
from flask_restx import Resource
from qsystem import api, api_call_with_retry, db, socketio, my_print
Expand Down Expand Up @@ -75,10 +75,10 @@ def put(self, id):
sms_sent = send_walkin_reminder_sms(citizen, office_obj, request.headers['Authorization'].replace('Bearer ', ''))
if (json_data.get('is_first_reminder', False)) and (sms_sent):
citizen.reminder_flag = 1
citizen.notification_sent_time = datetime.utcnow()
citizen.notification_sent_time = datetime.now(timezone.utc)
if (json_data.get('is_second_reminder', False)) and (sms_sent):
citizen.reminder_flag = 2
citizen.notification_sent_time = datetime.utcnow()
citizen.notification_sent_time = datetime.now(timezone.utc)
if (citizen.notification_email):
# code/function call to send first email notification,
email_sent = False
Expand All @@ -87,10 +87,10 @@ def put(self, id):
send_email(request.headers['Authorization'].replace('Bearer ', ''), *email_sent)
if (json_data.get('is_first_reminder', False)) and email_sent:
citizen.reminder_flag = 1
citizen.notification_sent_time = datetime.utcnow()
citizen.notification_sent_time = datetime.now(timezone.utc)
if (json_data.get('is_second_reminder', False)) and email_sent:
citizen.reminder_flag = 2
citizen.notification_sent_time = datetime.utcnow()
citizen.notification_sent_time = datetime.now(timezone.utc)

except ValidationError as err:
return {'message': err.messages}, 422
Expand Down
4 changes: 2 additions & 2 deletions api/app/resources/theq/citizen/citizen_left.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from app.models.theq import Citizen, CSR, CitizenState, ServiceReq, Period, Service, Office
from app.schemas.theq import CitizenSchema, ServiceReqSchema
from app.models.theq import SRState
from datetime import datetime
from datetime import datetime, timezone
from app.utilities.snowplow import SnowPlow
import os
from app.utilities.auth_util import Role, get_username
Expand Down Expand Up @@ -84,7 +84,7 @@ def post(self, id):

for p in service_request.periods:
if p.time_end is None:
p.time_end = datetime.utcnow()
p.time_end = datetime.now(timezone.utc)

# Make snowplow calls to finish any stopped services
if service_request.sr_id != active_sr:
Expand Down
4 changes: 2 additions & 2 deletions api/app/resources/theq/citizen/citizen_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from marshmallow import ValidationError
from app.schemas.theq import CitizenSchema
from sqlalchemy import exc
from datetime import datetime
from datetime import datetime, timezone
from app.utilities.snowplow import SnowPlow
from app.utilities.auth_util import Role, get_username, has_role
from app.auth.auth import jwt
Expand Down Expand Up @@ -82,7 +82,7 @@ def post(self, citizens_waiting):
json_data = {}
citizen = self.citizen_schema.load(json_data)
citizen.office_id = csr.office_id
citizen.start_time = datetime.utcnow()
citizen.start_time = datetime.now(timezone.utc)
citizen.start_position = citizens_waiting + 1

except ValidationError as err:
Expand Down
4 changes: 2 additions & 2 deletions api/app/resources/theq/service_requests_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
See the License for the specific language governing permissions and
limitations under the License.'''

from datetime import datetime
from datetime import datetime, timezone
from flask import request
from flask_restx import Resource
from marshmallow import ValidationError
Expand Down Expand Up @@ -102,7 +102,7 @@ def post(self, id):
csr_id=csr.csr_id,
reception_csr_ind=csr.receptionist_ind,
ps_id=period_state_being_served.ps_id,
time_start=datetime.utcnow()
time_start=datetime.now(timezone.utc)
)

db.session.add(new_period)
Expand Down
4 changes: 2 additions & 2 deletions api/app/resources/theq/videofiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import re
from os.path import isfile, join
from pathlib import Path
from datetime import datetime
from datetime import datetime, timezone
from app.utilities.auth_util import Role, has_any_role
from app.auth.auth import jwt

Expand Down Expand Up @@ -97,7 +97,7 @@ def get(self):
info = entry.stat()
new_info = {}
new_info['name'] = entry.name
new_info['date'] = datetime.utcfromtimestamp(info.st_mtime).strftime('%Y-%m-%d %I:%H:%M %p')
new_info['date'] = datetime.fromtimestamp(info.st_mtime, tz=timezone.utc).strftime('%Y-%m-%d %I:%H:%M %p')
new_info['size'] = "{:10.3f}".format(info.st_size / 2**20) + "Mb"
newfiles.append(new_info)

Expand Down

0 comments on commit f2f1404

Please sign in to comment.