django-namespaces


Namedjango-namespaces JSON
Version 0.0.25 PyPI version JSON
download
home_pageNone
SummaryAdd namespaces to your Django requests. Helps with project isolation.
upload_time2025-01-15 20:06:20
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseMIT
keywords django
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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)
```

### Custom Namespace Model

If you want to use a custom namespace model, you can do so by setting the `DJANGO_NAMESPACES_NAMESPACE_MODEL` setting.

```python
DJANGO_NAMESPACES_NAMESPACE_MODEL = "orgs.Organization"
```
Example model in `orgs.models.py`:
```python
from django.db import models


class Organization(models.Model):
    handle = models.CharField(max_length=255, unique=True)
```
- `handle` is the only required field to swap the model.

### 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

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "django-namespaces",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "Django",
    "author": null,
    "author_email": "Justin Mitchel <justin@codingforentrepreneurs.com>",
    "download_url": "https://files.pythonhosted.org/packages/17/16/307db30e1e852fb4185af5906e8335c095c22328ce3cf3bf97b85dbdf3c8/django_namespaces-0.0.25.tar.gz",
    "platform": null,
    "description": "# django-namespaces\n\nUse namespaces in requests using Django.\n\n\n## Motivation\n\nWith _django-namespaces_, we get a new way to group resources based on `request.namespace`.\n\n## Installation\n\nUse a virtual environment whenever using Python packages. The built-in [venv](https://docs.python.org/3/library/venv.html) module is great.\n```\npython3 -m venv venv\nsource venv/bin/activate\n$(venv) python -m pip install django-namespaces --upgrade\n```\n\n### Django Settings (`settings.py`)\n\n\n### Installed Apps\n\nAdd `django_namespaces` to `INSTALLED_APPS`:\n```python\nINSTALLED_APPS = [\n    # ...\n    \"django_namespaces\",\n]\n```\n\n### Middleware\n\nUpdate `MIDDLEWARE`:\n\n```python\nMIDDLEWARE = [\n    # ...\n    \"django_namespaces.middleware.NamespaceMiddleware\",\n]\n```\n\nThis gives us access to the `request.namespace` object in our views.\n\n### Template Context Processors\n\nAdd `django_namespaces.context_processors.user_namespaces` to `TEMPLATE_CONTEXT_PROCESSORS`:\n\n```python\nTEMPLATE_CONTEXT_PROCESSORS = [\n    # ...\n    \"django_namespaces.context_processors.user_namespaces\",\n]\n```\n\n## Usage Usage\n\n```python\nfrom django.contrib.auth import get_user_model\nfrom django_namespaces.models import Namespace\n\nUser = get_user_model()\nuser = User.objects.create_user(username=\"jon.snow\", password=\"youknowsomething\")\n\nnamespace = Namespace.objects.create(handle=\"winterfell\", user=user)\nnamespace2 = Namespace.objects.create(handle=\"thewall\", user=user)\n```\n\n```python\nimport django_namespaces\n\ndjango_namespaces.activate(\"winterfell\")\n```\nThis will add a namespace to the request object.\n\n```python\ndef my_hello_world_view(request):\n    print(request.namespace)  # <Namespace: winterfell>\n    print(request.namespace.handle)  # winterfell\n    return HttpResponse(\"Hello World\")\n```\n\n```python\nfrom django.db import models\nfrom django_namespaces.models import Namespace\n\n\nclass Location(models.Model):\n    name = models.CharField(max_length=255, blank=True, null=True)\n    namespace = models.ForeignKey(Namespace, on_delete=models.CASCADE)\n```\n\n### Custom Namespace Model\n\nIf you want to use a custom namespace model, you can do so by setting the `DJANGO_NAMESPACES_NAMESPACE_MODEL` setting.\n\n```python\nDJANGO_NAMESPACES_NAMESPACE_MODEL = \"orgs.Organization\"\n```\nExample model in `orgs.models.py`:\n```python\nfrom django.db import models\n\n\nclass Organization(models.Model):\n    handle = models.CharField(max_length=255, unique=True)\n```\n- `handle` is the only required field to swap the model.\n\n### Optional Views\nUsing views are optional. You can also use the `activate` function to activate a namespace.\n\n#### Update URLconf\nUpdate `urls.py` to include `namespaces.urls`:\n```python\nurlpatterns = [\n    # ...\n    path(\"namespaces/\", include(\"django_namespaces.urls\")),\n]\n```\n\n\n#### Create a Namespace\nCreate a namespace by visiting `http://localhost:8000/namespaces/create/` and filling out the form.\n\n\n#### Activate a Namespace\nActivate a namespace by visiting `http://localhost:8000/namespaces/` and hitting `activate` on your newly created namespace.\n\nYou can also use:\n\n\n#### Update URLconf\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Add namespaces to your Django requests. Helps with project isolation.",
    "version": "0.0.25",
    "project_urls": {
        "Changelog": "https://github.com/jmitchel3/django-namespaces",
        "Documentation": "https://github.com/jmitchel3/django-namespaces",
        "Funding": "https://github.com/jmitchel3/django-namespaces",
        "Repository": "https://github.com/jmitchel3/django-namespaces",
        "Twitter": "https://twitter.com/justinmitchel",
        "X": "https://x.com/justinmitchel"
    },
    "split_keywords": [
        "django"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a5b124a8caeb7daf24c6f790272aeb05cc55a9b8259c2f169eb3211f4b2332cb",
                "md5": "76e54c1389559859f0d78ac9d4c940ad",
                "sha256": "a7497dd499fe6b96d4fc2821d761a0cb9235bb57cb918d8f915f6f654ec9a35e"
            },
            "downloads": -1,
            "filename": "django_namespaces-0.0.25-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "76e54c1389559859f0d78ac9d4c940ad",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 21681,
            "upload_time": "2025-01-15T20:06:17",
            "upload_time_iso_8601": "2025-01-15T20:06:17.658652Z",
            "url": "https://files.pythonhosted.org/packages/a5/b1/24a8caeb7daf24c6f790272aeb05cc55a9b8259c2f169eb3211f4b2332cb/django_namespaces-0.0.25-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1716307db30e1e852fb4185af5906e8335c095c22328ce3cf3bf97b85dbdf3c8",
                "md5": "a82b477f985d5cb433a84319ef2b016e",
                "sha256": "b1796d983d24fd85429536c5baecc83f0a25368ab8fc5cedf38d5323f10ebe1f"
            },
            "downloads": -1,
            "filename": "django_namespaces-0.0.25.tar.gz",
            "has_sig": false,
            "md5_digest": "a82b477f985d5cb433a84319ef2b016e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 37840,
            "upload_time": "2025-01-15T20:06:20",
            "upload_time_iso_8601": "2025-01-15T20:06:20.244467Z",
            "url": "https://files.pythonhosted.org/packages/17/16/307db30e1e852fb4185af5906e8335c095c22328ce3cf3bf97b85dbdf3c8/django_namespaces-0.0.25.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-15 20:06:20",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jmitchel3",
    "github_project": "django-namespaces",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "django-namespaces"
}
        
Elapsed time: 1.51525s