Esempio n. 1
0
def create_request(jsonData, ticket_id):
    """
    Function that handles the create request from a ticket.
    """
    user_id = escape(jsonData["studentid"])
    text = escape(jsonData["message"])

    response_body = {}

    if text == '':
        response_body['message'] = "empty message"

    ticket = Ticket.query.get(ticket_id)
    if ticket is None:
        response_body['ticket'] = "No ticket exists with this id"

    if len(response_body) != 0:
        return Iresponse.create_response(response_body, 400)

    try:
        if user_id != ticket.user_id:
            notification = notifications.notify(user_id, ticket, text,
                                                Message.NTFY_TYPE_MESSAGE)
            notification = notification

        # Add experience if its a teaching assistant who is commenting.
        user = User.query.get(user_id)
        course = Course.query.get(ticket.course_id)
        if user in course.ta_courses:
            level_up = levels.add_experience(levels.EXP_FOR_RESPONSE, user_id)
            levels.notify_level_change(user_id, ticket, level_up)
    except Exception as e:
        return Iresponse.create_response(str(e), 400)

    return Iresponse.create_response("", 201)
Esempio n. 2
0
def add_ta_to_ticket(json_data):
    """
    Add a teaching assistant to a ticket.
    """
    ticket = Ticket.query.filter_by(
        id=uuid.UUID(json_data['ticketid'])).first()
    ta = User.query.filter_by(id=json_data['taid']).first()

    if ticket.status_id != TicketStatus.receiving_help:
        ticket.status_id = TicketStatus.receiving_help
        database.db.session.commit()

    # Check if the ta and ticket were found and add if not already there.
    if ticket and ta:
        if ta not in ticket.bound_tas:
            ticket.bound_tas.append(ta)
            level_up = levels.add_experience(levels.EXP_FOR_ASSING, ta.id)
            levels.notify_level_change(ta.id, ticket, level_up)
            return Iresponse.create_response(
                {
                    'ta': ta.serialize,
                    'status': "Receiving help"
                }, 200)
        return Iresponse.create_response({'status': "OK"}, 201)
    return Iresponse.create_response("Failure", 400)
Esempio n. 3
0
def create_request(json_data):
    """
    Function that handles the create request for a ticket.
    """
    name = escape(json_data["name"])
    name = name  # flake8
    studentid = escape(json_data["studentid"])
    email = escape(json_data["email"])
    subject = escape(json_data["subject"])
    message = escape(json_data["message"])
    courseid = escape(json_data["courseid"])
    labelid = escape(json_data["labelid"])
    files = json_data['files']

    response_body = {}

    bound_tas = list()
    label = None

    if labelid != "":
        label = Label.query.get(uuid.UUID(labelid))
        bound_tas = get_label_tas(label, studentid)

    if len(bound_tas) < 1:
        status_id = TicketStatus.unassigned
    else:
        status_id = TicketStatus.waiting_for_help

    ticket = Ticket(id=uuid.uuid4(),
                    user_id=studentid,
                    course_id=courseid,
                    status_id=status_id,
                    title=subject,
                    email=email,
                    label=label,
                    files=files,
                    timestamp=datetime.now())

    for ta in bound_tas:
        ticket.bound_tas.append(ta)
        level_up = levels.add_experience(levels.EXP_FOR_ASSING, ta.id)
        levels.notify_level_change(ta.id, ticket, level_up)

    if not database.addItemSafelyToDB(ticket):
        return Iresponse.internal_server_error()

    try:
        msg = notifications.notify(studentid, ticket, message,
                                   Message.NTFY_TYPE_MESSAGE)
        msg = msg
    except Exception as e:
        raise e
        return Iresponse.create_response(str(e), 400)

    response_body['ticketid'] = ticket.id
    response = Iresponse.create_response(response_body, 201)
    response.headers.add('Location', 'api/ticket/{0}'.format(ticket.id))
    return response
Esempio n. 4
0
def create_request(jsonData):
    """
    Process the request to create a node.
    """
    ticket_id = escape(jsonData["ticket_id"])
    user_id = escape(jsonData["user_id"])
    message = escape(jsonData["text"])

    response_body = {}

    if message == '':
        response_body['message'] = "empty message"

    if ticket_id == '':
        response_body['ticket_id'] = "No ticket id has been supplied"

    if user_id == '':
        response_body['user_id'] = "No user id has been supplieds"

    ticket = Ticket.query.get(ticket_id)
    if ticket is None:
        response_body['ticket'] = "No ticket exists with this id"

    if len(response_body) != 0:
        return Iresponse.create_response(response_body, 400)

    note = Note()
    note.id = uuid.uuid4()
    note.user_id = user_id
    note.ticket_id = ticket_id
    note.text = message
    note.timestamp = datetime.now()

    if not database.addItemSafelyToDB(note):
        return Iresponse.internal_server_error()

    parse_note(message, ticket)
    levels.add_experience(levels.EXP_FOR_NOTE, note.user_id)
    # add header location.
    return Iresponse.create_response(note.serialize, 201)
Esempio n. 5
0
def parse_note(message, ticket):
    """
    Function that parses a note for mentioned users.
    If a user is mentioned we append them to the
    linked tas in the ticket.
    """
    mentions = re.finditer('@[0-9]+', message)
    course = Course.query.get(ticket.course_id)
    if course is None:
        return

    ta_in_course = course.ta_courses
    for mention in mentions:
        user_id = mention.group(0).split('@')[1]

        for ta in ta_in_course:
            if str(ta.id) == user_id:
                if ta in ticket.bound_tas:
                    continue
                ticket.bound_tas.append(ta)
                level_up = levels.add_experience(levels.EXP_FOR_MENTION, ta.id)
                levels.notify_level_change(ta.id, None, level_up)
                database.db.session.commit()