Skip to content

Commit

Permalink
Use BaseHomeView in example project
Browse files Browse the repository at this point in the history
  • Loading branch information
medihack committed May 27, 2024
1 parent 8b25237 commit e1e2eb1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
1 change: 0 additions & 1 deletion adit_radis_shared/common/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def __call__(self, request: HtmxHttpRequest):
return self.get_response(request)

project_settings = ProjectSettings.get()
assert project_settings
if project_settings and project_settings.maintenance and not request.user.is_staff:
if request.path.startswith("/api/"):
raise ServiceUnavailable
Expand Down
16 changes: 12 additions & 4 deletions adit_radis_shared/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ def __str__(self) -> str:
return f"{self.__class__.__name__} [ID {self.id}]"

@classmethod
def get(cls):
return cls.objects.first()
def get(cls) -> "ProjectSettings":
project_settings = cls.objects.first()
# We made sure during startup that there is always a ProjectSettings
# (see common/apps.py)
assert project_settings
return project_settings


class AppSettings(models.Model):
Expand All @@ -25,5 +29,9 @@ class Meta:
abstract = True

@classmethod
def get(cls):
return cls.objects.first()
def get(cls) -> "AppSettings":
app_settings = cls.objects.first()
# We made sure during startup that there is always a AppSettings
# (see apps.py of the specific app)
assert app_settings
return app_settings
2 changes: 1 addition & 1 deletion adit_radis_shared/common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class BaseHomeView(TemplateView):
def get_context_data(self, **kwargs) -> dict[str, Any]:
context = super().get_context_data(**kwargs)
project_settings = ProjectSettings.get()
context["announcement"] = project_settings.announcement if project_settings else ""
context["announcement"] = project_settings.announcement
return context


Expand Down
4 changes: 2 additions & 2 deletions example_project/example_project/example_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from .views import (
AsyncExampleClassView,
ExampleListView,
HomeView,
admin_section,
example_messages,
example_toasts,
home,
)

urlpatterns = [
path("", home, name="home"),
path("", HomeView.as_view(), name="home"),
path("admin-section/", admin_section, name="admin_section"),
path(
"examples/",
Expand Down
5 changes: 3 additions & 2 deletions example_project/example_project/example_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
from django.views.generic import TemplateView

from adit_radis_shared.accounts.models import User
from adit_radis_shared.common.views import BaseHomeView


def home(request: HttpRequest) -> HttpResponse:
return render(request, "example_app/home.html", {})
class HomeView(BaseHomeView):
template_name = "example_app/home.html"


@login_required
Expand Down

0 comments on commit e1e2eb1

Please sign in to comment.