Skip to content

Commit

Permalink
fix adding unknown tags bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Mubangizi committed Jul 12, 2024
1 parent a99a285 commit fafb4d7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 27 deletions.
7 changes: 5 additions & 2 deletions app/controllers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ def post(self):
)
project.users.append(new_role)

if validated_project_data['tags']:
tags = create_tags(validated_project_data['tags'])
if validated_project_data['tags_add']:
tags = validated_project_data['tags']
validated_project_data.pop('tags', None)

saved = project.save()

Expand All @@ -150,6 +151,8 @@ def post(self):
kube_client.kube.delete_namespace(namespace_name)

return dict(status="fail", message="Internal Server Error"), 500
if tags:
add_tags_to_project(tags, project)

# create a billing invoice on project creation
new_invoice = BillingInvoice(project_id=project.id)
Expand Down
12 changes: 7 additions & 5 deletions app/helpers/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ def create_tags(tag_names):
existing_tags.append(tag_rec)
if none_existing_tags:
Tag.bulk_save(none_existing_tags)

new_tags = [Tag.find_first(name=tag.name)
for tag in none_existing_tags]
new_tags = []
for tag in none_existing_tags:
new_tag = Tag.find_first(name=tag.name)
new_tags.append(new_tag)
if new_tags:
existing_tags.append(new_tags)
existing_tags.extend(new_tags)
return existing_tags


Expand All @@ -40,6 +41,7 @@ def add_tags_to_project(tag_names, project):
return False
return True


def remove_tags_from_project(tag_names, project):
for tag in tag_names:
existing_tag = Tag.find_first(name=tag)
Expand All @@ -49,4 +51,4 @@ def remove_tags_from_project(tag_names, project):
tag_id=existing_tag.id, project_id=project.id)
if project_tag:
project_tag.delete()
return True
return True
15 changes: 15 additions & 0 deletions app/schemas/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ class ProjectListSchema(Schema):
description = fields.String()


class ProjectIndexSchema(Schema):
id = fields.Method("get_id", dump_only=True)
name = fields.Method("get_name", dump_only=True)
description = fields.Method("get_description", dump_only=True)

def get_id(self, obj):
return str(obj.project.id)

def get_name(self, obj):
return obj.project.name

def get_description(self, obj):
return obj.project.description


class ProjectSchema(Schema):

id = fields.UUID(dump_only=True)
Expand Down
10 changes: 7 additions & 3 deletions app/schemas/tags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from app.schemas.project import ProjectListSchema
from app.schemas.project import ProjectIndexSchema
from app.schemas.project_users import UserRoleSchema
from marshmallow import Schema, fields, validate, pre_load
from marshmallow import Schema, fields


class TagSchema(Schema):
Expand All @@ -9,6 +9,10 @@ class TagSchema(Schema):
name = fields.String(required=True)
is_super_tag = fields.Boolean()
date_created = fields.Date(dump_only=True)
projects_count = fields.Method("get_projects_count", dump_only=True)

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


class TagsProjectsSchema(TagSchema):
Expand All @@ -27,7 +31,7 @@ def get_is_super_tag(self, obj):


class TagsDetailSchema(TagSchema):
projects = fields.Nested(ProjectListSchema, many=False, dump_only=True)
projects = fields.Nested(ProjectIndexSchema, many=True, dump_only=True)


class TagFollowerSchema(Schema):
Expand Down
17 changes: 0 additions & 17 deletions migrations/versions/824b7f125075_.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,10 @@ def upgrade():
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.drop_table('project_database')
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('project_database',
sa.Column('id', postgresql.UUID(), server_default=sa.text('uuid_generate_v4()'), autoincrement=False, nullable=False),
sa.Column('project_id', postgresql.UUID(), autoincrement=False, nullable=True),
sa.Column('date_created', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
sa.Column('host', sa.VARCHAR(length=256), autoincrement=False, nullable=True),
sa.Column('name', sa.VARCHAR(length=256), autoincrement=False, nullable=False),
sa.Column('password', sa.VARCHAR(length=256), autoincrement=False, nullable=False),
sa.Column('user', sa.VARCHAR(length=256), autoincrement=False, nullable=False),
sa.Column('port', sa.INTEGER(), autoincrement=False, nullable=True),
sa.Column('database_flavour_name', sa.VARCHAR(length=256), autoincrement=False, nullable=True),
sa.Column('deleted', sa.BOOLEAN(), autoincrement=False, nullable=True),
sa.Column('disabled', sa.BOOLEAN(), autoincrement=False, nullable=True),
sa.Column('admin_disabled', sa.BOOLEAN(), autoincrement=False, nullable=True),
sa.ForeignKeyConstraint(['project_id'], ['project.id'], name='project_database_project_id_fkey'),
sa.PrimaryKeyConstraint('id', name='project_database_pkey')
)
op.drop_table('tag_followers')
# ### end Alembic commands ###

0 comments on commit fafb4d7

Please sign in to comment.