def test_multiple_associations_and_removals(self): square = Term.objects.get(display_name="Square") circle = Term.objects.get(display_name="Circle") red = Term.objects.get(display_name='Red') blue = Term.objects.get(display_name='Blue') TermRelationship.objects.create(term=square, sherdnote=self.note) TermRelationship.objects.create(term=red, sherdnote=self.note) related_terms = TermRelationship.objects.filter(sherdnote=self.note) self.assertEquals(len(related_terms), 2) self.assertEquals(related_terms[0].term, red) self.assertEquals(related_terms[0].sherdnote, self.note) self.assertEquals(related_terms[1].term, square) self.assertEquals(related_terms[1].sherdnote, self.note) post_data = {'vocabulary': [str(circle.id), str(blue.id)]} request = self.factory.post('/', post_data) update_vocabulary_terms(request, self.note) related_terms = TermRelationship.objects.filter(sherdnote=self.note) self.assertEquals(len(related_terms), 2) self.assertEquals(related_terms[0].term, blue) self.assertEquals(related_terms[0].sherdnote, self.note) self.assertEquals(related_terms[1].term, circle) self.assertEquals(related_terms[1].sherdnote, self.note)
def update_annotation(request, annotation): form = dict((key[len('annotation-'):], val) for key, val in request.POST.items() if key.startswith('annotation-')) ## @todo -- figure out how the clipform gets into the # annotations.mustache form # don't let a global annotation turn into a clip, or v.v. if form.get('range1') or form.get('range2'): assert not annotation.is_null() else: assert annotation.is_null() for field in formfields: if field not in form: continue default = None if field == 'tags': default = '' setattr(annotation, field, form[field] or default) annotation.save() update_vocabulary_terms(request, annotation)
def test_multiple_associations_and_removals(self): square = Term.objects.get(display_name="Square") circle = Term.objects.get(display_name="Circle") red = Term.objects.get(display_name='Red') blue = Term.objects.get(display_name='Blue') TermRelationship.objects.create(term=square, sherdnote=self.note) TermRelationship.objects.create(term=red, sherdnote=self.note) related_terms = TermRelationship.objects.filter(sherdnote=self.note) self.assertEquals(len(related_terms), 2) self.assertEquals(related_terms[0].term, red) self.assertEquals(related_terms[0].sherdnote, self.note) self.assertEquals(related_terms[1].term, square) self.assertEquals(related_terms[1].sherdnote, self.note) post_data = { 'vocabulary': [str(circle.id), str(blue.id)] } request = self.factory.post('/', post_data) update_vocabulary_terms(request, self.note) related_terms = TermRelationship.objects.filter(sherdnote=self.note) self.assertEquals(len(related_terms), 2) self.assertEquals(related_terms[0].term, blue) self.assertEquals(related_terms[0].sherdnote, self.note) self.assertEquals(related_terms[1].term, circle) self.assertEquals(related_terms[1].sherdnote, self.note)
def update_annotation(request, annotation): form = dict((key[len('annotation-'):], val) for key, val in request.POST.items() if key.startswith('annotation-')) # @todo -- figure out how the clipform gets into the # annotations.mustache form # don't let a global annotation turn into a clip, or v.v. if form.get('range1') or form.get('range2'): assert not annotation.is_null() else: assert annotation.is_null() for field in formfields: if field not in form: continue default = None if field == 'tags': default = '' setattr(annotation, field, form[field] or default) annotation.save() update_vocabulary_terms(request, annotation)
def update_annotation(request, annotation): if len(request.POST.keys()) > 0: form = dict((key[len('annotation-'):], val) for key, val in request.POST.items() if key.startswith('annotation-')) term_ids = request.POST.getlist('vocabulary') else: # Try to load the data from the body as JSON, if it's not # included as form data. try: form = json.loads(request.body) except JSONDecodeError: form = {} term_ids = form.get('terms') or form.get('vocabulary') if form.get('range1') or form.get('range2'): assert not annotation.is_null() for field in formfields: if field not in form: continue default = None if field == 'tags': default = '' setattr(annotation, field, form[field] or default) annotation.save() update_vocabulary_terms(request, annotation, term_ids)
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 = is_clipping(data) assert clipping assert annotationfields.intersection(data) # ^^ the model will take care of the edge case annotation = SherdNote(**data) annotation.save() update_vocabulary_terms(request, annotation) # 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) project_id = request.POST.get('project', None) if project_id: project = get_object_or_404(Project, id=project_id) ProjectNote.objects.create(project=project, annotation=annotation) if request.is_ajax(): response = { 'asset': { 'id': asset.id }, 'annotation': { 'id': annotation.id } } return HttpResponse(json.dumps(response), content_type="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 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() update_vocabulary_terms(request, annotation) # 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) project_id = request.POST.get('project', None) if project_id: project = get_object_or_404(Project, id=project_id) ProjectNote.objects.create(project=project, annotation=annotation) if request.is_ajax(): response = {'asset': {'id': asset.id}, 'annotation': {'id': annotation.id}} return HttpResponse(json.dumps(response), content_type="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_removal(self): term = Term.objects.get(display_name="Square") TermRelationship.objects.create(term=term, sherdnote=self.note) self.assertEquals(term.termrelationship_set.count(), 1) request = self.factory.post('/', {}) update_vocabulary_terms(request, self.note) self.assertEquals(term.termrelationship_set.count(), 0)
def test_simple_association(self): term = Term.objects.get(display_name="Square") self.assertEquals(term.termrelationship_set.count(), 0) post_data = {'vocabulary': [str(term.id)]} request = self.factory.post('/', post_data) update_vocabulary_terms(request, self.note) related = term.termrelationship_set.first() self.assertIsNotNone(related) self.assertEquals(related.term, term) self.assertEquals(related.sherdnote, self.note)
def test_removal_and_association(self): square = Term.objects.get(display_name="Square") circle = Term.objects.get(display_name="Circle") TermRelationship.objects.create(term=square, sherdnote=self.note) self.assertEquals(square.termrelationship_set.count(), 1) post_data = {'vocabulary': [str(circle.id)]} request = self.factory.post('/', post_data) update_vocabulary_terms(request, self.note) related = circle.termrelationship_set.first() self.assertEquals(related.term, circle) self.assertEquals(related.sherdnote, self.note)
def test_simple_association(self): term = Term.objects.get(display_name="Square") post_data = {'vocabulary': [str(term.id)]} related_terms = TermRelationship.objects.get_for_object(self.note) self.assertEquals(len(related_terms), 0) request = self.factory.post('/', post_data) update_vocabulary_terms(request, self.note) related_terms = TermRelationship.objects.get_for_object(self.note) self.assertEquals(len(related_terms), 1) self.assertEquals(related_terms[0].term, term) self.assertEquals(related_terms[0].object_id, self.note.id)
def test_simple_association(self): concept = Vocabulary.objects.get(display_name="Shapes") term = Term.objects.get(display_name="Square") post_data = {'vocabulary-%s' % str(concept.id): [str(term.id)]} sherd_note = SherdNote.objects.get(id=1) related_terms = TermRelationship.objects.get_for_object(sherd_note) self.assertEquals(len(related_terms), 0) request = self.factory.post('/', post_data) update_vocabulary_terms(request, sherd_note) related_terms = TermRelationship.objects.get_for_object(sherd_note) self.assertEquals(len(related_terms), 1) self.assertEquals(related_terms[0].term, term) self.assertEquals(related_terms[0].object_id, sherd_note.id)
def test_removal(self): term = Term.objects.get(display_name="Square") sherdnote_type = ContentType.objects.get_for_model(self.note) TermRelationship.objects.create(term=term, object_id=self.note.id, content_type=sherdnote_type) related_terms = TermRelationship.objects.get_for_object(self.note) self.assertEquals(len(related_terms), 1) request = self.factory.post('/', {}) update_vocabulary_terms(request, self.note) related_terms = TermRelationship.objects.get_for_object(self.note) self.assertEquals(len(related_terms), 0)
def test_removal(self): term = Term.objects.get(display_name="Square") sherd_note = SherdNote.objects.get(id=1) sherdnote_type = ContentType.objects.get_for_model(sherd_note) TermRelationship.objects.create(term=term, object_id=sherd_note.id, content_type=sherdnote_type) related_terms = TermRelationship.objects.get_for_object(sherd_note) self.assertEquals(len(related_terms), 1) request = self.factory.post('/', {}) update_vocabulary_terms(request, sherd_note) related_terms = TermRelationship.objects.get_for_object(sherd_note) self.assertEquals(len(related_terms), 0)
def test_removal_and_association(self): square = Term.objects.get(display_name="Square") circle = Term.objects.get(display_name="Circle") sherdnote_type = ContentType.objects.get_for_model(self.note) TermRelationship.objects.create(term=square, object_id=self.note.id, content_type=sherdnote_type) related_terms = TermRelationship.objects.get_for_object(self.note) self.assertEquals(len(related_terms), 1) post_data = {'vocabulary': [str(circle.id)]} request = self.factory.post('/', post_data) update_vocabulary_terms(request, self.note) related_terms = TermRelationship.objects.get_for_object(self.note) self.assertEquals(len(related_terms), 1) self.assertEquals(related_terms[0].term, circle) self.assertEquals(related_terms[0].object_id, self.note.id)
def test_removal_and_association(self): concept = Vocabulary.objects.get(display_name="Shapes") square = Term.objects.get(display_name="Square") circle = Term.objects.get(display_name="Circle") sherd_note = SherdNote.objects.get(id=1) sherdnote_type = ContentType.objects.get_for_model(sherd_note) TermRelationship.objects.create(term=square, object_id=sherd_note.id, content_type=sherdnote_type) related_terms = TermRelationship.objects.get_for_object(sherd_note) self.assertEquals(len(related_terms), 1) post_data = {'vocabulary-%s' % str(concept.id): [str(circle.id)]} request = self.factory.post('/', post_data) update_vocabulary_terms(request, sherd_note) related_terms = TermRelationship.objects.get_for_object(sherd_note) self.assertEquals(len(related_terms), 1) self.assertEquals(related_terms[0].term, circle) self.assertEquals(related_terms[0].object_id, sherd_note.id)
def test_multiple_associations_and_removals(self): shapes = Vocabulary.objects.get(display_name="Shapes") square = Term.objects.get(display_name="Square") circle = Term.objects.get(display_name="Circle") colors = Vocabulary.objects.get(display_name="Colors") red = Term.objects.get(display_name='Red') blue = Term.objects.get(display_name='Blue') sherd_note = SherdNote.objects.get(id=1) sherdnote_type = ContentType.objects.get_for_model(sherd_note) TermRelationship.objects.create(term=square, object_id=sherd_note.id, content_type=sherdnote_type) TermRelationship.objects.create(term=red, object_id=sherd_note.id, content_type=sherdnote_type) related_terms = TermRelationship.objects.get_for_object(sherd_note) self.assertEquals(len(related_terms), 2) self.assertEquals(related_terms[0].term, red) self.assertEquals(related_terms[0].object_id, sherd_note.id) self.assertEquals(related_terms[1].term, square) self.assertEquals(related_terms[1].object_id, sherd_note.id) post_data = { 'vocabulary-%s' % str(shapes.id): [str(circle.id)], 'vocabulary-%s' % str(colors.id): [str(blue.id)], } request = self.factory.post('/', post_data) update_vocabulary_terms(request, sherd_note) related_terms = TermRelationship.objects.get_for_object(sherd_note) self.assertEquals(len(related_terms), 2) self.assertEquals(related_terms[0].term, blue) self.assertEquals(related_terms[0].object_id, sherd_note.id) self.assertEquals(related_terms[1].term, circle) self.assertEquals(related_terms[1].object_id, sherd_note.id)
def test_multiple_associations_and_removals(self): shapes = Vocabulary.objects.get(display_name="Shapes") square = Term.objects.get(display_name="Square") circle = Term.objects.get(display_name="Circle") colors = Vocabulary.objects.get(display_name="Colors") red = Term.objects.get(display_name='Red') blue = Term.objects.get(display_name='Blue') sherd_note = SherdNote.objects.get(id=1) sherdnote_type = ContentType.objects.get_for_model(sherd_note) TermRelationship.objects.create(term=square, object_id=sherd_note.id, content_type=sherdnote_type) TermRelationship.objects.create(term=red, object_id=sherd_note.id, content_type=sherdnote_type) related_terms = TermRelationship.objects.get_for_object(sherd_note) self.assertEquals(len(related_terms), 2) self.assertEquals(related_terms[0].term, square) self.assertEquals(related_terms[0].object_id, sherd_note.id) self.assertEquals(related_terms[1].term, red) self.assertEquals(related_terms[1].object_id, sherd_note.id) post_data = { 'vocabulary-%s' % str(shapes.id): [str(circle.id)], 'vocabulary-%s' % str(colors.id): [str(blue.id)], } request = self.factory.post('/', post_data) update_vocabulary_terms(request, sherd_note) related_terms = TermRelationship.objects.get_for_object(sherd_note) self.assertEquals(len(related_terms), 2) self.assertEquals(related_terms[0].term, circle) self.assertEquals(related_terms[0].object_id, sherd_note.id) self.assertEquals(related_terms[1].term, blue) self.assertEquals(related_terms[1].object_id, sherd_note.id)