Skip to content

Commit 75e1ddd

Browse files
Fixed Bug: Validation fails -> new Profile has no ID (#181)
1 parent 1fd6663 commit 75e1ddd

3 files changed

Lines changed: 21 additions & 13 deletions

File tree

converter_app/models.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ class Profile:
2828
id [str] id of the profile and file name
2929
errors [collections.defaultdict] contains all errors if profile is not correct
3030
"""
31-
31+
3232
def __init__(self, profile_data, client_id, profile_id=None, is_default_profile: bool = False):
3333
self.isDisabled = profile_data.get('isDisabled', False)
3434
self.data = profile_data
3535
self.client_id = client_id
36-
self.id = profile_id
36+
self._id = profile_id
3737
self.errors = defaultdict(list)
3838
self._is_default_profile = is_default_profile
3939

@@ -112,13 +112,6 @@ def save(self):
112112
profiles_path = Path(current_app.config['PROFILES_DIR']).joinpath(self.client_id)
113113
profiles_path.mkdir(parents=True, exist_ok=True)
114114

115-
if self.id is None:
116-
if 'id' in self.data:
117-
self.id = self.data['id']
118-
else:
119-
# create a uuid for new profiles
120-
self.data['id'] = self.id = str(uuid.uuid4())
121-
122115
file_path = profiles_path.joinpath(self.id).with_suffix('.json')
123116
if 'isDefaultProfile' in self.data:
124117
del self.data['isDefaultProfile']
@@ -147,6 +140,20 @@ def as_dict(self):
147140
'isDefaultProfile': self._is_default_profile
148141
}
149142

143+
@property
144+
def id(self):
145+
if self._id is None:
146+
if 'id' in self.data:
147+
self._id = self.data['id']
148+
else:
149+
# create a uuid for new profiles
150+
self.id = str(uuid.uuid4())
151+
return self._id
152+
153+
@id.setter
154+
def id(self, new_id):
155+
self.data['id'] = self._id = new_id
156+
150157
@classmethod
151158
def load(cls, file_path: pathlib.PurePath):
152159
"""
@@ -180,7 +187,6 @@ def profile_from_file_path(cls, file_path: Path, client_id: str = ''):
180187
profile_data = cls.load(file_path)
181188
return cls(profile_data, client_id, profile_id)
182189

183-
184190
@classmethod
185191
def list(cls, client_id):
186192
"""

converter_app/router.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def create_profile():
202202
if profile.clean():
203203
Migrations().migrate_profile(profile)
204204
try:
205-
validate_profile(profile.data)
205+
validate_profile(profile.as_dict)
206206
profile.save()
207207
return jsonify(profile.as_dict), 201
208208
except jsonschema.exceptions.ValidationError as e:
@@ -232,9 +232,8 @@ def update_profile(profile_id):
232232

233233
if profile.clean():
234234
Migrations().migrate_profile(profile)
235-
236235
try:
237-
validate_profile(profile.data)
236+
validate_profile(profile.as_dict)
238237
profile.save()
239238
return jsonify(profile.as_dict), 200
240239
except jsonschema.exceptions.ValidationError as e:

converter_app/validation/schemas/schema_profile.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
"isDisabled": {
4848
"type": "boolean"
4949
},
50+
"isDefaultProfile": {
51+
"type": "boolean"
52+
},
5053
"ols": {
5154
"type": "string"
5255
},

0 commit comments

Comments
 (0)