1818from app .model import Message
1919from app .model import ProblemType
2020from app .model import Course
21+ from app .model import Section
2122from app .model import User
2223
2324from datetime import datetime
@@ -139,11 +140,23 @@ def edit_ticket():
139140 ticketID = request .form .get ("ticketIDModal" )
140141 ticket = Ticket .query .get (ticketID )
141142
142- if ticket is not None :
143- _attempt_edit_ticket (ticket )
143+ try :
144+ if ticket is not None :
145+ _attempt_edit_ticket (ticket )
146+
147+ else :
148+ flash ('Could not edit ticket. Ticket not found in database.' , category = 'error' )
149+
150+ except IntegrityError :
151+ db .session .rollback ()
152+ flash ('Could not updated ticket, invalid data' , category = 'error' )
153+
154+ except Exception as e :
155+ flash ('Could not updated ticket, unknown reason' , category = 'error' )
156+ print (f'Failed to updated ticket: { e } ' , file = sys .stderr )
144157
145158 else :
146- flash ('Could not edit ticket. Ticket not found in database. ' , category = 'error ' )
159+ flash ('Ticket updated successfully! ' , category = 'success ' )
147160
148161 return redirect (url_for ('views.view_tickets' ))
149162
@@ -222,12 +235,15 @@ def _attempt_create_ticket(form: ImmutableMultiDict):
222235 email = strip_or_none (form .get ("email" ))
223236 name = strip_or_none (form .get ("fullname" ))
224237
225- course = strip_or_none (form .get ("course" ))
226- section = strip_or_none (form .get ("section" ))
238+ course_id = strip_or_none (form .get ("course" ))
239+ section_id = strip_or_none (form .get ("section" ))
227240 assignment = strip_or_none (form .get ("assignment" ))
228241 question = strip_or_none (form .get ("question" ))
229242 problem_id = strip_or_none (form .get ("problem" ))
243+
230244 problem : ProblemType = ProblemType .query .get (problem_id )
245+ course : Course = Course .query .get (course_id )
246+ section : Section = Section .query .get (section_id )
231247
232248 if str_empty (email ):
233249 flash ('Could not submit ticket, email must not be empty!' , category = 'error' )
@@ -244,8 +260,11 @@ def _attempt_create_ticket(form: ImmutableMultiDict):
244260 elif problem_id is not None and not problem :
245261 flash ('Could not submit ticket, problem type is not valid!' , category = 'error' )
246262
247- # TODO: Check if course is a valid from a list of options
248- # TODO: Check if section is valid from a list of options
263+ elif course_id is not None and not course :
264+ flash ('Could not submit ticket, course is not valid!' , category = 'error' )
265+
266+ elif (section_id is not None and not section ) or (course and section not in course .sections ):
267+ flash ('Could not submit ticket, section is not valid!' , category = 'error' )
249268
250269 else :
251270 mode_val = strip_or_none (form .get ("mode" ))
@@ -259,34 +278,43 @@ def _attempt_create_ticket(form: ImmutableMultiDict):
259278 flash ('Could not submit ticket, must select a valid mode!' , category = 'error' )
260279
261280 else :
262- return Ticket (email , name , course , section , assignment , question , problem_id , mode )
281+ return Ticket (email , name , course_id , section_id , assignment , question , problem_id , mode )
263282
264283def _attempt_edit_ticket (ticket : Ticket ):
265- # get info back from popup modal form
266- course = strip_or_none (request .form .get ("courseField" ))
267- section = strip_or_none (request .form .get ("sectionField" ))
284+ # TODO: Need to produce error messages!!
285+
286+ course_id = strip_or_none (request .form .get ("courseField" ))
287+ section_id = strip_or_none (request .form .get ("sectionField" ))
268288 assignment = strip_or_none (request .form .get ("assignmentNameField" ))
269289 question = strip_or_none (request .form .get ("specificQuestionField" ))
270290 primaryTutor = strip_or_none (request .form .get ("primaryTutorInput" ))
271291 tutorNotes = strip_or_none (request .form .get ("tutorNotes" ))
272292 wasSuccessful = strip_or_none (request .form .get ("successfulSession" ))
273293 problem_id = strip_or_none (request .form .get ("problemTypeField" ))
294+
274295 problem : ProblemType = ProblemType .query .get (problem_id )
296+ new_course : Course = Course .query .get (course_id )
297+ section : Section = Section .query .get (section_id )
298+
299+ # Get the course of the current section
300+ current_course : Course = Course .query .get (ticket .course )
275301
276302 # check for change in values from edit
277- if course :
303+ if course_id and new_course :
278304 # new info for course came back, update it for current ticket
279- ticket .course = course
305+ ticket .course = new_course .id
306+ ticket .section = new_course .sections [0 ].id if len (new_course .sections ) > 0 else None
307+ current_course = new_course
280308
281- if section :
309+ if section_id and section and section in current_course . sections :
282310 # new info for section came back, update it for current ticket
283- ticket .section = section
311+ ticket .section = section . id
284312
285- if assignment != ticket .assignment_name :
313+ if assignment and assignment != ticket .assignment_name :
286314 # new info for assignment came back, update it for current ticket
287315 ticket .assignment_name = assignment
288316
289- if question != ticket .specific_question :
317+ if question and question != ticket .specific_question :
290318 # new info for question came back, update it for current ticket
291319 ticket .specific_question = question
292320
0 commit comments