Exemplo n.º 1
0
            user = User.get_by_id(user_id)
        else:
            user = User.get_by_username(username)

        if not user:
            abort(404)

        # Get the user's tasks
        tasks = Task.query.filter_by(user_id=user.id)

        args = task_collection_parser.parse_args()
        # fancy url argument query filtering!
        if args['complete'] is not None:
            tasks.filter_by(complete=args['complete'])

        return tasks

    @marshal_with(task_fields)
    def post(self, user_id=None, username=None):
        args = task_parser.parse_args()
        # user owns the task
        args['user_id'] = g.user.id
        task = Task.create(**args)
        return task, 201


api.add_resource(TaskResource, '/users/<int:user_id>/tasks/<int:task_id>',
                 '/users/<username>/tasks/<int:task_id>')
api.add_resource(TaskCollectionResource, '/users/<int:user_id>/tasks',
                 '/users/<username>/tasks')
Exemplo n.º 2
0
    @auth.login_required
    @self_only
    @marshal_with(user_fields)
    def post(self, user_id=0, username=None):
        g.user.update(**user_parser.parse_args())
        return g.user

    @auth.login_required
    @self_only
    def delete(self, user_id=0, username=None):
        g.user.delete()
        return 204


class UserCollectionResource(Resource):
    @marshal_with(user_collection_fields)
    @paginate()
    def get(self):
        users = User.query
        return users

    @marshal_with(user_fields)
    def post(self):
        user = User.create(**user_parser.parse_args())
        return user, 201


api.add_resource(UserResource, '/users/<int:user_id>', '/users/<username>')
api.add_resource(UserCollectionResource, '/users')
Exemplo n.º 3
0
            'task_kwargs') else {}
        result = current_task.delay(*task_args, **task_kwargs)
        return {'task_id': result.id}, 200


class TaskResultResource(Resource):
    """
    get celery task status
    """
    def get(self, task_id):
        from demo.celery_app.celeryconfig import CELERY_RESULT_BACKEND
        app = Celery(backend=CELERY_RESULT_BACKEND)
        result = AsyncResult(id=task_id, app=app)
        if result:
            failed = not result.successful() if result.date_done else None
            response_data = {
                'task_id': result.task_id,
                'status': result.status,
                'failed': failed,
                'date_done': result.date_done,
                'result': str(result.result)
            }
            return response_data, 200
        else:
            return errors.not_found(message=f'Task {task_id} not found.')


api.add_resource(TaskResource, '/tasks/funcs/<task_name>/')
api.add_resource(TaskCollectionResource, '/tasks/funcs/')
api.add_resource(TaskResultResource, '/tasks/<task_id>/')
Exemplo n.º 4
0
            user = User.get_by_id(user_id)
        else:
            user = User.get_by_username(username)

        if not user:
            abort(404)

        # Get the user's tasks
        tasks = Task.query.filter_by(user_id=user.id)

        args = task_collection_parser.parse_args()
        # fancy url argument query filtering!
        if args['complete'] is not None:
            tasks.filter_by(complete=args['complete'])

        return tasks

    @marshal_with(task_fields)
    def post(self, user_id=None, username=None):
        args = task_parser.parse_args()
        # user owns the task
        args['user_id'] = g.user.id
        task = Task.create(**args)
        return task, 201


api.add_resource(TaskResource, '/users/<int:user_id>/tasks/<int:task_id>',
                 '/users/<username>/tasks/<int:task_id>')
api.add_resource(TaskCollectionResource, '/users/<int:user_id>/tasks',
                 '/users/<username>/tasks')
Exemplo n.º 5
0
    @auth.login_required
    @self_only
    @marshal_with(user_fields)
    def post(self, user_id=None, username=None):
        g.user.update(**user_parser.parse_args())
        return g.user

    @auth.login_required
    @self_only
    def delete(self, user_id=None, username=None):
        g.user.delete()
        return 204


class UserCollectionResource(Resource):
    @marshal_with(user_collection_fields)
    @paginate()
    def get(self):
        users = User.query
        return users

    @marshal_with(user_fields)
    def post(self):
        user = User.create(**user_parser.parse_args())
        return user, 201


api.add_resource(UserResource, '/users/<int:user_id>', '/users/<username>')
api.add_resource(UserCollectionResource, '/users')
Exemplo n.º 6
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 10/18/19 4:43 PM
# @Author  : dengsc

import logging
from demo.api import api
from flask_restful import Resource
from flask import current_app

logger = logging.getLogger(__name__)


class URLResource(Resource):
    def get(self):
        """
        list all route
        :return:
        """
        url_map = []
        for route in current_app.url_map.iter_rules():
            url_map.append({
                'route': str(route),
                'methods': ','.join(route.methods)
            })
        return {'url_map': sorted(url_map, key=lambda d: d.get('route'))}, 200


api.add_resource(URLResource, '/')