Ejemplo n.º 1
0
def create_category(body: dict) -> tuple:

    existing_category = (Category.query.filter(
        Category.name == body.get('name')).one_or_none())

    if existing_category:
        response = {
            'code':
            409,
            'message':
            'Category {name} already exists'.format(name=body.get('name'))
        }
        return response, 409

    # if body.get('id') or body.get('id') == 0:
    #     del body['id']
    category_schema = CategorySchema()
    category_data = category_schema.load(body).data

    new_category = Category(**category_data)

    db.session.add(new_category)
    db.session.commit()

    category_data = category_schema.dump(new_category).data

    return category_data, 201
Ejemplo n.º 2
0
def get_category(category_id: int) -> tuple:

    category = Category.query.filter_by(id=category_id).one_or_none()
    if category:
        category_schema = CategorySchema(only=('name', 'image_path',
                                               'description'))
        category_data = category_schema.dump(category).data
        return category_data, 200
    else:
        response = {
            'code': 404,
            'message': 'Category with id {id} not found'.format(id=category_id)
        }
        return response, 404
Ejemplo n.º 3
0
def update_category(category_id: int, body: dict) -> tuple:
    category = Category.query.filter_by(id=category_id).one_or_none()

    if category is None:
        response = {
            'code': 404,
            'message': 'Category with id {id} not found'.format(id=category_id)
        }
        return response, 404

    existing_name = (Category.query.filter(
        Category.name == body.get('name')).filter(
            Category.id != category_id).one_or_none())

    if existing_name:
        response = {
            'code':
            409,
            'message':
            'Category name {name} already exists'.format(name=body.get('name'))
        }
        return response, 409
    elif category:
        if body.get('id') or body.get('id') == 0:
            del body['id']

        for key in body.keys():
            setattr(category, key, body[key])

        db.session.add(category)
        db.session.commit()

        # Serialize and return the newly created category in the response
        category_schema = CategorySchema(only=('name', 'image_path',
                                               'description'))
        category_data = category_schema.dump(category).data

        return category_data, 200
Ejemplo n.º 4
0
from flask import request
from flask_restful import Resource
from models import db, Category, CategorySchema

categories_schema = CategorySchema(many=True)
category_schema = CategorySchema()


class CategoryResource(Resource):
    def get(self):
        categories = Category.query.all()
        categories = categories_schema.dump(categories).data
        return {'status': 'success', 'data': categories}, 200

    def post(self):
        json_data = request.get_json(force=True)
        if not json_data:
            return {'message': 'No input data provided'}, 400
        # Validate and deserialize input
        data, errors = category_schema.load(json_data)
        if errors:
            return errors, 422
        category = Category.query.filter_by(name=data['name']).first()
        if category:
            return {'message': 'Category already exists'}, 400
        category = Category(name=json_data['name'])

        db.session.add(category)
        db.session.commit()

        result = category_schema.dump(category).data
Ejemplo n.º 5
0
from flask import Blueprint, request, jsonify, make_response
from flask_restful import Api, Resource
from models import db, Category, CategorySchema, Message, MessageSchema
from sqlalchemy.exc import SQLAlchemyError
import status

api_bp = Blueprint('api', __name__)
category_schema = CategorySchema()
message_schema = MessageSchema()
api = Api(api_bp)


class MessageResource(Resource):
    def get(self, id):
        message = Message.query.get_or_404(id)
        result = message_schema.dump(message).data
        return result

    def patch(self, id):
        message = Message.query.get_or_404(id)
        message_dict = request.get_json(force=True)
        if 'message' in message_dict:
            message.message = message_dict['message']
        if 'duration' in message_dict:
            message.duration = message_dict['duration']
        if 'printed_times' in message_dict:
            message.printed_times = message_dict['printed_times']
        if 'printed_once' in message_dict:
            message.printed_once = message_dict['printed_once']
        dumped_message, dump_errors = message_schema.dump(message)
        if dump_errors:
Ejemplo n.º 6
0
def get_categories() -> tuple:
    categories = Category.query.order_by(Category.id).all()

    categories_schema = CategorySchema(many=True)
    categories_data = categories_schema.dump(categories).data
    return categories_data, 200
Ejemplo n.º 7
0
import os

# App config
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.secret_key = os.environ['SUPER_SECRET_KEY']
app.static_folder = 'static'
db = SQLAlchemy(app)
ma = Marshmallow(app)
logMan = LoginManager(app)
logMan.login_view = 'login'

from models import Usercat, Category, Item
from models import CategorySchema, ItemSchema
category_schema = CategorySchema(many=True)
item_schema = ItemSchema(many=True)
db.create_all()


@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        if 'github' in request.form:
            return redirect('auth')
        elif 'redirect' in request.form:
            return redirect('catalog')
    return render_template('login.html')


@app.route('/logout', methods=['GET', 'POST'])