Julia Luna
2 years ago
commit
9c1a199ec8
11 changed files with 79 additions and 0 deletions
@ -0,0 +1,22 @@ |
|||||||
|
#!/usr/bin/env python |
||||||
|
"""Django's command-line utility for administrative tasks.""" |
||||||
|
import os |
||||||
|
import sys |
||||||
|
|
||||||
|
|
||||||
|
def main(): |
||||||
|
"""Run administrative tasks.""" |
||||||
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_scopedsites.settings') |
||||||
|
try: |
||||||
|
from django.core.management import execute_from_command_line |
||||||
|
except ImportError as exc: |
||||||
|
raise ImportError( |
||||||
|
"Couldn't import Django. Are you sure it's installed and " |
||||||
|
"available on your PYTHONPATH environment variable? Did you " |
||||||
|
"forget to activate a virtual environment?" |
||||||
|
) from exc |
||||||
|
execute_from_command_line(sys.argv) |
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__': |
||||||
|
main() |
@ -0,0 +1,3 @@ |
|||||||
|
from django.contrib import admin |
||||||
|
|
||||||
|
# Register your models here. |
@ -0,0 +1,6 @@ |
|||||||
|
from django.apps import AppConfig |
||||||
|
|
||||||
|
|
||||||
|
class ScopedsitesConfig(AppConfig): |
||||||
|
default_auto_field = 'django.db.models.BigAutoField' |
||||||
|
name = 'scopedsites' |
@ -0,0 +1,16 @@ |
|||||||
|
from django_scopes import scope |
||||||
|
|
||||||
|
from scopedsites.models import Domain |
||||||
|
|
||||||
|
|
||||||
|
class DomainScopeMiddleware: |
||||||
|
def __init__(self, get_response): |
||||||
|
self.get_response = get_response |
||||||
|
|
||||||
|
def __call__(self, request): |
||||||
|
d = Domain.get_from_request(request) |
||||||
|
|
||||||
|
with scope(domain=d): |
||||||
|
response = self.get_response(request) |
||||||
|
|
||||||
|
return response |
@ -0,0 +1,20 @@ |
|||||||
|
from django.db import models |
||||||
|
from django.shortcuts import get_object_or_404 |
||||||
|
from django.utils.translation import gettext_lazy as _ |
||||||
|
|
||||||
|
|
||||||
|
class Domain(models.Model): |
||||||
|
fqdn = models.CharField( |
||||||
|
_('FQDN'), |
||||||
|
max_length=255, |
||||||
|
unique=True, |
||||||
|
) |
||||||
|
|
||||||
|
name = models.CharField( |
||||||
|
_('display name'), |
||||||
|
max_length=255, |
||||||
|
) |
||||||
|
|
||||||
|
@classmethod |
||||||
|
def get_from_request(cls, request): |
||||||
|
return get_object_or_404(cls, fqdn__iexact=request.META.get('HTTP_HOST', '')) |
@ -0,0 +1,3 @@ |
|||||||
|
from django.test import TestCase |
||||||
|
|
||||||
|
# Create your tests here. |
Loading…
Reference in new issue