mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-21 19:03:11 +00:00
Allow update and quick check of need_regen info
This commit is contained in:
parent
ed1284c06d
commit
4fe1be5d31
6 changed files with 60 additions and 10 deletions
|
@ -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],
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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())
|
||||
]
|
||||
|
|
22
api/views.py
22
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):
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in a new issue