Skip to content
Snippets Groups Projects

auth

Files

+ 75
13
@@ -4,6 +4,8 @@ Module to interact with the database
from datetime import date, datetime, time, timedelta
from sqlalchemy.orm import Session
from sqlalchemy.sql import func
from uuid import uuid4
import secrets
import pytz
from db import models, schemas
@@ -177,31 +179,33 @@ def get_current_graph(place: str, db: Session):
# Define CRUD operation for the comments
def get_comments(place: str, page: int, db: Session):
""" Get the 10 last comments for the given place """
""" Get the 20 last comments for the given place """
if page == 0:
comments = db.query(models.Comments).order_by(models.Comments.published_at.desc(), models.Comments.id.desc()).all()
else:
comments = db.query(
models.Comments).filter(
models.Comments,
models.Users.username).join(
models.Users).filter(
models.Comments.place == place).order_by(
models.Comments.published_at.desc(),
models.Comments.id.desc()).slice(
(page -
1) *
10,
page *
10).all()
return comments
models.Comments.published_at.desc(),
models.Comments.id.desc()).slice(
(page -
1) *
20,
page *
20).all()
return list(schemas.Comment(**comment.__dict__, username=username) for comment, username in comments)
def create_comment(place: str, new_comments: schemas.CommentBase, db: Session):
def create_comment(user: schemas.User, place: str, new_comments: schemas.CommentBase, db: Session):
""" Add a new comment to the database """
date = datetime.now(tz=pytz.timezone("Europe/Paris"))
db_comment = models.Comments(**new_comments.dict(), published_at=date, place=place)
db_comment = models.Comments(**new_comments.dict(), published_at=date, place=place, user_id=user.id)
db.add(db_comment)
db.commit()
db.refresh(db_comment)
return db_comment
return schemas.Comment(**db_comment.__dict__, username=user.username)
def delete_comment(id: int, db: Session):
@@ -337,3 +341,61 @@ def get_restaurants(db: Session):
restaurants.append(restaurant)
return restaurants
# Define CRUD operation for the authentication
def init_user(db: Session):
""" Add a news to the database """
cookie = uuid4()
state = secrets.token_urlsafe(30)
expiration_date = datetime.now(tz=pytz.timezone("Europe/Paris")) + timedelta(minutes=10)
db_user = models.Users(state=state, cookie=cookie, expiration_date=expiration_date)
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user
def get_user(cookie: str, db: Session):
""" Get user infos """
user = db.query(models.Users).filter(models.Users.cookie == cookie).one()
if pytz.timezone("Europe/Paris").localize(user.expiration_date) < datetime.now(tz=pytz.timezone("Europe/Paris")):
return
return user
def delete_state(user: schemas.User, db: Session):
""" Delete the state of a user """
user.state = None
db.add(user)
db.commit()
def update_user(user: schemas.User, user_info: dict, db: Session):
full_name = f"{user_info['firstName']} {user_info['lastName']}"
expiration_date = datetime.now(tz=pytz.timezone("Europe/Paris")) + timedelta(days=3)
existing_user = db.query(models.Users).filter(models.Users.username == full_name).first()
if existing_user:
existing_user.cookie = user.cookie
existing_user.expiration_date = expiration_date
db.delete(user)
db.add(existing_user)
db.commit()
db.refresh(existing_user)
return existing_user
else:
user.username = full_name
user.expiration_date = expiration_date
db.add(user)
db.commit()
db.refresh(user)
return user
def end_session(cookie: str, db: Session):
user = db.query(models.Users).filter(models.Users.cookie == cookie).one()
user.expiration_date = datetime.now(tz=pytz.timezone("Europe/Paris"))
db.add(user)
db.commit()
return
Loading