Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Remove startup errors for app #6

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions server/app.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from colorama import init, Fore
from flask import Flask, request, abort
from flask import Flask, request, jsonify
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.engine import URL
import json

# Initialize Colorama
init(autoreset=True)

# Load database configuration from JSON file
with open("db.json") as json_data_file:
db_config = json.load(json_data_file)

# Create SQLAlchemy engine
url = URL.create(
drivername=db_config["drivername"],
username=db_config["username"],
Expand All @@ -16,19 +21,26 @@
database=db_config["database"],
port=db_config["port"],
)

engine = create_engine(url)
connection = engine.connect()
Session = sessionmaker(bind=engine)

init(autoreset=True)
# Create Flask app
app = Flask(__name__)

# Example route to add a new plant
@app.route("/plants", methods=["POST"])
def add_plant():
# Get JSON data from request
new_plant = request.get_json()
new_plant["id"] = len(plants) + 1
plants.append(new_plant)
# Perform database operations here
# Example: Add new plant to the database
# session = Session()
# session.add(new_plant)
# session.commit()
# session.close()
# Return response
return jsonify(new_plant), 201

from cli import create, stats, update, water, archive
if __name__ == "__main__":
# Run the Flask app
app.run(debug=True)
11 changes: 5 additions & 6 deletions server/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from app import engine
from models.base import Base
from models.plant import Plant
# from app import engine
# from models.base import Base

# TODO: Implement way to make this configurable
# # TODO: Implement way to make this configurable

Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
# Base.metadata.drop_all(engine)
# Base.metadata.create_all(engine)
30 changes: 16 additions & 14 deletions server/models/plant.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
from sqlalchemy import Column, Integer, String, DateTime, Boolean, ForeignKey
from datetime import datetime
from models import Base
from sqlalchemy.orm import Mapped, relationship, mapped_column
from sqlalchemy.ext.declarative import declarative_base
from typing import List

from enum import Enum

Base = declarative_base()

class DeathCause(Enum, str):
TOO_LITTLE_WATER = "too little water"
TOO_MUCH_WATER = "too much water"
TOO_LITTLE_HUMIDITY = "too little humidity"
TOO_MUCH_HUMIDITY = "too much humidity"
TOO_LITTLE_SUN = "too little sun"
TOO_MUCH_SUN = "too much sun"
PROPAGATION = "propagation"
PESTS = "pests"
MOLD = "mold"
NEGLECT = "neglect"
UNKNOWN = "unknown"
# class DeathCause(Enum, str):
# TOO_LITTLE_WATER = "too little water"
# TOO_MUCH_WATER = "too much water"
# TOO_LITTLE_HUMIDITY = "too little humidity"
# TOO_MUCH_HUMIDITY = "too much humidity"
# TOO_LITTLE_SUN = "too little sun"
# TOO_MUCH_SUN = "too much sun"
# PROPAGATION = "propagation"
# PESTS = "pests"
# MOLD = "mold"
# NEGLECT = "neglect"
# UNKNOWN = "unknown"


class Plant(Base):
Expand All @@ -44,7 +46,7 @@ class Plant(Base):

# Death Info
dead = Column(Boolean, default=False, nullable=False)
dead_cause = Column(Enum(DeathCause), nullable=True)
# dead_cause = Column(Enum(DeathCause), nullable=True)
dead_on = Column(DateTime(), default=None, nullable=True)

def __repr__(self) -> str:
Expand Down
Loading