Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions backend/app/models/authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from beanie import Document
from charset_normalizer.md import List
from pydantic import BaseModel, EmailStr, Field

from app.models.mongomodel import MongoModel
from app.models.pyobjectid import PyObjectId


Expand Down
2 changes: 1 addition & 1 deletion backend/app/models/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class GroupAndRole(BaseModel):
role: RoleType


class DatasetRoles(MongoModel):
class DatasetRoles(BaseModel):
dataset_id: str
user_roles: List[UserAndRole] = []
group_roles: List[GroupAndRole] = []
6 changes: 3 additions & 3 deletions backend/app/models/errors.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from datetime import datetime
from typing import Optional
from pydantic import Field
from pydantic import Field, BaseModel

from app.models.mongomodel import MongoModel, MongoDBRef
from app.models.mongomodel import MongoDBRef


class ServiceUnreachable(Exception):
Expand All @@ -16,7 +16,7 @@ def __str__(self):
return f"{self.service} could not be reached."


class Error(MongoModel):
class Error(BaseModel):
message: str # Shorthand message of the error
trace: str # Full stack trace of the error
resource: Optional[MongoDBRef] = None
Expand Down
5 changes: 2 additions & 3 deletions backend/app/models/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from beanie import Document, PydanticObjectId
from pydantic import Field, BaseModel

from app.models.mongomodel import MongoModel
from app.models.pyobjectid import PyObjectId
from app.models.users import UserOut

Expand All @@ -20,7 +19,7 @@ class FileContentType(BaseModel):
main_type: str = "N/A"


class FileVersion(MongoModel):
class FileVersion(BaseModel):
file_id: PyObjectId
creator: UserOut
created: datetime = Field(default_factory=datetime.utcnow)
Expand All @@ -36,7 +35,7 @@ class Settings:
name = "file_versions"


class FileBase(MongoModel):
class FileBase(BaseModel):
name: str = "N/A"


Expand Down
5 changes: 2 additions & 3 deletions backend/app/models/folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
from typing import Optional

from beanie import Document, PydanticObjectId
from pydantic import Field
from pydantic import Field, BaseModel

from app.models.mongomodel import MongoModel
from app.models.pyobjectid import PyObjectId
from app.models.users import UserOut


class FolderBase(MongoModel):
class FolderBase(BaseModel):
name: str = "N/A"


Expand Down
1 change: 0 additions & 1 deletion backend/app/models/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from pydantic import Field, BaseModel
from beanie import Document, View, PydanticObjectId
from app.models.mongomodel import OID, MongoModel
from app.models.users import UserOut
from app.models.authorization import Provenance, RoleType

Expand Down
41 changes: 0 additions & 41 deletions backend/app/models/mongomodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,6 @@ def __modify_schema__(cls, field_schema):
field_schema.update(type="string")


class MongoModel(BaseModel):
id: PyObjectId = Field(default_factory=PyObjectId)

def __init__(self, **pydict):
super().__init__(**pydict)
if "_id" in pydict.keys():
self.id = pydict.pop("_id", None)

class Config(BaseConfig):
allow_population_by_field_name = True
json_encoders = {
datetime: lambda dt: dt.isoformat(),
ObjectId: lambda oid: str(oid),
}

@classmethod
def from_mongo(cls, data: dict):
"""We must convert _id into "id"."""
if not data:
return data
id = data.pop("_id", None)
return cls(**dict(data, id=id))

def to_mongo(self, **kwargs):
# include all key/value pairs by default. Is this the correct default behaviour?
exclude_unset = kwargs.pop("exclude_unset", False)
by_alias = kwargs.pop("by_alias", True)

parsed = self.dict(
exclude_unset=exclude_unset,
by_alias=by_alias,
**kwargs,
)

# Mongo uses `_id` as default key. We should stick to that as well.
if "_id" not in parsed and "id" in parsed:
parsed["_id"] = parsed.pop("id")

return parsed


class MongoDBRef(BaseModel):
collection: str
resource_id: PyObjectId
Expand Down
6 changes: 2 additions & 4 deletions backend/app/models/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
from pymongo import MongoClient
from beanie import Document, View, PydanticObjectId

from app.models.mongomodel import MongoModel

pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")


class UserBase(MongoModel):
class UserBase(BaseModel):
email: EmailStr


Expand Down Expand Up @@ -57,7 +55,7 @@ class Settings:
name = "user_keys"


class UserAPIKeyOut(MongoModel):
class UserAPIKeyOut(BaseModel):
# don't show the raw key
name: str
user: EmailStr
Expand Down