django - Store JSON input -
i'm new django , trying content security policy reporting running on application , running issues parsing , storing json violation output. have reports posting /csp_reports/ , have built new app called security_report , added following urls.py:
url(r'^csp_report/$', 'security_report.views.secreport'),
my models this:
import os
from django.db import models django.contrib.auth.models import user django.conf import settings class security_report(models.model): id=models.autofield(primary_key=true) received=models.datetimefield(null=true) csp_report=models.charfield() blocked_uri=models.charfield() column_number=models.charfield() document_uri=models.charfield() line_number = models.integerfield() original_policy= models.charfield() referrer = models.charfield() status_code = models.integerfield() violated_directive = models.charfield() source_file = models.charfield() script_sample = models.charfield() class meta: app_label = "events"
and views.py not complete , not working below. stuck on how grab each individual parameter , dump database. json.loads right way this? if so, how perform save()? testing blocked_uri in view test out. keep hitting except: , getting "not saved"
from security_report.models import security_report django.utils import simplejson django.http import httpresponse, httprequest django.views.decorators.csrf import csrf_exempt, csrf_protect import json @csrf_exempt def secreport(request): if request.method == "post": json_data = simplejson.loads(request.raw_post_data) try: data = json_data['csp-report'] blocked_uri = data['blocked-uri'] document_uri = data['document-uri'] referrer = data['referrer'] script_sample = data['script-sample'] violated_directive = data['violated-directive'] source_file = data['source-file'] b = security_report() b.blocked_uri = blocked_uri b.save() response = ("saved database!") except: response = "not saved" return httpresponse(response) else: return httpresponse("not post request")
Comments
Post a Comment