Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: convert to backend repo #119

Merged
merged 4 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
}
},
"name": "package-python",
"postCreateCommand": "pipenv install --dev",
"remoteUser": "root",
"service": "base-service",
"shutdownAction": "none",
Expand Down
6 changes: 5 additions & 1 deletion codeforlife/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
import typing as t

from django.db.models import Model as _Model
from django_stubs_ext.db.models import TypedModelMeta

if t.TYPE_CHECKING:
from django_stubs_ext.db.models import TypedModelMeta
else:
TypedModelMeta = object

Id = t.TypeVar("Id")

Expand Down
5 changes: 3 additions & 2 deletions codeforlife/settings/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ def get_databases(base_dir: Path): # pragma: no cover
# URLs
# https://docs.djangoproject.com/en/3.2/ref/settings/#root-urlconf

ROOT_URLCONF = "service.urls"
ROOT_URLCONF = "src.service.urls"

# App
# https://docs.djangoproject.com/en/3.2/ref/settings/#wsgi-application

WSGI_APPLICATION = "service.wsgi.application"
WSGI_APPLICATION = "src.service.wsgi.application"

# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
Expand Down Expand Up @@ -183,6 +183,7 @@ def get_databases(base_dir: Path): # pragma: no cover
# https://docs.djangoproject.com/en/3.2/ref/settings/#installed-apps

INSTALLED_APPS = [
"src.api",
"codeforlife.user",
"corsheaders",
"rest_framework",
Expand Down
32 changes: 11 additions & 21 deletions codeforlife/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,30 @@
from django.contrib import admin
from django.contrib.auth.views import LogoutView
from django.http import HttpResponse
from django.shortcuts import render
from django.urls import URLPattern, URLResolver, include, path, re_path
from rest_framework import status

from .settings import SERVICE_IS_ROOT, SERVICE_NAME
from .views import csrf

UrlPatterns = t.List[t.Union[URLResolver, URLPattern]]

def service_urlpatterns(
api_urls_path: str = "api.urls",
frontend_template_name: str = "frontend.html",

def get_urlpatterns(
api_urls_path: str = "src.api.urls",
include_user_urls: bool = True,
):
) -> UrlPatterns:
"""Generate standard url patterns for each service.

Args:
api_urls_path: The path to the api's urls.
frontend_template_name: The name of the frontend template to serve.
include_user_urls: Whether or not to include the CFL's user urls.

Returns:
The standard url patterns for each service.
"""

# Specific url patterns.
urlpatterns: t.List[t.Union[URLResolver, URLPattern]] = [
urlpatterns: UrlPatterns = [
path(
"admin/",
admin.site.urls,
Expand Down Expand Up @@ -62,9 +60,13 @@ def service_urlpatterns(
),
name="session-expired",
),
path(
"api/",
include(api_urls_path),
name="api",
),
]

# General url patterns.
if include_user_urls:
urlpatterns.append(
path(
Expand All @@ -73,18 +75,6 @@ def service_urlpatterns(
name="user",
)
)
urlpatterns += [
path(
"api/",
include(api_urls_path),
name="api",
),
re_path(
r"^(?!api/).*",
lambda request: render(request, frontend_template_name),
name="frontend",
),
]

if SERVICE_IS_ROOT:
return urlpatterns
Expand Down
6 changes: 5 additions & 1 deletion codeforlife/user/models/otp_bypass_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
from django.db.utils import IntegrityError
from django.utils.crypto import get_random_string
from django.utils.translation import gettext_lazy as _
from django_stubs_ext.db.models import TypedModelMeta

from .user import User

if t.TYPE_CHECKING:
from django_stubs_ext.db.models import TypedModelMeta
else:
TypedModelMeta = object


class OtpBypassToken(models.Model):
"""A single use token to bypass a user's OTP authentication factor."""
Expand Down
8 changes: 7 additions & 1 deletion codeforlife/user/models/student.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
Created on 14/02/2024 at 17:16:44(+00:00).
"""

import typing as t

from common.models import Student, StudentModelManager
from django.db import models
from django_stubs_ext.db.models import TypedModelMeta

if t.TYPE_CHECKING:
from django_stubs_ext.db.models import TypedModelMeta
else:
TypedModelMeta = object


# TODO: This model is legacy and should be removed in the new data schema.
Expand Down
6 changes: 5 additions & 1 deletion codeforlife/user/models/teacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@

from common.models import Teacher, TeacherModelManager
from django.db import models
from django_stubs_ext.db.models import TypedModelMeta

from .klass import Class
from .school import School
from .student import Student

if t.TYPE_CHECKING:
from django_stubs_ext.db.models import TypedModelMeta
else:
TypedModelMeta = object

AnyTeacher = t.TypeVar("AnyTeacher", bound=Teacher)


Expand Down
5 changes: 4 additions & 1 deletion codeforlife/user/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@
from django.db.models import F
from django.db.models.query import QuerySet
from django.utils.crypto import get_random_string
from django_stubs_ext.db.models import TypedModelMeta
from pyotp import TOTP

from ... import mail
from .klass import Class
from .school import School

if t.TYPE_CHECKING: # pragma: no cover
from django_stubs_ext.db.models import TypedModelMeta

from .auth_factor import AuthFactor
from .otp_bypass_token import OtpBypassToken
from .session import Session
from .student import Independent, Student
from .teacher import Teacher
else:
TypedModelMeta = object


class User(_User):
Expand Down