Skip to content

Commit 86b785a

Browse files
committed
fix: inline cause projet admin to not save
1 parent 0faf9fb commit 86b785a

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

controller/sentry/admin.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@
3434
@admin.register(Project)
3535
class ProjectAdmin(
3636
ChartMixin,
37-
AdminConfirmMixin,
38-
ActionFormMixin,
39-
DjangoObjectActions,
4037
DynamicArrayMixin,
4138
admin.ModelAdmin,
4239
):

controller/sentry/inlines.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def has_add_permission(self, request, obj=None) -> bool: # pylint: disable=unus
2929
"""Don't allow adding."""
3030
return False
3131

32+
def has_change_permission(self, request, obj=None) -> bool: # pylint: disable=unused-argument
33+
"""Don't allow modifying."""
34+
return False
35+
3236

3337
class ProjectEventInline(EventInlineMixin, admin.TabularInline):
3438
"""ProjectEventInline derived from EventInlineMixin and admin.TabularInline."""

controller/sentry/mixins.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,18 @@ def change_view(
6666
form_url (str): form_url (Default to Blank string)
6767
extra_context (Optional[dict]): extra context (Default to None)
6868
"""
69-
if extra_context is None:
70-
extra_context = {}
69+
response = super().change_view(request, object_id, form_url, extra_context)
70+
71+
# This could be a redirect and not have context_data
72+
if not hasattr(response, "context_data"):
73+
return response
7174

7275
if result := self.get_chart_data(object_id):
7376
dataset, options = result
74-
extra_context["adminchart_chartjs_config"] = {
77+
response.context_data["adminchart_chartjs_config"] = {
7578
"type": "line",
7679
"data": dataset,
7780
"options": options,
7881
}
7982

80-
return super().change_view(request, object_id, form_url, extra_context)
83+
return response

controller/sentry/tests/test_admin.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ def __init__(self, user) -> None:
2424
self.GET = {}
2525

2626

27+
class MockResponse:
28+
def __init__(self, context_data=None) -> None:
29+
if context_data:
30+
self.context_data = {}
31+
32+
2733
@pytest.fixture
2834
def user_with_group(request, django_user_model, user_group):
2935
marker = request.node.get_closest_marker("user_group")
@@ -319,6 +325,7 @@ def test_project_event_inlines(admin_with_user):
319325
site, request = admin_with_user
320326
inline = ProjectEventInline(Project, site.admin_site)
321327
assert not inline.has_add_permission({})
328+
assert not inline.has_change_permission({})
322329

323330
project = Project(sentry_id="123")
324331
event = Event(project=project, type=EventType.FIRING, timestamp=timezone.now())
@@ -337,6 +344,7 @@ def test_app_event_inlines(admin_with_user):
337344
inline = AppEventInline(App, site.admin_site)
338345
app = App(reference="123")
339346
assert not inline.has_add_permission({})
347+
assert not inline.has_change_permission({})
340348
assert len(inline.get_form_queryset(app)) == 0
341349

342350
project = Project(sentry_id="123")
@@ -350,7 +358,7 @@ def test_app_event_inlines(admin_with_user):
350358
inline.save_new_instance({}, {})
351359

352360

353-
@patch("django_object_actions.utils.BaseDjangoObjectActions.change_view")
361+
@patch("django.contrib.admin.ModelAdmin.change_view")
354362
@pytest.mark.django_db
355363
@pytest.mark.parametrize("user_group", ["Developer"])
356364
@pytest.mark.admin_site(model_class=Project)
@@ -364,7 +372,27 @@ def test_project_chart_no_data(super_call: Mock, admin_with_user):
364372
super_call.assert_called_once_with(request, project.sentry_id, "", extra_context)
365373

366374

367-
@patch("django_object_actions.utils.BaseDjangoObjectActions.change_view")
375+
@patch("django.contrib.admin.ModelAdmin.change_view")
376+
@pytest.mark.django_db
377+
@pytest.mark.parametrize("user_group", ["Developer"])
378+
@pytest.mark.admin_site(model_class=Project)
379+
def test_project_chart_no_context(super_call: Mock, admin_with_user):
380+
site, request = admin_with_user
381+
project = Project(sentry_id="123")
382+
project.detection_result = {
383+
"signal": [0, 1],
384+
"avg_filter": [0, 1],
385+
"std_filter": [0, 2],
386+
"series": [0, 5],
387+
"intervals": ["a", "b"],
388+
}
389+
project.save()
390+
super_call.return_value = MockResponse(context_data=False)
391+
response = site.change_view(request, project.sentry_id)
392+
assert not hasattr(response, "context_data")
393+
394+
395+
@patch("django.contrib.admin.ModelAdmin.change_view")
368396
@pytest.mark.django_db
369397
@pytest.mark.parametrize("user_group", ["Developer"])
370398
@pytest.mark.admin_site(model_class=Project)
@@ -379,7 +407,8 @@ def test_project_chart(super_call: Mock, admin_with_user):
379407
"intervals": ["a", "b"],
380408
}
381409
project.save()
382-
site.change_view(request, project.sentry_id)
410+
super_call.return_value = MockResponse(context_data=True)
411+
response = site.change_view(request, project.sentry_id)
383412

384413
expected_context = {
385414
"adminchart_chartjs_config": {
@@ -424,4 +453,6 @@ def test_project_chart(super_call: Mock, admin_with_user):
424453
}
425454
}
426455

427-
super_call.assert_called_once_with(request, project.sentry_id, "", expected_context)
456+
super_call.assert_called_once_with(request, project.sentry_id, "", None)
457+
458+
assert response.context_data == expected_context

0 commit comments

Comments
 (0)