Skip to content

Commit 8bcc9ee

Browse files
Ch:enable requesting user view if they follow a tag or not (#527)
* ch:enable requesting user view if they follow a tag or not * ch: optimized function * use the check exists method
1 parent 61f2ed5 commit 8bcc9ee

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

app/models/model_mixin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ def check_exists(cls, **kwargs):
129129
result = cls.query.filter_by(**kwargs).count()
130130

131131
if result > 0:
132-
return False
133-
return True
132+
return True
133+
return False
134134

135135
@classmethod
136136
def get_by_id(cls, id):

app/models/tags.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def __repr__(self):
2121
return f"<Tag {self.name}>"
2222

2323

24+
2425
class ProjectTag(ModelMixin):
2526
__tablename__ = "project_tag"
2627

app/schemas/project.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from app.models.project_users import ProjectFollowers
77

88

9-
109
class ProjectListSchema(Schema):
1110
id = fields.UUID(dump_only=True)
1211
name = fields.String()
@@ -69,7 +68,6 @@ class ProjectSchema(Schema):
6968
tags_remove = fields.List(fields.String, load_only=True)
7069

7170
def get_is_following(self, obj):
72-
# Assuming current_user is available in the view context
7371
current_user_id = get_jwt_identity()
7472
current_user = User.get_by_id(current_user_id)
7573
return obj.is_followed_by(current_user)
@@ -84,4 +82,4 @@ def get_prometheus_url(self, obj):
8482
return obj.cluster.prometheus_url
8583

8684
def get_followers_count(self, obj):
87-
return ProjectFollowers.count(project_id=obj.id)
85+
return ProjectFollowers.count(project_id=obj.id)

app/schemas/tags.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
11
from app.schemas.project import ProjectIndexSchema
22
from app.schemas.project_users import UserRoleSchema
33
from marshmallow import Schema, fields
4+
from flask_jwt_extended import get_jwt_identity
5+
from app.models.user import User
6+
from app.models.tags import TagFollowers
7+
8+
49
class TagListSchema(Schema):
510
id = fields.UUID(dump_only=True)
611
name = fields.String(required=True)
712
is_super_tag = fields.Boolean()
813

14+
915
class TagSchema(Schema):
1016

1117
id = fields.UUID(dump_only=True)
1218
name = fields.String(required=True)
1319
is_super_tag = fields.Boolean()
1420
date_created = fields.Date(dump_only=True)
1521
projects_count = fields.Method("get_projects_count", dump_only=True)
22+
is_following = fields.Method("get_is_following", dump_only=True)
1623

1724
def get_projects_count(self, obj):
1825
return len(obj.projects)
1926

27+
def get_is_following(self, obj):
28+
current_user_id = get_jwt_identity()
29+
tag_id = obj.id
30+
return TagFollowers.check_exists(user_id=current_user_id, tag_id=tag_id)
31+
2032

2133
class TagsProjectsSchema(TagSchema):
2234
name = fields.Method("get_name", dump_only=True)

0 commit comments

Comments
 (0)