dding event_user table
This commit is contained in:
parent
d057fee986
commit
536dc0fd97
3 changed files with 18 additions and 2 deletions
2
app.py
2
app.py
|
@ -8,6 +8,7 @@ from flask_jwt_extended import JWTManager
|
||||||
from middlewares.errorHandlers import handle_auth_error, handle_invalid_token
|
from middlewares.errorHandlers import handle_auth_error, handle_invalid_token
|
||||||
from flask_jwt_extended.exceptions import NoAuthorizationError
|
from flask_jwt_extended.exceptions import NoAuthorizationError
|
||||||
from jwt.exceptions import InvalidTokenError
|
from jwt.exceptions import InvalidTokenError
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
class App:
|
class App:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -34,6 +35,7 @@ class App:
|
||||||
self.app.config['JWT_TOKEN_LOCATION'] = ['cookies']
|
self.app.config['JWT_TOKEN_LOCATION'] = ['cookies']
|
||||||
self.app.config['JWT_COOKIE_NAME'] = 'access_token_cookie'
|
self.app.config['JWT_COOKIE_NAME'] = 'access_token_cookie'
|
||||||
self.app.config['JWT_COOKIE_CSRF_PROTECT'] = False
|
self.app.config['JWT_COOKIE_CSRF_PROTECT'] = False
|
||||||
|
self.app.config['JWT_ACCESS_TOKEN_EXPIRES'] = timedelta(days=30)
|
||||||
|
|
||||||
def register_blueprints(self):
|
def register_blueprints(self):
|
||||||
self.app.register_blueprint(userRoutes, url_prefix='/user')
|
self.app.register_blueprint(userRoutes, url_prefix='/user')
|
||||||
|
|
|
@ -24,7 +24,9 @@ def validate_event_post_request(f):
|
||||||
return jsonify({"message": "Invalid description"}), 400
|
return jsonify({"message": "Invalid description"}), 400
|
||||||
# Validate 'time' (ensure it's a valid datetime string)
|
# Validate 'time' (ensure it's a valid datetime string)
|
||||||
try:
|
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:
|
except ValueError:
|
||||||
return jsonify({"message": "Invalid time format. Use YYYY-MM-DDTHH:MM:SS"}), 400
|
return jsonify({"message": "Invalid time format. Use YYYY-MM-DDTHH:MM:SS"}), 400
|
||||||
|
|
||||||
|
|
14
models.py
14
models.py
|
@ -1,10 +1,17 @@
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from flask_bcrypt import Bcrypt
|
from flask_bcrypt import Bcrypt
|
||||||
|
from sqlalchemy import Table, Column, Integer, ForeignKey, String
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
db = SQLAlchemy()
|
db = SQLAlchemy()
|
||||||
bcrypt = Bcrypt()
|
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):
|
class Event(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
title = db.Column(db.String(100), nullable=False)
|
title = db.Column(db.String(100), nullable=False)
|
||||||
|
@ -14,6 +21,8 @@ class Event(db.Model):
|
||||||
duedate = db.Column(db.DateTime)
|
duedate = db.Column(db.DateTime)
|
||||||
created_at = db.Column(db.DateTime, default=db.func.now())
|
created_at = db.Column(db.DateTime, default=db.func.now())
|
||||||
user_id = db.Column(db.String(36), db.ForeignKey('user.id'), nullable=False)
|
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):
|
def to_dict(self):
|
||||||
return {
|
return {
|
||||||
|
@ -32,6 +41,8 @@ class User(db.Model):
|
||||||
email = db.Column(db.String(120), unique=True, nullable=False)
|
email = db.Column(db.String(120), unique=True, nullable=False)
|
||||||
password_hash = db.Column(db.String(128))
|
password_hash = db.Column(db.String(128))
|
||||||
location = db.Column(db.String(100), nullable=False)
|
location = db.Column(db.String(100), nullable=False)
|
||||||
|
events = db.relationship('Event', secondary=user_event_association, back_populates='users')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def set_password(self, password):
|
def set_password(self, password):
|
||||||
|
@ -46,4 +57,5 @@ class User(db.Model):
|
||||||
'name': self.name,
|
'name': self.name,
|
||||||
'email': self.email,
|
'email': self.email,
|
||||||
'location': self.location
|
'location': self.location
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue