Skip to content

Commit

Permalink
Merge branch 'ng' of github.com:allegro/ralph into django-2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
matyldv committed Nov 22, 2024
2 parents b5efa9f + 016e4d7 commit 8e6f23e
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 13 deletions.
20 changes: 20 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
ralph (20241118.1) bionic; urgency=medium

[ Olga Matyla ]
* Add property_of to invoice report (#3864)
* Fix selected count (#3865)

[ Paweł Szulc ]
* Fix renderer classes ordering

-- Paweł Szulc <[email protected]> Mon, 18 Nov 2024 12:48:57 +0000

ralph (20241108.1) bionic; urgency=medium

* Allow updating dchosts via dchost-list endpoint
* Fix browsable api
* Fix flake
* Add 404 if no serializer found for dc-hosts endpoint

-- Paweł Szulc <[email protected]> Fri, 08 Nov 2024 11:09:13 +0000

ralph (20241104.2) bionic; urgency=medium

[ awieckowski ]
Expand Down
5 changes: 1 addition & 4 deletions src/ralph/admin/templates/admin/actions.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ <h4>{% trans "Actions" %}</h4>
{% trans "Go" %}
</button>
{% if actions_selection_counter %}
<script type="text/javascript">
var _actions_icnt="{{ cl.result_list|length|default:"0" }}";
</script>
<span class="action-counter">{{ selection_note }}</span>
<span class="action-counter" data-actions-icnt="{{ cl.result_list|length|default:"0" }}">{{ selection_note }}</span>
{% endif %}
</div>
{% endif %}
7 changes: 7 additions & 0 deletions src/ralph/assets/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from django.db.models import Count
from django.forms import BaseInlineFormSet
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _

Expand Down Expand Up @@ -129,11 +130,17 @@ class ServiceEnvironmentAdmin(CustomFieldValueAdminMixin, RalphAdmin):
fields = ('service', 'environment', 'remarks', 'tags')


class PolymorphicInlineFormset(BaseInlineFormSet):
def get_queryset(self):
return super().get_queryset()[:]


class ServiceEnvironmentInline(RalphTabularInline):
model = ServiceEnvironment
raw_id_fields = ['environment']
fields = ('environment',)
min_num = 1
formset = PolymorphicInlineFormset


class BaseObjectsList(ScanStatusInTableMixin, Table):
Expand Down
31 changes: 29 additions & 2 deletions src/ralph/assets/api/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
import django_filters
from django.db.models import Prefetch
from rest_framework.exceptions import ValidationError
from rest_framework.exceptions import NotFound, ValidationError
from rest_framework.permissions import SAFE_METHODS

from ralph.api import RalphAPIViewSet
from ralph.api.filters import BooleanFilter
Expand All @@ -10,9 +11,12 @@
from ralph.assets.api import serializers
from ralph.assets.api.filters import NetworkableObjectFilters
from ralph.assets.models import BaseObject
from ralph.data_center.models import Cluster, DataCenterAsset
from ralph.lib.api.utils import renderer_classes_without_form
from ralph.licences.api import BaseObjectLicenceViewSet
from ralph.licences.models import BaseObjectLicence
from ralph.networks.models import IPAddress
from ralph.virtual.models import CloudHost, VirtualServer


class BusinessSegmentViewSet(RalphAPIViewSet):
Expand Down Expand Up @@ -258,7 +262,8 @@ class DCHostViewSet(BaseObjectViewSetMixin, RalphAPIViewSet):
BaseObject.polymorphic_objects
)
serializer_class = serializers.DCHostSerializer
http_method_names = ["get", "options", "head"]
renderer_classes = renderer_classes_without_form(RalphAPIViewSet.renderer_classes)
http_method_names = ["get", "options", "head", "patch", "post"]
filter_fields = [
"id",
"service_env",
Expand Down Expand Up @@ -296,6 +301,28 @@ class DCHostViewSet(BaseObjectViewSetMixin, RalphAPIViewSet):
}
additional_filter_class = DCHostFilterSet

def get_serializer_class(self, *args, **kwargs):
if self.request.method not in SAFE_METHODS:
try:
obj_ = self.get_object()
if isinstance(obj_, VirtualServer):
from ralph.virtual.api import VirtualServerSaveSerializer
return VirtualServerSaveSerializer
elif isinstance(obj_, DataCenterAsset):
from ralph.data_center.api.serializers import DataCenterAssetSaveSerializer
return DataCenterAssetSaveSerializer
elif isinstance(obj_, CloudHost):
from ralph.virtual.api import SaveCloudHostSerializer
return SaveCloudHostSerializer
elif isinstance(obj_, Cluster):
from ralph.data_center.api.serializers import ClusterSerializer
return ClusterSerializer
else:
raise NotFound()
except AssertionError: # for some reason when opening browsable api this raises
pass
return serializers.DCHostSerializer

def get_queryset(self):
return (
self.queryset.dc_hosts()
Expand Down
3 changes: 2 additions & 1 deletion src/ralph/assets/invoice_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class InvoiceReportMixin(object):

_invoice_report_select_related = []
_invoice_report_common_fields = [
'invoice_no', 'invoice_date', 'provider', 'price_currency'
'invoice_no', 'invoice_date', 'provider', 'price_currency', 'property_of__name'
]
_price_field = 'price'
_invoice_report_name = 'invoice'
Expand Down Expand Up @@ -122,6 +122,7 @@ def _get_report_data(self, request, queryset):
'datetime': datetime.datetime.now().strftime(
self._invoice_report_datetime_format
),
'property_of': first_item.property_of.name,
},
'items': list(map(self._parse_item, queryset)),
'sum_price': str(
Expand Down
29 changes: 27 additions & 2 deletions src/ralph/assets/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,8 @@ def test_get_dc_hosts_list(self):
VirtualServerFullFactory.create_batch(20, parent=dc_assets[0])
CloudHostFullFactory.create_batch(20, hypervisor=dc_assets[0])
url = reverse('dchost-list') + "?limit=100"
with self.assertNumQueries(31):
response = self.client.get(url, format='json')
with self.assertQueriesMoreOrLess(30, plus_minus=1):
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['count'], 63)

Expand Down Expand Up @@ -1089,6 +1089,31 @@ def test_filter_by_env_name(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['count'], 1)

def test_patch_dchost_virtual_server(self):
new_hypervisor = DataCenterAssetFullFactory()
url = reverse('dchost-detail', args=(self.virtual.id,))
data = {
'hostname': 'new-hostname',
'hypervisor': new_hypervisor.id,
}
response = self.client.patch(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.virtual.refresh_from_db()
self.assertEqual(self.virtual.hostname, 'new-hostname')
self.assertEqual(self.virtual.parent.id, new_hypervisor.id)

def test_patch_dchost_cloudhost(self):
new_hypervisor = DataCenterAssetFullFactory()
url = reverse('dchost-detail', args=(self.cloud_host.id,))
data = {
'hostname': 'new-hostname',
'hypervisor': new_hypervisor.id,
}
response = self.client.patch(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.cloud_host.refresh_from_db()
self.assertEqual(self.cloud_host.hostname, 'new-hostname')
self.assertEqual(self.cloud_host.hypervisor.id, new_hypervisor.id)

class ConfigurationModuleAPITests(RalphAPITestCase):
def setUp(self):
Expand Down
11 changes: 7 additions & 4 deletions src/ralph/lib/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ def render_form_for_serializer(self, serializer):


def renderer_classes_without_form(renderer_classes):
return [OnlyRawBrowsableAPIRenderer] + [
rc for rc in renderer_classes
if not isinstance(rc(), BrowsableAPIRenderer)
]
def _gen():
for rc in renderer_classes:
if not isinstance(rc(), BrowsableAPIRenderer):
yield rc
else:
yield OnlyRawBrowsableAPIRenderer
return [rc for rc in _gen()]

0 comments on commit 8e6f23e

Please sign in to comment.