Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
NidalChateur committed Feb 19, 2024
2 parents eb7a5c1 + 7ba2cce commit b668989
Show file tree
Hide file tree
Showing 25 changed files with 1,013 additions and 93 deletions.
8 changes: 4 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""Script implementing the MVC pattern using SQLAlchemy ORM and WTForms
To activate Sentry :
1. Create a new Sentry project at https://sentry.io/ and get your dsn_key
2. in the .env file, replace 'DEBUG=True' by 'SENTRY=dsn_key'
1. Create a new Sentry project at https://sentry.io/ and get your 'dsn_key'
2. in the .env file, set SENTRY_DSN='dsn_key' without any quotes
"""

from src.controllers.login import Controller
from src.utils.shell import clear_shell

if __name__ == "__main__":
Controller.clear_shell()
clear_shell()
Controller.run()
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ htmldir = flake8_html_report
python_files = test*

[coverage:run]
omit = tests/*, main.py, src/settings/*
omit = tests/*, main.py, src/settings/*, shell.py, fernet.py

12 changes: 11 additions & 1 deletion src/controllers/collaborator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from ..models.collaborator import Collaborator
from ..models.department import Department
from ..utils.fernet import Fernet
from ..utils.session import session_is_expired
from ..views.collaborator import View

model1 = Collaborator
Expand All @@ -14,8 +15,17 @@ class Controller:
def run(self, session, user):
self._menu(session, user)

@classmethod
def return_to_menu(self, session, user):
self._menu(session, user)

@classmethod
def _menu(self, session, user) -> bool:
if session_is_expired(user):
View.logout()

return None

View.print_menu()

choice = View.get_user_choice()
Expand Down Expand Up @@ -43,7 +53,7 @@ def _menu(self, session, user) -> bool:
if choice == 6:
self._delete(session)

self._menu(session, user)
self.return_to_menu(session, user)

@classmethod
def _list(self, session):
Expand Down
12 changes: 11 additions & 1 deletion src/controllers/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from ..models.contract_event import Contract
from ..models.customer import Customer
from ..utils.session import session_is_expired
from ..utils.slugify import slugify
from ..views.contract import View

Expand All @@ -14,8 +15,17 @@ class Controller:
def run(self, session, user):
self._menu(session, user)

@classmethod
def return_to_menu(self, session, user):
self._menu(session, user)

@classmethod
def _menu(self, session, user) -> bool:
if session_is_expired(user):
View.logout()

return None

if user.role == "Gestion":
View.print_gestion_menu()

Expand Down Expand Up @@ -56,7 +66,7 @@ def _menu(self, session, user) -> bool:
if choice == 8 and user.role == "Gestion":
self._delete(session)

self._menu(session, user)
self.return_to_menu(session, user)

@classmethod
def _list(self, session):
Expand Down
12 changes: 11 additions & 1 deletion src/controllers/customer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=no-member
from ..models.company import Company
from ..models.customer import Customer
from ..utils.session import session_is_expired
from ..utils.slugify import slugify
from ..views.customer import View

Expand All @@ -10,8 +11,17 @@ class Controller:
def run(self, session, user):
self._menu(session, user)

@classmethod
def return_to_menu(self, session, user):
self._menu(session, user)

@classmethod
def _menu(self, session, user) -> bool:
if session_is_expired(user):
View.logout()

return None

if user.role == "Commercial":
View.print_commercial_menu()
else:
Expand Down Expand Up @@ -42,7 +52,7 @@ def _menu(self, session, user) -> bool:
if choice == 6 and user.role == "Commercial":
self._delete(session, user)

self._menu(session, user)
self.return_to_menu(session, user)

@classmethod
def _list(self, session):
Expand Down
12 changes: 11 additions & 1 deletion src/controllers/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from ..models.collaborator import Collaborator
from ..models.contract_event import Contract, Event
from ..models.location import Location
from ..utils.session import session_is_expired
from ..utils.slugify import slugify
from ..views.event import View

Expand All @@ -14,8 +15,17 @@ class Controller:
def run(self, session, user):
self._menu(session, user)

@classmethod
def return_to_menu(self, session, user):
self._menu(session, user)

@classmethod
def _menu(self, session, user) -> bool:
if session_is_expired(user):
View.logout()

return None

if user.role == "Gestion":
View.print_gestion_menu()

Expand Down Expand Up @@ -59,7 +69,7 @@ def _menu(self, session, user) -> bool:
if choice == 6 and user.role == "Commercial":
self._delete(session, user)

self._menu(session, user)
self.return_to_menu(session, user)

@classmethod
def _list(self, session):
Expand Down
14 changes: 13 additions & 1 deletion src/controllers/home.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ..utils.session import session_is_expired
from ..views.home import View
from . import collaborator, contract, customer, event

Expand All @@ -7,8 +8,17 @@ class Controller:
def run(self, session, user):
self._menu(session, user)

@classmethod
def return_to_menu(self, session, user):
self._menu(session, user)

@classmethod
def _menu(self, session, user):
if session_is_expired(user):
View.logout()

return None

if user.role == "Gestion":
View.print_gestion_menu()
else:
Expand All @@ -19,6 +29,8 @@ def _menu(self, session, user):
if choice == 0:
View.logout()

return None

if choice == 1:
customer.Controller.run(session, user)

Expand All @@ -31,4 +43,4 @@ def _menu(self, session, user):
if choice == 4 and user.role == "Gestion":
collaborator.Controller.run(session, user)

self._menu(session, user)
self.return_to_menu(session, user)
36 changes: 21 additions & 15 deletions src/controllers/login.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import os

from ..forms.collaborator import FirstConnexionForm
from ..models.collaborator import Collaborator
from ..models.department import Department
Expand All @@ -10,18 +8,31 @@


class Controller:
session = init_db()

@classmethod
def run(self):
session = init_db()
# session = init_db()

# collaborators = session.query(Collaborator).all()

collaborators = session.query(Collaborator).all()
# if collaborators:
# self._menu(session)
# else:
# self._create_gestion_account(session)
# self._menu(session)

collaborators = self.session.query(Collaborator).all()

if collaborators:
self._menu(session)
self._menu(self.session)
else:
self._create_gestion_account(session)
self._create_gestion_account(self.session)
self._menu(self.session)

self.run()
@classmethod
def return_to_menu(self, session):
self._menu(session)

@classmethod
def _menu(self, session):
Expand All @@ -33,6 +44,8 @@ def _menu(self, session):
# 1. quit
View.logout()

return None

if choice == 1:
# 1. login
user = self._login(session)
Expand All @@ -48,7 +61,7 @@ def _menu(self, session):
user = self._first_login(session)
self._create_password(session, user)

self._menu(session)
self.return_to_menu(session)

@classmethod
def _create_gestion_account(self, session):
Expand Down Expand Up @@ -172,10 +185,3 @@ def _create_password(self, session, user):
View.print_password_update_success()
else:
View.print_forms_errors(form)

@classmethod
def clear_shell(self):
if os.name == "posix": # Unix/Linux
os.system("clear")
if os.name == "nt": # Windows
os.system("cls")
6 changes: 4 additions & 2 deletions src/models/collaborator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from datetime import datetime

import bcrypt
from sqlalchemy import Column, Date, DateTime, ForeignKey, Integer, String, func
from sqlalchemy import Column, Date, DateTime, ForeignKey, Integer, String
from sqlalchemy.orm import relationship

from src.utils.fernet import Fernet
Expand Down Expand Up @@ -87,7 +89,7 @@ def check_password(self, password: str, hashed_password: bytes) -> bool:
def authenticate(self, session, email: str, password: str):
collaborator = self.get_with_clear_email(session, email)
if collaborator and self.check_password(password, collaborator.password):
collaborator.last_login = func.now()
collaborator.last_login = datetime.utcnow()
session.commit()

return collaborator
Expand Down
2 changes: 0 additions & 2 deletions src/settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ def create_fernet() -> Fernet:

fernet = create_fernet()

PUBLIC_KEY = "public_key"


def init_db(Base=Base):
engine = create_engine("sqlite:///" + str(BASE_DIR / "db.sqlite3"))
Expand Down
37 changes: 0 additions & 37 deletions src/utils/jwt_token.py

This file was deleted.

8 changes: 8 additions & 0 deletions src/utils/session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from datetime import datetime, timedelta


def session_is_expired(user) -> bool:
"""used to disconnect the user after 12 hours"""

if datetime.utcnow() > user.last_login + timedelta(hours=12):
return True
8 changes: 8 additions & 0 deletions src/utils/shell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os


def clear_shell():
if os.name == "posix": # Unix/Linux
os.system("clear")
if os.name == "nt": # Windows
os.system("cls")
9 changes: 2 additions & 7 deletions src/views/collaborator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,8 @@ def print_menu(self):
console.print(f"\nMenu {self.name.title()}", style="bold", justify="center")
console.print("__________________", justify="center")

console.print("\n0. Retour", style="bold", justify="center")
console.print("\n1. Lister", style="bold", justify="center")
console.print("\n2. Rechercher", style="bold", justify="center")
console.print("\n3. Détail", style="bold", justify="center")
console.print("\n4. Créer", style="bold", justify="center")
console.print("\n5. Modifier", style="bold", justify="center")
console.print("\n6. Supprimer", style="bold", justify="center")
self.print_read_only_menu()
self.print_edit_menu()

@classmethod
def print_list(self, qs: list, list_name: str, page_size=5):
Expand Down
5 changes: 1 addition & 4 deletions src/views/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ def print_menu(self):
console.print(f"\nMenu {self.name.title()}", style="bold", justify="center")
console.print("____________", justify="center")

console.print("\n0. Retour", style="bold", justify="center")
console.print("\n1. Lister", style="bold", justify="center")
console.print("\n2. Rechercher", style="bold", justify="center")
console.print("\n3. Détail", style="bold", justify="center")
self.print_read_only_menu()

@classmethod
def print_list(self, qs: list[model], list_name: str, page_size=5):
Expand Down
Loading

0 comments on commit b668989

Please sign in to comment.