Source code for django_webapps_fullstack.landing.views

"""
This module implements the public-facing entry point for the dingx logistics platform. It provides the
landing page for new visitors and handles multi-language support through language selection functionality.
The landing page serves as the gateway to user registration and login.

**Business Context:**

The landing app is the first point of contact for potential users of the dingx platform. It presents
the value proposition of the logistics management system and guides visitors to either register a new
account or log in to an existing one. The module supports internationalization, allowing users to
select their preferred language before registration.

**Key Business Workflows:**

1. **Landing Page:**

- :func:`home`: First page visitors see when accessing the platform
    - Presents dingx logistics service information
    - Provides call-to-action for registration
    - Automatically redirects logged-in users to their dashboard
    - Available in multiple languages

2. **Language Selection:**

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

**User Journey:**

1. **New Visitor:**
   - Arrives at home page
   - Selects preferred language (optional)
   - Clicks to register → Redirected to account/register
   - Creates account and starts using platform

2. **Returning User:**
   - Arrives at home page
   - Already logged in → Automatically redirected to dashboard overview
   - Not logged in → Can select language and navigate to login

**Technical Features:**

- Language cookie management with Django settings
- Session detection for logged-in users
- Automatic redirection based on authentication state
- Support for multiple languages via Django i18n
- Clean separation of public and authenticated areas

**Integration:**

The landing app integrates with:
- Account app for registration and login
- Dashboard app for logged-in user redirection
- Django's internationalization framework for multi-language support

**Security:**

- No authentication required (public access)
- Safe handling of invalid language codes
- No sensitive data exposure
"""

# 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 installed languages from Django’s "settings.py"
installed_languages = settings.LANGUAGES


[docs] def home(request): """ Prepare the home page. After "submit" the page, the user will will redirected to the "overview" page of the dashboard, if the user is already logged in. Otherwise a new account could be registered. """ if request.method == "GET": context = { 'installed_languages': installed_languages, } return render(request, "home.html", context) elif request.method == "POST": try: if request.session['partner_id']: # The user is already logged in and the # partner_id exists to manage the user's products. return redirect(reverse("overview")) except: # User can register a new account. return redirect(reverse("register"))
[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)