Ejemplo n.º 1
0
    def _collection_post(
            self, schema, before_add=None, after_add=None):
        user_id = self.request.authenticated_userid
        document_in = self.request.validated
        document = schema.objectify(document_in)
        document.document_id = None

        if before_add:
            before_add(document, user_id=user_id)

        DBSession.add(document)
        DBSession.flush()
        DocumentRest.create_new_version(document, user_id)

        if document.type != AREA_TYPE:
            update_areas_for_document(document, reset=False)

        if after_add:
            after_add(document, user_id=user_id)

        if document_in.get('associations', None):
            create_associations(document, document_in['associations'], user_id)

        notify_es_syncer(self.request.registry.queue_config)

        return {'document_id': document.document_id}
Ejemplo n.º 2
0
    def _collection_post(self, schema, before_add=None, after_add=None):
        user_id = self.request.authenticated_userid
        document_in = self.request.validated
        document = schema.objectify(document_in)
        document.document_id = None

        if before_add:
            before_add(document, user_id=user_id)

        DBSession.add(document)
        DBSession.flush()
        DocumentRest.create_new_version(document, user_id)

        if document.type != AREA_TYPE:
            update_areas_for_document(document, reset=False)

        if after_add:
            after_add(document, user_id=user_id)

        if document_in.get('associations', None):
            create_associations(document, document_in['associations'], user_id)

        notify_es_syncer(self.request.registry.queue_config)

        return {'document_id': document.document_id}
Ejemplo n.º 3
0
    def _put(
            self, clazz, schema, clazz_locale=None, before_update=None,
            after_update=None):
        user_id = self.request.authenticated_userid
        id = self.request.validated['id']
        document_in = \
            schema.objectify(self.request.validated['document'])
        self._check_document_id(id, document_in.document_id)

        # get the current version of the document
        document = self._get_document(clazz, id, clazz_locale=clazz_locale)

        if document.redirects_to:
            raise HTTPBadRequest('can not update merged document')
        if document.protected and not self.request.has_permission('moderator'):
            raise HTTPForbidden('No permission to change a protected document')

        self._check_versions(document, document_in)

        # remember the current version numbers of the document
        old_versions = document.get_versions()

        # update the document with the input document
        document.update(document_in)

        if before_update:
            before_update(document, document_in, user_id=user_id)

        try:
            DBSession.flush()
        except StaleDataError:
            raise HTTPConflict('concurrent modification')

        # when flushing the session, SQLAlchemy automatically updates the
        # version numbers in case attributes have changed. by comparing with
        # the old version numbers, we can check if only figures or only locales
        # have changed.
        (update_types, changed_langs) = document.get_update_type(old_versions)

        if update_types:
            # A new version needs to be created and persisted
            self._update_version(
                document, user_id, self.request.validated['message'],
                update_types,  changed_langs)

            if document.type != AREA_TYPE and UpdateType.GEOM in update_types:
                update_areas_for_document(document, reset=True)

            if after_update:
                after_update(document, update_types, user_id=user_id)

            # And the search updated
            notify_es_syncer(self.request.registry.queue_config)

        associations = self.request.validated.get('associations', None)
        if associations:
            synchronize_associations(document, associations, user_id)

        return {}
Ejemplo n.º 4
0
    def test_update_document_reset_first(self):
        # add an association with wp2, which should be removed after the update
        self.session.add(
            AreaAssociation(document_id=self.waypoint2.document_id,
                            area_id=self.area1.document_id))
        self.session.flush()

        update_areas_for_document(self.waypoint2, reset=True)

        updated_links = self._get_links_for_document(self.waypoint2)
        self.assertEqual(len(updated_links), 0)
Ejemplo n.º 5
0
    def test_update_document_reset_first(self):
        # add an association with wp2, which should be removed after the update
        self.session.add(AreaAssociation(
            document_id=self.waypoint2.document_id,
            area_id=self.area1.document_id))
        self.session.flush()

        update_areas_for_document(self.waypoint2, reset=True)

        updated_links = self._get_links_for_document(self.waypoint2)
        self.assertEqual(len(updated_links), 0)
Ejemplo n.º 6
0
    def test_update_document(self):
        # waypoint inside the area
        update_areas_for_document(self.waypoint1)

        added_links = self._get_links_for_document(self.waypoint1)
        self.assertEqual(len(added_links), 1)
        self.assertEqual(added_links[0].area_id, self.area1.document_id)

        # wp outside the area
        update_areas_for_document(self.waypoint2)

        added_links = self._get_links_for_document(self.waypoint2)
        self.assertEqual(len(added_links), 0)

        # wp without geometry
        update_areas_for_document(self.waypoint3)

        added_links = self._get_links_for_document(self.waypoint3)
        self.assertEqual(len(added_links), 0)

        # wp with empty geometry
        update_areas_for_document(self.waypoint4)

        added_links = self._get_links_for_document(self.waypoint4)
        self.assertEqual(len(added_links), 0)
Ejemplo n.º 7
0
    def test_update_document(self):
        # waypoint inside the area
        update_areas_for_document(self.waypoint1)

        added_links = self._get_links_for_document(self.waypoint1)
        self.assertEqual(len(added_links), 1)
        self.assertEqual(
            added_links[0].area_id, self.area1.document_id)

        # wp outside the area
        update_areas_for_document(self.waypoint2)

        added_links = self._get_links_for_document(self.waypoint2)
        self.assertEqual(len(added_links), 0)

        # wp without geometry
        update_areas_for_document(self.waypoint3)

        added_links = self._get_links_for_document(self.waypoint3)
        self.assertEqual(len(added_links), 0)

        # wp with empty geometry
        update_areas_for_document(self.waypoint4)

        added_links = self._get_links_for_document(self.waypoint4)
        self.assertEqual(len(added_links), 0)
Ejemplo n.º 8
0
    def _create_document(self,
                         document_in,
                         schema,
                         before_add=None,
                         after_add=None,
                         allow_anonymous=False):
        if allow_anonymous and document_in.get('anonymous') and \
           self.request.registry.anonymous_user_id:
            user_id = self.request.registry.anonymous_user_id
        else:
            user_id = self.request.authenticated_userid

        document = schema.objectify(document_in)
        document.document_id = None

        if before_add:
            before_add(document, user_id)

        DBSession.add(document)
        DBSession.flush()
        DocumentRest.create_new_version(document, user_id)

        if document.type != AREA_TYPE:
            update_areas_for_document(document, reset=False)

        if document.type != MAP_TYPE:
            update_maps_for_document(document, reset=False)

        if after_add:
            after_add(document, user_id=user_id)

        if document_in.get('associations', None):
            check_association = association_permission_checker(
                self.request, skip_outing_check=document.type == OUTING_TYPE)

            added_associations = create_associations(
                document,
                document_in['associations'],
                user_id,
                check_association=check_association)
            update_cache_version_associations(added_associations, [],
                                              document.document_id)

        update_feed_document_create(document, user_id)

        notify_es_syncer(self.request.registry.queue_config)
        return document
Ejemplo n.º 9
0
    def _create_document(
            self, document_in, schema, before_add=None, after_add=None):
        user_id = self.request.authenticated_userid

        document = schema.objectify(document_in)
        document.document_id = None

        if before_add:
            before_add(document, user_id)

        DBSession.add(document)
        DBSession.flush()
        DocumentRest.create_new_version(document, user_id)

        if document.type != AREA_TYPE:
            update_areas_for_document(document, reset=False)

        if document.type != MAP_TYPE:
            update_maps_for_document(document, reset=False)

        if after_add:
            after_add(document, user_id=user_id)

        if document_in.get('associations', None):
            check_association = association_permission_checker(
                self.request, skip_outing_check=document.type == OUTING_TYPE)

            added_associations = create_associations(
                document, document_in['associations'], user_id,
                check_association=check_association)
            update_cache_version_associations(
                added_associations, [], document.document_id)

        update_feed_document_create(document, user_id)

        notify_es_syncer(self.request.registry.queue_config)
        return document
Ejemplo n.º 10
0
    def update_document(document,
                        document_in,
                        request,
                        before_update=None,
                        after_update=None,
                        manage_versions=None):
        user_id = request.authenticated_userid

        # remember the current version numbers of the document
        old_versions = document.get_versions()

        if before_update:
            before_update(document, document_in)

        # update the document with the input document
        document.update(document_in)

        if manage_versions:
            manage_versions(document, old_versions)

        try:
            DBSession.flush()
        except StaleDataError:
            raise HTTPConflict('concurrent modification')

        # when flushing the session, SQLAlchemy automatically updates the
        # version numbers in case attributes have changed. by comparing with
        # the old version numbers, we can check if only figures or only locales
        # have changed.
        (update_types, changed_langs) = document.get_update_type(old_versions)

        if update_types:
            # A new version needs to be created and persisted
            DocumentRest.update_version(document, user_id,
                                        request.validated['message'],
                                        update_types, changed_langs)

            if document.type != AREA_TYPE and UpdateType.GEOM in update_types:
                update_areas_for_document(document, reset=True)

            if document.type != MAP_TYPE and UpdateType.GEOM in update_types:
                update_maps_for_document(document, reset=True)

            if after_update:
                after_update(document, update_types, user_id=user_id)

            update_cache_version(document)

        associations = request.validated.get('associations', None)
        if associations:
            check_association_add = \
                association_permission_checker(request)
            check_association_remove = \
                association_permission_removal_checker(request)

            added_associations, removed_associations = \
                synchronize_associations(
                    document, associations, user_id,
                    check_association_add=check_association_add,
                    check_association_remove=check_association_remove)

        if update_types or associations:
            # update search index
            notify_es_syncer(request.registry.queue_config)
            update_feed_document_update(document, user_id, update_types)
        if associations and (removed_associations or added_associations):
            update_cache_version_associations(added_associations,
                                              removed_associations)

        return update_types
Ejemplo n.º 11
0
    def test_update_document_forwarded(self):
        # waypoint inside the area but forwarded (should be ignored)
        update_areas_for_document(self.waypoint5)

        added_links = self._get_links_for_document(self.waypoint1)
        self.assertEqual(len(added_links), 0)
Ejemplo n.º 12
0
    def _put(
            self, clazz, schema, clazz_locale=None, before_update=None,
            after_update=None):
        user_id = self.request.authenticated_userid
        id = self.request.validated['id']
        document_in = \
            schema.objectify(self.request.validated['document'])
        self._check_document_id(id, document_in.document_id)

        # get the current version of the document
        document = self._get_document(clazz, id, clazz_locale=clazz_locale)

        if document.redirects_to:
            raise HTTPBadRequest('can not update merged document')
        if document.protected and not self.request.has_permission('moderator'):
            raise HTTPForbidden('No permission to change a protected document')

        self._check_versions(document, document_in)

        # remember the current version numbers of the document
        old_versions = document.get_versions()

        # update the document with the input document
        document.update(document_in)

        if before_update:
            before_update(document, document_in, user_id=user_id)

        try:
            DBSession.flush()
        except StaleDataError:
            raise HTTPConflict('concurrent modification')

        # when flushing the session, SQLAlchemy automatically updates the
        # version numbers in case attributes have changed. by comparing with
        # the old version numbers, we can check if only figures or only locales
        # have changed.
        (update_types, changed_langs) = document.get_update_type(old_versions)

        if update_types:
            # A new version needs to be created and persisted
            DocumentRest.update_version(
                document, user_id, self.request.validated['message'],
                update_types,  changed_langs)

            if document.type != AREA_TYPE and UpdateType.GEOM in update_types:
                update_areas_for_document(document, reset=True)

            if document.type != MAP_TYPE and UpdateType.GEOM in update_types:
                update_maps_for_document(document, reset=True)

            if after_update:
                after_update(document, update_types, user_id=user_id)

            update_cache_version(document)

        associations = self.request.validated.get('associations', None)
        if associations:
            check_association_add = \
                association_permission_checker(self.request)
            check_association_remove = \
                association_permission_removal_checker(self.request)

            added_associations, removed_associations = \
                synchronize_associations(
                    document, associations, user_id,
                    check_association_add=check_association_add,
                    check_association_remove=check_association_remove)

        if update_types or associations:
            # update search index
            notify_es_syncer(self.request.registry.queue_config)
            update_feed_document_update(document, user_id, update_types)
        if associations and (removed_associations or added_associations):
            update_cache_version_associations(
                added_associations, removed_associations)

        return {}
Ejemplo n.º 13
0
    def test_update_document_forwarded(self):
        # waypoint inside the area but forwarded (should be ignored)
        update_areas_for_document(self.waypoint5)

        added_links = self._get_links_for_document(self.waypoint1)
        self.assertEqual(len(added_links), 0)