def create_annotation(request): asset = get_object_or_404(Asset, pk=request.POST['annotation-context_pk']) form = dict((key[len('annotation-'):], val) for key, val in request.POST.items() if key.startswith('annotation-')) del form['context_pk'] data = {'author': request.user, 'asset': asset} for field in formfields: if form.get(field) != '': data[field] = form[field] clipping = False for field in NULL_FIELDS: if field in data: clipping = True assert clipping assert annotationfields.intersection(data) # ^^ the model will take care of the edge case annotation = SherdNote(**data) annotation.save() #need to create global annotation if it doesn't exist already #so it appears in the user's list asset.global_annotation(annotation.author, auto_create=True) if request.is_ajax(): response = { 'asset': { 'id': asset.id }, 'annotation': { 'id': annotation.id } } return HttpResponse(simplejson.dumps(response), mimetype="application/json") else: #new annotations should redirect 'back' to the asset # at the endpoint of the last annotation # so someone can create a new annotation ~lizday url_fragment = '' if annotation.range2: url_fragment = '#start=%s' % str(annotation.range2) redirect_to = request.GET.get( 'next', annotation.asset.get_absolute_url() + url_fragment) return HttpResponseRedirect(redirect_to)