I am building a high performance API. I have been using Tastypie for ages and sometimes I just need more simplicity. For this API I have decided to use Django Simple Rest (https://github.com/croach/django-simple-rest). It provides the base of what is needed and I can use forms and the ORM to validate and save data with no generic API library overhead.
I want to verify the data that is coming in. I am using model forms to do so. It's nice and simple, it verifies data against the model but I need a little bit more.
I want to make sure no script or HTML gets posted. For some fields I might allow HTML. I know I can use html5lib to do all sorts of validation and I probably will but the only examples I have seen are where you specify every field. I am trying to work out a way to by default prevent javascript or HTML being entered into a field and to be able to override as appropriate. I don't want to have to describe every model in forms, I want something generic.
Here is my simplerest put function.
def put(self, request, *args, **kwargs):
data = json.loads(request.body)
try:
todo = Item.objects.get(id=kwargs.get('id'))
except Item.DoesNotExist:
return HttpNotFound()
form = TodoForm( instance=todo, data=data )
if not form.is_valid():
return JsonFormErrors( form )
form.save()
return JsonStatus(True, message="saved successfully")
Here is my form.
from django import forms
from .models import *
class TodoForm(forms.ModelForm):
class Meta:
model = Item
fields = ('id', 'text')
What is the best way to provide generic protection to all my put methods and forms with an ability to override the behaviour if I want to accept HTML.
I appreciate your help!
Rich