Revision 534c4a37
b/rapi/api.py | ||
---|---|---|
14 | 14 |
from flowspy.flowspec.views import get_peer_techc_mails as get_peer_techcs_contact |
15 | 15 |
from django.conf import settings |
16 | 16 |
from django.core.exceptions import ObjectDoesNotExist |
17 |
from tastypie.exceptions import NotFound
|
|
17 |
from tastypie.exceptions import *
|
|
18 | 18 |
from tastypie import http |
19 | 19 |
import logging |
20 | 20 |
import datetime |
... | ... | |
99 | 99 |
sourceport_dict = None |
100 | 100 |
if sourceport_dict: |
101 | 101 |
for sp in sourceport_dict: |
102 |
sourceport_pk = MatchPort.objects.get(port=sp.obj.__dict__['port']).pk
|
|
102 |
sourceport_pk = get_port_pk_or_create(sp)
|
|
103 | 103 |
sourceport_list.append(sourceport_pk) |
104 | 104 |
|
105 | 105 |
try: |
... | ... | |
108 | 108 |
destport_dict = None |
109 | 109 |
if destport_dict: |
110 | 110 |
for dp in destport_dict: |
111 |
destport_pk = MatchPort.objects.get(port=dp.obj.__dict__['port']).pk
|
|
111 |
destport_pk = get_port_pk_or_create(dp)
|
|
112 | 112 |
destport_list.append(destport_pk) |
113 | 113 |
|
114 | 114 |
try: |
... | ... | |
117 | 117 |
port_dict = None |
118 | 118 |
if port_dict: |
119 | 119 |
for prt in port_dict: |
120 |
port_pk = MatchPort.objects.get(port=prt.obj.__dict__['port']).pk
|
|
120 |
port_pk = get_port_pk_or_create(prt)
|
|
121 | 121 |
port_list.append(port_pk) |
122 | 122 |
|
123 | 123 |
datadict = bundle.obj.__dict__ |
... | ... | |
149 | 149 |
d = { 'clientip' : "REST_API: %s" %requesters_address, 'user' : route.applier.username } |
150 | 150 |
logger.info(mail_body, extra=d) |
151 | 151 |
else: |
152 |
raise Exception("ERRORS %s %s" %(form.errors, form.__dict__))
|
|
152 |
raise ApiFieldError("ERRORS %s %s" %(form.errors, form.__dict__))
|
|
153 | 153 |
|
154 | 154 |
return bundle |
155 | 155 |
|
156 | 156 |
def obj_delete(self, request=None, **kwargs): |
157 | 157 |
obj = kwargs.pop('pk', None) |
158 | 158 |
if obj: |
159 |
route = get_object_or_404(Route, pk=obj) |
|
159 |
try: |
|
160 |
route = Route.objects.get(pk=obj) |
|
161 |
except: |
|
162 |
raise NotFound("A model instance matching the provided arguments could not be found.") |
|
160 | 163 |
applier_peer = route.applier.get_profile().peer |
161 | 164 |
requester_peer = request.user.get_profile().peer |
162 | 165 |
if applier_peer == requester_peer: |
... | ... | |
175 | 178 |
d = { 'clientip' : "REST_API: %s" %requesters_address, 'user' : route.applier.username } |
176 | 179 |
logger.info(mail_body, extra=d) |
177 | 180 |
else: |
178 |
return http.HttpForbidden()
|
|
181 |
raise NotFound("A model instance matching the provided arguments could not be found.")
|
|
179 | 182 |
else: |
180 | 183 |
raise NotFound("A model instance matching the provided arguments could not be found.") |
181 | 184 |
|
... | ... | |
236 | 239 |
sourceport_dict = None |
237 | 240 |
if sourceport_dict: |
238 | 241 |
for sp in sourceport_dict: |
239 |
sourceport_pk = MatchPort.objects.get(port=sp.obj.__dict__['port']).pk
|
|
242 |
sourceport_pk = get_port_pk_or_create(sp)
|
|
240 | 243 |
sourceport_list.append(sourceport_pk) |
241 | 244 |
else: |
242 | 245 |
sourceport_list.extend([x.pk for x in route_edit.sourceport.all() if x]) |
... | ... | |
247 | 250 |
destport_dict = None |
248 | 251 |
if destport_dict: |
249 | 252 |
for dp in destport_dict: |
250 |
destport_pk = MatchPort.objects.get(port=dp.obj.__dict__['port']).pk
|
|
253 |
destport_pk = get_port_pk_or_create(dp)
|
|
251 | 254 |
destport_list.append(destport_pk) |
252 | 255 |
else: |
253 | 256 |
destport_list.extend([x.pk for x in route_edit.destinationport.all() if x]) |
... | ... | |
257 | 260 |
port_dict = None |
258 | 261 |
if port_dict: |
259 | 262 |
for prt in port_dict: |
260 |
port_pk = MatchPort.objects.get(port=prt.obj.__dict__['port']).pk
|
|
263 |
port_pk = get_port_pk_or_create(prt)
|
|
261 | 264 |
port_list.append(port_pk) |
262 | 265 |
else: |
263 | 266 |
port_list.extend([x.pk for x in route_edit.port.all() if x]) |
... | ... | |
297 | 300 |
d = { 'clientip' : "REST_API: %s" %requesters_address, 'user' : route.applier.username } |
298 | 301 |
logger.info(mail_body, extra=d) |
299 | 302 |
else: |
300 |
raise Exception("ERRORS %s %s" %(form.errors, form.__dict__))
|
|
303 |
raise ApiFieldError("ERRORS %s %s" %(form.errors, form.__dict__))
|
|
301 | 304 |
return bundle |
302 | 305 |
|
303 | 306 |
|
304 | 307 |
class Meta: |
305 | 308 |
queryset = Route.objects.all() |
309 |
default_format = "application/json" |
|
306 | 310 |
resource_name = 'rule' |
307 | 311 |
allowed_methods = ['get', 'post', 'delete', 'put'] |
308 | 312 |
fields = ['comments', 'source', 'destination', 'expires', 'status', 'id', 'sourceport', 'destinationport', 'then'] |
309 | 313 |
authentication = BasicAuthentication(restauthBackend()) |
310 | 314 |
authorization= Authorization() |
315 |
|
|
316 |
def get_port_pk_or_create(prt): |
|
317 |
try: |
|
318 |
port_pk = MatchPort.objects.get(port=prt.obj.__dict__['port']).pk |
|
319 |
except: |
|
320 |
try: |
|
321 |
assert (int(prt.obj.__dict__['port'])) |
|
322 |
mp = MatchPort(port=int(int(prt.obj.__dict__['port']))) |
|
323 |
mp.save() |
|
324 |
port_pk=mp.pk |
|
325 |
except: |
|
326 |
raise ApiFieldError("Port should be an integer") |
|
327 |
return port_pk |
Also available in: Unified diff