-
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.
able to delete and add tags to project
- Loading branch information
Showing
10 changed files
with
172 additions
and
12 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 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,52 @@ | ||
from app.models.tags import ProjectTag, Tag | ||
|
||
|
||
def create_tags(tag_names): | ||
""" | ||
Create tags | ||
""" | ||
none_existing_tags = [] | ||
existing_tags = [] | ||
for tag in tag_names: | ||
tag = tag.strip() | ||
tag_rec = Tag.find_first(name=tag) | ||
if not tag_rec: | ||
none_existing_tags.append(Tag(name=tag)) | ||
else: | ||
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] | ||
if new_tags: | ||
existing_tags.append(new_tags) | ||
return existing_tags | ||
|
||
|
||
def add_tags_to_project(tag_names, project): | ||
tags = create_tags(tag_names) | ||
project_tags = [] | ||
for tag in tags: | ||
project_tag = ProjectTag.find_first( | ||
tag_id=tag.id, project_id=project.id) | ||
if not project_tag: | ||
project_tags.append(ProjectTag( | ||
tag_id=tag.id, project_id=project.id)) | ||
|
||
if project_tags: | ||
saved_tags = ProjectTag.bulk_save(project_tags) | ||
if not saved_tags: | ||
return False | ||
return True | ||
|
||
def remove_tags_from_project(tag_names, project): | ||
for tag in tag_names: | ||
existing_tag = Tag.find_first(name=tag) | ||
if not existing_tag: | ||
continue | ||
project_tag = ProjectTag.find_first( | ||
tag_id=existing_tag.id, project_id=project.id) | ||
if project_tag: | ||
project_tag.delete() | ||
return 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
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 |
---|---|---|
@@ -1,14 +1,29 @@ | ||
from app.schemas.project import ProjectListSchema | ||
from marshmallow import Schema, fields, validate, pre_load | ||
|
||
|
||
class TagSchema(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' | ||
), | ||
]) | ||
name = fields.String(required=True) | ||
is_super_tag = fields.Boolean() | ||
date_created = fields.Date(dump_only=True) | ||
|
||
|
||
class TagsProjectsSchema(TagSchema): | ||
name = fields.Method("get_name", dump_only=True) | ||
id = fields.Method("get_id", dump_only=True) | ||
is_super_tag = fields.Method("get_is_super_tag", dump_only=True) | ||
|
||
def get_id(self, obj): | ||
return str(obj.tag.id) | ||
|
||
def get_name(self, obj): | ||
return obj.tag.name | ||
|
||
def get_is_super_tag(self, obj): | ||
return obj.tag.is_super_tag | ||
|
||
|
||
class TagsDetailSchema(TagSchema): | ||
projects = fields.Nested(ProjectListSchema, many=False, 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,38 @@ | ||
"""empty message | ||
Revision ID: 403631504272 | ||
Revises: c7f9222b60b8 | ||
Create Date: 2024-07-04 22:38:45.147122 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '403631504272' | ||
down_revision = 'c7f9222b60b8' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('project_tag', | ||
sa.Column('id', postgresql.UUID(as_uuid=True), server_default=sa.text('uuid_generate_v4()'), nullable=False), | ||
sa.Column('project_id', postgresql.UUID(as_uuid=True), nullable=True), | ||
sa.Column('tag_id', postgresql.UUID(as_uuid=True), nullable=True), | ||
sa.Column('date_created', sa.DateTime(), nullable=True), | ||
sa.ForeignKeyConstraint(['project_id'], ['project.id'], ), | ||
sa.ForeignKeyConstraint(['tag_id'], ['tag.id'], ), | ||
sa.PrimaryKeyConstraint('id') | ||
) | ||
op.add_column('tag', sa.Column('is_super_tag', sa.Boolean(), nullable=True)) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_column('tag', 'is_super_tag') | ||
op.drop_table('project_tag') | ||
# ### end Alembic commands ### |