Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perspective view better pagination -- https://github.com/ispras/lingvodoc-react/issues/1133 #1513

Merged
merged 35 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
521645b
init
vmonakhov Jul 25, 2024
d7e7dc9
next steps
vmonakhov Jul 26, 2024
14a6d7e
minor
vmonakhov Jul 26, 2024
2131ecf
PerspectivePage methods moved to DictionaryPerspective
vmonakhov Jul 26, 2024
0b78326
fixes
vmonakhov Jul 27, 2024
a83f4e7
next steps
vmonakhov Jul 29, 2024
23f0b1f
sql to alchemy
vmonakhov Jul 29, 2024
a76917c
sql to alchemy
vmonakhov Jul 29, 2024
9b0f786
sql to alchemy
vmonakhov Jul 30, 2024
104915c
refactoring
vmonakhov Jul 30, 2024
0131473
filtering
vmonakhov Jul 30, 2024
455c31b
filtering
vmonakhov Jul 31, 2024
300ae53
sorting
vmonakhov Jul 31, 2024
68fb05a
refactoring
vmonakhov Jul 31, 2024
26f6a07
checking edit mode
vmonakhov Jul 31, 2024
747d2a0
refactoring
vmonakhov Jul 31, 2024
3bb71e5
fix
vmonakhov Jul 31, 2024
180ce25
regexp
vmonakhov Aug 1, 2024
97119ac
refactoring
vmonakhov Aug 1, 2024
34e4b44
fixed and cleaned-up
vmonakhov Aug 2, 2024
e303459
complex sorting
vmonakhov Aug 2, 2024
0400e54
correct sorting within lex entry
vmonakhov Aug 3, 2024
1e226af
sort by lowercase
vmonakhov Aug 3, 2024
4bcd988
fixed filtering
vmonakhov Aug 3, 2024
1317953
minor
vmonakhov Aug 3, 2024
66aa44b
cleanup
vmonakhov Aug 5, 2024
5f4a1ae
fixes and cleanup
vmonakhov Aug 6, 2024
77f577f
more correct filtering
vmonakhov Aug 6, 2024
90981e7
next steps
vmonakhov Aug 6, 2024
2a32cfa
outerjoin and labels
vmonakhov Aug 8, 2024
2bdf07c
with sorting_cte
vmonakhov Aug 9, 2024
f1c53b9
created entries calculating
vmonakhov Aug 11, 2024
463eadf
new entries showing
vmonakhov Aug 11, 2024
5b5befc
fixes
vmonakhov Aug 11, 2024
2beedad
fixes
vmonakhov Aug 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
315 changes: 242 additions & 73 deletions lingvodoc/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
Index,
literal,
or_,
func,
desc,
Sequence,
Table,
tuple_)
Expand Down Expand Up @@ -66,7 +68,7 @@
# Project imports.

import lingvodoc.cache.caching as caching

from pdb import set_trace as A

# Setting up logging.
log = logging.getLogger(__name__)
Expand Down Expand Up @@ -1022,6 +1024,25 @@ class Dictionary(
domain = Column(Integer, default=0)


class PerspectivePage(
CompositeIdMixin,
TableNameMixin,
ParentMixin,
CreatedAtMixin,
TranslationMixin,
StateMixin,
MarkedForDeletionMixin,
AdditionalMetadataMixin,
ObjectTOCMixin,
Base):
"""
This object used to compile and get list of lexical entries with extra information
after filtering, sorting and pagination on backend
Parent: DictionaryPerspective
"""
__parentname__ = 'DictionaryPerspective'


class DictionaryPerspective(
CompositeIdMixin,
TableNameMixin,
Expand Down Expand Up @@ -1899,99 +1920,247 @@ def remove_keys(obj, rubbish):
@classmethod
def graphene_track_multiple(
cls,
lexs,
lexes,
publish = None,
accept = None,
delete = False,
filter = None,
sort_by_field = None,
is_ascending = None,
is_case_sens = True,
is_regexp = False,
created_entries = [],
check_perspective = True):

deleted_per = []
alive_lexes = []

if check_perspective:
deleted_per = DictionaryPerspective.get_deleted()

filtered_lexes = []
for x in lexes:

deleted_persps = DictionaryPerspective.get_deleted()
for i in lexs:
if (i[2], i[3]) not in deleted_persps:
filtered_lexes.append(i)
if len(x) >= 4 and (x[2], x[3]) in deleted_per:
continue

else:
alive_lexes.append({'client_id': x[0], 'object_id': x[1]})

filtered_lexes = lexs
temp_table_name = 'lexical_entries_temp_table' + str(uuid.uuid4()).replace("-", "")

ls = []
DBSession.execute(
'''create TEMPORARY TABLE %s (client_id BIGINT, object_id BIGINT) on COMMIT DROP;''' % temp_table_name)

for i, x in enumerate(filtered_lexes):
ls.append({'traversal_lexical_order': i, 'client_id': x[0], 'object_id': x[1]})
class Tempo(Base):

if not ls:
return []
__tablename__ = temp_table_name
__table_args__ = {'prefixes': ['TEMPORARY']}

pub_filter = ""
client_id = Column(SLBigInteger(), primary_key=True)
object_id = Column(SLBigInteger(), primary_key=True)

if publish is not None or accept is not None or delete is not None:
where_cond = list()
if accept:
where_cond.append("publishingentity.accepted = True")
if accept is False:
where_cond.append("publishingentity.accepted = False")
if publish:
where_cond.append("publishingentity.published = True")
if publish is False:
where_cond.append("publishingentity.published = False")
if delete:
where_cond.append("cte_expr.marked_for_deletion = True")
if delete is False:
where_cond.append("cte_expr.marked_for_deletion = False")
where_cond = ["WHERE", " AND ".join(where_cond)]
pub_filter = " ".join(where_cond)
DBSession.execute(
Tempo.__table__
.insert()
.values(alive_lexes))

temp_table_name = 'lexical_entries_temp_table' + str(uuid.uuid4()).replace("-", "")
# We need just lexical entry and entity id and entity's content for sorting and filtering

DBSession.execute(
'''create TEMPORARY TABLE %s (traversal_lexical_order INTEGER, client_id BIGINT, object_id BIGINT) on COMMIT DROP;''' % temp_table_name)
entities_query = (
DBSession
.query(
Entity.client_id,
Entity.object_id,
Entity.parent_client_id,
Entity.parent_object_id,
Entity.content)

DBSession.execute(
'''insert into %s (traversal_lexical_order, client_id, object_id) values (:traversal_lexical_order, :client_id, :object_id);''' % temp_table_name,
ls)
.filter(
Entity.parent_client_id == Tempo.client_id,
Entity.parent_object_id == Tempo.object_id))

statement = text('''
WITH cte_expr AS
(SELECT
entity.*,
{0}.traversal_lexical_order AS traversal_lexical_order
FROM entity
INNER JOIN {0}
ON
entity.parent_client_id = {0}.client_id
AND entity.parent_object_id = {0}.object_id
)
SELECT
cte_expr.client_id,
cte_expr.object_id,
cte_expr.parent_client_id,
cte_expr.parent_object_id,
cte_expr.self_client_id,
cte_expr.self_object_id,
cte_expr.link_client_id,
cte_expr.link_object_id,
cte_expr.field_client_id,
cte_expr.field_object_id,
cte_expr.locale_id,
cte_expr.marked_for_deletion,
cte_expr.content,
cte_expr.additional_metadata,
cte_expr.created_at,
publishingentity.*
FROM cte_expr
LEFT JOIN publishingentity
ON publishingentity.client_id = cte_expr.client_id AND publishingentity.object_id = cte_expr.object_id
{1}
ORDER BY cte_expr.traversal_lexical_order;
'''.format(temp_table_name, pub_filter))
filed_lexes = entities_query.with_entities('parent_client_id', 'parent_object_id')

# Collect all empty lexes including created ones

empty_lexes = (
DBSession
.query(
Tempo.client_id,
Tempo.object_id)

.filter(
tuple_(Tempo.client_id, Tempo.object_id)
.notin_(filed_lexes))

.all())

# Apply user's custom filter

if filter:

# We filter using Entity model in parallels twice,
# so we need to use cte(), we can't use .with_entities

# Filter from special fields
filtered_entities = entities_query.filter(
Entity.field_id != (66, 25))

if is_regexp:
if is_case_sens:
filtered_entities = filtered_entities.filter(
Entity.content.op('~')(filter)).cte()
else:
filtered_entities = filtered_entities.filter(
Entity.content.op('~*')(filter)).cte()
else:
if is_case_sens:
filtered_entities = filtered_entities.filter(
Entity.content.like(f"%{filter}%")).cte()
else:
filtered_entities = filtered_entities.filter(
Entity.content.ilike(f"%{filter}%")).cte()

filtered_lexes = (
DBSession
.query(
filtered_entities.c.parent_client_id,
filtered_entities.c.parent_object_id))

entities_query = entities_query.filter(
Entity.parent_id
.in_(filtered_lexes))

entities_cte = entities_query.cte()

# Create sorting_cte to order by it

sorting_cte = None

if sort_by_field:

field_entities = entities_query.filter(Entity.field_id == sort_by_field).cte()

alpha_entities = (
DBSession
.query(
field_entities.c.parent_client_id.label('lex_client_id'),
field_entities.c.parent_object_id.label('lex_object_id'),
func.min(func.lower(field_entities.c.content)).label('first_entity'),
func.max(func.lower(field_entities.c.content)).label('last_entity'))

.filter(func.length(field_entities.c.content) > 0)

.group_by('lex_client_id', 'lex_object_id')

.cte()
)

sorting_cte = (
DBSession
.query(
entities_cte.c.parent_client_id,
entities_cte.c.parent_object_id,
entities_cte.c.client_id,
entities_cte.c.object_id,
alpha_entities.c.first_entity,
alpha_entities.c.last_entity,
field_entities.c.content.label('order_content'))

.outerjoin(
alpha_entities, and_(
alpha_entities.c.lex_client_id == entities_cte.c.parent_client_id,
alpha_entities.c.lex_object_id == entities_cte.c.parent_object_id))

.outerjoin(
field_entities, and_(
field_entities.c.client_id == entities_cte.c.client_id,
field_entities.c.object_id == entities_cte.c.object_id))

.cte())

entries = DBSession.query(Entity, PublishingEntity).from_statement(statement) .options(joinedload('publishingentity')).yield_per(100)
entities_cte = sorting_cte

return entries
# Finally, filter and sort Entity and PublishingEntity objects

entities_result = (
DBSession
.query(
Entity,
PublishingEntity)

.outerjoin(
PublishingEntity))

# Pre-filtering

if accept is not None:
entities_result = entities_result.filter(PublishingEntity.accepted == accept)
if publish is not None:
entities_result = entities_result.filter(PublishingEntity.published == publish)
if delete is not None:
entities_result = entities_result.filter(Entity.marked_for_deletion == delete)

# Get new entities from entities_before_custom_filtering

new_entities_result = (
entities_result
.filter(

tuple_(Entity.parent_client_id, Entity.parent_object_id)
.in_(filed_lexes),

tuple_(Entity.parent_client_id, Entity.parent_object_id)
.in_(created_entries)))

# Filter and join at once to get and sort old entities

old_entities_result = (
entities_result
.filter(

entities_cte.c.client_id == Entity.client_id,
entities_cte.c.object_id == Entity.object_id,

tuple_(Entity.parent_client_id, Entity.parent_object_id)
.notin_(created_entries)))

# Custom sorting

if sorting_cte is not None:

if is_ascending:

old_entities_result = old_entities_result.order_by(
entities_cte.c.first_entity,
entities_cte.c.parent_client_id,
entities_cte.c.parent_object_id,
func.lower(entities_cte.c.order_content)
)

else:

old_entities_result = old_entities_result.order_by(
desc(entities_cte.c.last_entity),
entities_cte.c.parent_client_id,
entities_cte.c.parent_object_id,
desc(func.lower(entities_cte.c.order_content))
)

# Default sorting

old_entities_result = old_entities_result.order_by(
Entity.parent_client_id,
Entity.parent_object_id,
Entity.client_id,
Entity.object_id)

return (
new_entities_result,
old_entities_result
.options(
joinedload('publishingentity'))
.yield_per(100),
empty_lexes)


class Entity(
Expand Down
Loading