Skip to content

Commit

Permalink
limit relations to 50
Browse files Browse the repository at this point in the history
  • Loading branch information
csae8092 committed Nov 5, 2024
1 parent 1b3159d commit 77d6c36
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
10 changes: 7 additions & 3 deletions apis_core/apis_entities/detail_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ def get(self, request, *args, **kwargs):
rel.get_related_entity_classb().__name__.lower(),
]
prefix = "{}{}-".format(match[0].title()[:2], match[1].title()[:2])
table = get_generic_relations_table(
relation_class=rel, entity_instance=instance, detail=True
)
if match[0] == entity:
link_to_relations = f"{rel.get_listview_url()}?source={pk}"
rel_type = match[1]
Expand Down Expand Up @@ -66,6 +63,13 @@ def get(self, request, *args, **kwargs):
objects = rel.objects.filter(**dict_1)
if callable(getattr(objects, "filter_for_user", None)):
objects = objects.filter_for_user()
disable_sort = False
if objects.count() > 50:
objects = objects[:50]
disable_sort = True
table = get_generic_relations_table(
relation_class=rel, entity_instance=instance, detail=True, disable_sort=disable_sort
)
tb_object = table(data=objects, prefix=prefix)
tb_object_open = request.GET.get(prefix + "page", None)
RequestConfig(request, paginate={"per_page": 10}).configure(tb_object)
Expand Down
7 changes: 6 additions & 1 deletion apis_core/apis_relations/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ class GenericRelationListViewTable(tables.Table):
render_start_date_written = generic_render_start_date_written
render_end_date_written = generic_render_end_date_written


class Meta:
model = relation_class


# the fields list also serves as the defining order of them, as to avoid duplicated definitions
fields = [
related_entity_field_name_a,
Expand Down Expand Up @@ -88,7 +90,7 @@ def __init__(self, *args, **kwargs):
return GenericRelationListViewTable


def get_generic_relations_table(relation_class, entity_instance, detail=None):
def get_generic_relations_table(relation_class, entity_instance, detail=None, disable_sort=False):
"""
Creates a table class according to the relation and entity class given by the parameters.
Expand Down Expand Up @@ -140,6 +142,9 @@ class RelationTableBase(tables.Table):
render_start_date_written = generic_render_start_date_written
render_end_date_written = generic_render_end_date_written

if disable_sort:
orderable = disable_sort

class Meta:
"""
Meta class needed for django-tables2 plugin.
Expand Down

0 comments on commit 77d6c36

Please sign in to comment.