From 217570d6c0ac039cea7f0c023b8db430bdd966d0 Mon Sep 17 00:00:00 2001 From: Lukas Winkler Date: Sun, 29 Aug 2021 13:58:21 +0200 Subject: [PATCH] add campaign overview --- campaigns/forms.py | 5 ++ .../campaigns}/campaign_confirm_delete.html | 0 .../templates/campaigns}/campaign_detail.html | 0 .../templates/campaigns}/campaign_edit.html | 0 .../campaigns}/campaign_overview.html | 2 +- campaigns/urls.py | 8 +++ campaigns/views.py | 70 ++++++++++++++++++- notes/urls.py | 9 ++- notes/views/campaign_views.py | 38 ---------- notes/views/debugviews.py | 4 +- rpg_notes/settings.py | 8 ++- rpg_notes/urls_public.py | 33 +++++++++ {notes/templates => templates}/base.html | 0 .../templates => templates}/navbarbase.html | 6 +- users/models.py | 6 ++ 15 files changed, 137 insertions(+), 52 deletions(-) create mode 100644 campaigns/forms.py rename {notes/templates/notes => campaigns/templates/campaigns}/campaign_confirm_delete.html (100%) rename {notes/templates/notes => campaigns/templates/campaigns}/campaign_detail.html (100%) rename {notes/templates/notes => campaigns/templates/campaigns}/campaign_edit.html (100%) rename {notes/templates/notes => campaigns/templates/campaigns}/campaign_overview.html (68%) create mode 100644 campaigns/urls.py delete mode 100644 notes/views/campaign_views.py create mode 100644 rpg_notes/urls_public.py rename {notes/templates => templates}/base.html (100%) rename {notes/templates => templates}/navbarbase.html (85%) diff --git a/campaigns/forms.py b/campaigns/forms.py new file mode 100644 index 0000000..1bbd720 --- /dev/null +++ b/campaigns/forms.py @@ -0,0 +1,5 @@ +from django import forms + + +class CampaignForm(forms.Form): + name = forms.CharField(widget=forms.TextInput) diff --git a/notes/templates/notes/campaign_confirm_delete.html b/campaigns/templates/campaigns/campaign_confirm_delete.html similarity index 100% rename from notes/templates/notes/campaign_confirm_delete.html rename to campaigns/templates/campaigns/campaign_confirm_delete.html diff --git a/notes/templates/notes/campaign_detail.html b/campaigns/templates/campaigns/campaign_detail.html similarity index 100% rename from notes/templates/notes/campaign_detail.html rename to campaigns/templates/campaigns/campaign_detail.html diff --git a/notes/templates/notes/campaign_edit.html b/campaigns/templates/campaigns/campaign_edit.html similarity index 100% rename from notes/templates/notes/campaign_edit.html rename to campaigns/templates/campaigns/campaign_edit.html diff --git a/notes/templates/notes/campaign_overview.html b/campaigns/templates/campaigns/campaign_overview.html similarity index 68% rename from notes/templates/notes/campaign_overview.html rename to campaigns/templates/campaigns/campaign_overview.html index 2ef5931..37c4e30 100644 --- a/notes/templates/notes/campaign_overview.html +++ b/campaigns/templates/campaigns/campaign_overview.html @@ -2,7 +2,7 @@ {% block mainpage %} {% for campaign in campaigns %} -

{{ campaign.name }}

+

{{ campaign.name }}

{% endfor %} create Campaign {% endblock %} diff --git a/campaigns/urls.py b/campaigns/urls.py new file mode 100644 index 0000000..c361b32 --- /dev/null +++ b/campaigns/urls.py @@ -0,0 +1,8 @@ +from django.urls import path + +from campaigns import views + +urlpatterns = [ + path("", views.CampaignListView.as_view(), name="campaignlist"), + path("c/add", views.CampaignCreateView.as_view(), name="campaigncreate"), +] diff --git a/campaigns/views.py b/campaigns/views.py index 91ea44a..a2a09da 100644 --- a/campaigns/views.py +++ b/campaigns/views.py @@ -1,3 +1,69 @@ -from django.shortcuts import render +from django.shortcuts import redirect +from django.urls import reverse_lazy +from django.utils.text import slugify +from django.views import generic +from tenant_users.tenants.tasks import provision_tenant -# Create your views here. +from campaigns.forms import CampaignForm +from campaigns.models import Campaign +from users.models import TenantUser + + +class CampaignListView(generic.ListView): + template_name = "campaigns/campaign_overview.html" + model = Campaign + context_object_name = "campaigns" + + def get_queryset(self): + current_user: TenantUser = self.request.user + return current_user.tenants.all() + + +class CampaignCreateView(generic.FormView): + template_name = "campaigns/campaign_edit.html" + form_class = CampaignForm + + def form_valid(self, form): + name = form.cleaned_data.get("name") + slug = slugify(name).replace("-", "") + print(slug) + user = self.request.user + fqdn = provision_tenant(name, slug, user.email, is_staff=True) + return redirect("http://" + fqdn) + + +class CampaignDetailView(generic.DetailView): + template_name = "campaigns/campaign_detail.html" + model = Campaign + slug_url_kwarg = "campslug" + + def get_object(self, queryset=None): + return self.request.tenant + + +class CampaignEditView(generic.UpdateView): + template_name = "campaigns/campaign_edit.html" + model = Campaign + fields = ["name"] + slug_url_kwarg = "campslug" + + def get_object(self, queryset=None): + return self.request.tenant + + +class CampaignDeleteView(generic.DeleteView): + """ + broken at the moment + """ + template_name = "campaigns/campaign_confirm_delete.html" + model = Campaign + slug_url_kwarg = "campslug" + success_url = reverse_lazy('campaigndetail') + + def get_object(self, queryset=None): + return self.request.tenant + + def delete(self, request, *args, **kwargs): + self.object: Campaign = self.get_object() + self.object.delete_tenant() + return redirect("http://test.localhost:8000/") diff --git a/notes/urls.py b/notes/urls.py index 14051f9..225b518 100644 --- a/notes/urls.py +++ b/notes/urls.py @@ -1,13 +1,12 @@ from django.urls import path from notes import views +from campaigns import views as campaign_views urlpatterns = [ - # path("", views.CampaignListView.as_view(), name="campaignlist"), - # path("c/add", views.CampaignCreateView.as_view(), name="campaigncreate"), - # path("c/", views.CampaignDetailView.as_view(), name="campaigndetail"), - # path("c//edit", views.CampaignEditView.as_view(), name="campaignedit"), - # path("c//delete", views.CampaignDeleteView.as_view(), name="campaigndelete"), + path("", campaign_views.CampaignDetailView.as_view(), name="campaigndetail"), + path("edit", campaign_views.CampaignEditView.as_view(), name="campaignedit"), + path("delete", campaign_views.CampaignDeleteView.as_view(), name="campaigndelete"), path("loot", views.LootListView.as_view(), name="lootlist"), path("loot//edit", views.LootEditView.as_view(), name="lootedit"), path("loot//delete", views.LootDeleteView.as_view(), name="lootdelete"), diff --git a/notes/views/campaign_views.py b/notes/views/campaign_views.py deleted file mode 100644 index 5e363b8..0000000 --- a/notes/views/campaign_views.py +++ /dev/null @@ -1,38 +0,0 @@ -from django.urls import reverse_lazy -from django.views import generic - -from notes.forms import CampaignForm -from notes.models import Campaign - - -class CampaignListView(generic.ListView): - template_name = "notes/campaign_overview.html" - model = Campaign - context_object_name = "campaigns" - - -class CampaignDetailView(generic.DetailView): - template_name = "notes/campaign_detail.html" - model = Campaign - slug_url_kwarg = "campslug" - - -class CampaignCreateView(generic.CreateView): - template_name = "notes/campaign_edit.html" - model = Campaign - form_class = CampaignForm - slug_url_kwarg = "campslug" - - -class CampaignEditView(generic.UpdateView): - template_name = "notes/campaign_edit.html" - model = Campaign - form_class = CampaignForm - slug_url_kwarg = "campslug" - - -class CampaignDeleteView(generic.DeleteView): - template_name = "notes/campaign_confirm_delete.html" - model = Campaign - slug_url_kwarg = "campslug" - success_url = reverse_lazy('campaignlist') diff --git a/notes/views/debugviews.py b/notes/views/debugviews.py index 874b4fb..6abc5df 100644 --- a/notes/views/debugviews.py +++ b/notes/views/debugviews.py @@ -3,12 +3,12 @@ from django.views.decorators.cache import cache_page from notes.utils.assets import get_css -# @cache_page(60 * 15) +@cache_page(60 * 15) def debug_css(request): css, source_map = get_css(debug=True) return HttpResponse(css, content_type="text/css") -# @cache_page(60 * 15) +@cache_page(60 * 15) def debug_css_sourcemap(request): css, source_map = get_css(debug=True) return HttpResponse(source_map, content_type="application/json") diff --git a/rpg_notes/settings.py b/rpg_notes/settings.py index e1f4062..bf2904a 100644 --- a/rpg_notes/settings.py +++ b/rpg_notes/settings.py @@ -12,6 +12,8 @@ https://docs.djangoproject.com/en/3.2/ref/settings/ from pathlib import Path +from django_tenants.files.storage import TenantFileSystemStorage + from .secrets import * # Build paths inside the project like this: BASE_DIR / 'subdir'. @@ -74,6 +76,8 @@ AUTHENTICATION_BACKENDS = ( SESSION_COOKIE_DOMAIN = '.test.localhost' +DEFAULT_FILE_STORAGE = "django_tenants.files.storage.TenantFileSystemStorage" + MIDDLEWARE = [ 'django_tenants.middleware.main.TenantMainMiddleware', 'debug_toolbar.middleware.DebugToolbarMiddleware', @@ -88,11 +92,13 @@ MIDDLEWARE = [ ] ROOT_URLCONF = 'rpg_notes.urls' +PUBLIC_SCHEMA_URLCONF = 'rpg_notes.urls_public' +TEMPLATES_DIR=BASE_DIR / 'templates' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [BASE_DIR / 'templates'], + 'DIRS': [TEMPLATES_DIR], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ diff --git a/rpg_notes/urls_public.py b/rpg_notes/urls_public.py new file mode 100644 index 0000000..7114c09 --- /dev/null +++ b/rpg_notes/urls_public.py @@ -0,0 +1,33 @@ +"""rpg_notes URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/3.2/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +import debug_toolbar +from django.conf.urls.static import static +from django.contrib import admin +from django.urls import path, include + +from notes import views +from rpg_notes import settings + +urlpatterns = [ + path('admin/', admin.site.urls), + path('', include("campaigns.urls")) +] + +if settings.DEBUG: + urlpatterns.append(path('__debug__/', include(debug_toolbar.urls)), ) + urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + urlpatterns.append(path("css", views.debug_css, name="css")) + urlpatterns.append(path("css_sourcemap", views.debug_css_sourcemap, name="css_sourcemap")) diff --git a/notes/templates/base.html b/templates/base.html similarity index 100% rename from notes/templates/base.html rename to templates/base.html diff --git a/notes/templates/navbarbase.html b/templates/navbarbase.html similarity index 85% rename from notes/templates/navbarbase.html rename to templates/navbarbase.html index 98a912c..6775255 100644 --- a/notes/templates/navbarbase.html +++ b/templates/navbarbase.html @@ -9,9 +9,9 @@