Skip to content

Commit

Permalink
move ids to uuid (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgirardet authored Aug 30, 2020
1 parent 3d732f3 commit 3ed4a25
Show file tree
Hide file tree
Showing 48 changed files with 1,389 additions and 868 deletions.
14 changes: 7 additions & 7 deletions src/mycartable/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ def main_init_database(filename=None, prod=False):
filename=filename, create_db=create_db
)

if not prod:
from tests.python.factory import populate_database

populate_database()
# if not prod:
# from tests.python.factory import populate_database
#
# populate_database()

return package.database.db

Expand Down Expand Up @@ -82,9 +82,9 @@ def create_singleton_instance(prod=False):
ui_manager = UiManager()
databaseObject.ui = ui_manager

if not prod:
databaseObject.anneeActive = 2019
databaseObject.currentMatiere = 2
# if not prod:
# databaseObject.anneeActive = 2019
# databaseObject.currentMatiere = 2

return databaseObject, ui_manager

Expand Down
6 changes: 6 additions & 0 deletions src/mycartable/package/database/mixins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from contextlib import contextmanager
from uuid import UUID

from PySide2.QtGui import QColor
from pony.orm import Required
Expand Down Expand Up @@ -137,6 +138,8 @@ def position(self, new):
@classmethod
def get_by_position(cls, ref_id):
base_class = cls.base_class_position or cls
if isinstance(ref_id, str):
ref_id = UUID(ref_id)
return select(
p
for p in base_class
Expand Down Expand Up @@ -167,13 +170,16 @@ def _recalcule_position(self, old, new, query=None):
for sec in query:
if new <= sec.position < old:
sec._position += 1
print(new, self)
return new

@contextmanager
def init_position(self, pos, ref):
base_class = self.base_class_position or self.__class__
if isinstance(ref, (Entity, EntityProxy)):
ref = ref.id
elif isinstance(ref, str):
ref = UUID(ref)
nb = self.get_by_position(ref).count()
if pos is not None:
if pos >= nb:
Expand Down
28 changes: 21 additions & 7 deletions src/mycartable/package/database/sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import datetime
import re
from io import BytesIO
from uuid import UUID, uuid4

from PySide2.QtGui import QColor
from functools import cached_property
Expand All @@ -15,13 +16,18 @@
class Section(db.Entity, PositionMixin):
referent_attribute_name = "page"

id = PrimaryKey(int, auto=True)
id = PrimaryKey(UUID, auto=True, default=uuid4)
created = Required(datetime, default=datetime.utcnow)
modified = Optional(datetime)
page = Required("Page")
_position = Required(int)
annotations = Set("Annotation")

def __repr__(self):
return (
f"{self.__class__.__name__}[page={self.page.id}, position={self._position}]"
)

def __new__(cls, *args, **kwargs):
cls.base_class_position = Section
return super().__new__(cls)
Expand All @@ -30,11 +36,17 @@ def __init__(self, *args, page=None, position=None, **kwargs):
with self.init_position(position, page) as _position:
super().__init__(*args, _position=_position, page=page, **kwargs)

def to_dict(self, *args, **kwargs):
dico = super().to_dict(*args, **kwargs)
dico["created"] = self.created.isoformat()
dico["modified"] = self.modified.isoformat()
dico["position"] = dico.pop("_position")
def to_dict(self, **kwargs):
dico = super().to_dict(exclude=["_position"], **kwargs)
dico.update(
{
"id": str(self.id),
"created": self.created.isoformat(),
"modified": self.modified.isoformat(),
"position": self.position,
"page": str(self.page.id),
}
)
return dico

def before_insert(self):
Expand Down Expand Up @@ -297,7 +309,7 @@ def set(self, *, content, curseur, **kwargs):


class Annotation(db.Entity):
id = PrimaryKey(int, auto=True)
id = PrimaryKey(UUID, auto=True, default=uuid4)
x = Required(float)
y = Required(float)
section = Required(Section)
Expand All @@ -314,6 +326,7 @@ def as_type(self):
def to_dict(self, **kwargs):
dico = super().to_dict(**kwargs, related_objects=True)
dico.update(dico.pop("style").to_dict())
dico["id"] = str(self.id)
return dico

def set(self, **kwargs):
Expand Down Expand Up @@ -488,6 +501,7 @@ def to_dict(self, *args, **kwargs):
dico = super().to_dict(*args, **kwargs)
if "style" in dico: # pragma: no branch # ne pas l'ajouter sur a été exclude
dico["style"] = self.style.to_dict()
dico["tableau"] = str(self.tableau.id)
return dico

def set(self, **kwargs):
Expand Down
86 changes: 61 additions & 25 deletions src/mycartable/package/database/structure.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime, timedelta
from uuid import UUID, uuid4

from pony.orm import (
select,
Expand All @@ -23,10 +24,15 @@ def get_matieres(self):
matiere for groupe in self.groupes for matiere in groupe.matieres
).order_by(lambda m: (m.groupe.position, m.position))

def to_dict(self, *args, **kwargs):
dico = super().to_dict(*args, **kwargs)
dico["user"] = str(dico["user"])
return dico


class GroupeMatiere(db.Entity, PositionMixin, ColorMixin):
referent_attribute_name = "annee"
id = PrimaryKey(int, auto=True)
id = PrimaryKey(UUID, auto=True, default=uuid4)
nom = Required(str)
annee = Required(Annee)
_position = Required(int)
Expand All @@ -39,6 +45,9 @@ def __init__(self, position=None, annee=None, bgColor=None, fgColor=None, **kwar
with self.init_position(position, annee) as _position:
super().__init__(annee=annee, _position=_position, **kwargs)

def __repr__(self):
return f"Groupe[{self.nom} {self.annee.id}]"

def before_delete(self):
self.before_delete_position()

Expand All @@ -50,19 +59,20 @@ def to_dict(self, **kwargs):
dico["position"] = dico.pop("_position")
dico["fgColor"] = self.fgColor
dico["bgColor"] = self.bgColor
dico["id"] = str(self.id)
return dico


class Matiere(db.Entity, ColorMixin, PositionMixin):
referent_attribute_name = "groupe"

id = PrimaryKey(int, auto=True)
id = PrimaryKey(UUID, auto=True, default=uuid4)
nom = Required(str)
activites = Set("Activite")
groupe = Required("GroupeMatiere")
_position = Required(int)
_fgColor = Required(int, size=32, unsigned=True, default=4278190080)
_bgColor = Optional(int, size=32, unsigned=True, default=4294967295)
activites = Set("Activite")

def __init__(
self, bgColor=None, fgColor=None, groupe=None, position=None, **kwargs
Expand All @@ -71,20 +81,31 @@ def __init__(
with self.init_position(position, groupe) as _position:
super().__init__(groupe=groupe, _position=_position, **kwargs)

def __repr__(self):
return f"Matiere[{self.nom}]"

@property
def activites_list(self):
return self.to_dict()["activites"]

def to_dict(self, *args, **kwargs):
res = super().to_dict(
*args, with_collections=True, exclude=["_fgColor", "_bgColor"], **kwargs
def to_dict(self):
dico = super().to_dict(exclude=["_fgColor", "_bgColor", "_position"])
dico.update(
{
"id": str(self.id),
"groupe": str(self.groupe.id),
"fgColor": self.fgColor,
"bgColor": self.bgColor,
"position": self.position,
"activites": [
str(x.id) for x in self.activites.order_by(lambda x: x.position)
],
}
)
res["fgColor"] = self.fgColor
res["bgColor"] = self.bgColor
res["position"] = res.pop("_position")
return res
return dico

def pages_par_section(self):
"""devrait s'appeler page par activite"""
res = []
for x in Activite.get_by_position(self.id): # .order_by(Activite.famille):
entry = x.to_dict()
Expand All @@ -105,11 +126,11 @@ def after_delete(self):
class Activite(db.Entity, PositionMixin):
referent_attribute_name = "matiere"

id = PrimaryKey(int, auto=True)
id = PrimaryKey(UUID, auto=True, default=uuid4)
nom = Required(str)
matiere = Required(Matiere)
pages = Set("Page")
_position = Required(int)
pages = Set("Page")

def __init__(self, position=None, matiere=None, **kwargs):
with self.init_position(position, matiere) as _position:
Expand All @@ -121,20 +142,27 @@ def before_delete(self):
def after_delete(self):
self.after_delete_position()

def to_dict(self, **kwargs):
dico = super().to_dict(**kwargs)
dico["position"] = dico.pop("_position")
def to_dict(self,):
dico = super().to_dict(exclude=["_position"])
dico.update(
{
"id": str(self.id),
"position": self.position,
"matiere": str(self.matiere.id),
"pages": [str(p.id) for p in self.pages],
}
)
return dico


class Page(db.Entity):
id = PrimaryKey(int, auto=True)
id = PrimaryKey(UUID, auto=True, default=uuid4)
created = Required(datetime, default=datetime.utcnow)
modified = Optional(datetime)
titre = Optional(str, default="")
activite = Required("Activite")
sections = Set("Section")
lastPosition = Optional(int, default=0)
sections = Set("Section")

def before_insert(self):
self.modified = self.created
Expand All @@ -145,14 +173,22 @@ def before_update(self):
else:
self.modified = datetime.utcnow()

def to_dict(self, *args, **kwargs):
dico = super().to_dict(*args, **kwargs)
dico["matiere"] = self.activite.matiere.id
dico["matiereNom"] = self.activite.matiere.nom
dico["matiereFgColor"] = self.activite.matiere.fgColor
dico["matiereBgColor"] = self.activite.matiere.bgColor
dico["created"] = self.created.isoformat()
dico["modified"] = self.created.isoformat()
def to_dict(self):
dico = super().to_dict()
dico.update(
{
"id": str(self.id),
"created": self.created.isoformat(),
"modified": self.modified.isoformat(),
"activite": str(self.activite.id),
"lastPosition": self.lastPosition,
# "sections": [str(s.id) for s in self.sections],
"matiere": str(self.activite.matiere.id),
"matiereNom": self.activite.matiere.nom,
"matiereFgColor": self.activite.matiere.fgColor,
"matiereBgColor": self.activite.matiere.bgColor,
}
)
return dico

def _query_recents(self, annee):
Expand Down
5 changes: 4 additions & 1 deletion src/mycartable/package/database/style.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from uuid import UUID, uuid4

from package.database.mixins import ColorMixin
from pony.orm import Required, Optional, Set, PrimaryKey

from .root_db import db


class Style(db.Entity, ColorMixin):
styleId = PrimaryKey(int, auto=True)
styleId = PrimaryKey(UUID, auto=True, default=uuid4)
_fgColor = Required(int, size=32, unsigned=True, default=4278190080)
_bgColor = Required(int, size=32, unsigned=True, default=0)
family = Optional(str)
Expand All @@ -28,6 +30,7 @@ def to_dict(self, **kwargs):
)
dico["bgColor"] = self.bgColor
dico["fgColor"] = self.fgColor
dico["styleId"] = str(self.styleId)
return dico

def set(self, **kwargs):
Expand Down
11 changes: 10 additions & 1 deletion src/mycartable/package/database/utilisateur.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from pony.orm import Required, Set, Optional
from uuid import UUID, uuid4

from pony.orm import Required, Set, Optional, PrimaryKey

from .root_db import db


class Utilisateur(db.Entity):
id = PrimaryKey(UUID, auto=True, default=uuid4)

nom = Required(str)
prenom = Required(str)
annees = Set("Annee")
Expand All @@ -13,3 +17,8 @@ class Utilisateur(db.Entity):
def user(cls):
assert cls.select().count() <= 1, "Only one user allowed"
return cls.select().first()

def to_dict(self, *args, **kwargs):
dico = super().to_dict(*args, **kwargs)
dico["id"] = str(self.id)
return dico
6 changes: 3 additions & 3 deletions src/mycartable/package/database_mixins/activite_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ def getDeplacePageModel(self, annee: int) -> List:
new["bgColor"] = m.bgColor
for ac in m.activites.order_by(lambda x: x.position):
new["activites"].append(
{"nom": ac.nom, "id": ac.id,}
{"nom": ac.nom, "id": str(ac.id),}
)
res.append(new)
return res

@Slot(int, int)
@Slot(str, str)
@db_session
def changeActivite(self, pageId: int, activiteId: int):
def changeActivite(self, pageId: str, activiteId: str):
Page[pageId].activite = activiteId
self.pageActiviteChanged.emit()
Loading

0 comments on commit 3ed4a25

Please sign in to comment.