Created
February 1, 2015 10:55
-
-
Save allyjweir/a224fc08c1772633c4b1 to your computer and use it in GitHub Desktop.
Django Rest Framework and Annotator JS JSON requirements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#The serializer | |
class AnnotationSerializer(serializers.ModelSerializer): | |
tags = TagListSerializer(required=False) | |
class Meta: | |
model = Annotation | |
fields = ('pk', 'owner', 'datapoint', 'annotator_schema_version', 'text', 'quote', 'uri', 'range_start', | |
'range_end', 'range_startOffset', 'range_endOffset', 'tags') | |
#My database model for annotations | |
class Annotation(models.Model): | |
datapoint = models.ForeignKey('datapoint.Datapoint', related_name='%(class)s_parent_datapoint_relation') | |
owner = models.ForeignKey('users.User', related_name='%(class)s_creator_relation') | |
# Key fields from the Annotator JSON Format: http://docs.annotatorjs.org/en/latest/annotation-format.html | |
annotator_schema_version = models.CharField(max_length=8, blank=True) | |
text = models.TextField(blank=True) | |
quote = models.TextField() | |
uri = models.URLField(blank=True) | |
range_start = models.CharField(max_length=50) | |
range_end = models.CharField(max_length=50) | |
range_startOffset = models.BigIntegerField() | |
range_endOffset = models.BigIntegerField() | |
tags = TaggableManager(blank=True) | |
# Created/Modified | |
# See this for background: | |
# http://stackoverflow.com/questions/1737017/django-auto-now-and-auto-now-add/1737078#1737078 | |
created = models.DateTimeField(editable=False) | |
modified = models.DateTimeField(editable=False) | |
def save(self, *args, **kwargs): | |
""" On save, update timestamps """ | |
if not self.id: | |
self.created = datetime.datetime.today() | |
self.modified = datetime.datetime.today() | |
return super(Annotation, self).save(*args, **kwargs) | |
def __unicode__(self): | |
return self.datapoint.name + ":'" + self.quote + "'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment