adding contributor to access group

This commit is contained in:
SvyatoslavArtymovych 2023-05-25 14:11:19 +03:00
parent d80ae9938a
commit 9a982f2fd1
3 changed files with 187 additions and 6 deletions

View File

@ -32,8 +32,9 @@ def add_contributor(book_id: int):
form = f.AddContributorForm()
if form.validate_on_submit():
user_id = form.user_id.data
book_contributor = m.BookContributor.query.filter_by(
user_id=form.user_id.data, book_id=book_id
user_id=user_id, book_id=book_id
).first()
if book_contributor:
log(log.INFO, "Contributor: [%s] already exists", book_contributor)
@ -41,12 +42,22 @@ def add_contributor(book_id: int):
return redirect(url_for("book.settings", book_id=book_id))
role = m.BookContributor.Roles(int(form.role.data))
contributor = m.BookContributor(
user_id=form.user_id.data, book_id=book_id, role=role
)
contributor = m.BookContributor(user_id=user_id, book_id=book_id, role=role)
log(log.INFO, "New contributor [%s]", contributor)
contributor.save()
groups = (
db.session.query(m.AccessGroup)
.filter(
m.BookAccessGroups.book_id == book_id,
m.AccessGroup.id == m.BookAccessGroups.access_group_id,
m.AccessGroup.name == role.name.lower(),
)
.all()
)
for group in groups:
m.UserAccessGroups(user_id=user_id, access_group_id=group.id).save()
flash("Contributor was added!", "success")
return redirect(url_for("book.settings", book_id=book_id))
else:

View File

@ -1,6 +1,6 @@
import json
from flask import redirect, url_for, Blueprint, request, flash
from flask import redirect, url_for, Blueprint, flash
from flask_login import current_user
from app import forms as f, models as m, db
@ -35,6 +35,6 @@ def set():
flash("User are not contributor of this book!", "danger")
return redirect(url_for("book.my_library"))
permissions = json.loads(form.permissions.data)
# permissions = json.loads(form.permissions.data)
return {"status": "ok"}

View File

@ -0,0 +1,170 @@
"""access_groups
Revision ID: 0fc51411af21
Revises: 7baa732e01c6
Create Date: 2023-05-25 12:44:41.843011
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "0fc51411af21"
down_revision = "7baa732e01c6"
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"permissions",
sa.Column("access", sa.Integer(), nullable=True),
sa.Column(
"entity_type",
sa.Enum(
"UNKNOWN",
"BOOK",
"COLLECTION",
"SECTION",
"INTERPRETATION",
"COMMENT",
name="entity",
),
nullable=True,
),
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("is_deleted", sa.Boolean(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"access_groups",
sa.Column("name", sa.String(length=32), nullable=False),
sa.Column("book_id", sa.Integer(), nullable=True),
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("is_deleted", sa.Boolean(), nullable=True),
sa.ForeignKeyConstraint(
["book_id"],
["books.id"],
),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"books_access_groups",
sa.Column("book_id", sa.Integer(), nullable=True),
sa.Column("access_group_id", sa.Integer(), nullable=True),
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("is_deleted", sa.Boolean(), nullable=True),
sa.ForeignKeyConstraint(
["access_group_id"],
["access_groups.id"],
),
sa.ForeignKeyConstraint(
["book_id"],
["books.id"],
),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"permissions_access_groups",
sa.Column("permission_id", sa.Integer(), nullable=True),
sa.Column("access_group_id", sa.Integer(), nullable=True),
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("is_deleted", sa.Boolean(), nullable=True),
sa.ForeignKeyConstraint(
["access_group_id"],
["access_groups.id"],
),
sa.ForeignKeyConstraint(
["permission_id"],
["permissions.id"],
),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"users_access_groups",
sa.Column("user_id", sa.Integer(), nullable=True),
sa.Column("access_group_id", sa.Integer(), nullable=True),
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("is_deleted", sa.Boolean(), nullable=True),
sa.ForeignKeyConstraint(
["access_group_id"],
["access_groups.id"],
),
sa.ForeignKeyConstraint(
["user_id"],
["users.id"],
),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"collections_access_groups",
sa.Column("collection_id", sa.Integer(), nullable=True),
sa.Column("access_group_id", sa.Integer(), nullable=True),
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("is_deleted", sa.Boolean(), nullable=True),
sa.ForeignKeyConstraint(
["access_group_id"],
["access_groups.id"],
),
sa.ForeignKeyConstraint(
["collection_id"],
["collections.id"],
),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"sections_access_groups",
sa.Column("section_id", sa.Integer(), nullable=True),
sa.Column("access_group_id", sa.Integer(), nullable=True),
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("is_deleted", sa.Boolean(), nullable=True),
sa.ForeignKeyConstraint(
["access_group_id"],
["access_groups.id"],
),
sa.ForeignKeyConstraint(
["section_id"],
["sections.id"],
),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"interpretations_access_groups",
sa.Column("interpretation_id", sa.Integer(), nullable=True),
sa.Column("access_group_id", sa.Integer(), nullable=True),
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("is_deleted", sa.Boolean(), nullable=True),
sa.ForeignKeyConstraint(
["access_group_id"],
["access_groups.id"],
),
sa.ForeignKeyConstraint(
["interpretation_id"],
["interpretations.id"],
),
sa.PrimaryKeyConstraint("id"),
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("interpretations_access_groups")
op.drop_table("sections_access_groups")
op.drop_table("collections_access_groups")
op.drop_table("users_access_groups")
op.drop_table("permissions_access_groups")
op.drop_table("books_access_groups")
op.drop_table("access_groups")
op.drop_table("permissions")
# ### end Alembic commands ###