From 536dc0fd97b01da1d493097d1ca3635eb8c7be9d Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Mon, 8 Jan 2024 12:19:55 +0200 Subject: [PATCH] dding event_user table --- app.py | 2 ++ middlewares/eventMiddlewares.py | 4 +++- models.py | 14 +++++++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 2423249..c45bb06 100644 --- a/app.py +++ b/app.py @@ -8,6 +8,7 @@ from flask_jwt_extended import JWTManager from middlewares.errorHandlers import handle_auth_error, handle_invalid_token from flask_jwt_extended.exceptions import NoAuthorizationError from jwt.exceptions import InvalidTokenError +from datetime import timedelta class App: def __init__(self): @@ -34,6 +35,7 @@ class App: self.app.config['JWT_TOKEN_LOCATION'] = ['cookies'] self.app.config['JWT_COOKIE_NAME'] = 'access_token_cookie' self.app.config['JWT_COOKIE_CSRF_PROTECT'] = False + self.app.config['JWT_ACCESS_TOKEN_EXPIRES'] = timedelta(days=30) def register_blueprints(self): self.app.register_blueprint(userRoutes, url_prefix='/user') diff --git a/middlewares/eventMiddlewares.py b/middlewares/eventMiddlewares.py index 6af5ac2..4eb242e 100644 --- a/middlewares/eventMiddlewares.py +++ b/middlewares/eventMiddlewares.py @@ -24,7 +24,9 @@ def validate_event_post_request(f): return jsonify({"message": "Invalid description"}), 400 # Validate 'time' (ensure it's a valid datetime string) try: - datetime.strptime(data['duedate'], '%Y-%m-%dT%H:%M:%S') + time = datetime.strptime(data['duedate'], '%Y-%m-%dT%H:%M:%S') + if(time < datetime.now()): + return jsonify({"message": "Invalid time"}), 400 except ValueError: return jsonify({"message": "Invalid time format. Use YYYY-MM-DDTHH:MM:SS"}), 400 diff --git a/models.py b/models.py index fc0e07e..91b6615 100644 --- a/models.py +++ b/models.py @@ -1,10 +1,17 @@ from flask_sqlalchemy import SQLAlchemy from flask_bcrypt import Bcrypt +from sqlalchemy import Table, Column, Integer, ForeignKey, String import uuid db = SQLAlchemy() bcrypt = Bcrypt() + +user_event_association = Table('user_event', db.Model.metadata, + Column('user_id', String(36), ForeignKey('user.id')), + Column('event_id', Integer, ForeignKey('event.id')) +) + class Event(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) @@ -14,6 +21,8 @@ class Event(db.Model): duedate = db.Column(db.DateTime) created_at = db.Column(db.DateTime, default=db.func.now()) user_id = db.Column(db.String(36), db.ForeignKey('user.id'), nullable=False) + users = db.relationship('User', secondary=user_event_association, back_populates='events') + def to_dict(self): return { @@ -32,6 +41,8 @@ class User(db.Model): email = db.Column(db.String(120), unique=True, nullable=False) password_hash = db.Column(db.String(128)) location = db.Column(db.String(100), nullable=False) + events = db.relationship('Event', secondary=user_event_association, back_populates='users') + def set_password(self, password): @@ -46,4 +57,5 @@ class User(db.Model): 'name': self.name, 'email': self.email, 'location': self.location - } \ No newline at end of file + } +