예제 #1
0
파일: annotate.py 프로젝트: saracarl/readux
    def test_annotation_to_tei(self):
        teidoc = load_xmlobject_from_file(os.path.join(FIXTURE_DIR, 'teifacsimile.xml'),
            tei.AnnotatedFacsimile)

        note = Annotation(text="Here's the thing", quote="really",
            extra_data=json.dumps({'sample data': 'foobar',
                'tags': ['test', 'one', 'two']}))

        teinote = annotation_to_tei(note, teidoc)
        self.assert_(isinstance(teinote, tei.Note))
        self.assertEqual('annotation-%s' % note.id, teinote.id)
        self.assert_(teinote.href.endswith(note.get_absolute_url()))
        self.assertEqual(note.text, teinote.paragraphs[0])

        # todo: add a schema validation once we get the output to be valid
        # teidoc.schema_valid()
        # access errors with teidoc.schema_validation_errors()

        # annotation user should be set as note response
        user = get_user_model()(username='******')
        user.save()
        note.user = user
        teinote = annotation_to_tei(note, teidoc)
        self.assertEqual(user.username, teinote.resp)

        # tags should be set as interp ids ana attribute
        for tag in note.info()['tags']:
            self.assert_('#%s' % tag in teinote.ana)

        # test that markdown formatting is coming through
        footnote = '''Footnotes[^1] have a label and content.

[^1]: This is some footnote content.'''
        note.text = footnote
        teinote = annotation_to_tei(note, teidoc)
        self.assert_('<ref target="#fn1" type="noteAnchor">1</ref>' in
            teinote.serialize())

        # markdown should be included in a code element
        self.assertEqual(note.text, teinote.markdown)

        # related page references
        rel_pages = [
            'http://testpid.co/ark:/1234/11',
            'http://testpid.co/ark:/1234/22',
            'http://testpid.co/ark:/1234/qq'
        ]
        note.extra_data = json.dumps({'related_pages': rel_pages})
        teinote = annotation_to_tei(note, teidoc)
        self.assertEqual(len(rel_pages), len(teinote.related_pages))
        # first ark has a corresponding id in the fixture, should be converted
        self.assertEqual('#%s' % teidoc.page_id_by_xlink(rel_pages[0]),
            teinote.related_pages[0].target)
        for idx in range(len(rel_pages)):
            self.assertEqual(rel_pages[idx], teinote.related_pages[idx].text)
예제 #2
0
    def import_annotation(self, data):
        '''Create and save a new annotation, setting fields based on a
        dictionary of data passed in.  Raises an error if an annotation
        author is not found as a user in the database.'''
        note = Annotation()

        # NOTE: because we are using uuid for annotation id field,
        # importing an annotation twice does not error, but simply
        # replaces the old copy.  Might want to add checks for this...

        # required fields that should always be present
        # (not normally set by user)
        for field in ['updated', 'created', 'id']:
            setattr(note, field, data[field])
            del data[field]
        # user is special: annotation data only includes username,
        # but we need a user object
        # NOTE: this could result in making one person's annotations
        # available to someone else, if someone is using a different
        # username in another instance
        if 'user' in data:
            try:
                note.user = get_user_model().objects.get(username=data['user'])
                del data['user']
            except get_user_model().DoesNotExist:
                raise CommandError(
                    'Cannot import annotations for user %s (does not exist)' %
                    data['user'])

        for field in Annotation.common_fields:
            if field in data:
                setattr(note, field, data[field])
                del data[field]

        # put any other data that is left in extra data json field
        if data:
            note.extra_data.update(data)

        note.save()
예제 #3
0
    def import_annotation(self, data):
        '''Create and save a new annotation, setting fields based on a
        dictionary of data passed in.  Raises an error if an annotation
        author is not found as a user in the database.'''
        note = Annotation()

        # NOTE: because we are using uuid for annotation id field,
        # importing an annotation twice does not error, but simply
        # replaces the old copy.  Might want to add checks for this...

        # required fields that should always be present
        # (not normally set by user)
        for field in ['updated', 'created', 'id']:
            setattr(note, field, data[field])
            del data[field]
        # user is special: annotation data only includes username,
        # but we need a user object
        # NOTE: this could result in making one person's annotations
        # available to someone else, if someone is using a different
        # username in another instance
        if 'user' in data:
            try:
                note.user = get_user_model().objects.get(username=data['user'])
                del data['user']
            except get_user_model().DoesNotExist:
                raise CommandError('Cannot import annotations for user %s (does not exist)' % data['user'])

        for field in Annotation.common_fields:
            if field in data:
                setattr(note, field, data[field])
                del data[field]

        # put any other data that is left in extra data json field
        if data:
            note.extra_data.update(data)

        note.save()