Source code for django_landing_simple.landing_simple.views

"""
This module implements a simple, public-facing landing page for web applications. It provides
a minimal entry point for visitors with multi-language support through language selection
functionality and Google Analytics integration for visitor tracking.

**Business Context:**

The landing_simple app serves as a lightweight, standalone landing page solution for web
applications that don't require user authentication or complex workflows. It's designed for
informational websites, marketing pages, or simple web applications where the primary goal
is to present information to visitors and allow them to select their preferred language.

**Key Business Features:**

1. **Landing Page:**

- :func:`home`: Simple, static home page for all visitors
    - No authentication or user management required
    - Responsive design for all devices
    - Multi-language support for international audiences
    - Google Analytics integration for visitor tracking

2. **Language Selection:**

- :func:`set_language`: Users can select their preferred language
    - Language choice persists via cookie
    - Affects entire application interface
    - Supports all languages defined in Django settings
    - Immediate language activation for current request

**User Journey:**

1. **First-Time Visitor:**

- Arrives at home page
    - Views content in default language
    - Can select preferred language from language selector
    - Language preference saved for future visits
    - Navigates through application in selected language

2. **Returning Visitor:**

- Arrives at home page
    - Content displayed in previously selected language (via cookie)
    - Can change language at any time

**Technical Features:**

- Google Analytics (GA4) integration via Measurement ID
- Language cookie management with Django settings
- Multi-language support via Django i18n framework
- Simple, lightweight implementation
- No database dependencies
- No session management required

**Integration:**

The landing_simple app integrates with:
- Django's internationalization framework for multi-language support
- Google Analytics for visitor tracking and metrics
- Django settings for configuration (GOOGLE_MEASUREMENT_ID, LANGUAGES)

**Security:**

- No authentication required (public access)
- Safe handling of invalid language codes
- No sensitive data exposure
- No user data collection (beyond Google Analytics)

**Use Cases:**

- Simple marketing landing pages
- Product information websites
- Service description pages
- Static content delivery
- Multi-language informational sites
- Websites without user authentication requirements
"""

# Initialize Django settings if running standalone (e.g., in Thonny IDE)
# This allows the module to be imported for inspection without running Django server
import sys
import os
if __name__ == "__main__" or 'django.conf.settings' not in sys.modules or not hasattr(sys.modules.get('django.conf.settings', None), 'configured'):
    import django
    from django.conf import settings
    if not settings.configured:
        # Add the project root to Python path
        project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        if project_root not in sys.path:
            sys.path.insert(0, project_root)
        # Set the Django settings module
        os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings.settings')
        # Setup Django
        django.setup()

from django.shortcuts  import redirect, render
from django.urls       import reverse
from django.conf       import settings
from django.utils      import translation
from django.http       import HttpResponseRedirect


# get the Google Measurement ID from Django’s "settings.py"
google_measurement_id = getattr(settings, "GOOGLE_MEASUREMENT_ID", None)
# get the installed languages from Django’s "settings.py"
installed_languages = getattr(settings, "LANGUAGES", None)



[docs] def home(request): """ Prepare the home page. """ if request.method == "GET": context = {'google_measurement_id': google_measurement_id, 'installed_languages': installed_languages,} return render(request, "home.html", context) elif request.method == "POST": context = {'google_measurement_id': google_measurement_id, 'installed_languages': installed_languages,} return render(request, "home.html", context)
[docs] def set_language(request): """ Handle language selection by the user and set the appropriate language cookie. """ # Get the URL to redirect to after language is set (default to '/') next_url = request.POST.get('next', '/') # Get the language code from POST data lang_code = request.POST.get('language') # If the language code is valid (exists in settings.LANGUAGES) if lang_code and lang_code in dict(settings.LANGUAGES): # Prepare the redirect response response = HttpResponseRedirect(next_url) # Set the language cookie manually response.set_cookie( settings.LANGUAGE_COOKIE_NAME, # usually 'django_language' lang_code, max_age = settings.LANGUAGE_COOKIE_AGE, # how long the cookie lasts (seconds) path = settings.LANGUAGE_COOKIE_PATH, # usually "/" secure = settings.LANGUAGE_COOKIE_SECURE or None, # only over HTTPS if configured ) # Activate the selected language for this request immediately translation.activate(lang_code) request.LANGUAGE_CODE = translation.get_language() return response # If invalid language code, still redirect without setting anything return HttpResponseRedirect(next_url)