Skip to content

Commit

Permalink
change code to remove all state vars and use model fields directly in…
Browse files Browse the repository at this point in the history
… form
  • Loading branch information
Tom Gotsman committed May 31, 2024
1 parent 5e69092 commit 33097a1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 83 deletions.
118 changes: 43 additions & 75 deletions customer_data_app/customer_data_app/backend/backend.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import reflex as rx
from typing import Literal, Union
from sqlmodel import select
from sqlmodel import select, asc, desc, or_, func
from datetime import datetime, timedelta

LiteralStatus = Literal["Delivered", "Pending", "Cancelled"]
Expand Down Expand Up @@ -41,38 +41,34 @@ class MonthValues(rx.Base):
class State(rx.State):
"""The app state."""

id: int
name: str = ""
email: str = ""
phone: str = ""
address: str = ""
date: str = "" # In 'YYYY-MM-DD HH:MM:SS' format
payments: float = 0.0
status: LiteralStatus = "Pending"
users: list[Customer] = []
sort_value: str = ""
sort_reverse: bool = False
current_user: Customer = Customer()
# Values for current and previous month
current_month_values: MonthValues = MonthValues()
previous_month_values: MonthValues = MonthValues()


def load_entries(self) -> list[Customer]:
"""Get all users from the database."""
with rx.session() as session:
self.users = session.exec(select(Customer)).all()
query = select(Customer)
if self.sort_value:
sort_column = getattr(Customer, self.sort_value)
if self.sort_value == "payments":
self.users = sorted(
self.users, key=lambda user: user.payments, reverse=self.sort_reverse
)
order = desc(sort_column) if self.sort_reverse else asc(sort_column)
else:
self.users = sorted(
self.users, key=lambda user: str(getattr(
user, self.sort_value)).lower(), reverse=self.sort_reverse
)
order = desc(func.lower(sort_column)) if self.sort_reverse else asc(func.lower(sort_column))
query = query.order_by(order)

###### come back to add search functioanloty later
self.users = session.exec(query).all()

self.get_current_month_values()
self.get_previous_month_values()


def get_current_month_values(self):
"""Calculate current month's values."""
now = datetime.now()
Expand All @@ -86,6 +82,7 @@ def get_current_month_values(self):
num_delivers = len([user for user in current_month_users if user.status == "Delivered"])
self.current_month_values = MonthValues(num_customers=num_customers, total_payments=total_payments, num_delivers=num_delivers)


def get_previous_month_values(self):
"""Calculate previous month's values."""
now = datetime.now()
Expand All @@ -104,90 +101,61 @@ def get_previous_month_values(self):

self.previous_month_values = MonthValues(num_customers=num_customers, total_payments=total_payments, num_delivers=num_delivers)


def sort_values(self, sort_value: str):
self.sort_value = sort_value
self.load_entries()


def toggle_sort(self):
self.sort_reverse = not self.sort_reverse
self.load_entries()

def set_user_vars(self, user: Customer):
self.id = user["id"]
self.name = user["name"]
self.email = user["email"]
self.phone = user["phone"]
self.address = user["address"]
self.payments = user["payments"]
self.status = user["status"]
self.date = user["date"]

def add_customer(self, form_data: dict):
self.name = form_data.get("name")
self.email = form_data.get("email")
self.phone = form_data.get("phone")
self.address = form_data.get("address")
self.payments = form_data.get("payments")
self.status = form_data.get("status", "Pending")
self.date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
"""Add a customer to the database."""

def get_user(self, user: Customer):
self.current_user = user


def add_customer_to_db(self, form_data: dict):
self.current_user = form_data
self.current_user["date"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

with rx.session() as session:
if session.exec(
select(Customer).where(Customer.email == self.email)
select(Customer).where(Customer.email == self.current_user["email"])
).first():
return rx._x.toast.info("User already exists", variant="outline", position="bottom-right")
session.add(
Customer(
name=self.name,
email=self.email,
phone=self.phone,
address=self.address,
payments=self.payments,
status=self.status,
date=self.date,
)
)
return rx.window_alert("User with this email already exists")
session.add(Customer(**self.current_user))
session.commit()
self.load_entries()
return rx._x.toast.info(f"User {self.name} has been added.", variant="outline", position="bottom-right")

def update_customer(self, form_data: dict):
self.name = form_data.get("name")
self.email = form_data.get("email")
self.phone = form_data.get("phone")
self.address = form_data.get("address")
self.payments = form_data.get("payments")
self.status = form_data.get("status", "Pending")
return rx._x.toast.info(f"User {self.current_user["name"]} has been added.", variant="outline", position="bottom-right")


"""Update a customer in the database."""
def update_customer_to_db(self, form_data: dict):
self.current_user.update(form_data)
print(self.current_user)
with rx.session() as session:
customer = session.exec(
select(Customer).where(Customer.id == self.id)
select(Customer).where(Customer.id == self.current_user["id"])
).first()
customer.name = self.name
customer.email = self.email
customer.phone = self.phone
customer.address = self.address
customer.payments = self.payments
customer.status = self.status
for field in Customer.get_fields():
if field != "id":
setattr(customer, field, self.current_user[field])
session.add(customer)
session.commit()
self.load_entries()
return rx._x.toast.info(f"User {self.name} has been modified.", variant="outline", position="bottom-right")
return rx._x.toast.info(f"User {self.current_user["name"]} has been modified.", variant="outline", position="bottom-right")

def delete_customer(self, email: str):

def delete_customer(self, id: int):
"""Delete a customer from the database."""
with rx.session() as session:
customer = session.exec(
select(Customer).where(Customer.email == email)
).first()
customer = session.exec(select(Customer).where(Customer.id == id)).first()
session.delete(customer)
session.commit()
self.load_entries()
return rx._x.toast.info(f"User {email} has been deleted.", variant="outline", position="bottom-right")

def on_load(self):
self.load_entries()
return rx._x.toast.info(f"User {customer.name} has been deleted.", variant="outline", position="bottom-right")


@rx.var
def payments_change(self) -> float:
Expand Down
1 change: 0 additions & 1 deletion customer_data_app/customer_data_app/customer_data_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def index() -> rx.Component:

app.add_page(
index,
on_load=State.on_load,
title="Customer Data App",
description="A simple app to manage customer data.",
)
15 changes: 8 additions & 7 deletions customer_data_app/customer_data_app/views/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def show_customer(user: Customer):
update_customer_dialog(user),
rx.icon_button(
rx.icon("trash-2", size=22),
on_click=lambda: State.delete_customer(user.email),
on_click=lambda: State.delete_customer(getattr(user, "id")),
size="2",
variant="solid",
color_scheme="red",
Expand Down Expand Up @@ -151,7 +151,7 @@ def add_customer_button() -> rx.Component:
mt="4",
justify="end",
),
on_submit=State.add_customer,
on_submit=State.add_customer_to_db,
reset_on_submit=False,
),
width="100%",
Expand All @@ -176,7 +176,7 @@ def update_customer_dialog(user):
color_scheme="blue",
size="2",
variant="solid",
on_click=lambda: State.set_user_vars(user),
on_click=lambda: State.get_user(user),
),
),
rx.dialog.content(
Expand Down Expand Up @@ -225,7 +225,7 @@ def update_customer_dialog(user):
"email",
"email",
"mail",
user.email
user.email,
),
# Phone
form_field(
Expand All @@ -234,7 +234,7 @@ def update_customer_dialog(user):
"tel",
"phone",
"phone",
user.phone
user.phone,
),
# Address
form_field(
Expand All @@ -243,7 +243,7 @@ def update_customer_dialog(user):
"text",
"address",
"home",
user.address
user.address,
),
# Payments
form_field(
Expand Down Expand Up @@ -293,7 +293,7 @@ def update_customer_dialog(user):
mt="4",
justify="end",
),
on_submit=State.update_customer,
on_submit=State.update_customer_to_db,
reset_on_submit=False,
),
width="100%",
Expand Down Expand Up @@ -362,5 +362,6 @@ def main_table():
variant="surface",
size="3",
width="100%",
on_mount=State.load_entries,
),
)

0 comments on commit 33097a1

Please sign in to comment.