<file_sep>from bot import db


class Enzyme(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), index=True, nullable=False)
    catalogue_number = db.Column(db.String(64), index=True, unique=True, nullable=False)
    quantity = db.Column(db.Integer, nullable=False)

    def __repr__(self):
        return f'<Enzyme {self.name}>'
<file_sep>from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired


class AddForm(FlaskForm):
    name = StringField("Enzyme name", validators=[DataRequired()])
    catalogue_number = StringField("Catalogue number", validators=[DataRequired()])
    quantity = StringField("Quantity", validators=[DataRequired()])
    submit = SubmitField("Submit")<file_sep>from flask import request, jsonify, render_template
from bot import app, db
from bot.functions import send_message
from bot.models import Enzyme
from bot.forms import AddForm


@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "POST":
        r = request.get_json()
        chat_id = r["message"]["chat"]["id"]
        message = r["message"]["text"]
        input_data = message.split()
        if len(input_data) == 2:
            cat_id, amount = input_data
            data = db.session.query(Enzyme).filter(Enzyme.catalogue_number==cat_id).first()
            if data:
                data.quantity -= int(amount)
                db.session.commit()
                send_message(chat_id, text=f"Current amount is {data.quantity}")
            else:
                send_message(chat_id, "Wrong cat_id")
        else:
            send_message(chat_id, "Wrong data format")
        return jsonify(r)
    return render_template("index.html")


@app.route("/info/")
def db_info():
    all_data = db.session.query(Enzyme).all()
    return render_template("db_info.html", all_data=all_data)


@app.route("/addition/", methods=["GET", "POST"])
def addition():
    form = AddForm()
    if request.method == "POST" and form.validate_on_submit():
        name = form.name.data
        catalogue_number = form.catalogue_number.data
        quantity = form.quantity.data
        enzyme = Enzyme(name=name, catalogue_number=catalogue_number, quantity=quantity)
        db.session.add(enzyme)
        db.session.commit()
        return f"Enzyme {name} was successfully added"
    else:
        return render_template("addition.html", form=form)
<file_sep>import requests
from config import Config


URL = f"https://api.telegram.org/bot{Config.TOKEN}/"


def send_message(chat_id, text="blabla"):
    url = URL + "sendMessage"
    answer = {"chat_id": chat_id, "text": text}
    r = requests.post(url, json=answer)
    return r.json()
