wizyta oraz widok księdza
This commit is contained in:
266
backend/views.py
Normal file
266
backend/views.py
Normal file
@ -0,0 +1,266 @@
|
||||
from django.shortcuts import render, redirect
|
||||
from django.urls import path
|
||||
from .models import Parish, Submission
|
||||
import requests # noqa: F401
|
||||
from lxml import html # noqa: F401
|
||||
from django.http import HttpResponse
|
||||
from django.views.generic import CreateView, DeleteView, UpdateView
|
||||
from authtools.forms import UserCreationForm
|
||||
from authtools.models import User
|
||||
from django.contrib.auth.views import LoginView, LogoutView
|
||||
from django.urls import reverse_lazy
|
||||
from django import forms
|
||||
|
||||
|
||||
# Create your views here.
|
||||
|
||||
|
||||
def favicon(request):
|
||||
return HttpResponse(None)
|
||||
|
||||
|
||||
def manifest(request, slug):
|
||||
return render(
|
||||
request, "parish/manifest.json", {"parish": Parish.objects.get(slug=slug)}, content_type="application/json"
|
||||
)
|
||||
|
||||
|
||||
def index(request):
|
||||
parishes = Parish.objects.filter(visible=True)
|
||||
return render(request, "index.html", {"parishes": parishes})
|
||||
|
||||
|
||||
def announcements(request, slug):
|
||||
p = Parish.objects.get(slug=slug)
|
||||
ldict = {}
|
||||
exec(p.announcements, globals(), ldict)
|
||||
|
||||
return render(
|
||||
request, "parish/announcements.html", {"parish": p, "announcements": "is-active", "res": ldict.get("res", "")}
|
||||
)
|
||||
|
||||
|
||||
def intentions(request, slug):
|
||||
p = Parish.objects.get(slug=slug)
|
||||
ldict = {}
|
||||
exec(p.intentions, globals(), ldict)
|
||||
|
||||
return render(
|
||||
request, "parish/intentions.html", {"parish": p, "intentions": "is-active", "res": ldict.get("res", "")}
|
||||
)
|
||||
|
||||
|
||||
def live(request, slug):
|
||||
p = Parish.objects.get(slug=slug)
|
||||
if not p.channel:
|
||||
return redirect("announcements", slug=slug)
|
||||
|
||||
return render(request, "parish/live.html", {"parish": p, "live": "is-active"})
|
||||
|
||||
|
||||
def visit(request, slug):
|
||||
p = Parish.objects.get(slug=slug)
|
||||
c = {"parish": p, "visit": "is-active"}
|
||||
if not p.submissions:
|
||||
return redirect("announcements", slug=slug)
|
||||
|
||||
if not request.user.is_authenticated:
|
||||
return render(request, "parish/visit/auth.html", c)
|
||||
|
||||
if not Submission.objects.filter(user=request.user).exists():
|
||||
return VisitCreateView.as_view()(request, slug=slug)
|
||||
|
||||
c["submission"] = Submission.objects.get(user=request.user)
|
||||
return VisitDeleteView.as_view()(request, slug=slug)
|
||||
|
||||
|
||||
class SubmissionForm(forms.ModelForm):
|
||||
def __init__(self, slug, *args, **kwargs):
|
||||
super(SubmissionForm, self).__init__(*args, **kwargs)
|
||||
streets = Parish.objects.get(slug=slug).streets
|
||||
choices = zip(streets, streets)
|
||||
self.fields["street"] = forms.ChoiceField(label="Ulica", choices=choices)
|
||||
|
||||
class Meta:
|
||||
model = Submission
|
||||
fields = [
|
||||
"name",
|
||||
"phone",
|
||||
"street",
|
||||
"street_number",
|
||||
"apartement_number",
|
||||
]
|
||||
|
||||
|
||||
class VisitCreateView(CreateView):
|
||||
form_class = SubmissionForm
|
||||
template_name = "parish/visit/form.html"
|
||||
|
||||
def form_valid(self, form):
|
||||
form.instance.parish = Parish.objects.get(slug=self.kwargs["slug"])
|
||||
form.instance.user = self.request.user
|
||||
return super().form_valid(form)
|
||||
|
||||
def get_initial(self):
|
||||
return {"name": self.request.user.name}
|
||||
|
||||
def get_success_url(self):
|
||||
p = Parish.objects.get(slug=self.kwargs["slug"])
|
||||
return reverse_lazy("visit", args=[p.slug])
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
p = Parish.objects.get(slug=self.kwargs["slug"])
|
||||
context["parish"] = p
|
||||
context["visit"] = "is-active"
|
||||
return context
|
||||
|
||||
def get_form(self, form_class=None):
|
||||
form = super().get_form(form_class)
|
||||
form.fields["name"].disabled = True
|
||||
return form
|
||||
|
||||
def get_form_kwargs(self):
|
||||
return {**super().get_form_kwargs(), **self.kwargs}
|
||||
|
||||
|
||||
class VisitDeleteView(DeleteView):
|
||||
model = Submission
|
||||
template_name = "parish/visit/submitted.html"
|
||||
|
||||
def get_object(self):
|
||||
return Submission.objects.get(user=self.request.user)
|
||||
|
||||
def get_success_url(self):
|
||||
p = Parish.objects.get(slug=self.kwargs["slug"])
|
||||
return reverse_lazy("visit", args=[p.slug])
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
p = Parish.objects.get(slug=self.kwargs["slug"])
|
||||
context["parish"] = p
|
||||
context["visit"] = "is-active"
|
||||
return context
|
||||
|
||||
|
||||
class VisitLoginView(LoginView):
|
||||
redirect_authenticated_user = True
|
||||
template_name = "parish/visit/login.html"
|
||||
|
||||
def get_success_url(self):
|
||||
p = Parish.objects.get(slug=self.kwargs["slug"])
|
||||
return reverse_lazy("visit", args=[p.slug])
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
p = Parish.objects.get(slug=kwargs["slug"])
|
||||
if self.request.user.is_authenticated:
|
||||
return redirect("visit", slug=p.slug)
|
||||
|
||||
if not p.submissions:
|
||||
return redirect("announcements", slug=p.slug)
|
||||
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
p = Parish.objects.get(slug=self.kwargs["slug"])
|
||||
context["parish"] = p
|
||||
context["visit"] = "is-active"
|
||||
return context
|
||||
|
||||
|
||||
class VisitSignUpView(CreateView):
|
||||
form_class = UserCreationForm
|
||||
template_name = "parish/visit/signup.html"
|
||||
|
||||
def get_success_url(self):
|
||||
p = Parish.objects.get(slug=self.kwargs["slug"])
|
||||
return reverse_lazy("visit", args=[p.slug])
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
p = Parish.objects.get(slug=kwargs["slug"])
|
||||
if self.request.user.is_authenticated:
|
||||
return redirect("visit", slug=p.slug)
|
||||
|
||||
if not p.submissions:
|
||||
return redirect("announcements", slug=p.slug)
|
||||
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
p = Parish.objects.get(slug=self.kwargs["slug"])
|
||||
context["parish"] = p
|
||||
context["visit"] = "is-active"
|
||||
return context
|
||||
|
||||
|
||||
class UserUpdateForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ["email", "name"]
|
||||
|
||||
|
||||
class VisitAccountView(UpdateView):
|
||||
form_class = UserUpdateForm
|
||||
template_name = "parish/visit/update.html"
|
||||
|
||||
def get_object(self):
|
||||
return User.objects.get(id=self.request.user.id)
|
||||
|
||||
def get_success_url(self):
|
||||
p = Parish.objects.get(slug=self.kwargs["slug"])
|
||||
return reverse_lazy("visit", args=[p.slug])
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
p = Parish.objects.get(slug=kwargs["slug"])
|
||||
# if self.request.user.is_authenticated:
|
||||
# return redirect("visit", slug=p.slug)
|
||||
|
||||
if not p.submissions:
|
||||
return redirect("announcements", slug=p.slug)
|
||||
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
p = Parish.objects.get(slug=self.kwargs["slug"])
|
||||
context["parish"] = p
|
||||
context["visit"] = "is-active"
|
||||
return context
|
||||
|
||||
def get_form(self, form_class=None):
|
||||
form = super().get_form(form_class)
|
||||
form.fields["name"].label = "Imię i nazwisko"
|
||||
return form
|
||||
|
||||
|
||||
class VisitLogoutView(LogoutView):
|
||||
template_name = "parish/visit/auth.html"
|
||||
|
||||
def get_next_page(self):
|
||||
p = Parish.objects.get(slug=self.kwargs["slug"])
|
||||
return reverse_lazy("visit", args=[p.slug])
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
p = Parish.objects.get(slug=self.kwargs["slug"])
|
||||
context["parish"] = p
|
||||
context["visit"] = "is-active"
|
||||
return context
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
path("", index, name="index"),
|
||||
path("favicon.ico", favicon),
|
||||
path("<str:slug>/", announcements, name="parish"),
|
||||
path("<str:slug>/ogloszenia/", announcements, name="announcements"),
|
||||
path("<str:slug>/intencje/", intentions, name="intentions"),
|
||||
path("<str:slug>/transmisja/", live, name="live"),
|
||||
path("<str:slug>/wizyta/", visit, name="visit"),
|
||||
path("<str:slug>/wizyta/logowanie/", VisitLoginView.as_view(), name="visit_login"),
|
||||
path("<str:slug>/wizyta/rejestracja/", VisitSignUpView.as_view(), name="visit_signup"),
|
||||
path("<str:slug>/wizyta/konto/", VisitAccountView.as_view(), name="visit_account"),
|
||||
path("<str:slug>/wizyta/wylogowanie/", VisitLogoutView.as_view(), name="visit_logout"),
|
||||
path("<str:slug>/manifest.json", manifest, name="manifest"),
|
||||
]
|
Reference in New Issue
Block a user