Skip to content

Commit

Permalink
Squashed migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
jmitchel3 committed Jan 13, 2025
1 parent 518508d commit 95ec54c
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 153 deletions.
5 changes: 3 additions & 2 deletions sample_project/example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""
from __future__ import annotations

from pathlib import Path

Expand Down Expand Up @@ -51,7 +52,7 @@
"django_namespaces.middleware.NamespaceMiddleware",
]

# DJANGO_NAMESPACE_BLOCKED_LIST = 'example.blocked.blocked_namespaces'
# DJANGO_NAMESPACE_BLOCKED_LIST_LOCATION = 'example.blocked.blocked_namespaces'

ROOT_URLCONF = "example.urls"

Expand All @@ -66,7 +67,7 @@
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"django_namespaces.context_processors.user_namespaces_ctx"
"django_namespaces.context_processors.user_namespaces_ctx",
],
},
},
Expand Down
9 changes: 5 additions & 4 deletions src/django_namespaces/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ def DJANGO_NAMESPACE_MODEL(self) -> str:
)

@property
def DJANGO_NAMESPACE_BLOCKED_LIST(self) -> int:
def DJANGO_NAMESPACE_BLOCKED_LIST_LOCATION(self) -> str:
"""
A list of namespaces that are not allowed to be created
Dot notation for the location of the list of namespaces
that are not allowed to be created.
"""
return getattr(
django_settings,
"DJANGO_NAMESPACE_BLOCKED_LIST",
"DJANGO_NAMESPACE_BLOCKED_LIST_LOCATION",
"django_namespaces.blocked.blocked_namespaces",
)

Expand All @@ -44,7 +45,7 @@ def DJANGO_NAMESPACE_NEEDS_ACTIVATION_TEMPLATE(self) -> str:
)

@property
def DJANGO_NAMESPACE_MAX_HANDLE_LENGTH(self) -> str:
def DJANGO_NAMESPACE_MAX_HANDLE_LENGTH(self) -> int:
"""
The max length of a namespace handle
"""
Expand Down
14 changes: 8 additions & 6 deletions src/django_namespaces/import_utils.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
from __future__ import annotations

import importlib
from typing import Any


def import_module_from_str(string_path):
def import_module_from_str(string_path: str) -> Any:
paths = string_path.split(".")
if len(paths) == 3:
module = importlib.import_module(f"{paths[0]}.{paths[1]}")
return getattr(module, paths[2])
elif len(paths) == 2:
module = importlib.import_module(f"{paths[0]}")
return getattr(module, paths[1])
else:
try:
module = importlib.import_module(f"{string_path}")
except Exception as E:
raise f"Failed to import {string_path} with \n:{E}"
try:
module = importlib.import_module(f"{string_path}")
except Exception as e:
raise Exception(f"Failed to import {string_path} with \n:{e}")
return module
77 changes: 77 additions & 0 deletions src/django_namespaces/migrations/0001_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Generated by Django 5.1.4 on 2025-01-13 21:28
from __future__ import annotations

import uuid

import django.db.models.deletion
from django.conf import settings
from django.db import migrations
from django.db import models

import django_namespaces.validators


class Migration(migrations.Migration):
replaces = [
("django_namespaces", "0001_initial"),
("django_namespaces", "0002_alter_namespace_options_alter_namespace_slug"),
("django_namespaces", "0003_alter_namespace_slug"),
(
"django_namespaces",
"0004_alter_namespace_options_rename_slug_namespace_handle",
),
("django_namespaces", "0005_namespace_default"),
("django_namespaces", "0006_alter_namespace_default"),
]

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="Namespace",
fields=[
(
"id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
),
),
(
"handle",
models.SlugField(
help_text="Namespaces must be unique across this website.",
unique=True,
validators=[django_namespaces.validators.valid_project_id],
),
),
("title", models.CharField(blank=True, max_length=255, null=True)),
("description", models.TextField(blank=True, null=True)),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
(
"default",
models.BooleanField(
default=True,
help_text="Default namespace for this user (only one possible)",
),
),
],
options={
"ordering": ["handle", "-updated_at", "-created_at"],
},
),
]
31 changes: 0 additions & 31 deletions src/django_namespaces/migrations/0001_initial.py

This file was deleted.

This file was deleted.

19 changes: 0 additions & 19 deletions src/django_namespaces/migrations/0003_alter_namespace_slug.py

This file was deleted.

This file was deleted.

18 changes: 0 additions & 18 deletions src/django_namespaces/migrations/0005_namespace_default.py

This file was deleted.

22 changes: 0 additions & 22 deletions src/django_namespaces/migrations/0006_alter_namespace_default.py

This file was deleted.

23 changes: 17 additions & 6 deletions src/django_namespaces/validators.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
from __future__ import annotations

from django.core.exceptions import ValidationError
from django.utils.text import slugify
from django.utils.translation import gettext as _

from django_namespaces.conf import settings as django_namespace_settings
from django_namespaces.import_utils import import_module_from_str

DJANGO_NAMESPACE_BLOCKED_LIST = django_namespace_settings.DJANGO_NAMESPACE_BLOCKED_LIST
DJANGO_NAMESPACE_BLOCKED_LIST_LOCATION = (
django_namespace_settings.DJANGO_NAMESPACE_BLOCKED_LIST_LOCATION
)


def get_blocked_list():
return import_module_from_str(DJANGO_NAMESPACE_BLOCKED_LIST)
def get_blocked_list(as_slugs: bool = False) -> list[str]:
blocked_items = import_module_from_str(DJANGO_NAMESPACE_BLOCKED_LIST_LOCATION)
if isinstance(blocked_items, list):
if as_slugs:
return [slugify(x) for x in blocked_items]
return blocked_items
raise ValueError(
f"The blocked list at {DJANGO_NAMESPACE_BLOCKED_LIST_LOCATION} is not a list"
)


def valid_project_id(value):
blocked_list = get_blocked_list()
blocked_list_as_slugs = [slugify(x) for x in blocked_list]
def valid_project_id(value: str) -> None:
blocked_list = get_blocked_list(as_slugs=False)
blocked_list_as_slugs = get_blocked_list(as_slugs=True)
if value in blocked_list:
raise ValidationError(_(f"{value} is not allowed as a namespace."))
if value in blocked_list_as_slugs:
Expand Down

0 comments on commit 95ec54c

Please sign in to comment.