WETAccel: Enterprise-Grade Django Template for Government of Canada’s Web Initiatives

Table of Contents

WETAccel View Documentation

landing(request)

Overview:

The landing function is designed to handle requests to the main landing page of the website. When a user accesses this page, they are immediately redirected to the English version of the site.

Parameters:

  • request: The HttpRequest object containing metadata about the server request. This is a standard parameter for any Django view function.

Returns:

  • HttpResponse object to redirect the user to the English version of the site (/en/).

Description:

When a user accesses the main landing page, this function is triggered. Its primary action is to redirect the user to the English version of the site (/en/). This could be useful in cases where the main landing page does not have content by itself, but you want to redirect users to a specific language version (in this case English) by default.

Example:

If a user accesses https://example.com/, they will be redirected to https://example.com/en/ because of the action in this function.

index(request, lang)

Overview:

The index function handles the rendering of the homepage content based on the specified language. The function is designed to offer a localized user experience by presenting the appropriate version of the homepage depending on the language code provided in the URL.

Parameters:

  • request: The HttpRequest object containing metadata about the client’s server request.
  • lang: A string parameter representing the desired language code (e.g., “en” for English). It determines which localized version of the homepage is to be rendered.

Returns:

  • HttpResponse object with the rendered homepage content in the chosen language.

Description:

When users access the site with a specific language code in the path (e.g., /en/), the index function serves the homepage content tailored for that language. The function populates the context with relevant data, including the language, page title, and breadcrumbs. It then uses Django’s render function to present the corresponding language-specific HTML template for the homepage.

The HTML template’s path is dynamically constructed using the language code, allowing the site to have separate templates for each language version of the homepage.

Example:

If a user accesses https://example.com/en/, the index function will render the English version of the homepage using the template located at en/pages/home/index.html, with the breadcrumbs pointing to the English home.

Similarly, if a user visits https://example.com/fr/, the function will use the fr/pages/home/index.html template to present the homepage in French.”

search(request, lang)

Overview:

The search function handles the rendering of the search page content based on the specified language. The function provides a localized user experience by displaying the appropriate version of the search page depending on the language code provided in the URL.

Parameters:

  • request: The HttpRequest object containing metadata about the client’s server request.
  • lang: A string parameter representing the desired language code (e.g., “en” for English). It determines which localized version of the search page is to be rendered.

Returns:

  • HttpResponse object with the rendered search page content in the chosen language.

Description:

When users access the site with a specific language code followed by /search in the path (e.g., /en/search), the search function serves the search page content tailored for that language. The function populates the context with relevant data, including the language, page title, and breadcrumbs. It then uses Django’s render function to present the corresponding language-specific HTML template for the search page.

The HTML template’s path is dynamically constructed using the language code, which allows for separate templates for each language version of the search page.

Example:

If a user accesses https://example.com/en/search, the search function will render the English version of the search page using the template located at en/pages/search/index.html, with breadcrumbs pointing to the English home and the search page itself.

Likewise, if a user visits https://example.com/fr/search, the function will use the fr/pages/search/index.html template to present the search page in French.”

privacy(request, lang)

Overview:

The privacy function is responsible for rendering the privacy policy page tailored to a specific language. This ensures users can access the privacy policy in their preferred language, making the content more accessible and user-friendly.

Parameters:

  • request: The HttpRequest object which encapsulates metadata about the client’s server request.
  • lang: A string parameter indicating the desired language code (e.g., “en” for English). This determines which localized version of the privacy policy page is to be displayed.

Returns:

  • HttpResponse object containing the rendered privacy policy page content in the selected language.

Description:

When users navigate to the privacy policy page with a specific language code in the URL (e.g., /en/privacy), the privacy function serves the content tailored to that language. The function sets up the context with necessary data, such as the language, page title, and breadcrumbs. Subsequently, Django’s render method is used to present the corresponding localized HTML template for the privacy policy page.

The path for the HTML template is dynamically constructed using the provided language code, allowing for distinct templates for each language version of the privacy policy page.

Example:

If a user navigates to https://example.com/en/privacy, the privacy function will display the English version of the privacy policy page, sourcing the template found at en/pages/privacy/index.html. The breadcrumbs will direct users to the English home page and the English version of the privacy policy.

Similarly, if a user visits https://example.com/fr/privacy, the function will serve the privacy policy page in French using the template located at fr/pages/privacy/index.html.”

login_view(request, lang)

Overview:

The login_view function handles the login process for users, presenting them with a login page tailored to their chosen language. It supports both rendering the login page (on a GET request) and processing the form submission (on a POST request).

Parameters:

  • request: The HttpRequest object which contains all the details of the client’s server request, including any POST data for form submissions.
  • lang: A string parameter indicating the desired language code (e.g., “en” for English). This determines the language of the displayed login page and any error messages.

Returns:

  • HttpResponse object containing the rendered login page content, a redirection to the dashboard upon successful login, or an error message upon unsuccessful login.

Description:

The function behavior varies depending on the HTTP request method:

  • GET: The function renders the login page in the specified language using the provided context data. This ensures the user sees a login form tailored to their language preference.
  • POST: This request method indicates that the user has submitted the login form. The function then attempts to authenticate the user based on the provided username and password. If the authentication succeeds, the user is logged in and redirected to their dashboard in the selected language. If the authentication fails, an error message is displayed on the login page, notifying the user of the incorrect credentials.

If neither GET nor POST request methods are used, the function returns a “BAD REQUEST” response.

Example:

  • A user navigates to https://example.com/en/login and is presented with an English version of the login page.
  • If the user submits valid credentials, they are redirected to https://example.com/en/dashboard.
  • If the user submits invalid credentials, they remain on the English login page with an error message indicating the invalid credentials.

Similarly, for a French user navigating to https://example.com/fr/login, all interactions will be presented in French.

Important Notes:

Ensure that your site uses HTTPS, especially for authentication purposes. This ensures that user data, like usernames and passwords, are transmitted securely.”

register(request, lang, step)

Overview:

The register function facilitates the user registration process, which is divided into multiple steps. Depending on the step, it presents the user with the corresponding registration page tailored to their chosen language and validates the entered information.

Parameters:

  • request: The HttpRequest object, which contains all details of the client’s server request, including any POST data for form submissions.
  • lang: A string parameter representing the desired language code (e.g., “en” for English). It determines the language of the displayed registration page and any error or success messages.
  • step: A string parameter representing the current registration step. The supported steps are “username”, “password”, and “complete”.

Returns:

  • HttpResponse object containing the rendered registration page content or a redirection to the login page upon successful registration.

Description:

The function’s behavior is driven by the step parameter:

  • “username”: Renders the username step of the registration process, allowing users to select their username, first name, last name, and email.
  • “password”: Handles the creation of the user with the provided details from the previous step and prompts users to set their password. It validates the username and performs checks to ensure it’s unique, alphanumeric, and within the specified length constraints. The user’s details are saved to the database at this step.
  • “complete”: Validates the provided password, checks its length, and ensures it matches the password confirmation field. On successful validation, the user’s password is saved to the database, and the user is redirected to the login page with a success message.

Error Handling:

The function contains numerous error checks, such as validating the length and format of the username and password. If any validation fails, an error message is displayed, guiding the user on the necessary corrections.

Example:

  • A user navigates to https://example.com/en/registration/username and is presented with the username registration step in English.
  • Upon successful submission of valid username details, the user progresses to https://example.com/en/registration/password to set their password.
  • Once the password is correctly set and confirmed, the user completes the registration process and is redirected to the login page.

Important Notes:

Ensure that your site uses HTTPS, especially for registration and authentication purposes. Securely handling and transmitting user credentials is of paramount importance.

Be cautious about potential database-related issues. For instance, while this function checks for username uniqueness, race conditions can arise if two users attempt to register with the same username simultaneously.”

dashboard_home(request, lang)

Overview:

The dashboard_home function provides users access to the main dashboard page of the website in their preferred language. This function serves as an entry point to the user’s dashboard, where they can see relevant content and interact with available functionalities.

Parameters:

  • request: The HttpRequest object, which encapsulates all metadata related to the client’s server request.
  • lang: A string parameter representing the desired language code (e.g., “en” for English). It determines the language of the dashboard page and the associated content.

Returns:

  • HttpResponse object containing the rendered dashboard home page tailored to the specified language.

Description:

When a user navigates to the dashboard page, this function is invoked. It creates a context dictionary containing the user’s preferred language, the page title, and a breadcrumb navigation trail. This context is then used to render the dashboard home page in the user’s chosen language.

Example:

If a user with English preference accesses https://example.com/en/dashboard, they are presented with the dashboard’s home page in English, showing the breadcrumbs: “Home > Dashboard”.

Notes:

It’s crucial to ensure that users have the necessary permissions to access the dashboard. The function, as presented, doesn’t include any authentication or authorization checks, which may need to be implemented depending on the application’s security requirements.”

dashboard_table(request, lang)

Overview:

The dashboard_table function provides users with access to the “Table Example” page within the dashboard. This page showcases a sample table structure or data representation suitable for users to understand or interact with.

Parameters:

  • request: The HttpRequest object, containing all metadata about the client’s server request.
  • lang: A string parameter representing the desired language code (e.g., “en” for English). It determines the language of the “Table Example” page and its associated content.

Returns:

  • HttpResponse object containing the rendered “Table Example” page specific to the chosen language.

Description:

When a user accesses the “Table Example” page from their dashboard, this function gets triggered. It creates a context dictionary that encapsulates the user’s preferred language, the page’s title, and a breadcrumb trail for easier navigation. The context is then used to render the “Table Example” page in the user’s specified language.

Example:

If a user with a preference for English navigates to https://example.com/en/dashboard/table, they will be presented with a sample table in English, and the breadcrumbs would display: “Home > Dashboard > Table Example”.

Notes:

As this page is part of the dashboard, it’s recommended to have necessary authentication and authorization mechanisms in place to ensure only authorized users can access this page. The given function does not include these checks, so they may need to be incorporated based on the security requirements of the application.”

dashboard_form(request, lang)**

Overview:

The dashboard_form function gives users access to the “Form Example” page within the dashboard. This page presumably demonstrates a sample form or user input mechanism that offers a clear visual and interactive experience for users.

Parameters:

  • request: The HttpRequest object, which contains metadata about the client’s server request.
  • lang: A string parameter representing the selected language code (e.g., “en” for English). It determines the language of the “Form Example” page and its content.

Returns:

  • HttpResponse object with the rendered “Form Example” page tailored to the chosen language.

Description:

When a user decides to view the “Form Example” page from the dashboard, this function is activated. It constructs a context dictionary, which contains the user’s preferred language, the title of the page, and a breadcrumb trail for enhanced navigation. The context is then employed to present the “Form Example” page in the language specified by the user.

Example:

If an English-speaking user accesses https://example.com/en/dashboard/form, they will see a sample form displayed in English, and the breadcrumbs will indicate: “Home > Dashboard > Form Example”.

Notes:

As this page is a segment of the dashboard, implementing the necessary authentication and authorization measures is crucial to ensure that only privileged users can access this page. The presented function does not encompass these verifications, so they might need integration based on the security requisites of the application.”

dashboard_profile(request, lang)

Overview:

The dashboard_profile function facilitates user interaction with the “Profile” page inside the dashboard. On this page, users can view and edit their profile details, including first name, last name, email, and password.

Parameters:

  • request: The HttpRequest object, which includes metadata about the client’s server request.
  • lang: A string parameter representing the language code (e.g., “en” for English). It dictates the language of the “Profile” page and its contents.

Returns:

  • HttpResponse object that showcases the rendered “Profile” page in the user-selected language, potentially displaying messages related to successful updates or errors.

Description:

When users navigate to the “Profile” page on the dashboard, this function is invoked. A context dictionary is initialized, containing the user’s chosen language, the page’s title, and a breadcrumb path. If the user submits changes to their profile details (via POST request), these details are updated in the database. Specific validations are also performed when updating the password.

Example:

If a user accesses https://example.com/en/dashboard/profile and attempts to modify their password but provides mismatched passwords, they will receive a message indicating “Passwords do not match.”

Notes:

  • Security: Ensuring that only authenticated users can view and modify their profile details is paramount. The described function does not encompass such verifications, so they might be elsewhere in the application or require implementation.
  • Feedback: Messages are presented to inform the user about successful updates or any encountered errors.
  • Data Consistency: The function fetches the currently logged-in user’s username from the request, making sure updates are applied to the right user.
  • Password Handling: The function contains a robust validation process for passwords, ensuring they meet length requirements and that they match before saving.”

logout_view(request, lang)

Overview:

The logout_view function is responsible for handling user logout operations from the application. Once triggered, it logs out the user and displays a dedicated “Logout” page.

Parameters:

  • request: The HttpRequest object, containing metadata about the client’s server request.
  • lang: A string parameter representing the language code (e.g., “en” for English). This dictates the language of the “Logout” page and its contents.

Returns:

  • HttpResponse object rendering the “Logout” page in the specified language, which typically would display a message confirming the user has been logged out or provide options for re-login.

Description:

This function is designed to cater to users who wish to log out of the application. Upon invoking this function, the user’s session is terminated, and they are presented with the “Logout” page. A context dictionary is constructed, which includes the selected language, page title, and a breadcrumb path.

Example:

If a user selects to log out and accesses https://example.com/en/logout, they will be logged out and directed to the logout confirmation page rendered in English.

Notes:

  • Security: This function leverages Django’s built-in logout method, ensuring the user’s session is correctly terminated.
  • User Feedback: After logout, it is usually good practice to inform users they have been successfully logged out and possibly provide them with options to re-login or navigate elsewhere in the application.
  • Language Consideration: The function accommodates multiple languages, tailoring the logout page experience to the user’s preferred language.”

forgot_password(request, lang)

Overview:

The forgot_password function handles the process of password recovery for users who have forgotten their passwords. When provided a username, the function will check its existence, and if valid, will send an email to the user with a password reset link.

Parameters:

  • request: The HttpRequest object, which contains metadata about the client’s server request.
  • lang: A string parameter representing the language code (e.g., “en” for English). This determines the language of the “Forgot Password” page and its contents.

Returns:

  • HttpResponse object rendering the “Forgot Password” page in the specified language. Depending on the flow, it may display an input field for the username or messages related to the process.

Description:

Upon loading the function (GET request), users are presented with a form to enter their username. If the form is submitted (POST request):

  1. The function verifies if the provided username exists.
  2. If the username doesn’t exist, an error message is displayed.
  3. If the username exists, a password reset link is generated.
  4. An email is sent to the associated email address containing the reset link.
  5. The user receives feedback about the sent email, and they are then redirected to the login page.

Notes:

  • Email Content: The function uses both plain text and HTML email templates to send the password reset link. The HTML content is fetched from "pages/auth/email/password-reset.html".
  • Validation: The function checks for the presence and validity of the provided username. Proper feedback is given using Django’s messaging framework.
  • Security: Always ensure that the email system is secure, and consider using tokens or cryptographic methods in the password reset link to enhance security.
  • Language Consideration: The function accommodates multiple languages, tailoring the “Forgot Password” experience to the user’s selected language.

Example:

If a user forgets their password and navigates to https://example.com/en/forgot-password, they will enter their username, and if valid, receive an email with instructions on resetting their password.

reset_password(request, lang)

Overview:

The reset_password function enables users to reset their passwords. Typically, this function is triggered through a link in a password reset email. It provides a form where the user can enter and confirm their new password.

Parameters:

  • request: The HttpRequest object, which contains metadata about the client’s server request.
  • lang: A string parameter representing the language code (e.g., “en” for English). This determines the language of the “Reset Password” page and its contents.

Returns:

  • HttpResponse object rendering the “Reset Password” page in the specified language. Depending on the flow, it may display an input field for the new password, messages related to the process, or redirect to the login page.

Description:

Upon loading the function (GET request):

  1. The function expects a username parameter in the query string.
  2. If the username parameter is missing or invalid, an error message is displayed.

Upon form submission (POST request):

  1. The function retrieves the new password and its confirmation from the form.
  2. The function checks for password match and length constraints.
  3. If all checks pass, the function tries to fetch the user by the provided username.
  4. If the user is found, their password is updated, and a success message is displayed.
  5. The user is redirected to the login page.

Notes:

  • Validation: The function checks for the presence and validity of the provided password and username. Proper feedback is given using Django’s messaging framework.
  • Password Constraints: Passwords must be between 8 and 20 characters.
  • Language Consideration: The function accommodates multiple languages, tailoring the “Reset Password” experience to the user’s selected language.
  • Error Handling: The function uses a try-except block to handle potential exceptions when fetching the user by username. If an exception occurs, it is inferred that the user doesn’t exist, and an error message is displayed.

Example:

If a user clicks on a password reset link like https://example.com/en/reset-password?username=johndoe, they will be taken to a page where they can set their new password. Upon successful submission, they are redirected to the login page.