-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
203 lines (170 loc) · 6.16 KB
/
models.py
File metadata and controls
203 lines (170 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from datetime import datetime
from enum import StrEnum, auto
from typing import Any, List
import strawberry
from bson import ObjectId
from pydantic import BaseModel, ConfigDict, EmailStr, Field, field_validator
from pydantic_core import core_schema
from utils import get_curr_time_str, get_utc_time
class PyObjectId(ObjectId):
"""
Class for handling MongoDB document ObjectIds for 'id' fields in Models.
"""
@classmethod
def __get_pydantic_core_schema__(cls, source_type: Any, handler):
return core_schema.union_schema(
[
# check if it's an instance first before doing any further work
core_schema.is_instance_schema(ObjectId),
core_schema.no_info_plain_validator_function(cls.validate),
],
serialization=core_schema.to_string_ser_schema(),
)
@classmethod
def validate(cls, v):
if not ObjectId.is_valid(v):
raise ValueError("Invalid ObjectId")
return ObjectId(v)
@classmethod
def __get_pydantic_json_schema__(cls, field_schema):
field_schema.update(type="string")
# sample pydantic model
class Mails(BaseModel):
"""
Model for Mails
Attributes:
id (PyObjectId): Unique ObjectId of the document.
uid (str): User id. Defaults to None.
subject (str): Subject of the mail.
body (str): Body of the mail.
to_recipients (List[pydantic.networks.EmailStr]): List
of 'to' recipients.
cc_recipients (List[pydantic.networks.EmailStr]): List
of 'cc' recipients.
html_body (bool): Whether the body is in HTML or not.
sent_time (datetime): Time when the mail was sent.
"""
id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
uid: str | None = None
subject: str = Field(..., max_length=100)
body: str = Field(...)
to_recipients: List[EmailStr] = Field(...)
cc_recipients: List[EmailStr] = Field([])
html_body: bool = Field(default=False)
sent_time: datetime = Field(default_factory=get_utc_time, frozen=True)
@field_validator("to_recipients")
@classmethod
# validates the to_recipients field
def validate_unique_to(cls, value):
"""
Validates the 'to_recipients' field to check for duplicate emails.
"""
if len(value) != len(set(value)):
raise ValueError(
"Duplicate Emails are not allowed in 'to_recipients'"
)
return value
@field_validator("cc_recipients")
@classmethod
# validates the cc_recipients field
def validate_unique_cc(cls, value):
"""
Validates the 'cc_recipients' field to check for duplicate emails.
"""
if len(value) != len(set(value)):
raise ValueError(
"Duplicate Emails are not allowed in 'cc_recipients'"
)
return value
model_config = ConfigDict(
populate_by_name=True,
arbitrary_types_allowed=True,
extra="forbid",
str_strip_whitespace=True,
validate_assignment=True,
)
# Enum for storing category of the team for the recruit
@strawberry.enum
class Team(StrEnum):
"""
Enum for storing category of team for the recruit.
"""
Design = auto()
Finance = auto()
Logistics = auto()
Stats = auto()
Corporate = auto()
class CCRecruitment(BaseModel):
"""
Model for CC Recruitment form
Attributes:
id (PyObjectId): Unique ObjectId of the document.
uid (str): User id.
email (pydantic.networks.EmailStr): Email of the user.
teams (List[Team]): List of teams the user wants to apply for.
design_experience (str): Design experience of the user. Defaults to
None.
why_this_position (str): Why the user wants this position.
why_cc (str): Why the user wants to join CC.
ideas1 (str): reasons for not participating in a event.
ideas (str): Ideas the user has for CC.
other_bodies (str): Other bodies the user is a part of. Defaults to
None.
good_fit (str | None): Why the user is a good fit for CC.
sent_time (datetime): Time when the form was submitted.
apply_year (int): Year of application. Defaults to 2024.
"""
id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
uid: str = Field(..., max_length=100)
email: EmailStr = Field(...)
teams: List[Team] = []
design_experience: str | None = None
why_this_position: str = Field()
why_cc: str = Field()
ideas1: str = Field("")
ideas: str = Field(None)
other_bodies: str | None = None
good_fit: str = Field()
sent_time: datetime = Field(default_factory=get_utc_time, frozen=True)
apply_year: int = 2024
model_config = ConfigDict(
populate_by_name=True,
arbitrary_types_allowed=True,
extra="forbid",
str_strip_whitespace=True,
validate_assignment=True,
)
class StorageFile(BaseModel):
"""
Model for files being stored
Attributes:
id (PyObjectId): Unique ObjectId of the document.
title (str): Title of the file.
filename (str): Name of the file.
filetype (str): Type of the file.
latest_version (int): Latest version of the file.
modified_time (str): Time when the file was last modified.
creation_time (str): Time when the file was created.
"""
id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
title: str = Field(
...,
max_length=100,
min_length=2,
)
filename: str = Field(
...,
max_length=125,
min_length=2,
)
filetype: str = "pdf"
latest_version: int = 1
modified_time: str = Field(default_factory=get_curr_time_str)
creation_time: str = Field(default_factory=get_curr_time_str, frozen=True)
model_config = ConfigDict(
populate_by_name=True,
arbitrary_types_allowed=True,
extra="forbid",
str_strip_whitespace=True,
validate_assignment=True,
)