def _test_delete_locale(self, document_id, lang):
        available_langs = get_available_langs(document_id)

        initial_other_locale_archives_count = self.session. \
            query(ArchiveDocumentLocale). \
            filter(ArchiveDocumentLocale.document_id == document_id). \
            filter(ArchiveDocumentLocale.lang != lang).count()

        initial_other_locale_versions_count = self.session. \
            query(DocumentVersion). \
            filter(DocumentVersion.document_id == document_id). \
            filter(DocumentVersion.lang != lang).count()

        self._delete_locale(document_id, lang, 200)

        # Check that deleted locale is no longer available
        available_langs.remove(lang)
        final_available_langs = get_available_langs(document_id)
        # if the document had only one locale, it is deleted and
        # get_available_langs() returns None
        self.assertEqual(not final_available_langs, not available_langs)
        if available_langs:
            self.assertCountEqual(final_available_langs, available_langs)

        # Check that archives have been removed too
        count = self.session.query(ArchiveDocumentLocale). \
            filter(ArchiveDocumentLocale.document_id == document_id). \
            filter(ArchiveDocumentLocale.lang == lang).count()
        self.assertEqual(0, count)

        # but not other locale archives
        other_locale_archives_count = self.session. \
            query(ArchiveDocumentLocale). \
            filter(ArchiveDocumentLocale.document_id == document_id). \
            filter(ArchiveDocumentLocale.lang != lang).count()
        self.assertEqual(other_locale_archives_count,
                         initial_other_locale_archives_count)

        # Check that the document versions table is cleared
        count = self.session.query(DocumentVersion). \
            filter(DocumentVersion.document_id == document_id). \
            filter(DocumentVersion.lang == lang).count()
        self.assertEqual(0, count)

        # but not for other locales
        other_locale_versions_count = self.session. \
            query(DocumentVersion). \
            filter(DocumentVersion.document_id == document_id). \
            filter(DocumentVersion.lang != lang).count()
        self.assertEqual(other_locale_versions_count,
                         initial_other_locale_versions_count)

        # Check the feed has been updated
        feed_items_langs = self.session.query(DocumentChange.langs).filter(
            DocumentChange.document_id == document_id
        ).all()
        for langs in feed_items_langs:
            self.assertNotIn(lang, langs)
Beispiel #2
0
    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)
Beispiel #3
0
    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)
Beispiel #4
0
    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)
Beispiel #5
0
def validate_document(request, **kwargs):
    if 'id' not in request.validated or \
            'document_type' not in request.validated:
        return

    document_id = request.validated['id']
    document_type = request.validated['document_type']

    if 'lang' in request.validated:
        # Tests specific to a locale-only deletion
        lang = request.validated['lang']
        available_langs = get_available_langs(document_id)
        if lang not in available_langs:
            request.errors.add('querystring', 'lang', 'locale not found')
            return
        request.validated['is_only_locale'] = len(available_langs) == 1
        if not request.validated['is_only_locale']:
            # When there are other locales left, the whole document is not
            # deleted. Following tests are then not required.
            request.validated['document_type'] = document_type
            return

    if document_type == WAYPOINT_TYPE:
        if _is_main_waypoint_of_route(document_id):
            request.errors.add(
                'querystring', 'document_id',
                'This waypoint cannot be deleted '
                'because it is a main waypoint.')
            return

        if _is_only_waypoint_of_route(document_id):
            request.errors.add(
                'querystring', 'document_id',
                'This waypoint cannot be deleted because '
                'it is the only waypoint associated to some routes.')
            return

    elif document_type == ROUTE_TYPE:
        if _is_only_route_of_outing(document_id):
            request.errors.add(
                'querystring', 'document_id',
                'This route cannot be deleted because '
                'it is the only route associated to some outings.')
            return
Beispiel #6
0
    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)
Beispiel #7
0
    def _load_document_info(self, document_id, lang, clazz):
        is_route = clazz == Route
        locales_type = with_polymorphic(DocumentLocale, RouteLocale) \
            if is_route else DocumentLocale
        locales_attr = getattr(clazz, 'locales')
        locales_type_eager = locales_attr.of_type(RouteLocale) \
            if is_route else locales_attr
        locales_load_only = [
            DocumentLocale.lang, DocumentLocale.title, DocumentLocale.version
        ]
        if is_route:
            locales_load_only.append(RouteLocale.title_prefix)

        document_query = DBSession. \
            query(clazz). \
            options(load_only(
                Document.document_id, Document.version,
                Document.redirects_to, Document.protected)). \
            join(locales_type). \
            filter(getattr(clazz, 'document_id') == document_id). \
            filter(DocumentLocale.lang == lang). \
            options(contains_eager(locales_type_eager, alias=locales_type).
                    load_only(*locales_load_only))
        document_query = add_load_for_profiles(document_query, clazz)
        document = document_query.first()

        if not document:
            # the requested locale might not be available, try to get the
            # document with all locales and set the "best"
            document_query = DBSession. \
                query(clazz). \
                options(load_only(
                    Document.document_id, Document.version,
                    Document.redirects_to, Document.protected)). \
                filter(getattr(clazz, 'document_id') == document_id). \
                options(joinedload(locales_type_eager).
                        load_only(*locales_load_only))
            document_query = add_load_for_profiles(document_query, clazz)
            document = document_query.first()

            if not document:
                raise HTTPNotFound('document not found')

            set_best_locale([document], lang)

        if document.redirects_to:
            return {
                'redirects_to': document.redirects_to,
                'available_langs': get_available_langs(document.redirects_to)
            }

        assert len(document.locales) == 1
        locale = document.locales[0]

        return {
            'document_id':
            document.document_id,
            'locales': [{
                'lang':
                locale.lang,
                'title':
                locale.title if clazz != UserProfile else document.name,
                'title_prefix':
                locale.title_prefix if is_route else None
            }]
        }
Beispiel #8
0
    def _load_document_info(self, document_id, lang, clazz):
        is_route = clazz == Route
        locales_type = with_polymorphic(DocumentLocale, RouteLocale) \
            if is_route else DocumentLocale
        locales_attr = getattr(clazz, 'locales')
        locales_type_eager = locales_attr.of_type(RouteLocale) \
            if is_route else locales_attr
        locales_load_only = [
            DocumentLocale.lang, DocumentLocale.title, DocumentLocale.version]
        if is_route:
            locales_load_only.append(RouteLocale.title_prefix)

        document_query = DBSession. \
            query(clazz). \
            options(load_only(
                Document.document_id, Document.version,
                Document.redirects_to, Document.protected)). \
            join(locales_type). \
            filter(getattr(clazz, 'document_id') == document_id). \
            filter(DocumentLocale.lang == lang). \
            options(contains_eager(locales_type_eager, alias=locales_type).
                    load_only(*locales_load_only))
        document_query = add_load_for_profiles(document_query, clazz)
        document = document_query.first()

        if not document:
            # the requested locale might not be available, try to get the
            # document with all locales and set the "best"
            document_query = DBSession. \
                query(clazz). \
                options(load_only(
                    Document.document_id, Document.version,
                    Document.redirects_to, Document.protected)). \
                filter(getattr(clazz, 'document_id') == document_id). \
                options(joinedload(locales_type_eager).
                        load_only(*locales_load_only))
            document_query = add_load_for_profiles(document_query, clazz)
            document = document_query.first()

            if not document:
                raise HTTPNotFound('document not found')

            set_best_locale([document], lang)

        if document.redirects_to:
            return {
                'redirects_to': document.redirects_to,
                'available_langs': get_available_langs(
                    document.redirects_to)
            }

        assert len(document.locales) == 1
        locale = document.locales[0]

        return {
            'document_id': document.document_id,
            'locales': [{
                'lang': locale.lang,
                'title':
                    locale.title
                    if clazz != UserProfile else document.name,
                'title_prefix': locale.title_prefix if is_route else None
            }]
        }