Browse Source

Can add items to shelf

pull/1/head
squeaky otter 1 year ago
parent
commit
c91ace030f
Signed by: dashie GPG Key ID: C2D57B325840B755
  1. 4
      .tmuxinator.yml
  2. 4
      api/commands/users.py
  3. 0
      api/controllers/__init__.py
  4. 0
      api/controllers/api/__init__.py
  5. 0
      api/controllers/api/v1/__init__.py
  6. 36
      api/controllers/api/v1/items.py
  7. 48
      api/migrations/versions/001_23580c5fc67e_.py
  8. 30
      api/migrations/versions/002_71744666aed6_.py
  9. 28
      api/migrations/versions/003_b37ac6d8c8f6_.py
  10. 3
      api/models.py
  11. 14
      front/src/views/SearchNew.vue

4
.tmuxinator.yml

@ -9,7 +9,7 @@ windows:
- export APP_SETTINGS='config.development_secret.Config'
- source venv/bin/activate
- cd api
- flask run
- flask run -h 0.0.0.0
# - workers:
# - export FLASK_ENV=development
# - export APP_SETTINGS='config.development_secret.Config'
@ -18,5 +18,5 @@ windows:
# - celery worker -A tasks.celery --loglevel=error
- frontend:
- cd front
- yarn dev
- yarn serve

4
api/commands/users.py

@ -31,9 +31,7 @@ def list():
table.add_rows([["ID", "username"]])
for user in users.all():
table.add_row(
[user.id, user.name]
)
table.add_row([user.id, user.name])
print(table.draw())

0
api/controllers/__init__.py

0
api/controllers/api/__init__.py

0
api/controllers/api/v1/__init__.py

36
api/controllers/api/v1/items.py

@ -2,6 +2,7 @@ from flask import Blueprint, jsonify, request
from flask_security import auth_required
import openfoodfacts
import barcodenumber
from models import db, Item, SubItem
bp_api_v1_items = Blueprint("bp_api_v1_items", __name__)
@ -61,6 +62,37 @@ def add():
description: item has been added
"""
# datas = request.get_json()
datas = request.get_json()
return jsonify("ok")
openfoodfactsid = datas.get('openFoodFactsId', None)
if not openfoodfactsid:
return jsonify({"error": "no_openfoodfacts_id"}), 400
qty = datas.get('qty', 1)
if int(qty) <= 0:
qty = 1
expiry = datas.get('expiry', None)
off = openfoodfacts.products.get_product(openfoodfactsid)
if off["status"] == 1:
off = off["product"]
else:
off = None
# Try to get item matching OFF ID
item = Item.query.filter(Item.openfoodfacts_id == openfoodfactsid).first()
if not item:
# create it
item = Item(openfoodfacts_id=openfoodfactsid, openfoodfacts_product=off)
db.session.add(item)
subitem = SubItem(qty=qty, expiry=expiry, item=item)
db.session.add(subitem)
else:
# just add another sub item
subitem = SubItem(qty=qty, expiry=expiry, item=item)
db.session.add(subitem)
db.session.commit()
return jsonify({"state": "added", "item_id": item.flake_id})

48
api/migrations/versions/001_23580c5fc67e_.py

@ -0,0 +1,48 @@
"""Add item and sub item
Revision ID: 23580c5fc67e
Revises: 77dc08b8493f
Create Date: 2020-08-10 08:23:31.139463
"""
from alembic import op # noqa: E402
import sqlalchemy as sa # noqa: E402
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = "23580c5fc67e"
down_revision = "77dc08b8493f"
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"item",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("qty", sa.Integer(), nullable=True),
sa.Column("expiry", sa.DateTime(), nullable=True),
sa.Column("flake_id", postgresql.UUID(as_uuid=True), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("openfoodfacts_product", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"sub_item",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("qty", sa.Integer(), nullable=True),
sa.Column("expiry", sa.DateTime(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("item_id", sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(["item_id"], ["item.id"],),
sa.PrimaryKeyConstraint("id"),
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("sub_item")
op.drop_table("item")
# ### end Alembic commands ###

30
api/migrations/versions/002_71744666aed6_.py

@ -0,0 +1,30 @@
"""Remove useless fields
Revision ID: 71744666aed6
Revises: 23580c5fc67e
Create Date: 2020-08-10 08:30:43.759215
"""
from alembic import op # noqa: E402
import sqlalchemy as sa # noqa: E402
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = '71744666aed6'
down_revision = '23580c5fc67e'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('item', 'expiry')
op.drop_column('item', 'qty')
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('item', sa.Column('qty', sa.INTEGER(), autoincrement=False, nullable=True))
op.add_column('item', sa.Column('expiry', postgresql.TIMESTAMP(), autoincrement=False, nullable=True))
# ### end Alembic commands ###

28
api/migrations/versions/003_b37ac6d8c8f6_.py

@ -0,0 +1,28 @@
"""Add OpenFoodFacts ID to item
Revision ID: b37ac6d8c8f6
Revises: 71744666aed6
Create Date: 2020-08-10 08:32:21.399627
"""
from alembic import op # noqa: E402
import sqlalchemy as sa # noqa: E402
# revision identifiers, used by Alembic.
revision = 'b37ac6d8c8f6'
down_revision = '71744666aed6'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('item', sa.Column('openfoodfacts_id', sa.String(length=255), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('item', 'openfoodfacts_id')
# ### end Alembic commands ###

3
api/models.py

@ -120,10 +120,9 @@ class Logging(db.Model):
# Item (main item)
class Item(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
qty = db.Column(db.Integer)
expiry = db.Column(db.DateTime())
flake_id = db.Column(UUID(as_uuid=True), unique=False, nullable=True)
created_at = db.Column(db.DateTime(timezone=False), default=datetime.datetime.utcnow)
openfoodfacts_id = db.Column(db.String(255))
openfoodfacts_product = db.Column(JSONB())
sub_items = db.relationship("SubItem", backref="item", lazy="dynamic", cascade="delete")

14
front/src/views/SearchNew.vue

@ -1,6 +1,6 @@
<template>
<div class="search">
<h1>Searching for {{ query }}</h1>
<h1>Searching for <i>{{ query }}</i></h1>
<template v-if="warn">
<b-alert show variant="warning">Too many items returned, only the first {{page_size}} results are shown.</b-alert>
@ -33,6 +33,9 @@
</div>
<b-modal id="modal-add" :title="modalTitle" @ok="saveItem" @cancel="cancelItem" ref="modal-add">
<template v-if="errorSaving">
<b-alert show fade variant="danger">Error while saving item!</b-alert>
</template>
<b-form>
<b-row>
<b-col cols="6">
@ -73,7 +76,8 @@ export default {
addItem: {
qty: 1,
expiry: null
}
},
errorSaving: false
}
},
validations: {
@ -113,16 +117,20 @@ export default {
this.$v.addItem.$touch();
if (!this.$v.addItem.$anyError) {
console.log('save form!!!')
let itemDatas = {
openFoodFactsId: this.choosen._id,
qty: this.addItem.qty,
expiry: this.addItem.expiry
}
Axios.post("/api/v1/items/new", itemDatas, {withCredentials: true}).then(() => {
this.errorSaving = false
this.cancelItem()
this.$refs['modal-add'].hide()
})
.catch((error) => {
console.log('Got an error while trying to save item:', error.request, error.response)
this.errorSaving = true
})
}
}
}

Loading…
Cancel
Save