def _get_in_lang(self, id, lang, clazz, schema, editing_view, clazz_locale=None, adapt_schema=None, include_maps=True, include_areas=True, set_custom_associations=None, set_custom_fields=None): document = self._get_document( clazz, id, clazz_locale=clazz_locale, lang=lang) if document.redirects_to: return { 'redirects_to': document.redirects_to, 'available_langs': get_available_langs(document.redirects_to) } set_available_langs([document]) self._set_associations(document, lang, editing_view) if not editing_view and set_custom_associations: set_custom_associations(document, lang) if not editing_view and include_areas: self._set_areas(document, lang) if include_maps: self._set_maps(document, lang) if set_custom_fields: set_custom_fields(document) if adapt_schema: schema = adapt_schema(schema, document) return to_json_dict(document, schema, with_special_locales_attrs=True)
def _get_documents( self, clazz, schema, clazz_locale, adapt_schema, custom_filter, include_areas, set_custom_fields, meta_params, load_documents): base_query = DBSession.query(clazz).\ filter(getattr(clazz, 'redirects_to').is_(None)) base_total_query = DBSession.query(getattr(clazz, 'document_id')).\ filter(getattr(clazz, 'redirects_to').is_(None)) if custom_filter: base_query = custom_filter(base_query) base_total_query = custom_filter(base_total_query) base_query = add_load_for_locales(base_query, clazz, clazz_locale) base_query = base_query.options(joinedload(getattr(clazz, 'geometry'))) if clazz == Outing: base_query = base_query. \ order_by(clazz.date_end.desc()). \ order_by(clazz.document_id.desc()) else: base_query = base_query.order_by(clazz.document_id.desc()) base_query = add_load_for_profiles(base_query, clazz) base_total_query = add_profile_filter(base_total_query, clazz) if include_areas: base_query = base_query. \ options( joinedload(getattr(clazz, '_areas')). load_only( 'document_id', 'area_type', 'version', 'protected', 'type'). joinedload('locales'). load_only( 'lang', 'title', 'version') ) documents, total = load_documents(base_query, base_total_query) set_available_langs(documents, loaded=True) lang = meta_params['lang'] if lang is not None: set_best_locale(documents, lang) if include_areas: self._set_areas_for_documents(documents, lang) if set_custom_fields: set_custom_fields(documents, lang) return { 'documents': [ to_json_dict( doc, schema if not adapt_schema else adapt_schema(schema, doc) ) for doc in documents ], 'total': total }
def _get_in_lang(self, id, lang, clazz, schema, clazz_locale=None, adapt_schema=None, include_maps=True, include_areas=True, set_custom_associations=None): document = self._get_document( clazz, id, clazz_locale=clazz_locale, lang=lang) if document.redirects_to: return { 'redirects_to': document.redirects_to, 'available_langs': get_available_langs(document.redirects_to) } set_available_langs([document]) editing_view = self.request.GET.get('e', '0') != '0' self._set_associations(document, lang, editing_view) if not editing_view and set_custom_associations: set_custom_associations(document, lang) if not editing_view and include_areas: self._set_areas(document, lang) if include_maps: self._set_maps(document, lang) if adapt_schema: schema = adapt_schema(schema, document) return to_json_dict(document, schema)
def test_set_available_langs(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_langs([waypoint]) self.assertEqual(set(waypoint.available_langs), set(["en", "fr"]))
def _get_documents_from_ids( document_ids, base_query, documents_config, lang): """ Load the documents for the ids and return them as json dict. The returned list contains None values for documents that could not be loaded, and the list has the same order has the document id list. """ base_query = base_query.options( load_only(*documents_config.get_load_only_fields()) ) base_query = add_load_for_locales( base_query, documents_config.clazz, documents_config.clazz_locale, documents_config.get_load_only_fields_locales()) if len(documents_config.get_load_only_fields_geometry()) > 1: # only load the geometry if the fields list contains other columns than # 'version' base_query = base_query.options( joinedload(getattr(documents_config.clazz, 'geometry')). load_only(*documents_config.get_load_only_fields_geometry()) ) if documents_config.include_areas: base_query = base_query. \ options( joinedload(getattr(documents_config.clazz, '_areas')). load_only( 'document_id', 'area_type', 'version', 'protected', 'type'). joinedload('locales'). load_only( 'lang', 'title', 'version') ) documents = _load_documents( document_ids, documents_config.clazz, base_query) set_available_langs(documents, loaded=True) if lang is not None: set_best_locale(documents, lang) if documents_config.include_areas: _set_areas_for_documents(documents, lang) if documents_config.set_custom_fields: documents_config.set_custom_fields(documents, lang) # make sure the documents are returned in the same order document_index = {doc.document_id: doc for doc in documents} documents = [document_index.get(id) for id in document_ids] return [ to_json_dict( doc, documents_config.schema if not documents_config.adapt_schema else documents_config.adapt_schema( documents_config.schema, doc) ) if doc else None for doc in documents ]
def _get_documents_from_ids(document_ids, base_query, documents_config, lang): """ Load the documents for the ids and return them as json dict. The returned list contains None values for documents that could not be loaded, and the list has the same order has the document id list. """ base_query = base_query.options( load_only(*documents_config.get_load_only_fields())) base_query = add_load_for_locales( base_query, documents_config.clazz, documents_config.clazz_locale, documents_config.get_load_only_fields_locales()) if len(documents_config.get_load_only_fields_geometry()) > 1: # only load the geometry if the fields list contains other columns than # 'version' base_query = base_query.options( joinedload(getattr(documents_config.clazz, 'geometry')).load_only( *documents_config.get_load_only_fields_geometry())) if documents_config.include_areas: base_query = base_query. \ options( joinedload(getattr(documents_config.clazz, '_areas')). load_only( 'document_id', 'area_type', 'version', 'protected', 'type'). joinedload('locales'). load_only( 'lang', 'title', 'version') ) documents = _load_documents(document_ids, documents_config.clazz, base_query) set_available_langs(documents, loaded=True) if lang is not None: set_best_locale(documents, lang) if documents_config.include_areas: _set_areas_for_documents(documents, lang) if documents_config.include_img_count: _set_img_count_for_documents(documents, document_ids) if documents_config.set_custom_fields: documents_config.set_custom_fields(documents, lang) # make sure the documents are returned in the same order document_index = {doc.document_id: doc for doc in documents} documents = [document_index.get(id) for id in document_ids] return [ to_json_dict( doc, documents_config.schema if not documents_config.adapt_schema else documents_config.adapt_schema(documents_config.schema, doc), with_special_geometry_attrs=True) if doc else None for doc in documents ]
def test_set_available_langs(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_langs([waypoint]) self.assertEqual(set(waypoint.available_langs), set(['en', 'fr']))
def _get_in_lang(self, id, lang, clazz, schema, clazz_locale=None, adapt_schema=None, include_maps=True, include_areas=True, set_custom_associations=None): document = self._get_document(clazz, id, clazz_locale=clazz_locale, lang=lang) if document.redirects_to: return { 'redirects_to': document.redirects_to, 'available_langs': get_available_langs(document.redirects_to) } set_available_langs([document]) editing_view = self.request.GET.get('e', '0') != '0' self._set_associations(document, lang, editing_view) if not editing_view and set_custom_associations: set_custom_associations(document, lang) if not editing_view and include_areas: self._set_areas(document, lang) if include_maps: self._set_maps(document, lang) if adapt_schema: schema = adapt_schema(schema, document) return to_json_dict(document, schema)
def _get_documents(self, clazz, schema, clazz_locale, adapt_schema, custom_filter, include_areas, set_custom_fields, meta_params, load_documents): base_query = DBSession.query(clazz).\ filter(getattr(clazz, 'redirects_to').is_(None)) base_total_query = DBSession.query(getattr(clazz, 'document_id')).\ filter(getattr(clazz, 'redirects_to').is_(None)) if custom_filter: base_query = custom_filter(base_query) base_total_query = custom_filter(base_total_query) base_query = add_load_for_locales(base_query, clazz, clazz_locale) base_query = base_query.options(joinedload(getattr(clazz, 'geometry'))) if clazz == Outing: base_query = base_query. \ order_by(clazz.date_end.desc()). \ order_by(clazz.document_id.desc()) else: base_query = base_query.order_by(clazz.document_id.desc()) base_query = add_load_for_profiles(base_query, clazz) base_total_query = add_profile_filter(base_total_query, clazz) if include_areas: base_query = base_query. \ options( joinedload(getattr(clazz, '_areas')). load_only( 'document_id', 'area_type', 'version', 'protected', 'type'). joinedload('locales'). load_only( 'lang', 'title', 'version') ) documents, total = load_documents(base_query, base_total_query) set_available_langs(documents, loaded=True) lang = meta_params['lang'] if lang is not None: set_best_locale(documents, lang) if include_areas: self._set_areas_for_documents(documents, lang) if set_custom_fields: set_custom_fields(documents, lang) return { 'documents': [ to_json_dict( doc, schema if not adapt_schema else adapt_schema(schema, doc)) for doc in documents ], 'total': total }