Пример #1
0
import logging
import json
from flask import request, jsonify
from flask_jwt_extended import create_access_token
from flask_restplus import Resource
from rest_api.api.billing.serializers import user
from rest_api.api.restplus import api

log = logging.getLogger(__name__)

ns = api.namespace('users', description='Operations related to users')


@ns.route('/login')
class LoginCollection(Resource):
    @api.response(400, 'Missing JSON request or parameter(s)')
    @api.response(401, 'Bad Username or Password')
    @api.expect(user)
    def post(self):
        if not request.is_json:
            return {'error': "Missing JSON in request"}

        username = request.json.get('username', None)
        password = request.json.get('password', None)
        if not username:
            return {'error': "Please provide username"}, 400
        if not password:
            return {'error': "Please provide password"}, 400

        if username != 'test' or password != 'test':
            return {'error': "Bad username and/or password"}, 401
Пример #2
0
from flask import request, abort, jsonify, make_response, session
from flask_restplus import Resource
from rest_api.api.restplus import api
from rest_api.database.models import Client

from flask_jwt_extended import (
    JWTManager, jwt_required, create_access_token,
    jwt_refresh_token_required, create_refresh_token,
    get_jwt_identity
)

ns = api.namespace("token", description="API token untuk client internal dan penerbit")

@ns.route('/claim')
class TokenClaim(Resource):

    @api.header("reset", "Isi yes untuk melakukan re-authenticate basic auth.")
    def get(self):
        auth = request.authorization

        if (request.headers.get("reset") == "yes" and not session.get("reset")):
            session["reset"] = "yes"
            return make_response('Autentikasi ulang tidak berhasil', 401, {'WWW-Authenticate' : 'Basic realm="Login required!"'})

        if not auth or not auth.username or not auth.password:
            return make_response('Tidak dapat diverifikasi', 401, {'WWW-Authenticate' : 'Basic realm="Login required!"'})


        client = Client.query.filter(Client.username == auth.username).first()
        if not client:
            session.clear()
Пример #3
0
from flask import request, abort
from flask_restplus import Resource
from rest_api.api.penerbit.serializers import book_response
from rest_api.api.restplus import api
from rest_api.database.models import Buku

ns = api.namespace('public/buku', description='API buku untuk public')


@ns.route('/')
class BukuCollection(Resource):
    @api.marshal_list_with(book_response)
    def get(self):
        """
        Menampilkan daftar buku
        """

        books = Buku.query.filter(Buku.status == "show").all()
        return books


@ns.route('/<int:id>')
class BukuItem(Resource):
    @api.marshal_with(book_response)
    def get(self, id):
        """
        Menampilkan buku berdasarkan id
        """
        buku = Buku.query.filter(Buku.id == id and Buku.status == "show").one()

        return buku
Пример #4
0
import logging
import json

from flask import request
from flask_restplus import Resource
from rest_api.api.billing.business import create_customer, update_customer, delete_customer
from rest_api.api.billing.serializers import customer
from rest_api.api.restplus import api
from rest_api.database.models import Customer

log = logging.getLogger(__name__)

ns = api.namespace('billing/customer',
                   description='Operations related to customers')


@ns.route('/')
class CustomerCollection(Resource):
    @api.marshal_list_with(customer)
    def get(self):
        """
        Returns list of customers
        """
        customers = Customer.query.all()
        return customers

    @api.response(201, 'Customer successfully created.')
    @api.expect(customer)
    def post(self):
        """
        Creates a new customer.
Пример #5
0
import logging
import traceback

from flask import request
from flask_restplus import Resource
from datetime import date

from sqlalchemy.orm.exc import NoResultFound

from rest_api.api.serializers import book, book_with_authors
from rest_api.api.restplus import api
from rest_api.database.models import Book, Author, db

log = logging.getLogger(__name__)

ns = api.namespace('books', description='Operations related to books')


@ns.route('/')
class BooksCollection(Resource):
    @api.marshal_list_with(book)
    def get(self):
        """
        Returns list of books.
        """
        books = Book.query.all()
        return books

    @api.expect(book)
    def post(self):
        """
Пример #6
0
import logging

from flask import request
from flask_restplus import Resource
from rest_api.api.blog.business import create_category, delete_category, update_category
from rest_api.api.blog.serializers import category, category_with_posts
from rest_api.api.restplus import api
from rest_api.database.models import NGO, NGO_Branch

log = logging.getLogger(__name__)

ns = api.namespace('beyondGiving/ngo',
                   description='Operations related to ngo Creation')


@ns.route('/')
class NGOCollection(Resource):
    @api.marshal_list_with(category)
    def get(self):
        ngos = NGO.query.all()
        return ngos

    @api.response(201, 'Category successfully created.')
    @api.expect(category)
    def post(self):
        """
        Creates a new NGO Instance
        """
        data = request.json
        create_category(data)
        return None, 201
import logging
import traceback

from flask import request
from flask_restplus import Resource
from rest_api.api.serializers import author, author_with_books
from rest_api.api.restplus import api
from rest_api.database.models import Author
from rest_api.database import db
from sqlalchemy.orm.exc import NoResultFound

log = logging.getLogger(__name__)

ns = api.namespace('authors', description='Operations related to authors')


@ns.route('/')
class AuthorCollection(Resource):
    @api.marshal_list_with(author)
    def get(self):
        """
        Returns list of authors.
        """
        categories = Author.query.all()
        return categories

    @api.response(201, 'Author successfully created.')
    @api.expect(author)
    def post(self):
        """
        Creates a new blog category.
Пример #8
0
import logging
import json
from flask import request
from flask_jwt_extended import jwt_required
from flask_restplus import Resource
from rest_api.api.billing.business import create_contract, update_contract, delete_contract
from rest_api.api.billing.serializers import contract
from rest_api.api.restplus import api
from rest_api.database.models import Contract

log = logging.getLogger(__name__)

ns = api.namespace('billing/contract',
                   description='Operations related to contracts')


@ns.route('/')
class ContractCollection(Resource):
    @api.marshal_list_with(contract)
    #@jwt_required
    #@api.doc(security='Bearer')
    def get(self):
        """
        Returns list of contracts
        """
        contracts = Contract.query.all()
        return contracts

    @api.response(201, 'Contract successfully created.')
    @api.expect(contract)
    def post(self):
Пример #9
0
from flask import request, abort
from flask_restplus import Resource
from rest_api.api.internal.business import create_buku, update_buku, delete_buku
from rest_api.api.internal.serializers import book_expect, book_response
from rest_api.api.restplus import api
from rest_api.database.models import Buku, Client

from flask_jwt_extended import (jwt_required, create_access_token,
                                jwt_refresh_token_required,
                                create_refresh_token, get_jwt_identity)

ns = api.namespace('internal/buku', description='API buku untuk internal')


@ns.route('/')
@api.header('Authorization',
            "JWT Authorization. Input: 'Bearer your-token'",
            required=True)
class BukuCollection(Resource):
    @api.marshal_list_with(book_response)
    @jwt_required
    def get(self):
        """
        Menampilkan daftar buku
        """

        account = get_jwt_identity()
        if (account.get("type") != "internal"):
            abort(401, "Autentikasi tidak sesuai")

        books = Buku.query.filter().all()
Пример #10
0
import logging

from flask import request
from flask_restplus import Resource
from rest_api.api.blog.business import create_category, delete_category, update_category
from rest_api.api.blog.serializers import category, category_with_posts
from rest_api.api.restplus import api
from rest_api.database.models import Category

log = logging.getLogger(__name__)

ns = api.namespace('blog/categories',
                   description='Operations related to blog categories')


@ns.route('/')
class CategoryCollection(Resource):
    @api.marshal_list_with(category)
    def get(self):
        """
        Returns list of blog categories.
        """
        categories = Category.query.all()
        return categories

    @api.response(201, 'Category successfully created.')
    @api.expect(category)
    def post(self):
        """
        Creates a new blog category.
        """
Пример #11
0
from flask import request, abort
from flask_restplus import Resource
from rest_api.api.penerbit.business import create_buku, update_buku, delete_buku
from rest_api.api.penerbit.serializers import book_expect, book_response
from rest_api.api.restplus import api
from rest_api.database.models import Buku

from flask_jwt_extended import (jwt_required, create_access_token,
                                jwt_refresh_token_required,
                                create_refresh_token, get_jwt_identity)

ns = api.namespace('penerbit/buku', description='API buku untuk penerbit')


@ns.route('/')
@api.header('Authorization',
            "JWT Authorization. Input: 'Bearer your-token'",
            required=True)
class BukuCollection(Resource):
    @api.marshal_list_with(book_response)
    @jwt_required
    def get(self):
        """
        Menampilkan daftar buku
        """

        account = get_jwt_identity()
        if (account.get("type") != "penerbit"):
            abort(401, "Autentikasi tidak sesuai")

        books = Buku.query.filter(Buku.client_id == account.get("id")).all()
Пример #12
0
from flask import request, abort
from flask_restplus import Resource
from rest_api.api.internal.business import create_client, update_client, delete_client
from rest_api.api.internal.serializers import client_response, client_expect
from rest_api.api.restplus import api
from rest_api.database.models import Buku, Client

ns = api.namespace('internal/client', description='API client untuk internal')

@ns.route('/')
class ClientCollection(Resource):

    @api.marshal_list_with(client_response)
    def get(self):
        """
        Menampilkan daftar client
        """
        clients = Client.query.all()
        return clients

    @api.response(201, 'Client berhasil dibuat')
    @api.expect(client_expect)
    def post(self):
        """
        Menambah client baru
        """

        data = request.json
        create_client(data)

        return None, 201
Пример #13
0
import logging

from flask import request
from flask_restplus import Resource
from rest_api.api.blog.business import create_blog_post, update_post, delete_post
from rest_api.api.blog.serializers import blog_post, page_of_blog_posts
from rest_api.api.blog.parsers import pagination_arguments
from rest_api.api.restplus import api
from rest_api.database.models import Post

log = logging.getLogger(__name__)

ns = api.namespace('blog/posts',
                   description='Operations related to blog posts')


@ns.route('/')
class PostsCollection(Resource):
    @api.expect(pagination_arguments)
    @api.marshal_with(page_of_blog_posts)
    def get(self):
        """
        Returns list of blog posts.
        """
        args = pagination_arguments.parse_args(request)
        page = args.get('page', 1)
        per_page = args.get('per_page', 10)

        posts_query = Post.query
        posts_page = posts_query.paginate(page, per_page, error_out=False)