Skip to content

Commit

Permalink
made models compatible to pydantic 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerskk committed Jul 26, 2024
1 parent 20a57b6 commit 2d5cccc
Show file tree
Hide file tree
Showing 22 changed files with 334 additions and 239 deletions.
14 changes: 8 additions & 6 deletions app/custom/custom_payload.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Optional, List, Dict

from pydantic import BaseModel

from typing import Optional, List
from pydantic import BaseModel, Field

class CustomPayload(BaseModel):
url: Optional[List[str]]
ontology: Optional[List[str]]
url: Optional[List[str]] = Field(default=None)
ontology: Optional[List[str]] = Field(default=None)

model_config = {
'validate_assignment': True
}
10 changes: 6 additions & 4 deletions app/custom/models/add_ontology.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import Optional, List

from pydantic import BaseModel

from pydantic import BaseModel, Field

class AddOntologyPayload(BaseModel):
url: Optional[List[str]]
url: Optional[List[str]] = Field(default=None)

model_config = {
'validate_assignment': True
}
15 changes: 10 additions & 5 deletions app/custom/models/add_template.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from typing import Optional, List

from pydantic import BaseModel

from pydantic import BaseModel, Field

class AddTemplatePayload(BaseModel):
url: Optional[List[str]]
url: Optional[List[str]] = Field(default=None)

model_config = {
'validate_assignment': True
}

class DeleteTemplatePayload(BaseModel):
ids: Optional[List[str]]
ids: Optional[List[str]] = Field(default=None)

model_config = {
'validate_assignment': True
}
15 changes: 10 additions & 5 deletions app/custom/models/delete_ontology.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from typing import Optional, List

from pydantic import BaseModel

from pydantic import BaseModel, Field

class DeleteOntologyPayload(BaseModel):
ontology: Optional[List[str]]
ontology: Optional[List[str]] = Field(default=None)

model_config = {
'validate_assignment': True
}

class DeleteOntologyResponse(BaseModel):
deleted: int
deleted: int = Field(default=0)

model_config = {
'validate_assignment': True
}
24 changes: 16 additions & 8 deletions app/custom/models/health.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
from typing import Optional, List, Dict

from pydantic import BaseModel

from pydantic import BaseModel, Field

class Neo4j(BaseModel):
status: str
version: str
status: str = Field(default="")
version: str = Field(default="")

model_config = {
'validate_assignment': True
}

class Services(BaseModel):
neo4j: Neo4j
swate: str
rabbitmq: str
swate: str = Field(default="")
rabbitmq: str = Field(default="")

model_config = {
'validate_assignment': True
}

class Health(BaseModel):
services: Services
status: str
status: str = Field(default="")

model_config = {
'validate_assignment': True
}
12 changes: 7 additions & 5 deletions app/custom/models/notifications.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import Optional, List

from pydantic import BaseModel

from typing import Optional
from pydantic import BaseModel, Field

class NotificationPayload(BaseModel):
mail_address: Optional[str]
mail_address: Optional[str] = Field(default=None)

model_config = {
'validate_assignment': True
}
24 changes: 14 additions & 10 deletions app/custom/models/status.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
from typing import Optional, List, Dict

from pydantic import BaseModel, validator
from typing import Optional, List
from pydantic import BaseModel, Field, validator

class MainOntology(BaseModel):
name: str
version: str
lastUpdated: str
name: str = Field(default="")
version: str = Field(default="")
lastUpdated: str = Field(default="")

@validator("version", "name", "lastUpdated", pre=True)
@validator("version", "name", "lastUpdated", pre=True, always=True)
def rewrite_name(cls, value):
if value is None:
value = ""
return value

model_config = {
'validate_assignment': True
}

class Status(BaseModel):
number_terms: int
number_ontologies: int
number_templates: int
number_relationships: int
main_ontologies : List[MainOntology]


main_ontologies: List[MainOntology]

model_config = {
'validate_assignment': True
}
14 changes: 8 additions & 6 deletions app/github/models/author.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import Optional

from pydantic import BaseModel

from pydantic import BaseModel, Field

class Author(BaseModel):
name: Optional[str]
email: Optional[str]
username: Optional[str]
name: Optional[str] = Field(default=None)
email: Optional[str] = Field(default=None)
username: Optional[str] = Field(default=None)

model_config = {
'validate_assignment': True
}
27 changes: 14 additions & 13 deletions app/github/models/commit.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from typing import Optional, List

from pydantic import BaseModel

from pydantic import BaseModel, Field
from app.github.models.author import Author


class Commit(BaseModel):
id: Optional[str]
timestamp: Optional[str]
message: Optional[str]
author: Optional[Author]
url: Optional[str]
distinct: Optional[bool]
added: Optional[List[str]]
modified: Optional[List[str]]
removed: Optional[List[str]]
id: Optional[str] = Field(default=None)
timestamp: Optional[str] = Field(default=None)
message: Optional[str] = Field(default=None)
author: Optional[Author] = Field(default=None)
url: Optional[str] = Field(default=None)
distinct: Optional[bool] = Field(default=None)
added: Optional[List[str]] = Field(default_factory=list)
modified: Optional[List[str]] = Field(default_factory=list)
removed: Optional[List[str]] = Field(default_factory=list)

model_config = {
'validate_assignment': True
}
33 changes: 17 additions & 16 deletions app/github/models/repository.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
from typing import Optional

from pydantic import BaseModel

from pydantic import BaseModel, Field
from app.github.models.user import User
from app.github.models.author import Author


class Repository(BaseModel):
id: Optional[int]
node_id: Optional[str]
name: Optional[str]
full_name: Optional[str]
html_url: Optional[str]
stargazers_count: int
watchers_count: int
language: Optional[str]
private: Optional[bool]
owner: Optional[User]
default_branch: Optional[str]
ref: Optional[str]
id: Optional[int] = Field(default=None)
node_id: Optional[str] = Field(default=None)
name: Optional[str] = Field(default=None)
full_name: Optional[str] = Field(default=None)
html_url: Optional[str] = Field(default=None)
stargazers_count: int = Field(default=0)
watchers_count: int = Field(default=0)
language: Optional[str] = Field(default=None)
private: Optional[bool] = Field(default=None)
owner: Optional[User] = Field(default=None)
default_branch: Optional[str] = Field(default=None)
ref: Optional[str] = Field(default=None)

model_config = {
'validate_assignment': True
}
18 changes: 10 additions & 8 deletions app/github/models/user.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from typing import Optional

from pydantic import BaseModel

from pydantic import BaseModel, Field

class User(BaseModel):
login: Optional[str]
name: Optional[str]
email: Optional[str]
id: Optional[int]
html_url: Optional[str]
login: Optional[str] = Field(default=None)
name: Optional[str] = Field(default=None)
email: Optional[str] = Field(default=None)
id: Optional[int] = Field(default=None)
html_url: Optional[str] = Field(default=None)

model_config = {
'validate_assignment': True
}
25 changes: 13 additions & 12 deletions app/github/webhook_payload.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from typing import Optional, List, Dict

from pydantic import BaseModel
from typing import Optional, List
from pydantic import BaseModel, Field

from app.github.models.commit import Commit
from app.github.models.author import Author
from app.github.models.repository import Repository
from app.github.models.user import User


class PushWebhookPayload(BaseModel):
ref: Optional[str]
before: Optional[str]
after: Optional[str]
commits: Optional[List[Commit]]
# commits: Optional[Dict(Commit)]
pusher: Optional[Author]
repository: Optional[Repository]
sender: Optional[User]
ref: Optional[str] = Field(default=None)
before: Optional[str] = Field(default=None)
after: Optional[str] = Field(default=None)
commits: Optional[List[Commit]] = Field(default_factory=list)
pusher: Optional[Author] = Field(default=None)
repository: Optional[Repository] = Field(default=None)
sender: Optional[User] = Field(default=None)

model_config = {
'validate_assignment': True
}
24 changes: 16 additions & 8 deletions app/helpers/models/building_type.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from typing import Optional

from pydantic import BaseModel, validator
from typing import Optional, List, Dict

from typing import Optional, List
from pydantic import BaseModel, Field, field_validator
from app.github.models.user import User
from app.github.models.author import Author

Expand All @@ -11,12 +8,23 @@ class BuildingType(BaseModel):
url: str
type: str

class BuildingObjects(BaseModel):
files: List[BuildingType] = []

class BuildingObjects(BaseModel):
files: List[BuildingType] = Field(default_factory=list)

# original
# @validator("name", "author", "version", "lastUpdated", pre=True)
# def strip_strings(cls, value):
# if value is not None:
# return value.strip()
# return value
# return value

# @field_validator("url", "type", mode="before")
# def strip_strings(cls, value):
# if value is not None and isinstance(value, str):
# return value.strip()
# return value

model_config = {
'validate_assignment': True
}
Loading

0 comments on commit 2d5cccc

Please sign in to comment.