def serialize(self): """ Ticket can be unassigned, so teaching assistant id can be None. Transforms the object into a json object. This will be used at the front-end, so dont include sensitive information in the json object. """ if self.ta_id is None: self.ta_id = "null" if self.label_id is None: serialized_label = {} else: serialized_label = self.label.serialize return { 'id': self.id, 'user_id': self.user_id, 'course_id': self.course_id, 'course': self.course.serialize, 'ta_id': self.ta_id, 'email': self.email, 'title': self.title, 'timestamp': self.timestamp, 'status': self.ticket_status.serialize, 'label': serialized_label, 'tas': database.serialize_list(self.bound_tas), 'files': database.serialize_list(self.files) }
def retrieve_user(): """ Returns the user model of the current user. """ current_identity = get_current_user() if current_identity is None: return Iresponse.create_response("", 404) student, ta, supv, usr = {}, {}, {}, {} student = database.serialize_list(current_identity.student_courses) ta = database.serialize_list(current_identity.ta_courses) supv = database.serialize_list(current_identity.supervisor_courses) usr = current_identity.serialize usr['student'] = student usr['ta'] = ta usr['supervisor'] = supv usr['roles'] = [] if len(usr['student']) >= 1: usr['roles'].append('student') if len(usr['ta']) >= 1: usr['roles'].append('ta') if len(usr['supervisor']) >= 1: usr['roles'].append('supervisor') return Iresponse.create_response({'user': usr}, 200)
def retrieve_user_tickets(): """ Returns all the tickets of the user. """ curr_user = get_current_user() tickets = Ticket.query.filter_by(user_id=curr_user.id).all() return Iresponse.create_response(database.serialize_list(tickets), 200)
def email_retrieve_labels(course_id): """ Returns all labels of given course. """ course = Course.query.get(course_id) if course is None: return Iresponse.create_response("", 404) return Iresponse.create_response(database.serialize_list(course.labels), 200)
def get_courses_user_is_ta_in(): """ Retrieve the courses where user is a teaching assistant. """ curr_user = get_current_user() if curr_user is None: return Iresponse.create_response("", 404) courses = curr_user.ta_courses return Iresponse.create_response(database.serialize_list(courses), 200)
def retrieve_all_request(ticket_id): """ Process the request to receive all notes of a certain ticket. """ notes = Note.query.filter_by(ticket_id=ticket_id).all() if len(notes) == 0: return Iresponse.create_response("", 404) return Iresponse.create_response(database.serialize_list(notes), 200)
def get_course_tas(course_id): """ Return all the tas in a course. """ course = Course.query.get(course_id) if course is None: return Iresponse.create_response("", 404) tas = course.ta_courses return Iresponse.create_response(database.serialize_list(tas), 200)
def get_user_ticket_for_course(course_id): """ Function that gets all the tickets of a user in the course with id: <course_id> """ curr_user = get_current_user() tickets = Ticket.query.filter(Ticket.user_id == curr_user.id, Ticket.course_id == course_id).all() return Iresponse.create_response(database.serialize_list(tickets), 200)
def retrieve_active_user_tickets(user_id): """ Gets all the active tickets of a user with id: <user_id> """ current_identity = get_current_user() user_id = current_identity.id tickets = Ticket.query.filter( Ticket.user_id == user_id, Ticket.status_id != TicketStatus.closed).all() return Iresponse.create_response(database.serialize_list(tickets), 200)
def retrieve_course_tickets(curr_course, curr_user): """ Inner fucntion that gets wrapped in a decorator. Retrieves the tickets by course id and returns them if found. If no tickets are found we return a 404. """ tickets = Ticket.query.filter_by(course_id=curr_course.id).all() if tickets is None: return Iresponse.create_response("", 404) return Iresponse.create_response(database.serialize_list(tickets), 200)
def get_course_students(course_id): """ Returns all the students in a course. """ course = Course.query.get(course_id) print(course_id) if course is None: return Iresponse.create_response("", 404) print(course.student_courses) return Iresponse.create_response( database.serialize_list(course.student_courses), 200)
def get_tickets_inner(curr_course, curr_user): """ Function that gets the ticket. This function is decorated. The decorator checks if a jwt is valid and if the user is a teaching assistant in the course. This is an inner function so we have access to the course_id in the decorator. """ ta_tickets = curr_user.ta_tickets tickets = list( filter(lambda ticket: ticket.course_id == curr_course.id, ta_tickets)) return Iresponse.create_response(database.serialize_list(tickets), 200)
def get_all_courses_for_ta(user_id): """ Function that returns all the courses a user is a teaching assistant in. """ user = User.query.get(user_id) if user is None: return Iresponse.create_response("", 404) courses = user.ta_courses if len(courses) == 0: return Iresponse.create_response("", 404) return Iresponse.create_response(database.serialize_list(courses), 200)
def retrieve_tickets(user_id): """ Function that returns all the tickets of a teaching assistant. """ user = User.query.get(user_id) if user is None: return Iresponse.create_response("", 404) tickets = user.ta_tickets if len(tickets) == 0: return Iresponse.create_response("", 404) return Iresponse.create_response(database.serialize_list(tickets), 200)
def get_unassigned_tickets_inner(curr_course, curr_user): """ Inner function, wrapped in a decorator, so we check if the user has the correct rigths, to get the unassigned tickets. """ tickets = Ticket.query.filter_by(course_id=curr_course.id).all() status = TicketStatus.query.filter_by( id=TicketStatus.unassigned).first() unassign_tickets = list( filter(lambda ticket: ticket.status_id == status.id, tickets)) print(unassign_tickets) return Iresponse.create_response( database.serialize_list(unassign_tickets), 200)
def retrieve_all_request(ticket_id, for_user, read=False): """ Function that handle the request that retrieves the messages of a ticket. In order to be able to get the messages, a user needs to have atleast rights for the ticket. """ body = {} ticket = Ticket.query.get(ticket_id) if ticket is None: body['ticket_id'] = "invalid" return Iresponse.create_response(body, 404) msgs = list(ticket.messages) if read: for_user.read_messages(msgs) messages = database.serialize_list(msgs) return Iresponse.create_response(messages, 200)
def retrieve_all_courses(): """ Retrieve all courses from database. """ courses = Course.query.all() return Iresponse.create_response(database.serialize_list(courses), 200)
def get_assigned_tickets(user): tickets = Ticket.query.filter(Ticket.bound_tas.contains(user)).all() return Iresponse.create_response(database.serialize_list(tickets), 200)