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)
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) next_url = annotation.asset.get_absolute_url() + url_fragment redirect_to = request.GET.get('next', next_url) return HttpResponseRedirect(redirect_to)
def test_seconds_to_code(self): self.assertRaises(TypeError, SherdNote.secondsToCode, None) self.assertEquals(SherdNote.secondsToCode(0), "0:00") self.assertEquals(SherdNote.secondsToCode(5), "0:05") self.assertEquals(SherdNote.secondsToCode(60), "01:00") self.assertEquals(SherdNote.secondsToCode(120), "02:00") self.assertEquals(SherdNote.secondsToCode(3600, True), "01:00:00") self.assertEquals(SherdNote.secondsToCode(6400), "01:46:40") self.assertEquals(SherdNote.secondsToCode(86400, True), "24:00:00") self.assertEquals(SherdNote.secondsToCode(363600, True), "101:00:00")
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() #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)
from django import template from djangosherd.models import SherdNote import re from djangohelpers.templatetags import TemplateTagNode from django.db.models import get_model Comment = get_model('comments', 'comment') filter_by = { 'tag': lambda note, tag: re.search(r'\b%s\b' % tag, note.tags), 'added': SherdNote.date_filter_for('added'), 'modified': SherdNote.date_filter_for('modified'), } class GetAnnotations(TemplateTagNode): noun_for = { "filter": "filters", "by": "author", "on": "asset", } def __init__(self, varname, author, asset, filters): TemplateTagNode.__init__(self, varname, author=author, asset=asset, filters=filters) def execute_query(self, author, asset, filters):
from django import template from djangosherd.models import SherdNote import re from djangohelpers.templatetags import TemplateTagNode from django.db.models import get_model Comment = get_model('comments','comment') filter_by = { 'tag': lambda note, tag: re.search(r'\b%s\b'%tag, note.tags), 'added': SherdNote.date_filter_for('added'), 'modified': SherdNote.date_filter_for('modified'), } class GetAnnotations(TemplateTagNode): noun_for = {"filter":"filters", "by":"author", "on":"asset", } def __init__(self, varname, author, asset, filters): TemplateTagNode.__init__(self, varname, author=author, asset=asset, filters=filters) def execute_query(self, author, asset, filters): notes = None if author is False: notes = SherdNote.objects.filter(asset=asset) elif author: notes = SherdNote.objects.filter(author=author, asset=asset) if notes and filters: for f,v in filters.items(): #don't filter global annotations out notes = [n for n in notes