def handle(self, *args, **options): print("Calculando country codes") b = kings aas = AdministrativeArea.objects.filter(depth=1) for aa in aas: cc = next(v['country_code'] for k, v in kings.items() if v['id'] == aa.osm_id) print(f' > {aa.osm_type} {aa.osm_id} {aa.name}') print(' - Lineas: ', end='') # Linea.objects.filter(envolvente__intersects=aa.geometry_simple).update(country_code=cc) print(Linea.objects \ .annotate(intersection_area=Area(Intersection(F('envolvente'), aa.geometry_simple)) / Area(F('envolvente'))) \ .filter(intersection_area__gt=A(sq_m=0.65)) \ .update(country_code=cc)) print(' - Recorrido: ', end='') print(Recorrido.objects \ .annotate(intersection_len=Length(Intersection(F('ruta_simple'), aa.geometry_simple)) / Length(F('ruta_simple'))) \ .filter(intersection_len__gt=D(m=0.65)) \ .update(country_code=cc)) print(' - Parada: ', end='') print( Parada.objects.filter( latlng__intersects=aa.geometry_simple).update( country_code=cc)) print(' - Poi: ', end='') print( Poi.objects.filter( latlng__intersects=aa.geometry_simple).update( country_code=cc)) print(' - AdministrativeArea: ', end='') print(AdministrativeArea.objects \ .annotate(intersection_area=Area(Intersection(F('geometry_simple'), aa.geometry_simple)) / Area(F('geometry_simple'))) \ .filter(intersection_area__gt=A(sq_m=0.65)) \ .update(country_code=cc)) print(' > DONE')
def get_loc(loc, sitemap_lang_code): cc = loc.split('/')[3] if len(cc) != 2: cc = 'ar' language_default = next((v['lang'] for k,v in kings.items() if v['country_code'] == cc), 'en') if language_default[:2] == sitemap_lang_code[:2]: return loc return loc + '?lang=' + sitemap_lang_code
def get_lang_list(context, obj=None): language_default = 'ar' if obj: language_default = next((v['lang'] for k, v in kings.items() if v['country_code'] == obj.country_code), 'en') else: request = context['request'] cc = request.path.split('/')[0] if len(cc) != 2: cc = 'ar' language_default = next( (v['lang'] for k, v in kings.items() if v['country_code'] == cc), 'en') langs = [] for l in settings.LANGUAGES: langs.append(l[:] + (l[0][:2] == language_default[:2], )) return langs
def process_request(self, request): country_code = 'ar' regex_match = country_code_prefix_re.match(request.path_info) if regex_match: country_code = regex_match.group(1) language = next( (v['lang'] for k, v in kings.items() if v['country_code'] == country_code), 'es') qlang = get_lang_from_qs(request) if qlang: language = qlang translation.activate(language) request.LANGUAGE_CODE = translation.get_language()
def get_alternates(loc, sitemap_lang_code): alternates = [] for (lang_code, lang_name) in settings.LANGUAGES: cc = loc.split('/')[3] if len(cc) != 2: cc = 'ar' language_default = next((v['lang'] for k,v in kings.items() if v['country_code'] == cc), 'en') if lang_code[:2] != sitemap_lang_code: # only add as alternate if this url is not the sitemap_lang_code # (if this url is the sitemap_lang_code, then it goes directly in <loc></loc> (not alternate)) if language_default[:2] == lang_code[:2]: alternates.append({'location': loc, 'lang': lang_code}) alternates.append({'location': loc, 'lang': 'x-default'}) alternates.append({'location': loc + '?lang=' + lang_code, 'lang': lang_code}) else: if language_default[:2] == lang_code[:2]: alternates.append({'location': loc, 'lang': lang_code}) alternates.append({'location': loc, 'lang': 'x-default'}) return alternates
def getsitemaps(cc): sitemaps = {} cclang = next((v['lang'] for k,v in kings.items() if v['country_code'] == cc), '')[:2] print('CCLANG:' + cclang) for (lang_code, lang_name) in settings.LANGUAGES: suffix = '' if cclang != lang_code[:2]: suffix = '_' + lang_code sitemaps['lineas' + suffix] = CBSitemap({ 'queryset': Linea.objects.defer('envolvente').filter(country_code=cc), }, priority=0.6, lang=lang_code) sitemaps['recorridos' + suffix] = CBSitemap({ 'queryset': Recorrido.objects.defer('ruta').filter(country_code=cc), }, priority=0.6, lang=lang_code) sitemaps['paradas' + suffix] = CBSitemap({ 'queryset': Parada.objects.defer('latlng').filter(country_code=cc), }, priority=0.4, lang=lang_code) sitemaps['pois' + suffix] = CBSitemap({ 'queryset': Poi.objects.defer('latlng').filter(country_code=cc), }, priority=0.6, lang=lang_code) sitemaps['administrativeareas' + suffix] = CBSitemap({ 'queryset': AdministrativeArea.objects.defer('geometry', 'geometry_simple').filter(country_code=cc), }, priority=0.6, lang=lang_code) return sitemaps
def redirect_nuevas_urls(request, slug_ciudad=None, slug_linea=None, slug_recorrido=None, country_code=None): """ # v1 cualbondi.com.ar/la-plata/recorridos/Norte/10/IDA/ cualbondi.com.ar/cordoba/recorridos/T%20(Transversal)/Central/IDA/ # v2 cualbondi.com.ar/la-plata/norte/10-desde-x-hasta-y # v3 cualbondi.com.ar/r/c123/asdasd """ ciudades = data.ciudades + data.ciudades_es ciudad = next((c for c in ciudades if c.slug == slug_ciudad), False) if not ciudad: country = next((v for k,v in kings.items() if v['country_code'] == country_code), False) if country: return redirect(get_object_or_404(AdministrativeArea, osm_id=str(country['id'])), permanent=True) else: raise Http404 # ciudad if not slug_linea: return redirect(get_object_or_404(AdministrativeArea, osm_type='r', osm_id=ciudad.osm_id), permanent=True) # linea lineas = Linea.objects.filter(slug=slug_linea, ciudades__slug=ciudad.slug) if len(lineas) == 0: raise Http404 if not slug_recorrido: return redirect(lineas[0], permanent=True) # recorrido recorridos = Recorrido.objects.filter(slug=slug_recorrido, ciudades__slug=ciudad.slug, linea__slug=lineas[0].slug) if len(recorridos) == 0: raise Http404 return redirect(recorridos[0], permanent=True)
name='revision_externa'), # Ranking aka agradecimientos url(r'^agradecimientos/$', agradecimientos, name='agradecimientos'), path(r'sitemap.xml', sitemaps_views.index, {'sitemaps': sitemaps}), path('sitemap-<section>.xml', sitemaps_views.sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'), url(r'^api/v3/', include(api3Urls)), url(r'^v3/', include(api3Urls)), url(r'^auth/', include('rest_framework_social_oauth2.urls', namespace='drfsocial')), url('', include('social_django.urls', namespace='social')) ] for name, k in kings.items(): cc = k['country_code'] urlpatterns.append( path( f'{cc}/sitemap.xml', sitemaps_views.index, { 'sitemaps': getsitemaps(cc), 'sitemap_url_name': f'django.contrib.sitemaps.views.sitemap-{cc}' })) urlpatterns.append( path(f'{cc}/sitemap-<section>.xml', sitemaps_views.sitemap, {'sitemaps': getsitemaps(cc)}, name=f'django.contrib.sitemaps.views.sitemap-{cc}')) if settings.DEBUG: import debug_toolbar