Skip to content

Commit

Permalink
refactor(backend): passed to UUID system (remove snowflake)
Browse files Browse the repository at this point in the history
  • Loading branch information
EliotAmn committed Jan 30, 2025
1 parent bce02f0 commit 315cc23
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
4 changes: 2 additions & 2 deletions app/models/Student.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ def to_dict(self):

@property
def id(self):
return self._id
return str(self._id)

def __init__(self, mongo_data=None):
if mongo_data is None:
return
self._id = mongo_data["_id"]
self._id = str(mongo_data["_id"])
self.login = mongo_data["login"]
self.password_hash = mongo_data.get("password_hash", None)
self.first_name = mongo_data.get("first_name", None)
Expand Down
19 changes: 7 additions & 12 deletions app/services/student_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
import random
import string
from datetime import datetime, timedelta

from snowflake import SnowflakeGenerator

from uuid import uuid4
from app.globals import Globals
from app.models.Student import Student
from app.services.mail_service import MailService
Expand All @@ -22,14 +20,14 @@ def get_student_by_login(login: str) -> Student:

@staticmethod
def get_student_by_id(student_id: str) -> Student or None:
student = Globals.database["students"].find_one({"_id": int(student_id)})
student = Globals.database["students"].find_one({"_id": student_id})
return Student(student) if student else None

@staticmethod
def filter_share_consent(student_ids: [str]) -> [str]:
students = Globals.database["students"].find(
{"_id": {"$in": [int(sid) for sid in student_ids]}, "is_consent_share": True})
return [str(student["_id"]) for student in students if student]
{"_id": {"$in": [sid for sid in student_ids]}, "is_consent_share": True})
return [student["_id"] for student in students if student]

@staticmethod
def get_students_by_public_scraper(scraper_id: str) -> [Student]:
Expand All @@ -52,8 +50,7 @@ def get_public_scraper_students() -> [Student]:
def add_student(student: Student):
if StudentService.get_student_by_login(student.login):
return
gen = SnowflakeGenerator(42)
student._id = next(gen)
student._id = str(uuid4()).replace("-", "")
student.last_update = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
Globals.database["students"].insert_one(student.to_dict())
StudentService.regenerate_scraper_token(student)
Expand All @@ -62,17 +59,15 @@ def add_student(student: Student):
@staticmethod
def update_student(student: Student):
student.last_update = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
Globals.database["students"].update_one({"_id": student.id},
{"$set": student.to_dict()})
Globals.database["students"].update_one({"_id": student.id}, {"$set": student.to_dict()})

@staticmethod
def delete_student(student: Student):
Globals.database["students"].delete_one({"_id": student.id})

@staticmethod
def get_student_by_scrapetoken(token: str) -> Student:
student = Globals.database["students"].find_one(
{"scraper_token": token})
student = Globals.database["students"].find_one({"scraper_token": token})
return Student(student) if student else None

@staticmethod
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ pycparser~=2.22
wsproto~=1.2.0
webdriver-manager~=4.0.2
PyJWT~=2.10.1
snowflake-id~=1.0.2
ics~=0.7.2

0 comments on commit 315cc23

Please sign in to comment.