Wrap _addToDb functionality in a try:except block

This commit is contained in:
Fred Boniface 2023-06-04 18:39:06 +01:00
parent e17d03b376
commit 89574d4bbd

View File

@ -141,18 +141,23 @@ def insertSchedule(sch_record):
return document return document
def _insertToDb(data :list, type :str): def _insertToDb(data :list, type :str):
if type == "full": try:
mongo.dropCollection("timetable") if type == "full":
mongo.putTimetable(data) mongo.dropCollection("timetable")
mongo.createSingleIndex("timetable", "headcode") mongo.putTimetable(data)
elif type == "update": mongo.createSingleIndex("timetable", "headcode")
for item in data: elif type == "update":
if item['transactionType'] == "Create": for item in data:
singleList = [item] if item['transactionType'] == "Create":
mongo.putTimetable(singleList) singleList = [item]
elif item['transactionType'] == "Delete": mongo.putTimetable(singleList)
mongo.deleteTimetableData({'trainUid': item['trainUid']}) elif item['transactionType'] == "Delete":
return True #If Successful else False mongo.deleteTimetableData({'trainUid': item['trainUid']}) ## Also need to consider the STP indicator and end date here else I am deleting LTP services when an STP is meant to be deleted.
return True #If Successfuls
except Exception as e:
log.out("timetable._insertToDb: Error inserting timetable data", "ERR")
log.out(f"timteable._insertToDb: {e}")
return False # If error inserting timetable
def _helpParseStops(schedule_segment): def _helpParseStops(schedule_segment):
stops = [] stops = []