<commit_before>from optparse import make_option
import os
import shutil
import tempfile

from django.db.models import Q
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.core.management.base import BaseCommand, CommandError
from django.test.client import RequestFactory
from django.utils import translation

# Workaround https://code.djangoproject.com/ticket/22865
from geotrek.common.models import FileType  # NOQA

from geotrek.altimetry.views import ElevationProfile, ElevationArea
from geotrek.common import models as common_models
from geotrek.trekking import models as trekking_models
from geotrek.common.views import DocumentPublicPDF
from geotrek.trekking.views import TrekViewSet, POIViewSet, TrekGPXDetail, TrekKMLDetail
from geotrek.flatpages.views import FlatPageViewSet


class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--url', '-u', action='store', dest='url',
                    default='http://localhost',
                    help='Base url'),
    )

    def mkdirs(self, name):
        dirname = os.path.dirname(name)
        if not os.path.exists(dirname):
            os.makedirs(dirname)

    def sync_view(self, lang, view, name, url='/', **kwargs):
        fullname = os.path.join(self.tmp_root, name)
        self.mkdirs(fullname)
        request = self.factory.get(url, HTTP_HOST=self.rooturl)
        translation.activate(lang)
        request.LANGUAGE_CODE = lang
        request.user = AnonymousUser()
        response = view(request, **kwargs)
        if hasattr(response, 'render'):
            response.render()
        if response.status_code != 200:
            raise CommandError('Failed to get {name} (status {code})'.format(name=name, code=response.status_code))
        f = open(fullname, 'w')
        f.write(response.content)
        f.close()
        if self.verbosity == '2':
            self.stdout.write(fullname)

    def sync_geojson(self, lang, viewset, name):
        if self.verbosity >= '1':
            self.stdout.write("Sync {lang} {name} GeoJSON".format(lang=lang, name=name))
        view = viewset.as_view({'get': 'list'})
        name = os.path.join('api', lang, '{name}.geojson'.format(name=name))
        self.sync_view(lang, view, name, url='/?format=geojson')

    def sync_pois(self, lang):
        if self.verbosity >= '1':
            self.stdout.write("Sync {lang} POI".format(lang=lang))
        view = POIViewSet.as_view({'get': 'list'})
        for trek in trekking_models.Trek.objects.filter(**{'published_{lang}'.format(lang=lang): True}):
            name = os.path.join('api', lang, 'treks', str(trek.pk), 'pois.geojson')
            self.sync_view(lang, view, name, pk=trek.pk)

    def sync_object_view(self, lang, model, view, basename_fmt):
        for obj in model.objects.filter(**{'published_{lang}'.format(lang=lang): True}):
            modelname = model._meta.model_name
            name = os.path.join('api', lang, '{modelname}s'.format(modelname=modelname), str(obj.pk), basename_fmt.format(obj=obj))
            self.sync_view(lang, view, name, pk=obj.pk)

    def sync_pdfs(self, lang, model):
        if self.verbosity >= '1':
            self.stdout.write("Sync {lang} PDF".format(lang=lang))
        view = DocumentPublicPDF.as_view(model=model)
        self.sync_object_view(lang, model, view, '{obj.slug}.pdf')

    def sync_profiles(self, lang, model):
        if self.verbosity >= '1':
            self.stdout.write("Sync {lang} altimetry profile".format(lang=lang))
        view = ElevationProfile.as_view(model=trekking_models.Trek)
        self.sync_object_view(lang, model, view, 'profile.json')<commit_msg>Fix geojson format for trek pois in API<commit_after>from optparse import make_option
import os
import shutil
import tempfile

from django.db.models import Q
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.core.management.base import BaseCommand, CommandError
from django.test.client import RequestFactory
from django.utils import translation

# Workaround https://code.djangoproject.com/ticket/22865
from geotrek.common.models import FileType  # NOQA

from geotrek.altimetry.views import ElevationProfile, ElevationArea
from geotrek.common import models as common_models
from geotrek.trekking import models as trekking_models
from geotrek.common.views import DocumentPublicPDF
from geotrek.trekking.views import TrekViewSet, POIViewSet, TrekGPXDetail, TrekKMLDetail
from geotrek.flatpages.views import FlatPageViewSet


class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--url', '-u', action='store', dest='url',
                    default='http://localhost',
                    help='Base url'),
    )

    def mkdirs(self, name):
        dirname = os.path.dirname(name)
        if not os.path.exists(dirname):
            os.makedirs(dirname)

    def sync_view(self, lang, view, name, url='/', **kwargs):
        fullname = os.path.join(self.tmp_root, name)
        self.mkdirs(fullname)
        request = self.factory.get(url, HTTP_HOST=self.rooturl)
        translation.activate(lang)
        request.LANGUAGE_CODE = lang
        request.user = AnonymousUser()
        response = view(request, **kwargs)
        if hasattr(response, 'render'):
            response.render()
        if response.status_code != 200:
            raise CommandError('Failed to get {name} (status {code})'.format(name=name, code=response.status_code))
        f = open(fullname, 'w')
        f.write(response.content)
        f.close()
        if self.verbosity == '2':
            self.stdout.write(fullname)

    def sync_geojson(self, lang, viewset, name):
        if self.verbosity >= '1':
            self.stdout.write("Sync {lang} {name} GeoJSON".format(lang=lang, name=name))
        view = viewset.as_view({'get': 'list'})
        name = os.path.join('api', lang, '{name}.geojson'.format(name=name))
        self.sync_view(lang, view, name, url='/?format=geojson')

    def sync_pois(self, lang):
        if self.verbosity >= '1':
            self.stdout.write("Sync {lang} POI".format(lang=lang))
        view = POIViewSet.as_view({'get': 'list'})
        for trek in trekking_models.Trek.objects.filter(**{'published_{lang}'.format(lang=lang): True}):
            name = os.path.join('api', lang, 'treks', str(trek.pk), 'pois.geojson')
            self.sync_view(lang, view, name, url='/?format=geojson', pk=trek.pk)

    def sync_object_view(self, lang, model, view, basename_fmt):
        for obj in model.objects.filter(**{'published_{lang}'.format(lang=lang): True}):
            modelname = model._meta.model_name
            name = os.path.join('api', lang, '{modelname}s'.format(modelname=modelname), str(obj.pk), basename_fmt.format(obj=obj))
            self.sync_view(lang, view, name, pk=obj.pk)

    def sync_pdfs(self, lang, model):
        if self.verbosity >= '1':
            self.stdout.write("Sync {lang} PDF".format(lang=lang))
        view = DocumentPublicPDF.as_view(model=model)
        self.sync_object_view(lang, model, view, '{obj.slug}.pdf')

    def sync_profiles(self, lang, model):
        if self.verbosity >= '1':
            self.stdout.write("Sync {lang} altimetry profile".format(lang=lang))
        view = ElevationProfile.as_view(model=trekking_models.Trek)
        self.sync_object_view(lang, model, view, 'profile.json')
{'fraction_non_alphanumeric': 0.13094278807413376, 'fraction_numerical': 0.0034918076819769003, 'mean_word_length': 3.2360637087599544, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5031014', 'n_tokens_mistral': 2357, 'n_tokens_neox': 2275, 'n_words': 501}