models.py 7.83 KB
Newer Older
1
2
from enum import Enum
import datetime
Jo Sama's avatar
Jo Sama committed
3
4
5
import json
import pydantic

6
from typing import Optional, List
Jo Sama's avatar
Jo Sama committed
7
8
9
10
11
12
13
14
15
16
17
18


class BaseModel(pydantic.BaseModel):
    @classmethod
    def from_dict(cls, data):
        return cls(**data)

    @classmethod
    def from_json(cls, json_data):
        data = json.loads(json_data)
        return cls.from_dict(data)

19
20
21
    def dict(self, by_alias=True, skip_defaults=True, *args, **kwargs):
        return super().dict(
            by_alias=by_alias, skip_defaults=skip_defaults, *args, **kwargs)
Jo Sama's avatar
Jo Sama committed
22

23
24
25
26
    def json(self, by_alias=True, skip_defaults=True, *args, **kwargs):
        return super().json(
            by_alias=by_alias, skip_defaults=skip_defaults, *args, **kwargs)

Jo Sama's avatar
Jo Sama committed
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
class Identifier(BaseModel):
    """ An object with only an ID that references some entity. """
    id: str


class Reference(BaseModel):
    """ Also called a searchlist. """
    id: str
    name: str


class OptionalFields(BaseModel):
    """ An abomination. """
    boolean1: Optional[bool]
    boolean2: Optional[bool]
    boolean3: Optional[bool]
    boolean4: Optional[bool]
    boolean5: Optional[bool]
    number1: Optional[int]
    number2: Optional[int]
    number3: Optional[int]
    number4: Optional[int]
    number5: Optional[int]
    date1: Optional[datetime.datetime]
    date2: Optional[datetime.datetime]
    date3: Optional[datetime.datetime]
    date4: Optional[datetime.datetime]
    date5: Optional[datetime.datetime]
    text1: Optional[str]
    text2: Optional[str]
    text3: Optional[str]
    text4: Optional[str]
    text5: Optional[str]
    searchlist1: Optional[Reference]
    searchlist2: Optional[Reference]
    searchlist3: Optional[Reference]
    searchlist4: Optional[Reference]
    searchlist5: Optional[Reference]


class AddressTypeEnum(str, Enum):
    gb = 'GB'
    nl = 'NL'
    memo = 'MEMO'


class Address(BaseModel):
75
76
    street: Optional[str] = ''
    number: Optional[str] = ''
77
    country: Optional[Reference]
78
79
80
81
    city: Optional[str] = ''
    county: Optional[str] = ''
    postcode: Optional[str] = ''
    memo: Optional[str] = ''
82
    type: Optional[AddressTypeEnum]
83
84
85
86
87
88
89
90
91
92
93
94
95
96

    class Config:
        fields = {
            'memo': {'alias': 'addressMemo'},
            'type': {'alias': 'addressType'},
        }


class BranchTypeEnum(str, Enum):
    independent = 'independentBranch'
    head = 'headBranch'
    has_a_head = 'hasAHeadBranch'


Jo Sama's avatar
Jo Sama committed
97
class Branch(BaseModel):
98
    id: str = None
99
100
101
    name: str = ''
    specification: str = ''
    client_reference_number: str = ''
102
103
104
    timezone: str = None
    extra_a: Optional[Reference]
    extra_b: Optional[Reference]
105
106
    phone: str = ''
    fax: str = ''
107
108
    address: Address = None
    postal_address: Address = None
109
110
    email: str = ''
    website: str = ''
111
112
113
114
115
116
117
118
    type: Optional[BranchTypeEnum]
    head_branch: Optional[Identifier]
    creator: Optional[Reference]
    created: Optional[datetime.datetime]
    modified: Optional[datetime.datetime]
    modifier: Optional[Reference]
    optional_fields_1: Optional[OptionalFields]
    optional_fields_2: Optional[OptionalFields]
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

    class Config:
        fields = {
            'client_reference_number': {'alias': 'clientReferenceNumber'},
            'timezone': {'alias': 'timeZone'},
            'extra_a': {'alias': 'extraA'},
            'extra_b': {'alias': 'extraB'},
            'postal_address': {'alias': 'postalAddress'},
            'type': {'alias': 'branchType'},
            'head_branch': {'alias': 'headBranch'},
            'optional_fields_1': {'alias': 'optionalFields1'},
            'optional_fields_2': {'alias': 'optionalFields2'},
            'created': {'alias': 'creationDate'},
            'modified': {'alias': 'modificationDate'},
        }

135
136
137
138
139
    def dict(self, *args, **kwargs):
        kwargs['exclude'] = {'id'}
        return super().dict(*args, **kwargs)


140
141
class BranchReference(BaseModel):
    id: str
Jo Sama's avatar
Jo Sama committed
142
    name: str
143
144
    client_reference_number: Optional[str]
    timezone: Optional[str]
Jo Sama's avatar
Jo Sama committed
145
146
    extra_a: Optional[Reference]
    extra_b: Optional[Reference]
Jo Sama's avatar
Jo Sama committed
147
148
149

    class Config:
        fields = {
150
151
152
153
            'client_reference_number': {'alias': 'clientReferenceNumber'},
            'timezone': {'alias': 'timeZone'},
            'extra_a': {'alias': 'extraA'},
            'extra_b': {'alias': 'extraB'},
Jo Sama's avatar
Jo Sama committed
154
155
        }

156
157
158
159
    def dict(self, *args, **kwargs):
        kwargs['include'] = {'id'}
        return super(BranchReference, self).dict(*args, **kwargs)

Jo Sama's avatar
Jo Sama committed
160
161
162
    def inclusive_dict(self, *args, **kwargs):
        return super(BranchReference, self).dict(*args, **kwargs)

Jo Sama's avatar
Jo Sama committed
163
164

class Operator(BaseModel):
165
    id: Optional[str]
Jo Sama's avatar
Jo Sama committed
166
167
168
    first_name: str
    last_name: str
    user_name: str
169
170
    phone: Optional[str]
    mobile: Optional[str]
Jo Sama's avatar
Jo Sama committed
171
    email: str
172
    branch: BranchReference
173
174
    # TODO: Remove the password field when proper auth is in place
    password: Optional[str]
Jo Sama's avatar
Jo Sama committed
175
176
177
178
179

    class Config:
        fields = {
            'first_name': {'alias': 'firstName'},
            'last_name': {'alias': 'surName'},
Jo Sama's avatar
Jo Sama committed
180
            'user_name': {'alias': 'loginName'},
Jo Sama's avatar
Jo Sama committed
181
            'phone': {'alias': 'telephone'},
Jo Sama's avatar
Jo Sama committed
182
            'mobile': {'alias': 'mobileNumber'},
Jo Sama's avatar
Jo Sama committed
183
        }
Jo Sama's avatar
Jo Sama committed
184

185
186
187
188
    def dict(self, *args, **kwargs):
        kwargs['exclude'] = {'id'}
        return super(Operator, self).dict(*args, **kwargs)

Jo Sama's avatar
Jo Sama committed
189
190
191
    def inclusive_dict(self, *args, **kwargs):
        return super(Operator, self).dict(*args, **kwargs)

Jo Sama's avatar
Jo Sama committed
192

193
194
195
196
197
198
199
200
201
202
203
class OperatorGroup(BaseModel):
    id: str
    groupName: str
    # TODO: add the other 40 fields if needed

    class Config:
        fields = {
            'name': {'alias': 'groupName'},
        }


204
205
206
207
208
class PermissionGroup(BaseModel):
    id: str
    name: str


209
210
211
212
213
class ExternalLink(BaseModel):
    id: str
    type: str
    date: str = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ')

214
215

class Department(BaseModel):
216
217
218
219
220
221
222
223
224
225
    id: Optional[str]
    name: str
    externalLinks: List[ExternalLink] = []

    def dict(self, *args, **kwargs):
        kwargs['exclude'] = {'id'}
        return super().dict(*args, **kwargs)


class DepartmentRef(BaseModel):
226
227
228
    id: str
    name: str

229
230
231
232
233
    def dict(self, *args, **kwargs):
        kwargs['include'] = {'id'}
        return super().dict(*args, **kwargs)


234
class BudgetHolder(BaseModel):
235
236
237
238
239
240
241
242
243
244
    id: Optional[str]
    name: str
    externalLink: List[ExternalLink] = []

    def dict(self, *args, **kwargs):
        kwargs['exclude'] = {'id'}
        return super().dict(*args, **kwargs)


class BudgetHolderRef(BaseModel):
245
246
247
    id: str
    name: str

248
249
250
251
    def dict(self, *args, **kwargs):
        kwargs['include'] = {'id'}
        return super().dict(*args, **kwargs)

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
class ExtraA(BaseModel):
    id: Optional[str]
    name: str
    externalLink: List[ExternalLink] = []


    def dict(self, *args, **kwargs):
        kwargs['exclude'] = {'id'}
        return super().dict(*args, **kwargs)


class ExtraARef(BaseModel):
    id: str
    name: Optional[str]

    def dict(self, *args, **kwargs):
        kwargs['include'] = {'id'}
        return super().dict(*args, **kwargs)

271

Jo Sama's avatar
Jo Sama committed
272
273
274
275
276
class Person(BaseModel):
    id: Optional[str]
    first_name: str
    last_name: str
    user_name: str
Jo Sama's avatar
Jo Sama committed
277
    networkLoginName: str
278
279
    phone: Optional[str]
    mobile: Optional[str]
Jo Sama's avatar
Jo Sama committed
280
    email: str
281
    employee_number: Optional[str]
Jo Sama's avatar
Jo Sama committed
282
    branch: BranchReference
283
284
    # TODO: Remove the password field when proper auth is in place
    password: Optional[str]
Jo Sama's avatar
Jo Sama committed
285

286
287
    department: Optional[DepartmentRef]
    budgetHolder: Optional[BudgetHolderRef]
288
    jobTitle: Optional[str]
289
    personExtraFieldA: Optional[ExtraARef]
290

Jo Sama's avatar
Jo Sama committed
291
292
293
294
    class Config:
        fields = {
            'first_name': 'firstName',
            'last_name': 'surName',
295
            'user_name': 'tasLoginName',
Jo Sama's avatar
Jo Sama committed
296
297
298
299
            'employee_number': 'employeeNumber',
            'phone': 'phoneNumber',
            'mobile': 'mobileNumber'
        }
300
301
302
303

    def dict(self, *args, **kwargs):
        kwargs['exclude'] = {'id'}
        return super(Person, self).dict(*args, **kwargs)
Jo Sama's avatar
Jo Sama committed
304
305
306

    def inclusive_dict(self, *args, **kwargs):
        return super(Person, self).dict(*args, **kwargs)