Exemplo n.º 1
0
    def put(self, entity_gid):
        data = request.get_json()

        # This will be valid here, due to authentication.
        user = request.oauth.user
        user.total_revisions += 1
        user.revisions_applied += 1

        if not is_uuid(entity_gid):
            abort(404)

        try:
            entity = db.session.query(self.entity_class).options(
                joinedload('master_revision.entity_data')).filter_by(
                    entity_gid=entity_gid).one()
        except NoResultFound:
            abort(404)

        if entity.master_revision is None:
            abort(403)  # Forbidden to PUT on an entity with no data yet

        entity_data = entity.master_revision.entity_data
        entity_data = entity_data.update(data, db.session)

        revision = EntityRevision(user_id=user.user_id)
        revision.entity = entity
        revision.entity_data = entity_data

        note_content = data.get('revision', {}).get('note', '')

        if note_content != '':
            note = RevisionNote(user_id=user.user_id,
                                revision_id=revision.revision_id,
                                content=data['revision']['note'])

            revision.notes.append(note)

        entity.master_revision.parent = revision
        entity.master_revision = revision
        entity.revision = revision

        db.session.add(revision)

        # Commit entity, data and revision
        db.session.commit()

        entity_out = marshal(revision.entity, self.entity_fields)
        data_out = marshal(revision.entity_data, self.entity_data_fields)

        entity_out.update(data_out)

        # Don't 500 if we fail to index; commit still succeeded
        try:
            es_conn = Elasticsearch()
            index_entity(es_conn, entity_out)
        except ElasticsearchException:
            pass

        return marshal(revision,
                       {'entity': fields.Nested(self.entity_stub_fields)})
Exemplo n.º 2
0
    def post(self):
        data = request.get_json()

        # This will be valid here, due to authentication.
        user = request.oauth.user
        user.total_revisions += 1
        user.revisions_applied += 1

        entity = self.entity_class()
        entity_data = self.entity_data_class.create(data, db.session)

        if entity_data is None:
            abort(400)

        revision = EntityRevision(user_id=user.user_id)
        revision.entity = entity
        revision.entity_data = entity_data

        note_content = data.get('revision', {}).get('note', '')

        if note_content != '':
            note = RevisionNote(user_id=user.user_id,
                                revision_id=revision.revision_id,
                                content=data['revision']['note'])

            revision.notes.append(note)

        entity.master_revision = revision

        db.session.add(revision)

        # Commit entity, data and revision
        try:
            db.session.commit()
        except IntegrityError:
            # There was an issue with the data we received, so 400
            print traceback.format_exc()
            abort(400)

        entity_out = marshal(revision.entity, structures.ENTITY_EXPANDED)
        data_out = marshal(revision.entity_data, self.entity_data_fields)

        entity_out.update(data_out)

        # Don't 500 if we fail to index; commit still succeeded
        try:
            es_conn = Elasticsearch()
            index_entity(es_conn, entity_out)
        except ElasticsearchException:
            pass

        return marshal(revision, {
            'entity': fields.Nested(self.entity_stub_fields)
        })
Exemplo n.º 3
0
    def delete(self, entity_gid):
        data = request.get_json()

        # This will be valid here, due to authentication.
        user = request.oauth.user
        user.total_revisions += 1
        user.revisions_applied += 1

        if not is_uuid(entity_gid):
            abort(404)

        try:
            entity = db.session.query(self.entity_class).options(
                joinedload('master_revision')
            ).filter_by(entity_gid=entity_gid).one()
        except NoResultFound:
            abort(404)

        if entity.master_revision is None:
            abort(403)  # Forbidden to DELETE an entity with no data yet

        if entity.master_revision.entity_data_id is None:
            abort(405)  # DELETE not allowed on a deleted resource

        # To delete an entity, create a new revision with entity_data set to
        # None
        revision = EntityRevision(user_id=user.user_id)
        revision.entity = entity
        revision.entity_data = None

        note_content = data.get('revision', {}).get('note', '')

        if note_content != '':
            note = RevisionNote(user_id=user.user_id,
                                revision_id=revision.revision_id,
                                content=data['revision']['note'])

            revision.notes.append(note)

        entity.master_revision.parent = revision
        entity.master_revision = revision

        db.session.add(revision)

        # Commit entity, data and revision
        db.session.commit()

        return marshal(revision, {
            'entity': fields.Nested(self.entity_stub_fields)
        })
Exemplo n.º 4
0
    def test_update_publication(self):
        # Create entity
        entity_gid = self.test_create_publication()
        entity = self.session.query(Entity).\
            filter_by(entity_gid=entity_gid).one()

        prev_master_revision_id = entity.master_revision_id
        prev_alias_id = entity.master_revision.entity_data.aliases[0].alias_id

        # Now, update it
        user = self.session.query(User).filter_by(user_id=1).one()

        new_entity_data = entity.master_revision.entity_data.update({
            'entity_gid': [entity_gid],
            'annotation': u"Testing this entity, so do actually use this.",
            'disambiguation': u'A different disambiguation.',
            'aliases': [
                [prev_alias_id, {
                    'name': u'ABCD',
                    'sort_name': u'DCBA',
                    'language_id': 2,
                    'default': True,
                    'primary': True
                }]
            ]
        }, self.session)

        rev = EntityRevision(user_id=user.user_id)
        rev.entity = entity
        rev.entity_data = new_entity_data

        self.session.add(rev)
        self.session.commit()

        entity = rev.entity
        entity_data = rev.entity_data

        # Check properties of entity
        self.assertEquals(entity.master_revision_id, prev_master_revision_id)

        annotation = entity_data.annotation
        disambiguation = entity_data.disambiguation
        aliases = entity_data.aliases

        # Check annotation
        self.assertEqual(annotation.content, u"Testing this entity, so do actually use this.")

        # Check disambiguation
        self.assertEqual(disambiguation.comment, u'A different disambiguation.')

        # Check aliases
        self.assertEqual(len(aliases), 1)
        self.assertEqual(aliases[0].name, u'ABCD')
        self.assertEqual(aliases[0].sort_name, u'DCBA')
        self.assertEqual(aliases[0].language_id, 2)
        self.assertEqual(aliases[0].primary, True)

        # Check properties of entity_tree
        self.assertEqual(entity_data.default_alias_id, aliases[0].alias_id)

        # Check properties of publication_data
        self.assertEqual(entity_data.publication_type_id, 1)
Exemplo n.º 5
0
    def test_create_publication(self):
        user = self.session.query(User).filter_by(user_id=1).one()
        revision_json = {
            'entity_gid': [],
            'publication_type': {
                'publication_type_id': 1
            },
            'annotation': u"Testing this entity, so don't actually use this.",
            'disambiguation': u'A disambiguation.',
            'aliases': [
                {
                    'name': u'ABC',
                    'sort_name': u'CBA',
                    'language_id': 1,
                    'default': False,
                    'primary': True
                }
            ]
        }

        entity = Publication()
        entity_data = PublicationData.create(revision_json, self.session)

        rev = EntityRevision(user_id=user.user_id)
        rev.entity = entity
        rev.entity_data = entity_data

        self.session.add(rev)
        self.session.commit()

        entity = rev.entity
        entity_data = rev.entity_data

        # Check properties of entity
        self.assertEquals(entity.master_revision_id, None)

        annotation = entity_data.annotation
        disambiguation = entity_data.disambiguation
        aliases = entity_data.aliases

        # Check annotation
        self.assertEqual(annotation.content, u"Testing this entity, so don't actually use this.")

        # Check disambiguation
        self.assertEqual(disambiguation.comment, u'A disambiguation.')

        # Check aliases
        self.assertEqual(len(aliases), 1)
        self.assertEqual(aliases[0].name, u'ABC')
        self.assertEqual(aliases[0].sort_name, u'CBA')
        self.assertEqual(aliases[0].language_id, 1)
        self.assertEqual(aliases[0].primary, True)

        # Check properties of entity_tree
        self.assertEqual(entity_data.default_alias_id, aliases[0].alias_id)

        # Check properties of publication_data
        self.assertEqual(entity_data.publication_type_id, 1)

        entity.master_revision = rev
        self.session.commit()

        return entity.entity_gid
Exemplo n.º 6
0
    def put(self, entity_gid):
        data = request.get_json()

        # This will be valid here, due to authentication.
        user = request.oauth.user
        user.total_revisions += 1
        user.revisions_applied += 1

        if not is_uuid(entity_gid):
            abort(404)

        try:
            entity = db.session.query(self.entity_class).options(
                joinedload('master_revision.entity_data')
            ).filter_by(entity_gid=entity_gid).one()
        except NoResultFound:
            abort(404)

        if entity.master_revision is None:
            abort(403)  # Forbidden to PUT on an entity with no data yet

        entity_data = entity.master_revision.entity_data
        entity_data = entity_data.update(data, db.session)

        revision = EntityRevision(user_id=user.user_id)
        revision.entity = entity
        revision.entity_data = entity_data

        note_content = data.get('revision', {}).get('note', '')

        if note_content != '':
            note = RevisionNote(user_id=user.user_id,
                                revision_id=revision.revision_id,
                                content=data['revision']['note'])

            revision.notes.append(note)

        entity.master_revision.parent = revision
        entity.master_revision = revision
        entity.revision = revision

        db.session.add(revision)

        # Commit entity, data and revision
        db.session.commit()

        entity_out = marshal(revision.entity, self.entity_fields)
        data_out = marshal(revision.entity_data, self.entity_data_fields)

        entity_out.update(data_out)

        # Don't 500 if we fail to index; commit still succeeded
        try:
            es_conn = Elasticsearch()
            index_entity(es_conn, entity_out)
        except ElasticsearchException:
            pass

        return marshal(revision, {
            'entity': fields.Nested(self.entity_stub_fields)
        })