Exemplo n.º 1
0
    def on_model_change(self, form, model, is_created):
        db.session.rollback()
        # if end date before start date or end date in the past, flag them invalid
        if current_user.is_authenticated and current_user.has_role(
            [1, 10], attr='rank'):

            data = form.data
            # print(data)
            model_id = None
            if not is_created:
                model_id = model.id

            try:
                # result = {}
                result = QuestionTag.update(tag_id=model_id, **data)
            except CRUDError as e:
                result = e.messages
            except valid_crud_errors as e:
                result = CRUDError(e).messages
            except ValidationError as e:
                result = {'errors': e.messages}
            except Exception as e:
                print(e)
                result = {'errors': {'generic': 'Something went wrong...'}}

            if 'errors' in result:

                raise validators.ValidationError(result['errors'])
            else:
                return True

            super().on_model_change(form, model, is_created)
        else:
            raise validators.ValidationError(
                'You are not authorized to make these changes')
Exemplo n.º 2
0
    def on_model_change(self, form, model, is_created):
        db.session.rollback()
        if current_user.is_authenticated and current_user.has_role(
            [1, 10], attr='rank'):
            data = form.data

            answers = []
            for answer in data['answers']:
                answers.append({'id': answer.id})
            data['answers'] = answers

            shows = []
            for show in data['shows']:
                shows.append({'id': show.id})
            data['shows'] = shows

            question_links = []
            for question_link in form.question_links:
                if not question_link._should_delete:
                    question_link = question_link.data
                    if 'id' in question_link and not question_link['id']:
                        del question_link['id']
                    question_links.append(question_link)
            data['question_links'] = question_links

            answer_links = []
            for answer_link in form.answer_links:
                if not answer_link._should_delete:
                    answer_link = answer_link.data
                    if 'id' in answer_link and not answer_link['id']:
                        del answer_link['id']
                    answer_links.append(answer_link)
            data['answer_links'] = answer_links

            model_id = None
            if not is_created:
                model_id = model.id

            try:
                result = Question.update(question_id=model_id, **data)
            except CRUDError as e:
                result = e.messages
            except valid_crud_errors as e:
                result = CRUDError(e).messages
            except ValidationError as e:
                result = {'errors': e.messages}
            except Exception as e:
                print(e)
                result = {'errors': {'generic': 'Something went wrong...'}}

            if 'errors' in result:
                db.session.rollback()
                raise validators.ValidationError(result['errors'])
            else:
                return True
        else:
            raise validators.ValidationError(
                'You are not authorized to make these changes')
Exemplo n.º 3
0
def delete(user_row, answer_id):
    try:
        result = Answer.delete(answer_id)
    except CRUDError as e:
        result = e.messages
    except valid_crud_errors as e:
        result = CRUDError(e).messages
    except ValidationError as e:
        result = {'errors': e.messages}
    return jsonify(result)
Exemplo n.º 4
0
def delete(user_row, question_tag_id):
    try:
        result = QuestionTag.delete(tag_id=question_tag_id, **data)
    except CRUDError as e:
        result = e.messages
    except valid_crud_errors as e:
        result = CRUDError(e).messages
    except ValidationError as e:
        result = {'errors': e.messages}

    return jsonify(result)
Exemplo n.º 5
0
def read(question_tag_id):
    try:
        result = QuestionTag.read(question_tag_id)
    except CRUDError as e:
        result = e.messages
    except valid_crud_errors as e:
        result = CRUDError(e).messages
    except ValidationError as e:
        result = {'errors': e.messages}

    return jsonify(result)
Exemplo n.º 6
0
def delete_showlist(user_row, user_id):
    try:
        user_row.verify_authorization('verify_self', user_id)
    except CRUDError as e:
        result = e.messages
    except valid_crud_errors as e:
        result = CRUDError(e).messages
    except AuthorizationError as e:
        result = e.messages
    else:
        try:
            result = User.delete_showlist(user_id, user_row=user_row)
        except CRUDError as e:
            result = e.messages
        except valid_crud_errors as e:
            result = CRUDError(e).messages
        except ValidationError as e:
            result = {'errors': e.messages}

    return jsonify(result)
Exemplo n.º 7
0
def read_multiple_showlist():
    data = request.get_json()
    try:
        result = read_showlists(data)
    except CRUDError as e:
        result = e.messages
    except valid_crud_errors as e:
        result = CRUDError(e).messages
    except ValidationError as e:
        result = {'errors': e.messages}

    return jsonify(result)
Exemplo n.º 8
0
def read_showlist(user_id):

    try:
        result = User.read_showlist(user_id)
    except CRUDError as e:
        result = e.messages
    except valid_crud_errors as e:
        result = CRUDError(e).messages
    except ValidationError as e:
        result = {'errors': e.messages}

    return jsonify(result)
Exemplo n.º 9
0
def delete(user_row, question_id):
    try:
        result = Question.delete(question_id)
        print('success')
    except CRUDError as e:
        result = e.messages
    except valid_crud_errors as e:
        result = CRUDError(e).messages
    except ValidationError as e:
        result = {'errors': e.messages}

    return jsonify(result)
Exemplo n.º 10
0
def create(user_row):
    data = request.get_json()

    try:
        result = QuestionTag.create(**data)
    except CRUDError as e:
        result = e.messages
    except valid_crud_errors as e:
        result = CRUDError(e).messages
    except ValidationError as e:
        result = {'errors': e.messages}

    return jsonify(result)
Exemplo n.º 11
0
def update_showlist(user_row, user_id):
    data = request.get_json()

    try:
        user_row.verify_authorization('verify_self', user_id)
    except CRUDError as e:
        result = e.messages
    except valid_crud_errors as e:
        result = CRUDError(e).messages
    except AuthorizationError as e:
        result = e.messages
    else:
        try:
            result = User.update_showlist(user_id, data)
        except CRUDError as e:
            result = e.messages
        except valid_crud_errors as e:
            result = CRUDError(e).messages
        except ValidationError as e:
            result = {'errors': e.messages}

    return jsonify(result)
Exemplo n.º 12
0
def update(user_row, question_id):
    data = request.get_json()

    try:
        result = Question.update(question_id=question_id, **data)
        print('success')
    except CRUDError as e:
        result = e.messages
    except valid_crud_errors as e:
        result = CRUDError(e).messages
    except ValidationError as e:
        result = {'errors': e.messages}

    return jsonify(result)
Exemplo n.º 13
0
def read(question_id):
    print('read question')

    try:
        result = Question.read(question_id)
        print('success')
    except CRUDError as e:
        result = e.messages
    except valid_crud_errors as e:
        result = CRUDError(e).messages
    except ValidationError as e:
        result = {'errors': e.messages}

    return jsonify(result)
Exemplo n.º 14
0
def update(user_row, answer_id):

    data = request.get_json()

    try:
        result = Answer.update(answer_id=answer_id, **data)
    except CRUDError as e:
        result = e.messages
    except valid_crud_errors as e:
        result = CRUDError(e).messages
    except ValidationError as e:
        result = {'errors': e.messages}

    return jsonify(result)
Exemplo n.º 15
0
    def on_model_change(self, form, model, is_created):
        db.session.rollback()
        if current_user.is_authenticated and current_user.has_role(
            [1, 10], attr='rank'):
            # set formdata to dicts
            data = form.data
            # print(data)
            # print('children',data['children'])
            children = []
            for child in data['children']:
                children.append({'id': child.id})
            data['children'] = children

            titles = []
            for title in form.titles:
                if not title._should_delete:
                    title = title.data
                    if 'id' in title and not title['id']:
                        del title['id']
                    titles.append(title)
            data['titles'] = titles

            if 'parent' in data and data['parent']:
                data['parent_id'] = data['parent'].id

            model_id = None
            if not is_created:
                model_id = model.id
            # print(data)
            try:
                result = Show.update(show_id=model_id, **data)
            except CRUDError as e:
                result = e.messages
            except valid_crud_errors as e:
                result = CRUDError(e).messages
            except ValidationError as e:
                result = {'errors': e.messages}
            except Exception as e:
                print(e)
                result = {'errors': {'generic': 'Something went wrong...'}}

            if 'errors' in result:
                db.session.rollback()
                raise validators.ValidationError(result['errors'])
            else:
                return True
        else:
            raise validators.ValidationError(
                'You are not authorized to make these changes')
Exemplo n.º 16
0
def read(user_id):
    # print('reading user')

    try:
        result = User.read(user_id)
    except CRUDError as e:
        result = e.messages
    except valid_crud_errors as e:
        result = CRUDError(e).messages
    except ValidationError as e:
        result = {'errors': e.messages}
    else:
        if 'password' in result:
            del result['password']
        if 'email' in result:
            del result['email']
        if not 'errors' in result:
            result = {'user': result}

    return jsonify(result)
Exemplo n.º 17
0
    def on_model_change(self, form, model, is_created):
        db.session.rollback()
        # if end date before start date or end date in the past, flag them invalid
        if current_user.is_authenticated and current_user.has_role(
                1, attr='rank'):
            # set formdata to dicts
            data = form.data
            # print(data)
            roles = []
            for role in data['roles']:
                roles.append({'id': role.id})
            data['roles'] = roles

            result = None
            try:
                if is_created:
                    result = User.create(**data)
                else:
                    if 'password' in data:
                        del data['password']
                    result = User.update(model.id, **data)
            except CRUDError as e:
                result = e.messages
            except valid_crud_errors as e:
                result = CRUDError(e).messages
            except ValidationError as e:
                result = {'errors': e.messages}
            except Exception as e:
                print(e)
                result = {'errors': {'generic': 'Something went wrong...'}}

            if 'errors' in result:
                db.session.rollback()
                raise validators.ValidationError(result['errors'])
            else:
                return True

        else:
            raise validators.ValidationError(
                'You are not authorized to make these changes')
Exemplo n.º 18
0
    def on_model_change(self, form, model, is_created):
        db.session.rollback()
        if current_user.is_authenticated and current_user.has_role(
            [1, 10], attr='rank'):
            data = form.data

            children = []
            for child in form.children:
                if not child._should_delete:
                    child = child.data
                    if 'id' in child and not child['id']:
                        del child['id']
                    children.append(child)
            data['children'] = children

            model_id = None
            if not is_created:
                model_id = model.id

            try:
                result = Answer.update(answer_id=model_id, **data)
            except CRUDError as e:
                result = e.messages
            except valid_crud_errors as e:
                result = CRUDError(e).messages
            except ValidationError as e:
                result = {'errors': e.messages}
            except Exception as e:
                print(e)
                result = {'errors': {'generic': 'Something went wrong...'}}

            if 'errors' in result:
                raise validators.ValidationError(result['errors'])
            else:
                return True
        else:
            raise validators.ValidationError(
                'You are not authorized to make these changes')