Exemple #1
0
    def post(self, request, *args, **kwargs):
        annot_id = kwargs.get('annot_id', None)
        note = get_object_or_404(SherdNote, pk=annot_id)

        # add this to the user's collection
        note.asset.global_annotation(request.user, True)

        data = {
            'author': self.request.user,
            'asset': note.asset,
            'range1': note.range1,
            'range2': note.range2,
            'annotation_data': note.annotation_data,
            'title': note.title
        }

        new_note = SherdNote(**data)
        new_note.save()

        ctx = {
            'asset': {
                'id': new_note.asset.id
            },
            'annotation': {
                'id': new_note.id
            }
        }
        return self.render_to_json_response(ctx)
Exemple #2
0
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)
Exemple #3
0
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)
Exemple #4
0
    def import_course_data(self, json_file, course):
        raw_data = open(json_file)
        json_data = json.load(raw_data)

        for asset_data in json_data['asset_set']:
            # Create asset
            author = User.objects.get(
                username=asset_data["author"]["username"])
            asset = Asset(author=author,
                          title=asset_data["title"],
                          course=course)

            asset.metadata_blob = asset_data["metadata_blob"]
            asset.save()

            # Add sources
            for key, value in asset_data["sources"].items():
                source_data = asset_data["sources"][key]
                source = Source(asset=asset,
                                label=source_data["label"],
                                url=source_data["url"],
                                primary=source_data["primary"],
                                media_type=source_data["media_type"],
                                size=source_data["size"],
                                height=source_data["height"],
                                width=source_data["width"])
                source.save()

            # Recreate annotations
            for ann_data in asset_data["annotations"]:
                ann_author = User.objects.get(
                    username=ann_data["author"]["username"])
                if ann_data["is_global_annotation"]:
                    ann = asset.global_annotation(ann_author, True)
                else:
                    ann = SherdNote(asset=asset,
                                    author=ann_author)

                ann.range1 = ann_data["range1"]
                ann.range2 = ann_data["range2"]
                ann.annotation_data = ann_data["annotation_data"]
                ann.title = ann_data["title"]

                tags = ""
                for tag in ann_data["metadata"]["tags"]:
                    tags = tags + "," + tag["name"]
                ann.tags = tags
                ann.body = ann_data["metadata"]["body"]
                ann.save()
Exemple #5
0
 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")
Exemple #6
0
 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")
Exemple #7
0
    def post(self, request, *args, **kwargs):
        annot_id = kwargs.get('annot_id', None)
        note = get_object_or_404(SherdNote, pk=annot_id)

        # add this to the user's collection
        note.asset.global_annotation(request.user, True)

        data = {
            'author': self.request.user, 'asset': note.asset,
            'range1': note.range1, 'range2': note.range2,
            'annotation_data': note.annotation_data, 'title': note.title
        }

        new_note = SherdNote(**data)
        new_note.save()

        ctx = {'asset': {'id': new_note.asset.id},
               'annotation': {'id': new_note.id}}
        return self.render_to_json_response(ctx)