Skip to content

Instantly share code, notes, and snippets.

@allyjweir
Created February 7, 2015 15:06
Show Gist options
  • Save allyjweir/f5e152c6b6aa25c20cf8 to your computer and use it in GitHub Desktop.
Save allyjweir/f5e152c6b6aa25c20cf8 to your computer and use it in GitHub Desktop.
from django.forms import widgets
from datapoint.models import Datapoint, Annotation
from rest_framework import serializers
from rest_framework.exceptions import ParseError
from tags.serializers import TagSerializer
from tags.models import Tag
from users.serializers import UserSerializer
import simplejson as json
class Range():
def __init__(self, start, end, startOffset, endOffset):
self.start = start
self.end = end
self.startOffset = startOffset
self.endOffset = endOffset
class RangeSerializer(serializers.BaseSerializer):
start = serializers.CharField(max_length=50)
end = serializers.CharField(max_length=50)
startOffset = serializers.IntegerField()
endOffset = serializers.IntegerField()
def to_representation(self, obj):
return '"ranges: "' + json.dumps(obj.__dict__)
def to_internal_value(self, data):
range_array = json.loads(data.get('ranges'))
return Range(range_array.start, range_array.end, range_array.startOffset, range_array.endOffset)
class DatapointSerializer(serializers.ModelSerializer):
tags = TagSerializer(many=True, required=False)
class Meta:
model = Datapoint
fields = ('pk', 'owner', 'project', 'collections', 'name',
'description', 'author', 'source', 'url', 'publish_date',
'tags')
class AnnotationSerializer(serializers.Serializer):
pk = serializers.CharField(label="id")
annotator_schema_version = serializers.CharField(max_length=8, allow_blank=True, required=False)
created = serializers.DateTimeField()
modified = serializers.DateTimeField()
text = serializers.CharField()
quote = serializers.CharField()
uri = serializers.URLField(max_length=200, min_length=None, allow_blank=True, required=False)
ranges = RangeSerializer()
user = serializers.CharField()
tags = TagSerializer(many=True, required=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment