Exemplo n.º 1
0
    def handle(self, *args, **options):
        locations = models.Location.objects.filter(managed_locally=True)
        for location in locations:
            url_parts = [location.slug]
            m = location
            while m.parent:
                url_parts.append(m.parent.slug)
                m = m.parent

            complete_url = '/'.join(reversed(url_parts))

            languages = models.Language.objects.all()
            for l in languages:
                cms_url = utils.get_cms_url(l.iso_code, complete_url)
                html_content = utils.get_cms_page(l.iso_code, complete_url)
                cache.set(cms_url, html_content)
        try:
            gdrive_tokens = models.AuthorizationToken.objects.filter(type=1)
            if not gdrive_tokens:
                return

            credentials_text = gdrive_tokens[0].token_text
            credentials = oauth2client.client.Credentials.new_from_json(credentials_text)

            documents, includes = self._list_folders(credentials)

            for key in documents.iterkeys():
                document_link = documents[key]['exportLinks']['text/html']

                try:
                    if '-' in key:
                        identifier = key.split('-')
                        location = models.Location.objects.filter(slug='-'.join(identifier[:-1]), managed_locally=False)
                        language = models.Language.objects.filter(iso_code=identifier[-1])

                        if location and language:
                            location = location[0]
                            language = language[0]

                            existing_content = models.LocationContent.objects.filter(parent=location,
                                                                                     language=language)
                            if existing_content:
                                content = existing_content[0]
                                content.html_url = document_link
                                content.title = location.name
                                content.save()
                            else:
                                models.LocationContent.objects.create(parent=location, language=language,
                                                                      title=location.name,
                                                                      html_url=document_link)

                            content = requests.get(document_link).text
                            cache.set(document_link, content)
                except Exception as e:
                    print(e.message)
                    print("Error loading one of the files: {}.".format(key))
        except Exception as e:
            pass

        self.stdout.write('Successfully created/updated content!')
Exemplo n.º 2
0
def index(request, page_id, language):
    location = models.Location.objects.filter(id=page_id)
    html_content = ""

    if location:
        location = location[0]

        if location.managed_locally:
            """
            Loading content from a CMS or CMS-like site
            """
            url_parts = [location.slug]
            m = location
            while m.parent:
                url_parts.append(m.parent.slug)
                m = m.parent

            complete_url = "/".join(reversed(url_parts))

            cms_url = utils.get_cms_url(language, complete_url)

            if cms_url in cache:
                html_content = cache.get(cms_url)
            else:
                html_content = utils.get_cms_page(language, complete_url)
                cache.set(cms_url, html_content)
        else:
            """
            Loading content from a google doc
            """
            html_content = ""

            content = location.content.filter(language__iso_code=language)

            default_content = location.content.all()
            content = content[0] if content else default_content[0] if default_content else None

            if content and content.html_url:
                doc_path = content.html_url
                cached = cache.get(doc_path)
                if cached:
                    html_content = cached
                else:
                    html_content = requests.get(doc_path).text

    languages = list(models.Language.objects.all().order_by("name"))

    return render(
        request,
        "index.html",
        context={
            "html_content": html_content,
            "languages": languages,
            "location": location,
            "service_map_enabled": settings.ENABLE_SERVICES or False,
        },
        context_instance=RequestContext(request),
    )
Exemplo n.º 3
0
def index(request, page_id, language):
    location = models.Location.objects.filter(id=page_id)
    html_content = ""

    if language in TRANSLATION_SHIM.keys():
        translation.activate(TRANSLATION_SHIM[language])
    else:
        translation.activate(language)

    # Handling Meraki:
    context = {}

    if 'source' in request.GET:
        context['is_captive'] = True

    if 'base_grant_url' in request.GET:
        context['is_captive'] = True
        context['is_meraki'] = True
        context['next'] = "{}?continue_url={}".format(
            request.GET['base_grant_url'],
            urllib.quote_plus(request.GET['user_continue_url']))

        print("Request from meraki: {}".format(' '.join(
            [': '.join(a) for a in request.GET.iterkeys()])))

    if 'provider' in request.GET:
        context['provider'] = request.GET['provider']

    if location:
        location = location[0]

        if location.managed_locally:
            """
            Loading content from a CMS or CMS-like site
            """
            url_parts = [location.slug]
            m = location
            while m.parent:
                url_parts.append(m.parent.slug)
                m = m.parent

            complete_url = '/'.join(reversed(url_parts))

            cms_url = utils.get_cms_url(language, complete_url)

            if cms_url in cache:
                html_content = cache.get(cms_url)
            else:
                html_content = utils.get_cms_page(language, complete_url)
                cache.set(cms_url, html_content)
        else:
            """
            Loading content from a google doc
            """
            html_content = ""

            content = location.content.filter(language__iso_code=language)

            default_content = location.content.all()
            content = content[0] if content else default_content[
                0] if default_content else None

            if content and content.html_url:
                doc_path = content.html_url
                cached = cache.get(doc_path)
                if cached:
                    html_content = cached
                else:
                    html_content = requests.get(doc_path).text

    languages = list(models.Language.objects.all().order_by('name'))

    context.update({
        "html_content": html_content,
        "languages": languages,
        "location": location,
        "language": language,
        "service_map_enabled": settings.ENABLE_SERVICES or False,
    })

    return render(request,
                  'index.html',
                  context=context,
                  context_instance=RequestContext(request))
Exemplo n.º 4
0
    def handle(self, *args, **options):
        locations = models.Location.objects.filter(managed_locally=True)
        for location in locations:
            url_parts = [location.slug]
            m = location
            while m.parent:
                url_parts.append(m.parent.slug)
                m = m.parent

            complete_url = '/'.join(reversed(url_parts))

            languages = models.Language.objects.all()
            for l in languages:
                cms_url = utils.get_cms_url(l.iso_code, complete_url)
                html_content = utils.get_cms_page(l.iso_code, complete_url)
                cache.set(cms_url, html_content)
        try:
            gdrive_tokens = models.AuthorizationToken.objects.filter(type=1)
            if not gdrive_tokens:
                return

            credentials_text = gdrive_tokens[0].token_text
            credentials = oauth2client.client.Credentials.new_from_json(
                credentials_text)

            documents, includes = self._list_folders(credentials)

            for key in documents.iterkeys():
                document_link = documents[key]['exportLinks']['text/html']

                try:
                    if '-' in key:
                        identifier = key.split('-')
                        location = models.Location.objects.filter(
                            slug='-'.join(identifier[:-1]),
                            managed_locally=False)
                        language = models.Language.objects.filter(
                            iso_code=identifier[-1])

                        if location and language:
                            location = location[0]
                            language = language[0]

                            existing_content = models.LocationContent.objects.filter(
                                parent=location, language=language)
                            if existing_content:
                                content = existing_content[0]
                                content.html_url = document_link
                                content.title = location.name
                                content.save()
                            else:
                                models.LocationContent.objects.create(
                                    parent=location,
                                    language=language,
                                    title=location.name,
                                    html_url=document_link)

                            content = requests.get(document_link).text
                            cache.set(document_link, content)
                except Exception as e:
                    print(e.message)
                    print("Error loading one of the files: {}.".format(key))
        except Exception as e:
            pass

        self.stdout.write('Successfully created/updated content!')
Exemplo n.º 5
0
def index(request, page_id, language):
    location = models.Location.objects.filter(id=page_id)
    html_content = ""

    if language in TRANSLATION_SHIM.keys():
        translation.activate(TRANSLATION_SHIM[language])
    else:
        translation.activate(language)

    # Handling Meraki:
    context = {
    }

    if 'source' in request.GET:
        context['is_captive'] = True

    if 'base_grant_url' in request.GET:
        context['is_captive'] = True
        context['is_meraki'] = True
        context['next'] = "{}?continue_url={}".format(
            request.GET['base_grant_url'],
            urllib.quote_plus(request.GET['user_continue_url'])
        )

        print("Request from meraki: {}".format(' '.join([': '.join(a) for a in request.GET.iterkeys()])))

    if 'provider' in request.GET:
        context['provider'] = request.GET['provider']

    if location:
        location = location[0]

        if location.managed_locally:
            """
            Loading content from a CMS or CMS-like site
            """
            url_parts = [location.slug]
            m = location
            while m.parent:
                url_parts.append(m.parent.slug)
                m = m.parent

            complete_url = '/'.join(reversed(url_parts))

            cms_url = utils.get_cms_url(language, complete_url)

            if cms_url in cache:
                html_content = cache.get(cms_url)
            else:
                html_content = utils.get_cms_page(language, complete_url)
                cache.set(cms_url, html_content)
        else:
            """
            Loading content from a google doc
            """
            html_content = ""

            content = location.content.filter(language__iso_code=language)

            default_content = location.content.all()
            content = content[0] if content else default_content[0] if default_content else None

            if content and content.html_url:
                doc_path = content.html_url
                cached = cache.get(doc_path)
                if cached:
                    html_content = cached
                else:
                    html_content = requests.get(doc_path).text

    languages = list(models.Language.objects.all().order_by('name'))

    context.update({
        "html_content": html_content,
        "languages": languages,
        "location": location,
        "language": language,
        "service_map_enabled": settings.ENABLE_SERVICES or False,
    })

    return render(request, 'index.html', context=context, context_instance=RequestContext(request))