def create(self, validated_data): print "In create" print validated_data annotation = Annotation() annotation.author = validated_data.get('author') annotation.body = validated_data.get('body') annotation.content_type = validated_data.get('content_type') annotation.object_id = validated_data.get('object_id') annotation.paragraph = validated_data.get('paragraph') annotation.privacy = validated_data.get('privacy') annotation.privacy_override = validated_data.get('privacy_override', False) #Get row from contentType which has content_type content_object = ContentType.objects.get_for_id(annotation.content_type.id) annotation.content_object = content_object.model_class().objects.get(id=annotation.object_id) print annotation.content_object annotation.save() print validated_data.get('shared_with') for user in validated_data.get('shared_with'): sharing = AnnotationShareMap(annotation=annotation, user=user) sharing.save() return annotation
def annotation(request, session_id): # GET => gather collections and render UI # POST => get annotation information and create new/update annotation. if not request.user.is_authenticated(): return redirect('login') collections = Collection.objects.filter(creator=request.user).all() try: annotationsession = AnnotationSession.objects.get(pk=session_id) session_annotations = annotationsession.annotations.all().order_by( "datestamp") except AnnotationSession.DoesNotExist: return HttpResponse(status=404) if request.method == "GET": context = RequestContext( request, { 'collections': collections, 'annotationsession': annotationsession, 'annotations': session_annotations }) template = loader.get_template("annotations/annotate.html") return HttpResponse(template.render(context)) elif request.method == "POST": # A new annotation or an old one? leftitem_id = request.POST.get("leftitem") rightitem_id = request.POST.get("rightitem") try: leftitem = Item.objects.get(pk=leftitem_id) rightitem = Item.objects.get(pk=rightitem_id) except Item.DoesNotExist: print("Hmmm it says these don't exist") return HttpResponse(status=404) leftitem_state = request.POST.get("leftitem_state") rightitem_state = request.POST.get("rightitem_state") annotationtext = request.POST.get("annotationtext", u"") try: previous = Annotation.objects.get(leftitem=leftitem, rightitem=rightitem, session=annotationsession) previous.annotation = annotationtext previous.leftitem_state = leftitem_state previous.rightitem_state = rightitem_state previous.save() return HttpResponse("{{'annotation_id':'{0}'}}".format( str(previous.id)), mimetype="application/json") except Annotation.DoesNotExist: new_anno = Annotation(leftitem=leftitem, rightitem=rightitem, session=annotationsession, leftitem_state=leftitem_state, rightitem_state=rightitem_state, creator=request.user) new_anno.save() return HttpResponse("{{'annotation_id':'{0}'}}".format( str(new_anno.id)), mimetype="application/json")
def create_annotation(self, body='', paragraph=None): annotation = Annotation() annotation.content_type = ContentType.objects.get(model='blogcontent', app_label="blogging") annotation.object_id = str(1) annotation.body = body annotation.paragraph = paragraph annotation.author = User.objects.get(id=1) annotation.save()
def update(request): """ Update the database with new annotations from the AudioAnnotator Flash interface. The Flash interface will output a string that looks like: "1,62637,119960,test\n2,137802,175384,test2\n" Each new line is a new annotation. The first field is the annotation id, the second and third are the start and end times, in milliseconds, and the fourth is the label. """ if request.method == "POST": annotations = request.POST.get('annotations', '') recording = Recording.objects.get( pk=request.POST.get('recording_id', '')) # Take the string of annotations from the AudioAnnotator and # parse it into annotations. for annotation in annotations.split("\n"): if (annotation == ''): break fields = annotation.split(",") ann_id = fields[0] ann_start_ms = int(fields[1]) ann_end_ms = int(fields[2]) ann_label = fields[3] # if (ann_label == None): # Annotation.delete(ann_id) # break if (ann_id == "0"): ann = Annotation(start_time_ms=ann_start_ms, end_time_ms=ann_end_ms, label=ann_label, recording=recording) ann.save() else: ann = Annotation.objects.get(pk=ann_id) ann.start_time_ms = ann_start_ms ann.end_time_ms = ann_end_ms ann.label = ann_label print ann ann.save() # Return back to the AudioAnnotator the latest collection of # annotations for this recording. annotations = Annotation.objects.all().filter(recording=r) output = "" for annotation in annotations: output += annotation.to_string + "\n" logging.info("***output=" + output) return HttpResponse(output)
def test_create_serializer_class(self): annotation = Annotation() annotation.content_type = ContentType.objects.get(model='blogcontent', app_label="blogging") annotation.object_id = str(1) annotation.body = "This is a test annotations" annotation.paragraph = "1" annotation.author = User.objects.get(id=1) annotation.save() obj = AnnotationSerializer(annotation) #print(obj.data) json = JSONRenderer().render(obj.data)
def post(self, request): result = json.loads(json.dumps(request.POST)) annotation_object = Annotation() for _, _, q in self.qualities: vars(annotation_object)[q] = result[q] annotation_object.audio_file = self.get_context_data().get( 'audio_file_name') annotation_object.user = self.request.user annotation_object.description = result.get('description') annotation_object.save() # How many have you annotated? user_annotations = Annotation.objects.filter(user=self.request.user) return render(request, 'annotation_submit.html', {'count': len(user_annotations)})