Notfellchen/src/fellchensammlung/views.py

127 lines
4.5 KiB
Python
Raw Normal View History

from django.shortcuts import render, redirect
from django.urls import reverse
from .mail import mail_admins_new_report
2024-03-17 10:26:32 +00:00
from fellchensammlung.models import AdoptionNotice, MarkdownContent, Animal, Rule, Image, Report, ModerationAction, \
Member
2024-03-22 11:45:50 +00:00
from .forms import AdoptionNoticeForm, AnimalForm, ImageForm, ReportForm
2024-03-17 10:26:32 +00:00
def index(request):
2024-03-18 13:53:31 +00:00
"""View function for home page of site."""
2024-03-18 13:21:42 +00:00
latest_adoption_list = AdoptionNotice.objects.order_by("-created_at")[:5]
context = {"adoption_notices": latest_adoption_list}
2024-03-18 13:53:31 +00:00
return render(request, 'fellchensammlung/index.html', context=context)
2024-03-18 07:26:21 +00:00
def adoption_notice_detail(request, adoption_notice_id):
2024-03-20 09:35:40 +00:00
adoption_notice = AdoptionNotice.objects.get(id=adoption_notice_id)
context = {"adoption_notice": adoption_notice}
return render(request, 'fellchensammlung/detail_adoption_notice.html', context=context)
2024-03-18 07:26:21 +00:00
def animal_detail(request, animal_id):
2024-03-19 05:15:38 +00:00
animal = Animal.objects.get(id=animal_id)
context = {"animal": animal}
return render(request, 'fellchensammlung/detail_animal.html', context=context)
2024-03-18 15:36:45 +00:00
2024-03-18 15:36:45 +00:00
def search(request):
2024-03-20 09:35:13 +00:00
latest_adoption_list = AdoptionNotice.objects.order_by("-created_at")
context = {"adoption_notices": latest_adoption_list}
return render(request, 'fellchensammlung/search.html', context=context)
2024-03-18 15:36:45 +00:00
def add_adoption(request):
if request.method == 'POST':
form = AdoptionNoticeForm(request.POST, request.FILES)
if form.is_valid():
instance = form.save()
return redirect(reverse("add-animal-to-adoption", args=[instance.pk]))
else:
form = AdoptionNoticeForm()
return render(request, 'fellchensammlung/form_add_adoption.html', {'form': form})
def add_animal_to_adoption(request, adoption_notice_id):
if request.method == 'POST':
form = AnimalForm(request.POST)
image_form = ImageForm(request.POST, request.FILES)
if form.is_valid():
form.cleaned_data["adoption_notice_id"] = adoption_notice_id
instance = form.save(commit=False)
instance.adoption_notice_id = adoption_notice_id
2024-03-20 10:38:30 +00:00
instance.save()
if 'image_-image' in request.FILES:
image = Image(image=request.FILES['image_-image'])
image.save()
instance.photos.add(image)
if "button_add_another_animal" in request.POST:
return redirect(reverse("add-animal-to-adoption", args=[str(adoption_notice_id)]))
else:
return redirect(reverse("adoption-notice-detail", args=[str(adoption_notice_id)]))
else:
form = AnimalForm()
image_form = ImageForm(request.POST, request.FILES, prefix="image_")
return render(request, 'fellchensammlung/form_add_animal_to_adoption.html',
{'form': form, "image_form": image_form})
2024-03-18 15:36:45 +00:00
2024-03-18 15:41:22 +00:00
def about(request):
2024-03-20 10:02:24 +00:00
rules = Rule.objects.all()
context = {"rules": rules}
2024-03-18 15:41:22 +00:00
return render(
request,
"fellchensammlung/about.html",
context=context
)
2024-03-22 11:45:50 +00:00
def report_adoption(request, adoption_notice_id):
"""
Form to report adoption notices
"""
if request.method == 'POST':
form = ReportForm(request.POST)
if form.is_valid():
report_instance = form.save(commit=False)
report_instance.adoption_notice_id = adoption_notice_id
report_instance.status = Report.WAITING
report_instance.save()
mail_admins_new_report(report_instance)
2024-03-22 11:45:50 +00:00
return redirect(reverse("report-detail-success", args=[report_instance.pk], ))
else:
form = ReportForm()
return render(request, 'fellchensammlung/form-report.html', {'form': form})
def report_detail(request, report_id, form_complete=False):
"""
Detailed view of a report, including moderation actions
"""
report = Report.objects.get(pk=report_id)
moderation_actions = ModerationAction.objects.filter(report_id=report_id)
context = {"report": report, "moderation_actions": moderation_actions, "form_complete": form_complete}
return render(request, 'fellchensammlung/detail-report.html', context)
def report_detail_success(request, report_id):
"""
Calls the report detail view with form_complete set to true, so success message shows
"""
return report_detail(request, report_id, form_complete=True)
def member_detail(request, user):
member = Member.objects.get(user=user)
context = {"member": member}
return render(request, 'fellchensammlung/detail-member.html', context=context)