fix inherit access groups on dnd

This commit is contained in:
SvyatoslavArtymovych 2023-06-15 15:57:21 +03:00
parent 162d0ce55b
commit 126c5f3783
6 changed files with 149339 additions and 5 deletions

View File

@ -1,4 +1,4 @@
from app import models as m
from app import models as m, db
from app.logger import log
@ -31,3 +31,46 @@ def copy_access_groups(
m.SectionAccessGroups(
section_id=copy_to.id, access_group_id=access_group.id
).save()
def recursive_copy_access_groups(
copy_from: m.Book or m.Collection or m.Interpretation or m.Section, copy_to
):
current_access_groups = None
match type(copy_to):
case m.Book:
current_access_groups = m.BookAccessGroups.query.filter_by(
book_id=copy_to.id
).all()
case m.Collection:
current_access_groups = m.CollectionAccessGroups.query.filter_by(
collection_id=copy_to.id
).all()
case m.Interpretation:
current_access_groups = m.InterpretationAccessGroups.query.filter_by(
interpretation_id=copy_to.id
).all()
case m.Section:
current_access_groups = m.SectionAccessGroups.query.filter_by(
section_id=copy_to.id
).all()
if current_access_groups:
for access_group in current_access_groups:
db.session.delete(access_group)
db.session.commit()
copy_access_groups(copy_from, copy_to)
if hasattr(copy_to, "active_children"):
for collection in copy_to.active_children:
recursive_copy_access_groups(copy_to, collection)
if hasattr(copy_to, "active_sections"):
for section in copy_to.active_sections:
recursive_copy_access_groups(copy_to, section)
if hasattr(copy_to, "active_interpretations"):
for interpretations in copy_to.active_interpretations:
recursive_copy_access_groups(copy_to, interpretations)

File diff suppressed because one or more lines are too long

View File

@ -5,6 +5,7 @@ from app.controllers import (
create_breadcrumbs,
register_book_verify_route,
)
from app.controllers.copy_access_groups import recursive_copy_access_groups
from app.controllers.notification_producer import collection_notification
from app.controllers.delete_nested_book_entities import (
delete_nested_collection_entities,
@ -269,6 +270,15 @@ def change_collection_position(book_id: int, collection_id: int):
)
collection.parent_id = collection_id
# current_access_group = m.CollectionAccessGroups.query.filter_by(
# collection_id=collection.id
# ).all()
# for access_group in current_access_group:
# db.session.delete(access_group)
# db.session.commit()
recursive_copy_access_groups(new_parent, collection)
if new_parent.active_children:
collections_to_edit = (
m.Collection.query.filter(

View File

@ -2,6 +2,7 @@ from flask import flash, redirect, url_for, request
from flask_login import login_required, current_user
from app.controllers import register_book_verify_route
from app.controllers.copy_access_groups import recursive_copy_access_groups
from app.controllers.notification_producer import section_notification
from app.controllers.delete_nested_book_entities import delete_nested_section_entities
from app import models as m, db, forms as f
@ -165,6 +166,8 @@ def change_section_position(book_id: int, section_id: int):
)
section.collection_id = collection_id
recursive_copy_access_groups(collection, section)
if collection.active_sections:
sections_to_edit = (
m.Section.query.filter(

View File

@ -15,6 +15,8 @@ const refreshAccessLevelTree = async (userId: string, bookId: string) => {
const id = parseInt(element.getAttribute('data-access-to-id'));
if (ids.includes(id)) {
element.checked = true;
} else {
element.checked = false;
}
});
});

View File

@ -74,7 +74,7 @@ def test_change_collection_ordering(client):
collection: m.Collection = db.session.get(m.Collection, 3)
assert collection.parent_id == collection_1.id
assert collection.position == 1
assert collection.position == 0
response: Response = client.post(
f"/book/{book.id}/{collection.id}/collection/change_position",
@ -156,7 +156,7 @@ def test_change_section_ordering(client):
collection: m.Collection = section.collection
assert len(collection.active_sections) == 1
assert section.position != new_position
assert section.position == 1
assert section.position == 0
response: Response = client.post(
f"/book/{book.id}/{section.id}/section/change_position",