Skip to content

Commit

Permalink
Fixes #7741: Fix 404 when attaching multiple images in succession
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Nov 9, 2021
1 parent be91235 commit 3ad773b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/release-notes/version-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
### Bug Fixes

* [#7701](https://github.com/netbox-community/netbox/issues/7701) - Fix conflation of assigned IP status & role in interface tables
* [#7741](https://github.com/netbox-community/netbox/issues/7741) - Fix 404 when attaching multiple images in succession
* [#7752](https://github.com/netbox-community/netbox/issues/7752) - Fix minimum version check under Python v3.10
* [#7766](https://github.com/netbox-community/netbox/issues/7766) - Add missing outer dimension columns to rack table
* [#7780](https://github.com/netbox-community/netbox/issues/7780) - Preserve mutli-line values during CSV file import
Expand Down
2 changes: 2 additions & 0 deletions netbox/extras/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ class ImageAttachment(BigIDModel):

objects = RestrictedQuerySet.as_manager()

clone_fields = ('content_type', 'object_id')

class Meta:
ordering = ('name', 'pk') # name may be non-unique

Expand Down
6 changes: 1 addition & 5 deletions netbox/extras/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,11 +475,7 @@ class ImageAttachmentEditView(generic.ObjectEditView):
def alter_obj(self, instance, request, args, kwargs):
if not instance.pk:
# Assign the parent object based on URL kwargs
try:
app_label, model = request.GET.get('content_type').split('.')
except (AttributeError, ValueError):
raise Http404("Content type not specified")
content_type = get_object_or_404(ContentType, app_label=app_label, model=model)
content_type = get_object_or_404(ContentType, pk=request.GET.get('content_type'))
instance.parent = get_object_or_404(content_type.model_class(), pk=request.GET.get('object_id'))
return instance

Expand Down
2 changes: 1 addition & 1 deletion netbox/templates/inc/image_attachments_panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <h5 class="card-header">
</div>
{% if perms.extras.add_imageattachment %}
<div class="card-footer text-end noprint">
<a href="{% url 'extras:imageattachment_add' %}?content_type={{ object|meta:"app_label" }}.{{ object|meta:"model_name" }}&object_id={{ object.pk }}" class="btn btn-primary btn-sm">
<a href="{% url 'extras:imageattachment_add' %}?content_type={{ object|content_type_id }}&object_id={{ object.pk }}" class="btn btn-primary btn-sm">
<i class="mdi mdi-plus-thick" aria-hidden="true"></i> Attach an image
</a>
</div>
Expand Down
20 changes: 20 additions & 0 deletions netbox/utilities/templatetags/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import yaml
from django import template
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.template.defaultfilters import date
from django.urls import NoReverseMatch, reverse
from django.utils import timezone
Expand Down Expand Up @@ -78,6 +79,25 @@ def meta(obj, attr):
return getattr(obj._meta, attr, '')


@register.filter()
def content_type(obj):
"""
Return the ContentType for the given object.
"""
return ContentType.objects.get_for_model(obj)


@register.filter()
def content_type_id(obj):
"""
Return the ContentType ID for the given object.
"""
content_type = ContentType.objects.get_for_model(obj)
if content_type:
return content_type.pk
return None


@register.filter()
def viewname(model, action):
"""
Expand Down

0 comments on commit 3ad773b

Please sign in to comment.