From 4fe1be5d31942f8d8f84afaed9d28fe772312cb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Sat, 26 May 2018 21:08:03 +0000 Subject: [PATCH] Allow update and quick check of need_regen info --- api/permissions.py | 12 ++++++------ api/serializers.py | 17 +++++++++++++++++ api/urls.py | 3 ++- api/views.py | 22 ++++++++++++++++++++-- machines/models.py | 14 ++++++++++++++ machines/serializers.py | 2 +- 6 files changed, 60 insertions(+), 10 deletions(-) diff --git a/api/permissions.py b/api/permissions.py index a1d79b21..66480af6 100644 --- a/api/permissions.py +++ b/api/permissions.py @@ -1,4 +1,4 @@ -from rest_framework import permissions +from rest_framework import permissions, exceptions from re2o.acl import can_create, can_edit, can_delete, can_view_all from . import acl @@ -17,17 +17,17 @@ class DefaultACLPermission(permissions.BasePermission): 'OPTIONS': [can_see_api, lambda model: model.can_view_all], 'HEAD': [can_see_api, lambda model: model.can_view_all], 'POST': [can_see_api, lambda model: model.can_create], - #'PUT': [], - #'PATCH': [], - #'DELETE': [], + 'PUT': [], # No restrictions, apply to objects + 'PATCH': [], # No restrictions, apply to objects + 'DELETE': [], # No restrictions, apply to objects } perms_obj_map = { 'GET': [can_see_api, lambda obj: obj.can_view], 'OPTIONS': [can_see_api, lambda obj: obj.can_view], 'HEAD': [can_see_api, lambda obj: obj.can_view], - #'POST': [], + 'POST': [], # No restrictions, apply to models 'PUT': [can_see_api, lambda obj: obj.can_edit], - #'PATCH': [], + 'PATCH': [can_see_api, lambda obj: obj.can_edit], 'DELETE': [can_see_api, lambda obj: obj.can_delete], } diff --git a/api/serializers.py b/api/serializers.py index 37530bbe..b41bd694 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -215,6 +215,8 @@ class ServiceSerializer(NamespacedHMSerializer): class ServiceLinkSerializer(NamespacedHMSerializer): + need_regen = serializers.BooleanField() + class Meta: model = machines.Service_link fields = ('service', 'server', 'last_regen', 'asked_regen', @@ -459,6 +461,21 @@ class WhitelistSerializer(NamespacedHMSerializer): fields = ('user', 'raison', 'date_start', 'date_end', 'active', 'api_url') +# Services + + +class ServiceRegenSerializer(NamespacedHMSerializer): + hostname = serializers.CharField(source='server.domain.name', read_only=True) + service_name = serializers.CharField(source='service.service_type', read_only=True) + + class Meta: + model = machines.Service_link + fields = ('hostname', 'service_name', 'need_regen', 'api_url') + extra_kwargs = { + 'api_url': {'view_name': 'servicelink-detail'} + } + + # DHCP diff --git a/api/urls.py b/api/urls.py index cc73ab2c..fc8ef7ef 100644 --- a/api/urls.py +++ b/api/urls.py @@ -90,5 +90,6 @@ router.register(r'users/whitelists', views.WhitelistViewSet) urlpatterns = [ url(r'^', include(router.urls)), url(r'^dhcp/hostmacip', views.HostMacIpView.as_view()), - url(r'^token-auth/', views.ObtainExpiringAuthToken.as_view()) + url(r'^services/regen', views.ServiceRegenView.as_view()), + url(r'^token-auth', views.ObtainExpiringAuthToken.as_view()) ] diff --git a/api/views.py b/api/views.py index 79f81d33..036a9241 100644 --- a/api/views.py +++ b/api/views.py @@ -31,7 +31,7 @@ from django.conf import settings from rest_framework.authtoken.views import ObtainAuthToken from rest_framework.authtoken.models import Token from rest_framework.response import Response -from rest_framework import viewsets, status, generics +from rest_framework import viewsets, generics import cotisations.models as cotisations import machines.models as machines @@ -160,7 +160,7 @@ class ServiceViewSet(viewsets.ReadOnlyModelViewSet): serializer_class = serializers.ServiceSerializer -class ServiceLinkViewSet(viewsets.ReadOnlyModelViewSet): +class ServiceLinkViewSet(viewsets.ModelViewSet): queryset = machines.Service_link.objects.all() serializer_class = serializers.ServiceLinkSerializer @@ -313,6 +313,24 @@ class WhitelistViewSet(viewsets.ReadOnlyModelViewSet): serializer_class = serializers.WhitelistSerializer +# Services views + + +class ServiceRegenView(generics.ListAPIView): + serializer_class = serializers.ServiceRegenSerializer + + def get_queryset(self): + queryset = machines.Service_link.objects.select_related( + 'server__domain' + ).select_related( + 'service' + ) + if 'hostname' in self.request.GET: + hostname = self.request.GET['hostname'] + queryset = queryset.filter(server__domain__name__iexact=hostname) + return queryset + + # DHCP views class HostMacIpView(generics.ListAPIView): diff --git a/machines/models.py b/machines/models.py index 7027ca6f..82d6e21f 100644 --- a/machines/models.py +++ b/machines/models.py @@ -1399,6 +1399,7 @@ class Service_link(RevMixin, AclMixin, models.Model): self.asked_regen = False self.save() + @property def need_regen(self): """ Décide si le temps minimal écoulé est suffisant pour provoquer une régénération de service""" @@ -1411,6 +1412,19 @@ class Service_link(RevMixin, AclMixin, models.Model): ) < timezone.now() ) + @need_regen.setter + def need_regen(self, value): + """ + Force to set the need_regen value. True means a regen is asked and False + means a regen has been done. + + :param value: (bool) The value to set to + """ + if not value: + self.last_regen = timezone.now() + self.asked_regen = value + self.save() + def __str__(self): return str(self.server) + " " + str(self.service) diff --git a/machines/serializers.py b/machines/serializers.py index 9476e9d0..f3a47c55 100644 --- a/machines/serializers.py +++ b/machines/serializers.py @@ -376,7 +376,7 @@ class ServiceServersSerializer(serializers.ModelSerializer): @staticmethod def get_regen_status(obj): """ The string representation of the regen status """ - return obj.need_regen() + return obj.need_regen class OuverturePortsSerializer(serializers.Serializer):