From 77d6c36da3d9de66ea9e2c164ee4363d4f6092c2 Mon Sep 17 00:00:00 2001 From: csae8092 Date: Tue, 5 Nov 2024 14:03:28 +0100 Subject: [PATCH] limit relations to 50 --- apis_core/apis_entities/detail_views.py | 10 +++++++--- apis_core/apis_relations/tables.py | 7 ++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/apis_core/apis_entities/detail_views.py b/apis_core/apis_entities/detail_views.py index c3bb6e6..ea4b446 100644 --- a/apis_core/apis_entities/detail_views.py +++ b/apis_core/apis_entities/detail_views.py @@ -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] @@ -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) diff --git a/apis_core/apis_relations/tables.py b/apis_core/apis_relations/tables.py index 4a1974b..c2aa065 100644 --- a/apis_core/apis_relations/tables.py +++ b/apis_core/apis_relations/tables.py @@ -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, @@ -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. @@ -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.