-
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.
- Loading branch information
Showing
10 changed files
with
157 additions
and
115 deletions.
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 was deleted.
Oops, something went wrong.
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,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 |
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
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,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 ### |