Skip to content

Commit

Permalink
Field reordering -- ispras/lingvodoc-react#1109 (#1500)
Browse files Browse the repository at this point in the history
* reorder columns

* auto reordering

* fix

* comments
  • Loading branch information
vmonakhov authored Mar 22, 2024
1 parent 85423a4 commit de4ce0a
Showing 1 changed file with 102 additions and 1 deletion.
103 changes: 102 additions & 1 deletion lingvodoc/schema/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
func,
or_,
tuple_,
union)
union,
desc)

import sqlalchemy.dialects.postgresql as postgresql

Expand Down Expand Up @@ -344,6 +345,7 @@
storage_file,
view_field_from_object)

from pdb import set_trace as A

# Setting up logging.
log = logging.getLogger(__name__)
Expand Down Expand Up @@ -7042,6 +7044,104 @@ def mutate(
'Exception:\n' + traceback_string))


class ReorderColumns(graphene.Mutation):
"""
curl 'https://lingvodoc.ispras.ru/api/graphql' \
-H 'Content-Type: application/json' \
-H 'Cookie: locale_id=2; auth_tkt=$TOKEN!userid_type:int; client_id=$ID' \
--data-raw '{ "operationName": "reorder_columns", "variables": {"perspectiveId": null}, \
"query": "mutation reorder_columns($perspectiveId: LingvodocID) \
{ reorder_columns(perspective_id: $perspectiveId, debug_flag: true) { triumph }}"}'
#! set perspectiveId to concrete LingvodocID for one perspective's positions reordering,
#! otherwise all perspectives with duplicated columns positions will be processed
"""

class Arguments:

perspective_id = LingvodocID()
debug_flag = graphene.Boolean()

triumph = graphene.Boolean()

@staticmethod
def mutate(
root,
info,
perspective_id = None,
debug_flag = False):

try:
client_id = info.context.client_id
if debug_flag:
log.debug(f"client_id: {client_id}")

client = DBSession.query(Client).filter_by(id = client_id).first()

if not client or client.user_id != 1:
return ResponseError('Only administrator can reorder columns.')

perspective_list = [perspective_id] if perspective_id else (
DBSession
.query(
dbColumn.parent_client_id,
dbColumn.parent_object_id)
.filter_by(
marked_for_deletion= 'false')
.group_by(
dbColumn.parent_client_id,
dbColumn.parent_object_id)
.having(
func.count(dbColumn.position) -
func.count(func.distinct(dbColumn.position)) > 0)
.all()) or []

for per_id in perspective_list:
column_list = (
DBSession
.query(dbColumn)
.filter_by(
parent_id = per_id,
marked_for_deletion = 'false')
.order_by(
dbColumn.position,
dbColumn.client_id,
dbColumn.object_id,
dbColumn.created_at)
.all()) or []

if debug_flag:
log.debug(f'Processing {per_id} ...')

for i in range(1, len(column_list)):
if column_list[i].position <= column_list[i-1].position:
column_list[i].position = column_list[i-1].position + 1

if debug_flag:
log.debug(f">> Changed {i}'th")

if debug_flag:
log.debug(f"Total processed {len(perspective_list)} perspectives.")

return ReorderColumns(triumph = True)

except Exception as exception:

traceback_string = (

''.join(
traceback.format_exception(
exception, exception, exception.__traceback__))[:-1])

log.warning('columns_reordering: exception')
log.warning(traceback_string)

return (

ResponseError(
'Exception:\n' + traceback_string))


class MyMutations(graphene.ObjectType):
"""
Mutation classes.
Expand Down Expand Up @@ -7109,6 +7209,7 @@ class MyMutations(graphene.ObjectType):
create_column = CreateColumn.Field()
update_column = UpdateColumn.Field()
delete_column = DeleteColumn.Field()
reorder_columns = ReorderColumns.Field()
create_grant = CreateGrant.Field()
update_grant = UpdateGrant.Field()
# delete_grant = DeleteGrant.Field()
Expand Down

0 comments on commit de4ce0a

Please sign in to comment.