예제 #1
0
def read_by_id(id):
    item = Item.query.filter(Item.id == id).one_or_none()

    if item:
        item_schema = ItemSchema()
        return item_schema.dump(item)

    else:
        abort(404, f'Person not found for id: {id}')
예제 #2
0
def create(item):
    name = item.get("name")
    amount = item.get("amount")
    existing_item = Item.query.filter(Item.name == name).one_or_none()

    # if item already exists, adds the amount created to the amount of that item
    if existing_item is not None:
        schema = ItemSchema()
        update = schema.load(item, session=db.session)

        update.id = existing_item.id
        update.amount = existing_item.amount + amount

        db.session.merge(update)
        db.session.commit()

        return schema.dump(update), 200

    # creates new item with the parameters given
    else:
        schema = ItemSchema()
        new_item = schema.load(item, session=db.session)

        db.session.add(new_item)
        db.session.commit()

        return schema.dump(new_item), 201
예제 #3
0
def getelements():
    """gets a number of elements from the database and returns them as a json response"""
    offset = request.args.get('offset', '')
    limit = request.args.get('limit', '')
    categ = request.args.get('category', '')

    # if limit or offset not provided return error message.
    if not limit or not offset:
        return render_template(
            "mes.html",
            error=
            "please provide a limit and an offset for your request to be made."
        )
    # if no category specified get items from all the categories.
    if not categ:
        items = Item.query.filter(
            Item.sold == 0).limit(limit).offset(offset).all()
    # else get items from the specifies category.
    else:
        items = Item.query.filter((Item.category == categ) & (
            Item.sold == 0)).limit(limit).offset(offset).as_dict().all()
    # Using flask_marshmallow Itemschema return items as a json response with status code 200.
    response = jsonify(ItemSchema(many=True).dump(items).data)
    response.status_code = 200
    return response
예제 #4
0
def search():
    """search for a specific item"""
    searchword = request.args.get('search', '')
    json = request.args.get('json', '')

    if not json:
        # if the user didn't provide a search word return error message.
        if not searchword:
            return render_template("mes.html",
                                   error="must provide a search query")
        # search the database for all the items that have names similar to the search word
        # and surf them as a webpage.
        items = Item.query.filter((Item.title.like("%{}%".format(searchword)))
                                  & (Item.sold == 0)).all()
        return render_template("index.html",
                               items=items,
                               categories=app.config['CATEGORIES'])
    # if the user doesn't provide a search word and a boolean for json return error message.
    if not searchword:
        return render_template("mes.html", error="must provide a search query")
    # search the database for all the items that have names similar to the search word
    # and surf them as a json response with status code 200.
    items = Item.query.filter(
        Item.title.like(("%{}%".format(searchword))) & (Item.sold == 0)).all()
    response = jsonify((ItemSchema(many=True).dump(items).data))
    response.status_code = 200
    return response
예제 #5
0
# 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'])
def logout():
예제 #6
0
def read_by_name():
    items = Item.query.order_by(Item.name).all()
    item_schema = ItemSchema(many=True)
    return item_schema.dump(items)
예제 #7
0
def read_by_number():
    items = Item.query.order_by(Item.amount).all()
    item_schema = ItemSchema(many=True)
    return item_schema.dump(items)