# 54. Creating a team and a page to manage the store

We will now implement functionality to create multiple user access levels for the website.&#x20;

In the admin panel's "Users" tab, each user is listed with their staff status and associated permissions.

<figure><img src="https://1987892552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBsBTgwgH8PgXSXzgW9sj%2Fuploads%2FS3ZZgPwNHb4IGAFGREIo%2Fimage.png?alt=media&#x26;token=b663ca5d-b86e-4456-a4dd-fccc060242bd" alt=""><figcaption><p>User list with corresponding status.</p></figcaption></figure>

<figure><img src="https://1987892552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBsBTgwgH8PgXSXzgW9sj%2Fuploads%2Ftrg040Xk4UYyTlDKf5QS%2Fimage.png?alt=media&#x26;token=a3d450d1-ed8c-4953-bf09-4707807c387e" alt=""><figcaption><p>The permissions of each user located at the bottom of their page</p></figcaption></figure>

Additionally, in the left column of the admin page, there is an option labeled "Groups."

<figure><img src="https://1987892552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBsBTgwgH8PgXSXzgW9sj%2Fuploads%2FBnLFnG5KcauIZstUMjTU%2Fimage.png?alt=media&#x26;token=3edddfc0-7c99-42c9-9dce-941c5f26ad1e" alt=""><figcaption></figcaption></figure>

Click the "Add" button in the "Groups" row and create a group named **Team**, as shown below:\


<figure><img src="https://1987892552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBsBTgwgH8PgXSXzgW9sj%2Fuploads%2FIO8VfV3KZZxrvqccchzj%2Fimage.png?alt=media&#x26;token=8cc920f3-d3d6-40d8-baa3-e539dc0b818e" alt=""><figcaption></figcaption></figure>

Now, in the "Users" tab, select a specific user and navigate to their "Group" section. You can assign the **Team** group to that user.

<figure><img src="https://1987892552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBsBTgwgH8PgXSXzgW9sj%2Fuploads%2FOfuUcSwFjmGh5c8KdGjU%2Fimage.png?alt=media&#x26;token=3cfa384d-9bb7-4bc3-b6b9-55deba046f61" alt=""><figcaption></figcaption></figure>

**Important note: Remember to click the save button in the bottom of the admin page everytime you perform a change.**

We can use these groups to create specific pages on the website.&#x20;

To achieve this, we'll define a function inside the `new_context.py` file to check if a user belongs to a specific group. This function is placed in `new_context.py` since it will be used across all pages in the navbar.

{% code title="new_context.py" overflow="wrap" lineNumbers="true" %}
```python
def is_part_of_team(request) :
    team = False
    if request.user.is_authenticated:
        if request.user.groups.filter(name="Team").exists(): #? if the user is authenticated and the group Team is created (parameters obtained from admin page)
            team = True
    return {"Team" : team}
```
{% endcode %}

Here’s a explanation of the code:

* **Function Name**: `is_part_of_team`
* **Parameters**: Accepts a `request` object, typically provided by Django during an HTTP request.
* **Purpose**: Checks if the currently authenticated user is part of the group named "Team."
* **Logic Flow**:
  * Initializes a variable `team` to `False`.
  * Verifies if the user is authenticated by checking `request.user.is_authenticated`.
  * If the user is authenticated, it checks if the user belongs to a group named "Team" using `request.user.groups.filter(name="Team").exists()`.
  * Sets `team` to `True` if the user is part of the "Team" group.
* **Return Value**: Returns a dictionary with the key `"Team"` and the boolean value of `team`.

Since we created a `new_context` function that will be available across all pages, add a reference to this function in the `settings.py` file at the bottom of the `TEMPLATES` list, [as previously done.](../cart-functionalities/15.-post-method-on-cart/15.2.-personalized-context-variable)

{% code title="settings.py" overflow="wrap" lineNumbers="true" %}
```python
'store.new_context.is_part_of_team',
            ],
        },
```
{% endcode %}

Now, in the `navbar.html` file, we will check if the user belongs to the Team group using the context function we created. If the user is part of the group, a link to the administration page will be displayed.&#x20;

This verification should be added right after the existing check for user authentication:

{% code title="navbar.html" overflow="wrap" lineNumbers="true" %}
```html
{% if request.user.is_authenticated %}
        <a href = "{% url 'your_account' %}">My Account</a>
    {% else %}
        <a href = "{% url 'perform_login' %}">Login</a>
    {% endif %}
    
    {% if Team %}
        <a href="{% url 'manage_store' %}">Manage Store</a>
    {% endif %}    
```
{% endcode %}

To create a new view page, add the corresponding path to the `urls.py` file in the store folder.

{% code title="store\urls.py" overflow="wrap" lineNumbers="true" %}
```python
path('managestore/', manage_store, name="manage_store"),
```
{% endcode %}

**Note:** You may arrange the views in any order you prefer. However, to prevent issues, follow the sequence provided in the source code on the GitHub repository.

Next, add a view function named `manage_store` at the end of the `views.py` file.

{% code title="views.py" overflow="wrap" lineNumbers="true" %}
```python
@login_required
def manage_store(request):
    if request.user.groups.filter(name="Team").exists():
        return render(request,"internal/manage_store.html")
    else:
        redirect('store')
```
{% endcode %}

Inside the function we added the filter so that when a user that is not inside the Team group enters the manage store option, he will instead be redirected to the store.

Within the `templates` folder, create a subfolder named `internal` to house all internal HTML files used for website management.&#x20;

Finally, within the `internal` folder, create the `manage_store.html` file.

{% code title="internal\manage_store.html" overflow="wrap" lineNumbers="true" %}
```html
{% extends 'base.html' %}
{% load static %}

{% block body %}

<h3>
    Manage store
</h3>

{% endblock %}
```
{% endcode %}

When accessing the website with an account in the Team group, the "Manage Store" option will appear in the navbar. Clicking this option will redirect you to the `manage_store.html` page.

<figure><img src="https://1987892552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBsBTgwgH8PgXSXzgW9sj%2Fuploads%2FQjzx18GJxl9USXreos4S%2Fimage.png?alt=media&#x26;token=818c69ff-dc60-499d-b1c7-4fc874714f29" alt=""><figcaption></figcaption></figure>
