From ba3a8f06993b9a0fcba27ad1321e67cbfbe965fc Mon Sep 17 00:00:00 2001 From: VaruneRampersad Date: Fri, 31 Jan 2025 18:08:09 +0000 Subject: [PATCH 1/5] lab 2 demo 31st jan --- .replit | 10 ++++----- instance/project.db | Bin 16384 -> 16384 bytes wsgi.py | 51 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/.replit b/.replit index 50ad76d0..203b5cad 100644 --- a/.replit +++ b/.replit @@ -1,11 +1,11 @@ -entrypoint = "wsgi.py" -modules = ["python-3.10:v18-20230807-322e88b"] +modules = ["python-3.10", "nix"] hidden = [".pythonlibs"] +run = "flask run -host 0.0.0.0 -p 8080 --reload" [nix] -channel = "stable-23_05" +channel = "stable-24_05" [deployment] -run = ["python3", "wsgi.py"] -deploymentTarget = "cloudrun" \ No newline at end of file +run = ["sh", "-c", "flask run -host 0.0.0.0 -p 8080 --reload"] +deploymentTarget = "cloudrun" diff --git a/instance/project.db b/instance/project.db index 880c27eb7f57dd2fafa3680ac3bbda6af0632149..e69d1b8fc8b58c629eb9fb5a0af26b66b6e3b3b0 100644 GIT binary patch delta 549 zcmaJ;&1zFo5Wcz5s9EG%s*5VP%F>oO|8o+M+JzJpX{n&wnS0I=!8B>oh|MNxpTNF> zuOPmH571{2+;;EIbBW?cJj|KJ@G6bFSHZ z5~s1@|34K6v*(lX(TmY?PtGBzSzm()CPk@@6gGug8y5u!@(^VTLTAzz*m2gHeDY8$ z=hz_B>{NC~qXdkKRZQLrGEzC4JX>%&Y2{3!OxPzK$=Pfa1`eP(@O>~MC^RuNsg>1r zQT?p0b2Q(pN8R>TYn?~gX>Z=i+SZt>qWV!?Wz4qm)DJW Date: Thu, 13 Feb 2025 22:25:16 +0000 Subject: [PATCH 2/5] Assistant checkpoint: Fixed Flask run command syntax in .replit Assistant generated file changes: - .replit: Fix Flask run command syntax --- User prompt: Running the code in my project results in the following: ``` Usage: flask run [OPTIONS] Try 'flask run --help' for help. Error: Got unexpected extra argument (0.0.0.0) ``` If applicable, propose a fix immediately. --- .replit | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.replit b/.replit index 203b5cad..b89e832b 100644 --- a/.replit +++ b/.replit @@ -1,11 +1,12 @@ + modules = ["python-3.10", "nix"] hidden = [".pythonlibs"] -run = "flask run -host 0.0.0.0 -p 8080 --reload" +run = "flask run --host=0.0.0.0 --port=8080 --reload" [nix] channel = "stable-24_05" [deployment] -run = ["sh", "-c", "flask run -host 0.0.0.0 -p 8080 --reload"] +run = ["sh", "-c", "flask run --host=0.0.0.0 --port=8080 --reload"] deploymentTarget = "cloudrun" From 347963b52479c949dfe0c112b22f3e3f9b2400d8 Mon Sep 17 00:00:00 2001 From: VaruneVigstar21 <19903445-VaruneVigstar21@users.noreply.replit.com> Date: Thu, 13 Feb 2025 22:28:30 +0000 Subject: [PATCH 3/5] User checkpoint: Configure Replit deployment for cloudrun and expose port 80. --- .replit | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.replit b/.replit index b89e832b..30438301 100644 --- a/.replit +++ b/.replit @@ -10,3 +10,7 @@ channel = "stable-24_05" [deployment] run = ["sh", "-c", "flask run --host=0.0.0.0 --port=8080 --reload"] deploymentTarget = "cloudrun" + +[[ports]] +localPort = 8080 +externalPort = 80 From ce4b51aedf7f128ed5dceb6952f872f8806b7fc1 Mon Sep 17 00:00:00 2001 From: VaruneRampersad Date: Thu, 13 Feb 2025 22:28:40 +0000 Subject: [PATCH 4/5] Assistant checkpoint: Added root route to Flask application Assistant generated file changes: - app.py: Add root route to Flask app --- User prompt: Running the code in my project results in the following: ``` * Debug mode: off WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on all addresses (0.0.0.0) * Running on http://127.0.0.1:8080 * Running on http://172.31.128.11:8080 Press CTRL+C to quit * Restarting with stat 172.31.128.11 - - [13/Feb/2025 22:27:57] "GET / HTTP/1.1" 404 - ``` If applicable, propose a fix immediately. --- app.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index b49413db..7027b37b 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,10 @@ -from flask import Flask +from flask import Flask, render_template app = Flask(__name__) app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///project.db" app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.app_context().push() + +@app.route('/') +def home(): + return "Hello World!" From 1c79794366d75f5e4610492a742d3a419c7caf64 Mon Sep 17 00:00:00 2001 From: VaruneRampersad Date: Thu, 13 Feb 2025 23:28:32 +0000 Subject: [PATCH 5/5] Lab 2 finished --- models.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ wsgi.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 138 insertions(+), 2 deletions(-) diff --git a/models.py b/models.py index 8318b132..c83e8fad 100644 --- a/models.py +++ b/models.py @@ -13,6 +13,8 @@ class User(db.Model): email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(120), nullable=False) + todos = db.relationship('Todo', backref='user', lazy=True, cascade="all, delete-orphan") + def __init__(self, username, email, password): self.username = username self.email = email @@ -24,3 +26,69 @@ def set_password(self, password): def __repr__(self): return f'' + +# Add method to User model in models.py +def add_todo_category(self, todo_id, category_text): + # Fetch the todo by id + todo = Todo.query.filter_by(id=todo_id, user_id=self.id).first() + # Make sure the todo exists and belongs to the current user + if not todo: + return False + + # Check if category already exists for current user + category = Category.query.filter_by(text=category_text, user_id=self.id).first() + if not category: + # Create new category + category = Category(user_id=self.id, text=category_text) + db.session.add(category) + db.session.commit() + + # Associate todo with the category if not already associated + if category not in todo.categories: + todo.categories.append(category) + db.session.add(todo) + db.session.commit() + + return True + +class Todo(db.Model): + id = db.Column(db.Integer, primary_key=True) + user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) #set userid as a foreign key to user.id + text = db.Column(db.String(255), nullable=False) + done = db.Column(db.Boolean, default=False) + + def toggle(self): + self.done = not self.done + db.session.add(self) + db.session.commit() + + def __init__(self, text): + self.text = text + + def __repr__(self): + + return f'' + +class TodoCategory(db.Model): + __tablename__ ='todo_category' + id = db.Column(db.Integer, primary_key=True) + todo_id = db.Column(db.Integer, db.ForeignKey('todo.id'), nullable=False) + category_id = db.Column(db.Integer, db.ForeignKey('category.id'), nullable=False) + last_modified = db.Column(db.DateTime, default=func.now(), onupdate=func.now()) + + def __repr__(self): + return f'' + +class Category(db.Model): + id = db.Column(db.Integer, primary_key=True) + user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) + text = db.Column(db.String(255), nullable=False) + user = db.relationship('User', backref=db.backref('categories', lazy='joined')) + todos = db.relationship('Todo', secondary='todo_category', backref=db.backref('categories', lazy=True)) + + def __init__(self, user_id, text): + self.user_id = user_id + self.text = text + + def __repr__(self): + return f'' \ No newline at end of file diff --git a/wsgi.py b/wsgi.py index 949606df..96c89f37 100644 --- a/wsgi.py +++ b/wsgi.py @@ -1,5 +1,5 @@ import click, sys -from models import db, User +from models import db, User,Todo from app import app from sqlalchemy.exc import IntegrityError @@ -10,6 +10,7 @@ def initialize(): db.init_app(app) db.create_all() bob = User('bob', 'bob@mail.com', 'bobpass') + bob.todos.append(Todo('wash car')) print(bob) print('database intialized') db.session.add(bob) @@ -60,4 +61,71 @@ def create_user(username, email, password): # print(e.orig) #optionally print the error raised by the database print("Username or email already taken!") #give the user a useful message else: - print(newuser) # print the newly created user \ No newline at end of file + print(newuser) # print the newly created user + +@app.cli.command('delete-user') +@click.argument('username', default='bob') +def delete_user(username): + bob = User.query.filter_by(username=username).first() + if not bob: + print(f'{username} not found!') + return + db.session.delete(bob) + db.session.commit() + print(f'{username} deleted') + +@app.cli.command('get-todos') +@click.argument('username', default='bob') +def get_user_todos(username): + bob = User.query.filter_by(username=username).first() + if not bob: + print(f'{username} not found!') + return + print(bob.todos) + +@app.cli.command('add-todo') +@click.argument('username', default='bob') +@click.argument('text', default='wash car') +def add_task(username, text): + bob = User.query.filter_by(username=username).first() + if not bob: + print(f'{username} not found!') + return + new_todo = Todo(text) + bob.todos.append(new_todo) + db.session.add(bob) + db.session.commit() + +@click.argument('todo_id', default=1) +@click.argument('username', default='bob') +@app.cli.command('toggle-todo') +def toggle_todo_command(todo_id, username): + user = User.query.filter_by(username=username).first() + if not user: + print(f'{username} not found!') + return + + todo = Todo.query.filter_by(id=todo_id, user_id=user.id).first() + if not todo: + print(f'{username} has no todo id {todo_id}') + + todo.toggle() + print(f'{todo.text} is {"done" if todo.done else "not done"}!') + +@click.argument('category', default='chores') +@click.argument('username', default='bob') +@click.argument('todo_id', default=1) +@app.cli.command('add-category', help="Adds a category to a todo") +def add_todo_category_command(category, todo_id, username,): + user = User.query.filter_by(username=username).first() + if not user: + print(f'user {username} not found!') + return + + res = user.add_todo_category(todo_id, category) + if not res: + print(f'{username} has no todo id {todo_id}') + return + + todo = Todo.query.get(todo_id) + print(todo) \ No newline at end of file