Beispiel #1
0
def remove_ta_from_ticket():
    """
    Unassign teaching assistant from ticket.
    """
    json_data = request.get_json()
    if json_data:
        if json_validation.validate_json(json_data, ['taid', 'ticketid']):
            return rp_ticket.remove_ta_from_ticket(json_data)
    return Iresponse.create_response("Invalid request", 400)
Beispiel #2
0
def user_reset_password():
    """
    Function that checks a code, resets it if correct and changes user pass
    """
    json_data = request.get_json()
    if not validate_json(json_data, ["code", "password", "psw_confirmation"]):
        return Iresponse.empty_json_request()

    return rp_user.reset_password(json_data)
Beispiel #3
0
def add_ta_to_ticket():
    """
    Assign teaching assistant to ticket.
    """
    json_data = request.get_json()
    if json_data:
        if json_validation.validate_json(json_data, ['taid', 'ticketid']):
            return rp_ticket.add_ta_to_ticket(json_data)
    return Iresponse.create_response("Invalid request", 400)
Beispiel #4
0
def user_set_reset_code():
    """
    Sets user reset code.
    """
    json_data = request.get_json()
    if not validate_json(json_data, ["email"]):
        return Iresponse.empty_json_request()

    email = json_data["email"]
    return rp_user.set_reset_code(email)
Beispiel #5
0
def labelSelected(label_id):
    """
    Return a boolean for selected labels.
    """
    json_data = request.get_json()

    if not validate_json(json_data, ["user_id"]):
        return Iresponse.create_response("", 404)

    user_id = json_data["user_id"]
    user = User.query.get(user_id)
    label = Label.query.get(label_id)
    return Iresponse.create_response({'bool': label in user.labels}, 200)
Beispiel #6
0
def userid_exists():
    """
    Function that checks if the id of a user already exists.
    """
    json_data = request.get_json()
    if not validate_json(json_data, ["studentid"]):
        return Iresponse.empty_json_request()

    studentid = json_data["studentid"]
    user = User.query.filter_by(id=studentid).first()

    if user is None:
        return Iresponse.create_response({"status": False}, 200)
    return Iresponse.create_response({"status": True}, 200)
Beispiel #7
0
def user_exists():
    """
    Function that checks if a user email already exists.
    """
    json_data = request.get_json()
    if not validate_json(json_data, ["email"]):
        return Iresponse.empty_json_request()

    email = json_data["email"]
    user = User.query.filter_by(email=email).first()

    if user is None:
        return Iresponse.create_response({"status": False}, 200)
    return Iresponse.create_response({"status": True}, 200)
Beispiel #8
0
def register_user():
    '''
    Expects a request with email, name, id and password (and confirmed)
    and enters new user into database.
    '''

    json_data = request.get_json()

    if not validate_json(
            json_data,
        ["email", "name", "studentid", "password", "password_confirmation"]):
        return Iresponse.empty_json_request()

    return rp_user.register_user(json_data)
Beispiel #9
0
def deselectLabel(label_id):
    """
    Remove label selected as teaching assistant.
    """
    json_data = request.get_json()

    if not validate_json(json_data, ["user_id"]):
        return Iresponse.create_response("", 404)

    user_id = json_data["user_id"]
    user = User.query.get(user_id)
    label = Label.query.get(label_id)
    user.labels.remove(label)
    database.db.session.add(user)
    database.db.session.commit()
    return Iresponse.create_response("", 200)
Beispiel #10
0
def selectLabel(label_id):
    """
    Select a label as teaching assistant to get notifications when new tickets
    are added with selected label.
    """
    json_data = request.get_json()

    if not validate_json(json_data, ["user_id"]):
        return Iresponse.create_response("", 404)

    user_id = json_data["user_id"]
    user = User.query.get(user_id)
    label = Label.query.get(label_id)
    user.labels.append(label)
    database.db.session.add(user)
    database.db.session.commit()
    return Iresponse.create_response("", 200)
Beispiel #11
0
def create_ticket():
    """
    Check ticket submission and add to database.
    """
    print(request.headers)
    # Mandatory check to comply with incompatible testing.
    formdata = None
    if request.get_json():
        data = request.get_json()
        formdata = {
            'subject': data['subject'],
            'message': data['message'],
            'courseid': data['courseid'],
            'labelid': data['labelid'],
        }
    else:
        formdata = request.form

    if hasattr(request, 'files'):
        if request.files != '':
            filecount = 0
            file_names = list()
            for file_id in request.files:
                filecount += 1
                if filecount > 5:
                    return Iresponse.create_response("Too many files", 400)
                file = request.files[file_id]

                if not rp_file.save_file(file, file_names):
                    print("invalid file")
                    return Iresponse.create_response("File too large", 400)

    if not json_validation.validate_json(
            formdata, ['message', 'subject', 'courseid', 'labelid']):
        return Iresponse.create_response("Malformed request", 400)

    message = escape(formdata['message'])
    subject = escape(formdata['subject'])
    courseid = escape(formdata['courseid'])
    labelid = escape(formdata['labelid'])
    ticket_data = {
        'message': message,
        'subject': subject,
        'courseid': courseid,
        'labelid': labelid,
        'files': file_names
    }

    if not course_validation.check_course_validity(courseid, labelid):
        for file in file_names:
            rp_file.remove_file(file)
        return Iresponse.create_response("Invalid Course/Label", 400)

    current_identity = get_current_user()

    ticket_data['studentid'] = current_identity.id
    ticket_data['name'] = current_identity.name
    ticket_data['email'] = current_identity.email

    if not json_validation.validate_ticket_data(ticket_data):
        for file in file_names:
            rp_file.remove_file(file)
        return Iresponse.create_response("Invalid ticket data", 400)

    response = rp_ticket.create_request(ticket_data)

    return response