Skip to content

Commit

Permalink
chore: rename project_tags to tags (#514)
Browse files Browse the repository at this point in the history
* rename project_tags to tags

* revert comment out volumes in compose file
  • Loading branch information
Mubangizi committed Jul 4, 2024
1 parent 060a447 commit 5635807
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 113 deletions.
12 changes: 6 additions & 6 deletions api_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3641,10 +3641,10 @@ paths:
500:
description: "Internal Server Error"

"/projects/tags/{tag_id}":
"/tags/{tag_id}":
get:
tags:
- projects
- tags
consumes:
- application/json
produces:
Expand All @@ -3670,7 +3670,7 @@ paths:

delete:
tags:
- projects
- tags
consumes:
- application/json
produces:
Expand All @@ -3696,10 +3696,10 @@ paths:
description: "Internal Server Error"


"/projects/tags":
"/tags":
post:
tags:
- projects
- tags
consumes:
- application/json
parameters:
Expand Down Expand Up @@ -3729,7 +3729,7 @@ paths:

get:
tags:
- projects
- tags
consumes:
- application/json
produces:
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from .user_role import UserRolesView
from .transactions import TransactionRecordView, TransactionRecordDetailView, CreditTransactionRecordView, CreditPurchaseTransactionRecordView
from .project import (
ProjectsView, ProjectDetailView, UserProjectsView, ProjectGetCostsView, ClusterProjectsView,ProjectPinView,
ProjectsView, ProjectDetailView, UserProjectsView, ProjectGetCostsView, ClusterProjectsView, ProjectPinView,
ProjectDisableView, ProjectEnableView)
from .app import (AppsView, ProjectAppsView, AppDetailView, AppLogsView,
AppRevertView, AppReviseView, AppRedeployView, AppDisableView, AppEnableView, AppDockerWebhookListenerView)
Expand All @@ -30,4 +30,4 @@
from .system_status import SystemSummaryView
from .project_users import ProjectUsersView, ProjectUsersTransferView, ProjectUsersHandleInviteView, ProjectFollowingView
from .activity_feed import ActivityFeedView
from .project_tags import ProjectTagsView, ProjectTagsDetailView
from .tags import TagsView, TagsDetailView
88 changes: 0 additions & 88 deletions app/controllers/project_tags.py

This file was deleted.

92 changes: 92 additions & 0 deletions app/controllers/tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

from flask_restful import Resource, request
from flask_jwt_extended import jwt_required
from app.schemas.tags import TagSchema
from app.models.tags import Tag
from app.helpers.decorators import admin_required


class TagsView(Resource):

@jwt_required
def post(self):
tags_data = request.get_json()
tag_schema = TagSchema()
none_existing_tags = []

for tag in tags_data:
validated_tag_data, errors = tag_schema.load({'name': tag})
if errors:
return dict(status="fail", message=errors), 400
if not Tag.find_first(name=validated_tag_data['name']):
none_existing_tags.append(Tag(**validated_tag_data))

if none_existing_tags:
if Tag.bulk_save(none_existing_tags):
return dict(
status='success',
message='Tags saved successfully'
), 201
else:
return dict(
status='fail',
message='An error occurred while saving tags'
), 500
else:
return dict(
status='success',
message='No new tags to save'
), 201

@jwt_required
def get(self):
keywords = request.args.get('keywords', None)

tag_schema = TagSchema(many=True)

tags = Tag.find_all()
print(tags)
if keywords:
tags = Tag.query.filter(
Tag.name.ilike(f'%{keywords}%'))

tags_data = tag_schema.dump(tags)

return dict(
status="success",
data=tags_data.data
), 200


class TagsDetailView(Resource):

@jwt_required
def get(self, tag_id):
tag_id_schema = TagSchema()

tag = Tag.get_by_id(tag_id)

tags_data = tag_id_schema.dump(tag)

return dict(
status="success",
data=tags_data.data
), 200

@admin_required
def delete(self, tag_id):

tag = Tag.get_by_id(tag_id)

deleted = tag.soft_delete()

if not deleted:
return dict(
status='fail',
message='An error occured during deletion'
), 500

return dict(
status='success',
message=f"Tag {tag_id} successfully deleted"
), 200
8 changes: 4 additions & 4 deletions app/models/model_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ def soft_delete(self):
db.session.rollback()
return False

def soft_delete(self):
@classmethod
def bulk_save(cls, objects):
try:
setattr(self, 'deleted', True)
setattr(self, 'name', f"{self.name}_deleted_{int(time.time())}")
db.session.bulk_save_objects(objects)
db.session.commit()
return True
except SQLAlchemyError as e:
except SQLAlchemyError:
db.session.rollback()
return False

Expand Down
10 changes: 3 additions & 7 deletions app/models/project_tag.py → app/models/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@
from sqlalchemy import text as sa_text



class ProjectTag(ModelMixin):
__tablename__ = "project_tags"
class Tag(ModelMixin):
__tablename__ = "tag"
query_class = SoftDeleteQuery

id = db.Column(UUID(as_uuid=True), primary_key=True,
server_default=sa_text("uuid_generate_v4()"))
server_default=sa_text("uuid_generate_v4()"))
name = db.Column(db.String)
deleted = db.Column(db.Boolean, default=False)
date_created = db.Column(db.DateTime, default=db.func.current_timestamp())



7 changes: 4 additions & 3 deletions app/routes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
UserAdminUpdateView, AppRevertView, ProjectGetCostsView, TransactionRecordView, CreditTransactionRecordView, CreditPurchaseTransactionRecordView,
BillingInvoiceView, BillingInvoiceNotificationView, SystemSummaryView, CreditDetailView, ProjectUsersView, ProjectUsersTransferView, AppReviseView,
ProjectUsersHandleInviteView, ClusterProjectsView, ProjectDisableView, ProjectEnableView, AppRedeployView, AppDisableView, AppEnableView,
ProjectTagsView,ProjectTagsDetailView,
TagsView, TagsDetailView,
UserDisableView, UserEnableView, AppDockerWebhookListenerView, UserFollowersView, UserFollowView, ProjectFollowingView, ActivityFeedView)
from app.controllers.app import AppRevisionsView
from app.controllers.billing_invoice import BillingInvoiceDetailView
Expand Down Expand Up @@ -150,8 +150,9 @@
api.add_resource(ProjectPinView, '/projects/<string:project_id>/pin')
# User Project routes
api.add_resource(UserProjectsView, '/users/<string:user_id>/projects')
api.add_resource(ProjectTagsView, '/projects/tags')
api.add_resource(ProjectTagsDetailView, '/projects/tags/<string:tag_id>')
# Tags Routes
api.add_resource(TagsView, '/tags')
api.add_resource(TagsDetailView, '/tags/<string:tag_id>')
# App routes
api.add_resource(AppsView, '/apps')
api.add_resource(AppDetailView, '/apps/<string:app_id>')
Expand Down
5 changes: 2 additions & 3 deletions app/schemas/project_tags.py → app/schemas/tags.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from marshmallow import Schema, fields, validate, pre_load


class ProjectTagSchema(Schema):
class TagSchema(Schema):

id = fields.UUID(dump_only=True)
name = fields.String(required=True, error_message={
Expand All @@ -10,6 +10,5 @@ class ProjectTagSchema(Schema):
validate.Regexp(
regex=r'^(?!\s*$)', error='name should be a valid string'
),
])
])
date_created = fields.Date(dump_only=True)

42 changes: 42 additions & 0 deletions migrations/versions/c7f9222b60b8_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""empty message
Revision ID: c7f9222b60b8
Revises: 2f06b8e0c98b
Create Date: 2024-07-04 08:52:22.750942
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = 'c7f9222b60b8'
down_revision = '2f06b8e0c98b'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('tag',
sa.Column('id', postgresql.UUID(as_uuid=True), server_default=sa.text('uuid_generate_v4()'), nullable=False),
sa.Column('name', sa.String(), nullable=True),
sa.Column('deleted', sa.Boolean(), nullable=True),
sa.Column('date_created', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.drop_table('project_tags')
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('project_tags',
sa.Column('id', postgresql.UUID(), server_default=sa.text('uuid_generate_v4()'), autoincrement=False, nullable=False),
sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=True),
sa.Column('deleted', sa.BOOLEAN(), autoincrement=False, nullable=True),
sa.Column('date_created', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
sa.PrimaryKeyConstraint('id', name='project_tags_pkey')
)
op.drop_table('tag')
# ### end Alembic commands ###

0 comments on commit 5635807

Please sign in to comment.