11from types import SimpleNamespace
22from unittest .mock import MagicMock , patch
33
4+ from django .contrib .auth .models import Group
45from django .contrib .contenttypes .models import ContentType
56from django .core .exceptions import PermissionDenied
67from django .test import RequestFactory , TestCase
78from filer .models import Folder
89
9- from django_smartbase_admin .admin .admin_base import SBAdmin
10+ from django_smartbase_admin .admin .admin_base import SBAdmin , SBAdminInline
11+ from django_smartbase_admin .admin .widgets import SBAdminAutocompleteWidget
1012from django_smartbase_admin .engine .admin_base_view import (
1113 SBADMIN_PARENT_INSTANCE_FIELD_NAME_VAR ,
14+ SBADMIN_PARENT_INSTANCE_MODEL_VAR ,
1215 SBADMIN_PARENT_INSTANCE_PK_VAR ,
1316)
1417
@@ -28,7 +31,10 @@ def _request(self, pk):
2831 return self .factory .post (
2932 "/" ,
3033 data = {
31- SBADMIN_PARENT_INSTANCE_FIELD_NAME_VAR : f"modal_inline_{ self .ct .app_label } _{ self .ct .model } " ,
34+ SBADMIN_PARENT_INSTANCE_FIELD_NAME_VAR : "modal_inline" ,
35+ SBADMIN_PARENT_INSTANCE_MODEL_VAR : (
36+ f"{ self .ct .app_label } .{ self .ct .model } "
37+ ),
3238 SBADMIN_PARENT_INSTANCE_PK_VAR : str (pk ),
3339 },
3440 )
@@ -54,3 +60,59 @@ def test_forged_parent_pk_outside_restricted_queryset_is_denied(self):
5460 self ._request (self .visible .pk ), obj
5561 )
5662 self .assertEqual (obj .object_id , self .visible .pk )
63+
64+ def test_generated_inline_parent_token_round_trips_through_parser (self ):
65+ inline = SBAdminInline .__new__ (SBAdminInline )
66+ inline .model = Group
67+ inline .parent_model = Folder
68+ inline .parent_instance = self .visible
69+ inline .sortable_field_name = None
70+ inline .sb_admin_add_modal = False
71+ with patch .object (
72+ inline , "get_sbadmin_inline_list_actions_processed" , return_value = []
73+ ):
74+ parent_data = inline .get_context_data (self .factory .get ("/" ))["parent_data" ]
75+
76+ request = self .factory .post (
77+ "/" ,
78+ data = {
79+ SBADMIN_PARENT_INSTANCE_FIELD_NAME_VAR : (
80+ f"modal_{ parent_data [SBADMIN_PARENT_INSTANCE_FIELD_NAME_VAR ]} "
81+ ),
82+ SBADMIN_PARENT_INSTANCE_MODEL_VAR : parent_data [
83+ SBADMIN_PARENT_INSTANCE_MODEL_VAR
84+ ],
85+ SBADMIN_PARENT_INSTANCE_PK_VAR : str (self .visible .pk ),
86+ },
87+ )
88+ parent_admin = MagicMock ()
89+ parent_admin .has_view_permission .return_value = True
90+ parent_admin .get_queryset .return_value = Folder .objects .all ()
91+ obj = SimpleNamespace ()
92+
93+ with patch .dict (
94+ "django_smartbase_admin.admin.site.sb_admin_site._registry" ,
95+ {Folder : parent_admin },
96+ ):
97+ _ChildAdmin .set_generic_relation_from_parent (request , obj )
98+
99+ self .assertEqual (obj .content_type , self .ct )
100+ self .assertEqual (obj .object_id , self .visible .pk )
101+
102+ def test_parent_model_metadata_does_not_change_widget_target (self ):
103+ input_id = "modal_auth_group_id_folder"
104+ request = self .factory .get (
105+ "/" ,
106+ data = {
107+ SBADMIN_PARENT_INSTANCE_FIELD_NAME_VAR : input_id ,
108+ SBADMIN_PARENT_INSTANCE_MODEL_VAR : "filer.folder" ,
109+ SBADMIN_PARENT_INSTANCE_PK_VAR : str (self .visible .pk ),
110+ },
111+ )
112+ widget = SimpleNamespace (input_id = input_id )
113+
114+ is_parent_widget = SBAdminAutocompleteWidget ._should_preselect_parent_instance (
115+ widget , request
116+ )
117+
118+ self .assertIs (is_parent_widget , True )
0 commit comments