Source code for django_webapps_fullstack.landing.views
"""This module implements the public-facing entry point for the dingx logistics platform. It provides thelanding 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 presentsthe value proposition of the logistics management system and guides visitors to either register a newaccount or log in to an existing one. The module supports internationalization, allowing users toselect 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 languages2. **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 platform2. **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 serverimportsysimportosif__name__=="__main__"or'django.conf.settings'notinsys.modulesornothasattr(sys.modules.get('django.conf.settings',None),'configured'):importdjangofromdjango.confimportsettingsifnotsettings.configured:# Add the project root to Python pathproject_root=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))ifproject_rootnotinsys.path:sys.path.insert(0,project_root)# Set the Django settings moduleos.environ.setdefault('DJANGO_SETTINGS_MODULE','settings.settings')# Setup Djangodjango.setup()fromdjango.shortcutsimportredirect,renderfromdjango.urlsimportreversefromdjango.confimportsettingsfromdjango.utilsimporttranslationfromdjango.httpimportHttpResponseRedirect# get the installed languages from Django’s "settings.py"installed_languages=settings.LANGUAGES
[docs]defhome(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. """ifrequest.method=="GET":context={'installed_languages':installed_languages,}returnrender(request,"home.html",context)elifrequest.method=="POST":try:ifrequest.session['partner_id']:# The user is already logged in and the# partner_id exists to manage the user's products.returnredirect(reverse("overview"))except:# User can register a new account.returnredirect(reverse("register"))
[docs]defset_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 datalang_code=request.POST.get('language')# If the language code is valid (exists in settings.LANGUAGES)iflang_codeandlang_codeindict(settings.LANGUAGES):# Prepare the redirect responseresponse=HttpResponseRedirect(next_url)# Set the language cookie manuallyresponse.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_SECUREorNone,# only over HTTPS if configured)# Activate the selected language for this request immediatelytranslation.activate(lang_code)request.LANGUAGE_CODE=translation.get_language()returnresponse# If invalid language code, still redirect without setting anythingreturnHttpResponseRedirect(next_url)