Skip to content

Commit 28d024f

Browse files
authored
fix: adding of project tags and failing activity logs (#608)
* add admin truethy on login * fix project tags creation
1 parent f53c279 commit 28d024f

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

app/controllers/project.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from app.helpers.activity_logger import log_activity
99
from app.helpers.kube import (create_kube_clients, delete_cluster_app,
1010
disable_project, enable_project, check_kube_error_code, deploy_user_app)
11+
from app.helpers.tags import add_tags_to_project, remove_tags_from_project
1112
from app.models.billing_invoice import BillingInvoice
1213
from app.models.project_users import ProjectUser
1314
from app.models.user import User
@@ -67,6 +68,8 @@ def post(self):
6768
cluster_id = validated_project_data['cluster_id']
6869
cluster = Cluster.get_by_id(cluster_id)
6970

71+
tags = validated_project_data.pop('tags_add', None)
72+
7073
if not cluster:
7174
return dict(
7275
status='fail',
@@ -153,6 +156,9 @@ def post(self):
153156

154157
return dict(status="fail", message="Internal Server Error"), 500
155158

159+
if tags:
160+
add_tags_to_project(tags, project)
161+
156162
# create a billing invoice on project creation
157163
new_invoice = BillingInvoice(project_id=project.id)
158164

@@ -177,7 +183,7 @@ def post(self):
177183
log_activity('Project', status='Success',
178184
operation='Create',
179185
description='Created project Successfully',
180-
a_project=project.id,
186+
a_project=project,
181187
a_cluster_id=cluster_id,
182188
)
183189

@@ -186,14 +192,19 @@ def post(self):
186192
except client.rest.ApiException as e:
187193
log_activity('Project', status='Failed',
188194
operation='Create',
195+
a_project=project,
189196
description=e.body,
190197
a_cluster_id=cluster_id)
191198
return dict(status='fail', message=str(e.body)), check_kube_error_code(e.status)
192199

193200
except Exception as err:
201+
try:
202+
err = err.body
203+
except:
204+
err = str(err)
194205
log_activity('Project', status='Failed',
195206
operation='Create',
196-
description=err.body,
207+
description=err,
197208
a_cluster_id=cluster_id)
198209
return dict(status='fail', message=str(err)), 500
199210

@@ -446,14 +457,14 @@ def delete(self, project_id):
446457
log_activity('Project', status='Failed',
447458
operation='Delete',
448459
description='Internal server error',
449-
a_project=project_id,
460+
a_project=project,
450461
a_cluster_id=project.cluster_id)
451462
return dict(status='fail', message='deletion failed'), 500
452463

453464
log_activity('Project', status='Success',
454465
operation='Delete',
455466
description='Deleted project Successfully',
456-
a_project=project.id,
467+
a_project=project,
457468
a_cluster_id=project.cluster_id)
458469
return dict(
459470
status='success',
@@ -478,7 +489,7 @@ def delete(self, project_id):
478489
log_activity('Project', status='Success',
479490
operation='Delete',
480491
description='Deleted project Successfully',
481-
a_project=project.id,
492+
a_project=project,
482493
a_cluster_id=project.cluster_id)
483494
return dict(
484495
status='success',
@@ -487,15 +498,15 @@ def delete(self, project_id):
487498
log_activity('Project', status='Failed',
488499
operation='Delete',
489500
description=e.reason,
490-
a_project=project_id,
501+
a_project=project,
491502
a_cluster_id=project.cluster_id)
492503
return dict(status='fail', message=e.reason), check_kube_error_code(e.status)
493504

494505
except Exception as e:
495506
log_activity('Project', status='Failed',
496507
operation='Delete',
497508
description=str(e),
498-
a_project=project_id,
509+
a_project=project,
499510
a_cluster_id=project.cluster_id)
500511
return dict(status='fail', message=str(e)), 500
501512

@@ -509,7 +520,7 @@ def patch(self, project_id):
509520
current_user_roles = get_jwt_claims()['roles']
510521

511522
project_schema = ProjectSchema(
512-
only=("name", "description", "organisation", "project_type"), partial=True)
523+
only=("name", "description", "organisation", "project_type", "is_public", "tags_add", "tags_remove"), partial=True)
513524

514525
project_data = request.get_json()
515526

@@ -541,21 +552,29 @@ def patch(self, project_id):
541552
if not is_authorised_project_user(project, current_user_id, 'admin'):
542553
return dict(status='fail', message='unauthorised'), 403
543554

555+
if validate_project_data.get('tags_add'):
556+
add_tags_to_project(validate_project_data['tags_add'], project)
557+
validate_project_data.pop('tags_add', None)
558+
if validate_project_data.get('tags_remove'):
559+
remove_tags_from_project(
560+
validate_project_data['tags_remove'], project)
561+
validate_project_data.pop('tags_remove', None)
562+
544563
updated = Project.update(project, **validate_project_data)
545564

546565
if not updated:
547566
log_activity('Project', status='Failed',
548567
operation='Update',
549568
description='Internal Server Error',
550-
a_project=project.id,
569+
a_project=project,
551570
a_cluster_id=project.cluster_id
552571
)
553572
return dict(status='fail', message='internal server error'), 500
554573

555574
log_activity('Project', status='Success',
556575
operation='Update',
557576
description='Updated project Successfully',
558-
a_project=project.id,
577+
a_project=project,
559578
a_cluster_id=project.cluster_id)
560579
return dict(
561580
status='success',
@@ -596,7 +615,7 @@ def get(self, user_id):
596615

597616
# returns deleted projects
598617
# pagination_meta_data, projects = paginate(
599-
# user.projects, per_page, page)
618+
# user.projects[::-1], per_page, page)
600619

601620
pagination = Project.query.filter(or_(Project.owner_id == current_user_id, Project.users.any(
602621
ProjectUser.user_id == current_user_id))).order_by(Project.date_created.desc()).paginate(
@@ -627,7 +646,6 @@ def get(self, user_id):
627646
pagination={**pagination_data,
628647
'pinned_count': len(parsed_pinned_projects)},
629648
pinned=parsed_pinned_projects,
630-
631649
projects=json.loads(user_projects),
632650
)
633651
), 200

app/helpers/kube.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ def disable_user_app(app: App, is_admin=False):
623623
log_activity('App', status='Success',
624624
operation='Disable',
625625
description='Disabled app Successfully',
626-
a_project=app.project.id,
626+
a_project=app.project,
627627
a_cluster_id=app.project.cluster_id)
628628
return True
629629

@@ -632,7 +632,7 @@ def disable_user_app(app: App, is_admin=False):
632632
log_activity('App', status='Failed',
633633
operation='Disable',
634634
description='Error disabling application',
635-
a_project=app.project.id,
635+
a_project=app.project,
636636
a_cluster_id=app.project.cluster_id)
637637
return SimpleNamespace(
638638
message=json.loads(e.body),
@@ -678,7 +678,7 @@ def enable_user_app(app: App):
678678
log_activity('App', status='Success',
679679
operation='Enable',
680680
description='Enabled app Successfully',
681-
a_project=app.project.id,
681+
a_project=app.project,
682682
a_cluster_id=app.project.cluster_id)
683683
return True
684684

@@ -687,7 +687,7 @@ def enable_user_app(app: App):
687687
log_activity('App', status='Failed',
688688
operation='Enable',
689689
description='Error enabling application',
690-
a_project=app.project.id,
690+
a_project=app.project,
691691
a_cluster_id=app.project.cluster_id)
692692
return SimpleNamespace(
693693
message=json.loads(e.body),

0 commit comments

Comments
 (0)