Skip to content

Commit

Permalink
chore: setup DRF in test app, add Booking app for tenants
Browse files Browse the repository at this point in the history
  • Loading branch information
thubamamba committed Sep 13, 2024
1 parent 3b7a3f1 commit aa57f08
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 6 deletions.
Empty file.
6 changes: 6 additions & 0 deletions django_test_app/bookings/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class BookingsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "django_test_app.bookings"
33 changes: 33 additions & 0 deletions django_test_app/bookings/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 4.2.15 on 2024-09-13 04:20

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="Booking",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("booking_agent_name", models.CharField(max_length=64)),
("booking_description", models.TextField()),
("booking_type", models.CharField(max_length=100)),
("booking_date", models.DateField()),
("created", models.DateTimeField(auto_now_add=True)),
("updated", models.DateTimeField(auto_now=True)),
],
),
]
Empty file.
16 changes: 16 additions & 0 deletions django_test_app/bookings/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.db import models


# Create your models here.
class Booking(models.Model):
"""Test Tenant object."""

booking_agent_name = models.CharField(max_length=64)
booking_description = models.TextField()
booking_type = models.CharField(max_length=100)
booking_date = models.DateField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)

def __str__(self):
return self.booking_agent_name
9 changes: 9 additions & 0 deletions django_test_app/bookings/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rest_framework import serializers

from django_test_app.bookings.models import Booking


class BookingSerializer(serializers.ModelSerializer):
class Meta:
model = Booking
fields = "__all__"
12 changes: 12 additions & 0 deletions django_test_app/bookings/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated

from django_test_app.bookings.models import Booking
from django_test_app.bookings.serializers import BookingSerializer


# Create your views here.
class BookingViewSet(viewsets.ModelViewSet):
queryset = Booking.objects.all()
serializer_class = BookingSerializer
permission_classes = (IsAuthenticated,)
5 changes: 3 additions & 2 deletions django_test_app/companies/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db import migrations, models
from django_tenants.postgresql_backend.base import \
_check_schema_name as check_schema_name
from django_tenants.postgresql_backend.base import (
_check_schema_name as check_schema_name,
)


class Migration(migrations.Migration):
Expand Down
1 change: 0 additions & 1 deletion django_test_app/companies/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.db import models
from django_tenants.models import DomainMixin

from tenant_users.tenants.models import TenantBase

_NameFieldLength = 64
Expand Down
30 changes: 30 additions & 0 deletions django_test_app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""

import os
from datetime import timedelta
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
Expand Down Expand Up @@ -39,6 +41,10 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
# DRF apps
"rest_framework",
"rest_framework.authtoken",
"rest_framework_simplejwt",
# django-tenant-users apps
"tenant_users.permissions",
"tenant_users.tenants",
Expand All @@ -51,6 +57,8 @@
"django.contrib.auth",
"django.contrib.contenttypes",
"tenant_users.permissions",
# Test project apps - tenant specific apps
"django_test_app.bookings",
]

INSTALLED_APPS = list(SHARED_APPS) + [
Expand All @@ -67,6 +75,28 @@
TENANT_MODEL = "companies.Company"
TENANT_DOMAIN_MODEL = "companies.Domain"

# DRF Configuration
REST_FRAMEWORK = {
"NON_FIELD_ERRORS_KEY": "errors",
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework_simplejwt.authentication.JWTAuthentication",
],
"DEFAULT_PERMISSION_CLASSES": [
"rest_framework.permissions.IsAuthenticated",
],
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
"PAGE_SIZE": 10,
}

# Simple JWT Configuration
SIMPLE_JWT = {
"AUTH_HEADER_TYPES": ("JWT",),
"ACCESS_TOKEN_LIFETIME": timedelta(days=20),
}

# Djoser Configuration
# In the event you are using Djoser with DRF for authentication, you easily set it up by following the Djoser documentation - https://djoser.readthedocs.io/en/latest/


MIDDLEWARE = [
"django_tenants.middleware.main.TenantMainMiddleware",
Expand Down
1 change: 0 additions & 1 deletion django_test_app/users/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.db import migrations, models

from tenant_users.permissions.models import PermissionsMixinFacade


Expand Down
1 change: 0 additions & 1 deletion django_test_app/users/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.db import models

from tenant_users.tenants.models import UserProfile

_NameFieldLength = 64
Expand Down
58 changes: 57 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ classifiers = [
python = "^3.8.1"
Django = ">=4.2"
django-tenants = "^3.6"
djangorestframework-simplejwt = "^5.3.1"


[tool.poetry.group.test.dependencies]
Expand Down

0 comments on commit aa57f08

Please sign in to comment.