-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
274 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
|
||
from flask_restful import Resource, request | ||
from flask_jwt_extended import jwt_required, get_jwt_identity, get_jwt_claims | ||
from app.schemas.project_tags import ProjectTagSchema | ||
from app.models.project_tag import ProjectTag | ||
from app.helpers.decorators import admin_required | ||
|
||
|
||
|
||
|
||
class ProjectTagsView(Resource): | ||
|
||
@jwt_required | ||
def post(self): | ||
tags_data = request.get_json() | ||
|
||
project_tag_schema = ProjectTagSchema() | ||
|
||
for _tag in tags_data: | ||
validated_tag_data , errors = project_tag_schema.load({'name' : _tag}) | ||
if not ProjectTag.find_first(name=validated_tag_data['name']): | ||
tag = ProjectTag(**validated_tag_data) | ||
tag.save() | ||
|
||
return dict( | ||
status='success', | ||
message='Tags saved successfully' | ||
) , 201 | ||
|
||
def get(self): | ||
keywords = request.args.get('keywords', None) | ||
|
||
project_tag_schema = ProjectTagSchema(many=True) | ||
|
||
project_tags = ProjectTag.find_all() | ||
|
||
if keywords : | ||
project_tags = ProjectTag.query.filter(ProjectTag.name.ilike(f'%{keywords}%')) | ||
|
||
tags_data = project_tag_schema.dump(project_tags) | ||
|
||
return dict( | ||
status="success", | ||
data=tags_data.data | ||
) , 200 | ||
|
||
class ProjectTagsDetailView(Resource): | ||
def get(self, tag_id): | ||
|
||
project_tag_id_schema = ProjectTagSchema() | ||
|
||
project_tag = ProjectTag.get_by_id(tag_id) | ||
|
||
tags_data = project_tag_id_schema.dump(project_tag) | ||
|
||
return dict( | ||
status="success", | ||
data=tags_data.data | ||
) , 200 | ||
|
||
@admin_required | ||
def delete(self, tag_id): | ||
|
||
current_user_roles = get_jwt_claims()['roles'] | ||
|
||
tag = ProjectTag.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 | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from sqlalchemy.dialects.postgresql import UUID | ||
from app.models import db | ||
from app.models.model_mixin import ModelMixin, SoftDeleteQuery | ||
from sqlalchemy import text as sa_text | ||
|
||
|
||
|
||
class ProjectTag(ModelMixin): | ||
__tablename__ = "project_tags" | ||
query_class = SoftDeleteQuery | ||
|
||
id = db.Column(UUID(as_uuid=True), primary_key=True, | ||
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()) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from marshmallow import Schema, fields, validate, pre_load | ||
|
||
|
||
class ProjectTagSchema(Schema): | ||
|
||
id = fields.UUID(dump_only=True) | ||
name = fields.String(required=True, error_message={ | ||
"required": "name is required"}, | ||
validate=[ | ||
validate.Regexp( | ||
regex=r'^(?!\s*$)', error='name should be a valid string' | ||
), | ||
]) | ||
date_created = fields.Date(dump_only=True) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""empty message | ||
Revision ID: 2f06b8e0c98b | ||
Revises: 5f6dfc8f1b35 | ||
Create Date: 2024-06-27 20:02:34.415744 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '2f06b8e0c98b' | ||
down_revision = '5f6dfc8f1b35' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('project_tags', | ||
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') | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table('project_tags') | ||
# ### end Alembic commands ### |