Beispiel #1
0
    def _paginate_offset(self, clazz, schema, adapt_schema):
        """Return a batch of documents with the given `offset` and `limit`.
        """
        validated = self.request.validated
        offset = validated['offset'] if 'offset' in validated else 0
        limit = min(
            validated['limit'] if 'limit' in validated else LIMIT_DEFAULT,
            LIMIT_MAX)

        base_query = DBSession.query(clazz)

        documents = base_query. \
            options(joinedload(getattr(clazz, 'locales'))). \
            options(joinedload(getattr(clazz, 'geometry'))). \
            order_by(clazz.document_id.desc()). \
            slice(offset, offset + limit). \
            all()
        set_available_cultures(documents, loaded=True)

        if validated.get('lang') is not None:
            set_best_locale(documents, validated.get('lang'))

        total = base_query.count()

        return {
            'documents': [
                to_json_dict(
                    doc,
                    schema if not adapt_schema else adapt_schema(schema, doc)
                ) for doc in documents
            ],
            'total': total
        }
Beispiel #2
0
    def _paginate_after(self, clazz, schema, adapt_schema):
        """
        Returns all documents for which `document_id` is smaller than the
        given id in `after`.
        """
        after = self.request.validated['after']
        limit = self.request.validated['limit']
        limit = min(LIMIT_DEFAULT if limit is None else limit, LIMIT_MAX)

        base_query = DBSession.query(clazz)

        documents = base_query. \
            options(joinedload(getattr(clazz, 'locales'))). \
            order_by(clazz.document_id.desc()). \
            filter(clazz.document_id < after). \
            limit(limit). \
            all()
        set_available_cultures(documents)

        return {
            'documents': [
                to_json_dict(
                    doc,
                    schema if not adapt_schema else adapt_schema(schema, doc)
                ) for doc in documents
            ],
            'total': -1
        }
Beispiel #3
0
    def _get_in_lang(self, id, lang, clazz, schema, adapt_schema=None):
        document = self._get_document(clazz, id, lang)
        set_available_cultures([document])

        if adapt_schema:
            schema = adapt_schema(schema, document)

        return to_json_dict(document, schema)
Beispiel #4
0
    def test_set_available_cultures(self):
        waypoint = self._get_waypoint()
        waypoint.geometry = DocumentGeometry(
            geom='SRID=3857;POINT(635956.075332665 5723604.677994)')
        self.session.add(waypoint)
        self.session.flush()

        set_available_cultures([waypoint])
        self.assertEqual(waypoint.available_cultures, ['en', 'fr'])