Metadata-Version: 2.4
Name: django-namespaces
Version: 0.0.21
Summary: Add namespaces to your Django requests. Helps with project isolation.
Project-URL: Changelog, https://github.com/jmitchel3/django-namespaces
Project-URL: Documentation, https://github.com/jmitchel3/django-namespaces
Project-URL: Funding, https://github.com/jmitchel3/django-namespaces
Project-URL: Repository, https://github.com/jmitchel3/django-namespaces
Project-URL: Twitter, https://twitter.com/justinmitchel
Project-URL: X, https://x.com/justinmitchel
Author-email: Justin Mitchel <justin@codingforentrepreneurs.com>
License: MIT
Keywords: Django
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: Django :: 4.2
Classifier: Framework :: Django :: 5.0
Classifier: Framework :: Django :: 5.1
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Natural Language :: English
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: Implementation :: CPython
Requires-Python: >=3.10
Requires-Dist: django>=4.2
Requires-Dist: requests>=2.30
Requires-Dist: swapper>=1.4.0
Description-Content-Type: text/markdown

# django-namespaces

Use namespaces in requests using Django.


## Motivation

With _django-namespaces_, we get a new way to group resources based on `request.namespace`.

## Installation

Use a virtual environment whenever using Python packages. The built-in [venv](https://docs.python.org/3/library/venv.html) module is great.
```
python3 -m venv venv
source venv/bin/activate
$(venv) python -m pip install django-namespaces --upgrade
```

### Django Settings (`settings.py`)


### Installed Apps

Add `django_namespaces` to `INSTALLED_APPS`:
```python
INSTALLED_APPS = [
    # ...
    "django_namespaces",
]
```

### Middleware

Update `MIDDLEWARE`:

```python
MIDDLEWARE = [
    # ...
    "django_namespaces.middleware.NamespaceMiddleware",
]
```

This gives us access to the `request.namespace` object in our views.

### Template Context Processors

Add `django_namespaces.context_processors.user_namespaces` to `TEMPLATE_CONTEXT_PROCESSORS`:

```python
TEMPLATE_CONTEXT_PROCESSORS = [
    # ...
    "django_namespaces.context_processors.user_namespaces",
]
```

## Usage Usage

```python
from django.contrib.auth import get_user_model
from django_namespaces.models import Namespace

User = get_user_model()
user = User.objects.create_user(username="jon.snow", password="youknowsomething")

namespace = Namespace.objects.create(handle="winterfell", user=user)
namespace2 = Namespace.objects.create(handle="thewall", user=user)
```

```python
import django_namespaces

django_namespaces.activate("winterfell")
```
This will add a namespace to the request object.

```python
def my_hello_world_view(request):
    print(request.namespace)  # <Namespace: winterfell>
    print(request.namespace.handle)  # winterfell
    return HttpResponse("Hello World")
```

```python
from django.db import models
from django_namespaces.models import Namespace


class Location(models.Model):
    name = models.CharField(max_length=255, blank=True, null=True)
    namespace = models.ForeignKey(Namespace, on_delete=models.CASCADE)
```

### Optional Views
Using views are optional. You can also use the `activate` function to activate a namespace.

#### Update URLconf
Update `urls.py` to include `namespaces.urls`:
```python
urlpatterns = [
    # ...
    path("namespaces/", include("django_namespaces.urls")),
]
```


#### Create a Namespace
Create a namespace by visiting `http://localhost:8000/namespaces/create/` and filling out the form.


#### Activate a Namespace
Activate a namespace by visiting `http://localhost:8000/namespaces/` and hitting `activate` on your newly created namespace.

You can also use:


#### Update URLconf
