Skip to content

Commit

Permalink
Merge branch 'heavy_refactor' into glottochronology
Browse files Browse the repository at this point in the history
  • Loading branch information
vmonakhov authored Jul 5, 2023
2 parents 1d7288a + d60120b commit 8b96faa
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 39 deletions.
1 change: 1 addition & 0 deletions lingvodoc/schema/gql_holders.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,7 @@ class Metadata(graphene.ObjectType):
bibliographicDataOfTheTranslation = graphene.String()

toc_mark = graphene.Boolean()
attached_users = graphene.List(graphene.Int)


metadata_key_set = {
Expand Down
28 changes: 27 additions & 1 deletion lingvodoc/schema/gql_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,8 @@ class Arguments:
id = LingvodocID(required=True)
translation_gist_id = LingvodocID()
additional_metadata = ObjectVal()
add_user_id = graphene.Int()
del_user_id = graphene.Int()

language = graphene.Field(Language)
triumph = graphene.Boolean()
Expand All @@ -706,9 +708,33 @@ def mutate(root, info, **args):
if translation_gist_id:
dblanguage.translation_gist_client_id = translation_gist_id[0]
dblanguage.translation_gist_object_id = translation_gist_id[1]
additional_metadata = args.get("additional_metadata")
flag_modified(dblanguage, 'translation_gist_client_id')
flag_modified(dblanguage, 'translation_gist_object_id')

additional_metadata = args.get('additional_metadata')
if additional_metadata:
update_metadata(dblanguage, additional_metadata)

new_user = args.get('add_user_id')
if new_user:
user_list = (
dblanguage.additional_metadata
.get('attached_users') or []
)
if new_user not in user_list:
user_list.append(new_user)
update_metadata(dblanguage, {'attached_users': user_list})

del_user = args.get('del_user_id')
if del_user:
user_list = (
dblanguage.additional_metadata
.get('attached_users') or []
)
if del_user in user_list:
user_list.remove(del_user)
update_metadata(dblanguage, {'attached_users': user_list})

language = Language(id=[dblanguage.client_id, dblanguage.object_id])
language.dbObject = dblanguage
return UpdateLanguage(language=language, triumph=True)
Expand Down
15 changes: 5 additions & 10 deletions lingvodoc/scripts/dictionary_dialeqt_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from lingvodoc.utils.search import get_id_to_field_dict

from lingvodoc.cache.caching import CACHE
from lingvodoc.utils.creation import get_attached_users, uniq_list

def find_lexical_entries_by_tags(tags, field_client_id, field_object_id):
return DBSession.query(LexicalEntry) \
Expand Down Expand Up @@ -743,12 +744,12 @@ def convert_db_new(

dictionary_client_id = dictionary.client_id
dictionary_object_id = dictionary.object_id
attached_users = get_attached_users((language_client_id, language_object_id))
for base in DBSession.query(BaseGroup).filter_by(dictionary_default=True):
new_group = Group(parent=base,
subject_object_id=dictionary.object_id,
subject_client_id=dictionary.client_id)
if user not in new_group.users:
new_group.users.append(user)
new_group.users = uniq_list(new_group.users + attached_users + [user])
DBSession.add(new_group)
DBSession.flush()
perspective_metadata = {"authors": []}
Expand Down Expand Up @@ -836,10 +837,7 @@ def convert_db_new(
new_group = Group(parent=base,
subject_object_id=first_perspective.object_id,
subject_client_id=first_perspective.client_id)
if user not in new_group.users:
new_group.users.append(user)
if owner not in new_group.users:
new_group.users.append(owner)
new_group.users = uniq_list(new_group.users + attached_users + [user, owner])
DBSession.add(new_group)
DBSession.flush()
first_perspective_client_id = first_perspective.client_id
Expand Down Expand Up @@ -869,10 +867,7 @@ def convert_db_new(
new_group = Group(parent=base,
subject_object_id=second_perspective.object_id,
subject_client_id=second_perspective.client_id)
if user not in new_group.users:
new_group.users.append(user)
if owner not in new_group.users:
new_group.users.append(owner)
new_group.users = uniq_list(new_group.users + attached_users + [user, owner])
DBSession.add(new_group)
second_perspective_client_id = second_perspective.client_id
second_perspective_object_id = second_perspective.object_id
Expand Down
29 changes: 10 additions & 19 deletions lingvodoc/utils/corpus_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
from lingvodoc.utils.search import get_id_to_field_dict

from lingvodoc.views.v2.utils import storage_file

from lingvodoc.utils.creation import get_attached_users, uniq_list

EAF_TIERS = {
"literary translation": "Translation of Paradigmatic forms",
Expand Down Expand Up @@ -548,19 +548,16 @@ def convert_five_tiers(
DBSession.add(dictionary)

dictionary_id = dictionary.id
attached_users = get_attached_users(language_id)

for base in DBSession.query(BaseGroup).filter_by(dictionary_default = True):

new_group = (

Group(
parent = base,
subject_client_id = dictionary_id[0],
subject_object_id = dictionary_id[1]))

if user not in new_group.users:
new_group.users.append(user)
parent=base,
subject_client_id=dictionary_id[0],
subject_object_id=dictionary_id[1]))

new_group.users = uniq_list(new_group.users + attached_users + [user])
DBSession.add(new_group)

origin_perspective = (
Expand Down Expand Up @@ -836,16 +833,13 @@ def convert_five_tiers(

DBSession.add(first_perspective)

owner_client = DBSession.query(Client).filter_by(id = dictionary.client_id).first()
owner_client = DBSession.query(Client).filter_by(id=dictionary.client_id).first()
owner = owner_client.user
for base in DBSession.query(BaseGroup).filter_by(perspective_default=True):
new_group = Group(parent=base,
subject_object_id=first_perspective.object_id,
subject_client_id=first_perspective.client_id)
if user not in new_group.users:
new_group.users.append(user)
if owner not in new_group.users:
new_group.users.append(owner)
new_group.users = uniq_list(new_group.users + attached_users + [user, owner])
DBSession.add(new_group)

first_perspective_id = first_perspective.id
Expand Down Expand Up @@ -877,16 +871,13 @@ def convert_five_tiers(

DBSession.add(second_perspective)

owner_client = DBSession.query(Client).filter_by(id = dictionary.client_id).first()
owner_client = DBSession.query(Client).filter_by(id=dictionary.client_id).first()
owner = owner_client.user
for base in DBSession.query(BaseGroup).filter_by(perspective_default=True):
new_group = Group(parent=base,
subject_object_id=second_perspective.object_id,
subject_client_id=second_perspective.client_id)
if user not in new_group.users:
new_group.users.append(user)
if owner not in new_group.users:
new_group.users.append(owner)
new_group.users = uniq_list(new_group.users + attached_users + [user, owner])
DBSession.add(new_group)

second_perspective_id = second_perspective.id
Expand Down
63 changes: 54 additions & 9 deletions lingvodoc/utils/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,46 @@ def add_user_to_group(user, group):
if user not in group.users:
group.users.append(user)

def uniq_list(input_list):
return list(set(input_list))

def get_attached_users(language_id):
language_client_id, language_object_id = language_id

base_cte = (
DBSession
.query(
Language.parent_client_id,
Language.parent_object_id,
Language.additional_metadata['attached_users'].label('attached_users'))
.filter(
Language.client_id == language_client_id,
Language.object_id == language_object_id)
.cte(recursive=True))

recursive_query = (
DBSession
.query(
Language.parent_client_id,
Language.parent_object_id,
Language.additional_metadata['attached_users'].label('attached_users'))
.filter(
Language.client_id == base_cte.c.parent_client_id,
Language.object_id == base_cte.c.parent_object_id))

language_cte = base_cte.union(recursive_query)

user_id_list_list = (
DBSession
.query(language_cte.c.attached_users)
.all())

# Concatenate results by tuples firstly and then by lists,
# exclude 'None' values, filter the values to be unique.
user_id_list = sum(filter(None, sum(user_id_list_list, ())), [])
user_id_list = uniq_list(user_id_list)
user_list = DBSession.query(User).filter(User.id.in_(user_id_list)).all()
return user_list

def create_perspective(id = (None, None),
parent_id=None,
Expand Down Expand Up @@ -94,22 +134,24 @@ def create_perspective(id = (None, None),
)
DBSession.add(dbperspective)
DBSession.flush()

owner_client = DBSession.query(Client).filter_by(id=parent.client_id).first()
owner = owner_client.user
client = DBSession.query(Client).filter_by(id=client_id).first()
user = DBSession.query(User).filter_by(id=client.user_id).first()
attached_users = get_attached_users(parent_id)

if not object_id or add_group:
for base in DBSession.query(BaseGroup).filter_by(perspective_default=True):
client = DBSession.query(Client).filter_by(id=client_id).first()
user = DBSession.query(User).filter_by(id=client.user_id).first()
new_group = Group(parent=base,
subject_object_id=dbperspective.object_id,
subject_client_id=dbperspective.client_id)
add_user_to_group(user, new_group)
add_user_to_group(owner, new_group)
subject_object_id=dbperspective.object_id,
subject_client_id=dbperspective.client_id)

new_group.users = uniq_list(new_group.users + attached_users + [user, owner])
DBSession.add(new_group)
DBSession.flush()
return dbperspective


def create_dbdictionary(id=None,
parent_id=None,
translation_gist_id=None,
Expand Down Expand Up @@ -147,13 +189,15 @@ def create_dbdictionary(id=None,

client = DBSession.query(Client).filter_by(id=client_id).first()
user = client.user
attached_users = get_attached_users(parent_id)

if not object_id or add_group:
for base in DBSession.query(BaseGroup).filter_by(dictionary_default=True):
new_group = Group(parent=base,
subject_object_id=dbdictionary_obj.object_id,
subject_client_id=dbdictionary_obj.client_id)
if user not in new_group.users:
new_group.users.append(user)

new_group.users = uniq_list(new_group.users + attached_users + [user])
DBSession.add(new_group)
DBSession.flush()
return dbdictionary_obj
Expand All @@ -162,6 +206,7 @@ def create_dictionary_persp_to_field(id=None,
parent_id=None,
field_id=None,
self_id=None,

link_id=None,
upper_level=None,
position=1):
Expand Down

0 comments on commit 8b96faa

Please sign in to comment.