Пример #1
0
def images():

    fields = [i for i in Image.__table__.c._data]

    if request.method == "GET":

        # if not current_user.can(Permission.QUALITATIVE_R):
        #     return jsonify(status="fail", data=[], reason="no permission")

        if not check_args(fields, request.args.keys()):
            return jsonify(status="fail", reason="error args", data=[])

        page = request.args.get('page')
        if page is None:
            page = 1

        page = int(page)

        query = Image.r_query()
        args = std_json(request.args)
        for k, v in args.items():
            if k in fields:
                query = query.filter(getattr(Image, k)==v)
        query = query.order_by(Image.id.desc())

        # pagenation = query.paginate(page, per_page=current_app.config['FLASKY_POSTS_PER_PAGE'],
        #                                             error_out=False)
        return jsonify(status="success", reason="", data=[item.to_json() for item in query],
                       # page={'current':pagenation.pages,'per_page':pagenation.per_page,'total':pagenation.total}
                       )

    if request.method == "POST":

        # if not current_user.can(Permission.QUALITATIVE_W):
        #     return jsonify(status="fail", data=[], reason="no permission")

        form = request.form
        if not check_args(fields, form.keys()):
            return jsonify(status="fail", reason="error form args", data=[])

        c = Image()

        for k, v in form.items():
            if hasattr(c, k):
                setattr(c, k, v)
        c.user_id = current_user.id
        c.show = False

        try:
            db.session.add(c)
            db.session.commit()
        except sqlalchemy.exc.OperationalError as e:
            return jsonify(status="fail", reason=e, data=[])

        return jsonify(status="success", reason="", data=[c.to_json()])
Пример #2
0
def users():

    fields = [i for i in User.__table__.c._data]

    if request.method == "GET":

        # if not current_user.can(Permission.USER_R):
        #     return jsonify(status="fail", data=[], reason="no permission")

        if not check_args(SPECIAL_ARGS+fields, request.args.keys()):
            return jsonify(status="fail", reason="error args", data=[])

        page = request.args.get('page')
        if page is None:
            page = 1

        page = int(page)

        query = User.r_query()

        args = std_json(request.args)

        for k, v in args.items():
            if k in fields:
                query = query.filter(getattr(User, k)==v)

        pagenation = query.paginate(page, per_page=current_app.config['FLASKY_POSTS_PER_PAGE'],
                                                    error_out=False)
        return jsonify(status="success", reason="", data=[item.to_json() for item in pagenation.items],
                       page={'current':pagenation.pages,'per_page':pagenation.per_page,'total':pagenation.total})

    if request.method == "POST":

        # if not current_user.can(Permission.USER_W):
        #     return jsonify(status="fail", data=[], reason="no permission")

        form = request.form
        if not check_args(SPECIAL_ARGS+fields, form.keys()):
            return jsonify(status="fail", reason="error form args", data=[])

        c = User()

        for k, v in form.items():
            if hasattr(c, k):
                setattr(c, k, v)

        c.password = form.get("password")

        try:
            db.session.add(c)
            db.session.commit()
        except sqlalchemy.exc.OperationalError as e:
            return jsonify(status="fail", reason=e, data=[])

        return jsonify(status="success", reason="", data=[c.to_json()])
Пример #3
0
def users_r(q_id):

    c = User.r_query().filter_by(id=q_id).first()
    fields = [i for i in User.__table__.c._data]


    if c is None:
        return jsonify(status="fail", reason="no this id thing", data=[])

    if request.method == "GET":
        #
        # if not current_user.can(Permission.USER_R):
        #     return jsonify(status="fail", data=[], reason="no permission")

        return jsonify(status="success", reason="", data=[c.to_json()])

    if request.method == "PUT":
        form = request.form

        # if not current_user.can(Permission.USER_W):
        #     return jsonify(status="fail", data=[], reason="no permission")

        if not check_args(SPECIAL_ARGS+fields, form.keys()):
            return jsonify(status="fail", reason="error form args", data=[])

        for k, v in form.items():
            if hasattr(c, k):
                setattr(c, k, v)

        c.password = form.get("password")

        try:
            db.session.add(c)
            db.session.commit()
        except sqlalchemy.exc.OperationalError as e:
            return jsonify(status="fail", reason=e, data=[])

        return jsonify(status="success", reason="", data=[c.to_json()])

    if request.method == "DELETE":

        # if not current_user.can(Permission.QUANTIFY_W):
        #     return jsonify(status="fail", data=[], reason="no permission")


        try:
            db.session.delete(c)
            db.session.commit()
        except sqlalchemy.exc.OperationalError as e:
            return jsonify(status="fail", reason=e, data=[])

        return jsonify(status="success", reason="", data=[c.to_json()])
Пример #4
0
def post_simple():

    fields = [i for i in Post.__table__.c._data]

    if request.method == "GET":

        # if not current_user.can(Permission.QUALITATIVE_R):
        #     return jsonify(status="fail", data=[], reason="no permission")

        if not check_args(fields, request.args.keys()):
            return jsonify(status="fail", reason="error args", data=[])

        page = request.args.get('page')
        if page is None:
            page = 1

        page = int(page)

        query = Post.r_query()
        args = std_json(request.args)

        if current_user.username != "Anonymous":
            if current_user.role.name != "Administrator":
                pass
            else:
                args['country_id'] = current_user.country.id

        for k, v in args.items():
            if k in fields:
                query = query.filter(getattr(Post, k)==v)

        query = query.order_by(Post.timestamp.desc()).limit(40)

        pagenation = query.paginate(page, per_page=current_app.config['FLASKY_POSTS_PER_PAGE'],
                                                    error_out=False)
        return jsonify(status="success", reason="", data=[item.to_json_simple() for item in pagenation.items],
                       page={'current':pagenation.pages,'per_page':pagenation.per_page,'total':pagenation.total}
                       )