summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarius Peter <marius.peter@tutanota.com>2022-07-03 20:21:02 +0200
committerMarius Peter <marius.peter@tutanota.com>2022-07-03 20:21:02 +0200
commiteba4c1a3113d24319bccc5c52d4d93838334bd31 (patch)
treefe95881d42a0b4191a9371f82eaf66b9390af047
parent5160b8a3ac9fc27c2b31206aed735a15de0099e9 (diff)
db now in models; modules.
-rw-r--r--app/__init__.py3
-rw-r--r--app/models.py45
-rw-r--r--app/modules/common.py4
-rw-r--r--app/modules/ferti/forms.py21
-rw-r--r--app/modules/ferti/routes.py7
-rw-r--r--app/modules/forms.py19
-rw-r--r--app/modules/invoices/forms.py10
-rw-r--r--app/modules/invoices/routes.py4
-rw-r--r--app/modules/orders/routes.py7
9 files changed, 67 insertions, 53 deletions
diff --git a/app/__init__.py b/app/__init__.py
index 04c4c99..54c3f69 100644
--- a/app/__init__.py
+++ b/app/__init__.py
@@ -4,8 +4,7 @@ from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
-
-db = SQLAlchemy()
+from .models import db
def create_app():
diff --git a/app/models.py b/app/models.py
index 5155ee5..009e507 100644
--- a/app/models.py
+++ b/app/models.py
@@ -7,8 +7,8 @@ from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from flask_login import UserMixin
-from . import db
+db = SQLAlchemy()
# Base = declarative_base()
@@ -196,7 +196,7 @@ class Invoice(db.Model):
self.amount_tax = amount_tax
def __repr__(self):
- return f"<Invoice for {self.customer.name} at {self.updated}>"
+ return f"<Invoice for {self.customer.name} billed {self.date_billed}>"
class Order(db.Model): # TODO
@@ -212,36 +212,30 @@ class Order(db.Model): # TODO
date_time_updated = db.Column(
"DateTimeUpdated", db.String, server_onupdate=date_time_now()
)
- product_id = db.Column(
- "ProductId",
- db.Integer,
- db.ForeignKey("Product.ProductId"),
- nullable=False,
- )
customer_id = db.Column(
"CustomerId",
db.Integer,
db.ForeignKey("Customer.CustomerId"),
nullable=False,
)
+ product_id = db.Column(
+ "ProductId",
+ db.Integer,
+ db.ForeignKey("Product.ProductId"),
+ nullable=False,
+ )
invoice_id = db.Column(
"InvoiceId",
db.Integer,
db.ForeignKey("Invoice.InvoiceId"),
nullable=True,
)
- customer = db.relationship(
- "Customer", back_populates="orders"
- )
- product = db.relationship(
- "Product", back_populates="orders"
- )
- invoice = db.relationship(
- "Invoice", back_populates="orders"
- )
+ customer = db.relationship("Customer", back_populates="orders")
+ product = db.relationship("Product", back_populates="orders")
+ invoice = db.relationship("Invoice", back_populates="orders")
def __repr__(self):
- return f"<Order {self.name} at {self.updated}>"
+ return f"<Order {self.name} at {self.date_time_created}>"
class Product(db.Model):
@@ -283,7 +277,7 @@ class Product(db.Model):
class FertiLog(db.Model):
- __tablename__ = "Fertilog"
+ __tablename__ = "FertiLog"
primary_key = db.Column("FertiLogId", db.Integer, primary_key=True)
nno3 = db.Column("NNO3", db.Float, default=0)
p = db.Column("P", db.Float, default=0)
@@ -307,6 +301,8 @@ class FertiLog(db.Model):
date_time_updated = db.Column(
"DateTimeUpdated", db.String, server_onupdate=date_time_now()
)
+ targets = db.relationship("FertiTarget", back_populates="ferti_log")
+
def __init__(self, nno3, p, k, ca, mg, s, na, cl, fe, zn, b, mn, cu, mo, si, nnh4):
self.nno3 = nno3
@@ -325,18 +321,21 @@ class FertiLog(db.Model):
self.mo = mo
self.si = si
self.nnh4 = nnh4
- self.date_time_created = date_time_now
def __repr__(self):
return f"<Log ID {self.primary_key}>"
class FertiTarget(FertiLog):
- __tablename__ = "Fertitarget"
+ __tablename__ = "FertiTarget"
primary_key = db.Column("FertiTargetId", db.Integer, primary_key=True)
- targeted_log = db.Column(
- "FertiLogId", db.Integer, db.ForeignKey("Fertilog.FertiLogId")
+ ferti_log_id = db.Column(
+ "FertiLogId", db.Integer, db.ForeignKey("FertiLog.FertiLogId")
)
+ ferti_log = db.relationship("FertiLog", back_populates="targets")
+
+ # def __init__(self):
+ # super(*args).__init__()
def __repr__(self):
return f"<Target ID {self.primary_key}>"
diff --git a/app/modules/common.py b/app/modules/common.py
index f9ccba6..ff0aec7 100644
--- a/app/modules/common.py
+++ b/app/modules/common.py
@@ -25,8 +25,8 @@ def add_item(module, table):
if form.validate_on_submit():
model = getattr(models, table)
table_fields = inspect.signature(model).parameters
- form_values = {key: request.form[key] for key in table_fields}
- print(f"Ready to insert {form_values}")
+ form_values = {key: request.form.get(key) for key in table_fields}
+ print(f"Ready to insert in {table} from {module} {form_values}")
record = model(**form_values)
db.session.add(record)
db.session.commit()
diff --git a/app/modules/ferti/forms.py b/app/modules/ferti/forms.py
index d926931..1e25ec3 100644
--- a/app/modules/ferti/forms.py
+++ b/app/modules/ferti/forms.py
@@ -12,13 +12,10 @@ from wtforms import (
)
from wtforms.validators import InputRequired, Length, NumberRange
+from ...models import FertiLog
+
class AddFertiLog(FlaskForm):
- target = SelectField(
- "Type",
- choices=[("False", "Log"), ("True", "Target")],
- validators=[InputRequired()],
- )
nno3 = FloatField("NNO3", default=0)
p = FloatField("P", default=0)
k = FloatField("K", default=0)
@@ -39,9 +36,15 @@ class AddFertiLog(FlaskForm):
class AddFertiTarget(AddFertiLog):
- targeted_log = SelectField(
- "Log to target",
- choices=[("value1", "Last"), ("value2", "Named")],
+ target_id = SelectField(
+ "Target log",
+ choices=[],
validators=[InputRequired()],
)
- submit = SubmitField("Add/Update Log")
+ submit = SubmitField("Add target")
+
+ def __init__(self, **kwargs):
+ super().__init__()
+ self.target_id.choices = [
+ (target.primary_key, target.date_time_created) for target in FertiLog.query.all()
+ ]
diff --git a/app/modules/ferti/routes.py b/app/modules/ferti/routes.py
index b3bb03f..c0f3ab5 100644
--- a/app/modules/ferti/routes.py
+++ b/app/modules/ferti/routes.py
@@ -21,7 +21,7 @@ import inspect
-from ... import db
+# from ... import db
from ...models import *
from .forms import *
@@ -34,11 +34,10 @@ ferti = Blueprint("ferti", __name__)
@login_required
def view():
modules = Module.query.all()
- logs = Fertilog.query.order_by(Fertilog.primary_key.desc()).all()
- targets = Fertitarget.query.all()
+ logs = FertiLog.query.order_by(FertiLog.primary_key.desc()).all()
+ targets = FertiTarget.query.all()
module = "ferti"
flash(f"Latest target is {targets}.", "info")
- flash(f"Still fighting against styling in {module}.", "error")
return render_template(
f"modules/{module}.html",
modules=modules,
diff --git a/app/modules/forms.py b/app/modules/forms.py
index 7e4acde..001557a 100644
--- a/app/modules/forms.py
+++ b/app/modules/forms.py
@@ -9,3 +9,22 @@ from .customers.forms import AddCustomer
from .ferti.forms import AddFertiLog, AddFertiTarget
from .invoices.forms import AddInvoice
from .products.forms import AddProduct
+
+
+# class AddUser(FlaskForm):
+# customer_id = SelectField("Customer", choices=[])
+# invoice_id_alt = StringField("Alternative invoice ID")
+# customer_reference = StringField("Customer reference")
+# date_billed = DateField('Date billed', format='%Y-%m-%d')
+# date_due = DateField('Date due', format='%Y-%m-%d')
+# amount_net = FloatField("Amount without tax", default=0)
+# amount_gross = FloatField("Amount with tax", default=0)
+# amount_tax = FloatField("Amount of tax", default=0)
+# submit = SubmitField("Add Invoice")
+
+# def __init__(self, **kwargs):
+# super().__init__()
+# self.customer_id.choices = [
+# (cus.primary_key, cus.name) for cus in Customer.query.all()
+# ]
+
diff --git a/app/modules/invoices/forms.py b/app/modules/invoices/forms.py
index 74df40e..7e2bc50 100644
--- a/app/modules/invoices/forms.py
+++ b/app/modules/invoices/forms.py
@@ -8,7 +8,7 @@ from wtforms import (
IntegerField,
FloatField,
BooleanField,
- DateTimeField,
+ DateField,
)
from wtforms.validators import InputRequired, Length, NumberRange
@@ -19,14 +19,14 @@ class AddInvoice(FlaskForm):
customer_id = SelectField("Customer", choices=[])
invoice_id_alt = StringField("Alternative invoice ID")
customer_reference = StringField("Customer reference")
- date_billed = StringField("Date billed")
- date_due = StringField("Date due")
+ date_billed = DateField('Date billed', format='%Y-%m-%d')
+ date_due = DateField('Date due', format='%Y-%m-%d')
amount_net = FloatField("Amount without tax", default=0)
amount_gross = FloatField("Amount with tax", default=0)
amount_tax = FloatField("Amount of tax", default=0)
- submit = SubmitField("Add new Invoice")
+ submit = SubmitField("Add Invoice")
- def __init__(self):
+ def __init__(self, **kwargs):
super().__init__()
self.customer_id.choices = [
(cus.primary_key, cus.name) for cus in Customer.query.all()
diff --git a/app/modules/invoices/routes.py b/app/modules/invoices/routes.py
index b8a0a68..b6a4fd9 100644
--- a/app/modules/invoices/routes.py
+++ b/app/modules/invoices/routes.py
@@ -15,7 +15,7 @@ from flask import (
from flask_login import login_required, current_user
-from ... import db
+# from ... import db
from ...models import *
from .forms import *
@@ -31,8 +31,6 @@ def view():
page = request.args.get('page', 1, type=int)
invoices = Invoice.query.order_by(Invoice.primary_key.desc()).paginate(page=page, per_page=5)
module = "invoices"
- flash(f"Successfully accessed module {module}.", "info")
- flash(f"Still fighting against styling in {module}.", "error")
return render_template(
f"modules/{module}.html",
modules=modules,
diff --git a/app/modules/orders/routes.py b/app/modules/orders/routes.py
index 0d2e66a..cf4e70a 100644
--- a/app/modules/orders/routes.py
+++ b/app/modules/orders/routes.py
@@ -15,13 +15,9 @@ from flask import (
from flask_login import login_required, current_user
-# from datetime import datetime
-import inspect
-
-from ... import db
-from ...models import Module, Order
+from ...models import db, Module, Order, Invoice
from .forms import *
@@ -34,6 +30,7 @@ def view():
modules = Module.query.all()
page = request.args.get('page', 1, type=int)
orders = Order.query.order_by(Order.primary_key.desc()).paginate(page=page, per_page=5)
+ invoices = Invoice.query.order_by(Invoice.primary_key.desc()).paginate(page=page, per_page=5)
module = "orders"
flash(f"Successfully accessed module {module}.", "info")
flash(f"Still fighting against styling in {module}.", "error")
Copyright 2019--2024 Marius PETER