diff --git a/pyproject.toml b/pyproject.toml index 0f228f5..421b1d2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "welearn-database" -version = "1.4.4" +version = "1.4.5.dev1" description = "All stuff related to relationnal database from the WeLearn project" authors = [ {name = "Théo",email = "theo.nardin@cri-paris.org"} diff --git a/tests/test_user_related.py b/tests/test_user_related.py index 1a02eee..79b1869 100644 --- a/tests/test_user_related.py +++ b/tests/test_user_related.py @@ -22,6 +22,7 @@ from welearn_database.data.models.user_related import ( UserProfile, ) +from welearn_database.exceptions import EarlyEnumerationVerificationError class TestUserRelatedCRUD(TestCase): @@ -63,6 +64,23 @@ def test_create_and_read_inferred_user(self): ) self.assertIsNotNone(result) + def test_create_and_read_inferred_user_with_university_data(self): + inferred_user = InferredUser( + id=uuid.uuid4(), + origin_referrer="test_ref", + university_title="test_university", + role="student", + ) + self.session.add(inferred_user) + self.session.commit() + result = ( + self.session.query(InferredUser) + .filter_by(origin_referrer="test_ref") + .first() + ) + self.assertEqual(result.university_title, "test_university") + self.assertEqual(result.role, "student") + def test_create_and_read_session(self): inferred_user = InferredUser(id=uuid.uuid4()) self.session.add(inferred_user) diff --git a/welearn_database/alembic/versions/8b780aea403a_add_university_data.py b/welearn_database/alembic/versions/8b780aea403a_add_university_data.py new file mode 100644 index 0000000..ab5a424 --- /dev/null +++ b/welearn_database/alembic/versions/8b780aea403a_add_university_data.py @@ -0,0 +1,44 @@ +"""add_university_data + +Revision ID: 8b780aea403a +Revises: f1ce0ad2845b +Create Date: 2026-06-03 11:34:02.441435 + +""" + +from typing import Sequence, Union + +import sqlalchemy as sa +from alembic import op +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision: str = "8b780aea403a" +down_revision: Union[str, None] = "f1ce0ad2845b" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # op.execute(""" + # CREATE TYPE user_related.university_role AS ENUM ('student', 'teacher', 'staff') + # """) + op.add_column( + "inferred_user", + sa.Column("university_title", sa.String(), nullable=True), + schema="user_related", + ) + op.add_column( + "inferred_user", + sa.Column( + "role", + sa.String(), + nullable=True, + ), + schema="user_related", + ) + + +def downgrade() -> None: + op.drop_column("inferred_user", "university_title", schema="user_related") + op.drop_column("inferred_user", "role", schema="user_related") diff --git a/welearn_database/data/models/document_related.py b/welearn_database/data/models/document_related.py index d990202..10657be 100644 --- a/welearn_database/data/models/document_related.py +++ b/welearn_database/data/models/document_related.py @@ -11,7 +11,6 @@ LargeBinary, UniqueConstraint, func, - text, types, ) from sqlalchemy.dialects.postgresql import ARRAY, ENUM, TIMESTAMP diff --git a/welearn_database/data/models/user_related.py b/welearn_database/data/models/user_related.py index 149a407..bd358ef 100644 --- a/welearn_database/data/models/user_related.py +++ b/welearn_database/data/models/user_related.py @@ -216,6 +216,8 @@ class InferredUser(Base): types.Uuid, primary_key=True, nullable=False, server_default="gen_random_uuid()" ) origin_referrer: Mapped[str | None] + university_title: Mapped[str | None] + role: Mapped[str | None] created_at: Mapped[datetime] = mapped_column( TIMESTAMP(timezone=False), nullable=False, diff --git a/welearn_database/exceptions.py b/welearn_database/exceptions.py index 3d1c8aa..d197295 100644 --- a/welearn_database/exceptions.py +++ b/welearn_database/exceptions.py @@ -1,4 +1,10 @@ -class InvalidURLScheme(Exception): +class WeLearnDatabaseException(Exception): + """ + Base class for all WeLearnDatabase exceptions + """ + + +class InvalidURLScheme(WeLearnDatabaseException): """ Scheme detected in URL is not accepted """ @@ -7,7 +13,7 @@ def __init__(self, msg="URL schema is not accepted", *args): super().__init__(msg, *args) -class InvalidDOI(Exception): +class InvalidDOI(WeLearnDatabaseException): """ Scheme detected in DOI is not accepted """ @@ -16,7 +22,7 @@ def __init__(self, msg="DOI schema is not accepted", *args): super().__init__(msg, *args) -class ContentIsTooShort(Exception): +class ContentIsTooShort(WeLearnDatabaseException): """ The string used as content is too short, it should be at least 25 characters long """ @@ -27,3 +33,14 @@ def __init__( *args, ): super().__init__(msg, *args) + + +class EarlyEnumerationVerificationError(WeLearnDatabaseException): + """ + SQLAlchemy detect an enumeration value that is not in the list of accepted values + """ + + def __init__( + self, msg="Enumeration value is not in the list of accepted values", *args + ): + super().__init__(msg, *args)