Commit c35eb1fe authored by Stauros Kroustouris's avatar Stauros Kroustouris

add commit_add, commit_edit, commit_delete hooks in drf viewsets.

parent 5b0e6618
......@@ -80,9 +80,12 @@ Same with Fragmentypes in `/api/fragmenttypes/<fragmenttype_id>/`, protocols in
Since we have the urls we want to connect with the rule we want to create, we can make a POST request like the following:
curl -X POST -H 'Authorization: Token <Your users token>' -F "name=Example" -F "comments=Description" -F "source=0.0.0.0/0" -F "sourceport=https://fod.example.com/api/ports/7/" -F "destination=203.0.113.12" https://fod.example.com/api/routes/
And here is a PUT request example:
curl -X PUT -F "name=Example" -F "comments=Description" -F "source=0.0.0.0/0" -F "sourceport=https://fod.example.com/api/ports/7/" -F "destination=83.212.9.93" https://fod.example.com/api/routes/12/ -H 'Authorization: Token <Your users token>'
##Contact##
......
from django.shortcuts import get_object_or_404
from rest_framework import status
from django.conf import settings
from rest_framework import viewsets
from flowspec.models import (
......@@ -41,7 +41,27 @@ class RouteViewSet(viewsets.ModelViewSet):
return Response(serializer.data)
def pre_save(self, obj):
obj.applier = self.request.user
# DEBUG
if settings.DEBUG:
if self.request.user.is_anonymous():
from django.contrib.auth.models import User
obj.applier = User.objects.all()[0]
elif self.request.user.is_authenticated():
obj.applier = self.request.user
else:
raise Exception('User is not Authenticated')
else:
obj.applier = self.request.user
def post_save(self, obj, created):
if created:
obj.commit_add()
else:
if obj.status not in ['EXPIRED', 'INACTIVE', 'ADMININACTIVE']:
obj.commit_edit()
def pre_delete(self, obj):
obj.commit_delete()
class PortViewSet(viewsets.ModelViewSet):
......@@ -62,4 +82,3 @@ class FragmentTypeViewSet(viewsets.ModelViewSet):
class MatchProtocolViewSet(viewsets.ModelViewSet):
queryset = MatchProtocol.objects.all()
serializer_class = MatchProtocolSerializer
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment