2024-05-30 07:26:04 +00:00
|
|
|
import logging
|
|
|
|
|
2024-04-12 21:37:03 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2024-03-19 17:18:55 +00:00
|
|
|
from django.shortcuts import render, redirect
|
|
|
|
from django.urls import reverse
|
2024-04-12 21:37:03 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2024-04-14 13:45:57 +00:00
|
|
|
|
|
|
|
from django.utils import translation
|
2024-03-25 09:49:56 +00:00
|
|
|
from .mail import mail_admins_new_report
|
2024-04-12 21:37:03 +00:00
|
|
|
from notfellchen import settings
|
2024-03-17 10:26:32 +00:00
|
|
|
|
2024-05-30 07:26:04 +00:00
|
|
|
from fellchensammlung import logger
|
2024-04-14 11:26:21 +00:00
|
|
|
from fellchensammlung.models import AdoptionNotice, Text, Animal, Rule, Image, Report, ModerationAction, \
|
2024-03-23 21:20:31 +00:00
|
|
|
Member
|
2024-05-30 07:52:42 +00:00
|
|
|
from .forms import AdoptionNoticeForm, ImageForm, ReportForm
|
2024-04-14 11:26:21 +00:00
|
|
|
from .models import Language
|
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]
|
2024-03-20 09:45:52 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2024-04-12 21:37:03 +00:00
|
|
|
def change_language(request):
|
|
|
|
if request.method == 'POST':
|
|
|
|
language_code = request.POST.get('language')
|
|
|
|
if language_code:
|
|
|
|
if language_code != settings.LANGUAGE_CODE and language_code in list(zip(*settings.LANGUAGES))[0]:
|
|
|
|
redirect_path = f'/{language_code}/'
|
|
|
|
elif language_code == settings.LANGUAGE_CODE:
|
|
|
|
redirect_path = '/'
|
|
|
|
else:
|
|
|
|
response = HttpResponseRedirect('/')
|
|
|
|
return response
|
|
|
|
translation.activate(language_code)
|
|
|
|
response = HttpResponseRedirect(redirect_path)
|
|
|
|
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, language_code)
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
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}
|
2024-03-25 10:04:01 +00:00
|
|
|
return render(request, 'fellchensammlung/details/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}
|
2024-03-25 10:04:01 +00:00
|
|
|
return render(request, 'fellchensammlung/details/detail_animal.html', context=context)
|
2024-03-18 15:36:45 +00:00
|
|
|
|
2024-03-19 17:18:55 +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")
|
2024-03-18 21:50:39 +00:00
|
|
|
context = {"adoption_notices": latest_adoption_list}
|
|
|
|
return render(request, 'fellchensammlung/search.html', context=context)
|
2024-03-19 17:18:55 +00:00
|
|
|
|
|
|
|
|
2024-04-12 21:37:03 +00:00
|
|
|
@login_required
|
2024-03-18 15:36:45 +00:00
|
|
|
def add_adoption(request):
|
2024-03-19 17:18:55 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
form = AdoptionNoticeForm(request.POST, request.FILES)
|
|
|
|
|
|
|
|
if form.is_valid():
|
|
|
|
instance = form.save()
|
2024-05-10 11:54:16 +00:00
|
|
|
return redirect(reverse("adoption-notice-edit", args=[instance.pk]))
|
2024-03-19 17:18:55 +00:00
|
|
|
else:
|
|
|
|
form = AdoptionNoticeForm()
|
2024-03-25 10:04:01 +00:00
|
|
|
return render(request, 'fellchensammlung/forms/form_add_adoption.html', {'form': form})
|
2024-03-19 17:18:55 +00:00
|
|
|
|
|
|
|
|
2024-04-12 21:37:03 +00:00
|
|
|
@login_required
|
2024-05-10 11:54:16 +00:00
|
|
|
def edit_adoption_notice(request, animal_id):
|
|
|
|
"""
|
|
|
|
View implements the following methods
|
|
|
|
* Updating an AdoptionNotice
|
|
|
|
* Adding animals to an AN
|
|
|
|
"""
|
|
|
|
|
|
|
|
def delete_photo():
|
|
|
|
print("Photo deleted")
|
|
|
|
|
|
|
|
def save_photo():
|
|
|
|
print("Photo save")
|
|
|
|
|
|
|
|
def add_photo():
|
|
|
|
print("Photo added")
|
|
|
|
|
|
|
|
def save_animal():
|
|
|
|
print("Animal saved")
|
|
|
|
|
2024-03-19 17:18:55 +00:00
|
|
|
if request.method == 'POST':
|
2024-05-10 11:54:16 +00:00
|
|
|
form = AnimalForm(request.POST, animal_id=animal_id, )
|
|
|
|
for key in request.POST:
|
|
|
|
if key.startswith("delete_photo_"):
|
|
|
|
action = delete_photo
|
|
|
|
if key.startswith("save_photo_"):
|
|
|
|
action = save_photo
|
|
|
|
if key.startswith("add_photo"):
|
|
|
|
action = add_photo
|
|
|
|
if key.startswith("save_animal"):
|
|
|
|
action = save_animal
|
|
|
|
|
|
|
|
pk = key.split("_")[-1]
|
|
|
|
|
|
|
|
action(animal_id, pk, form_data=request.POST)
|
2024-03-19 17:18:55 +00:00
|
|
|
|
|
|
|
if form.is_valid():
|
2024-05-10 11:54:16 +00:00
|
|
|
animal = form.save()
|
|
|
|
return render(request, 'fellchensammlung/forms/form_add_animal_to_adoption.html',
|
|
|
|
{'form': form})
|
2024-03-20 10:38:30 +00:00
|
|
|
|
2024-05-10 11:54:16 +00:00
|
|
|
else:
|
|
|
|
form = AnimalForm(animal_id)
|
|
|
|
image_form = ImageForm(request.POST, request.FILES, prefix="image_")
|
|
|
|
return render(request, 'fellchensammlung/forms/form_add_animal_to_adoption.html',
|
|
|
|
{'form': form})
|
2024-03-20 12:40:00 +00:00
|
|
|
|
|
|
|
|
2024-05-10 11:54:16 +00:00
|
|
|
@login_required
|
|
|
|
def change_animal(request, animal_id):
|
|
|
|
"""
|
|
|
|
View implements the following methods
|
|
|
|
* Updating an Animal
|
|
|
|
* Adding photos to an animal
|
|
|
|
"""
|
|
|
|
|
|
|
|
def delete_photo():
|
|
|
|
print("Photo deleted")
|
|
|
|
|
|
|
|
def save_photo():
|
|
|
|
print("Photo save")
|
|
|
|
|
|
|
|
def add_photo():
|
|
|
|
print("Photo added")
|
|
|
|
|
|
|
|
def save_animal():
|
|
|
|
print("Animal saved")
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = AnimalForm(request.POST, animal_id=animal_id, )
|
|
|
|
for key in request.POST:
|
|
|
|
if key.startswith("delete_photo_"):
|
|
|
|
action = delete_photo
|
|
|
|
if key.startswith("save_photo_"):
|
|
|
|
action = save_photo
|
|
|
|
if key.startswith("add_photo"):
|
|
|
|
action = add_photo
|
|
|
|
if key.startswith("save_animal"):
|
|
|
|
action = save_animal
|
|
|
|
|
|
|
|
pk = key.split("_")[-1]
|
|
|
|
|
|
|
|
action(animal_id, pk, form_data=request.POST)
|
|
|
|
|
|
|
|
return render(request, 'fellchensammlung/forms/form_add_animal_to_adoption.html',
|
|
|
|
{'form': form})
|
|
|
|
|
2024-03-19 17:18:55 +00:00
|
|
|
else:
|
2024-05-10 11:54:16 +00:00
|
|
|
form = AnimalForm(animal_id)
|
2024-03-20 12:40:00 +00:00
|
|
|
image_form = ImageForm(request.POST, request.FILES, prefix="image_")
|
2024-03-25 10:04:01 +00:00
|
|
|
return render(request, 'fellchensammlung/forms/form_add_animal_to_adoption.html',
|
2024-05-10 11:54:16 +00:00
|
|
|
{'form': form})
|
2024-03-19 17:18:55 +00:00
|
|
|
|
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()
|
2024-04-14 12:39:49 +00:00
|
|
|
|
2024-04-14 13:45:57 +00:00
|
|
|
language_code = translation.get_language()
|
2024-04-14 13:39:54 +00:00
|
|
|
lang = Language.objects.get(languagecode=language_code)
|
2024-04-14 12:39:49 +00:00
|
|
|
|
|
|
|
legal = {}
|
|
|
|
for text_code in ["terms_of_service", "privacy_statement", "imprint"]:
|
|
|
|
try:
|
2024-05-01 08:09:36 +00:00
|
|
|
legal[text_code] = Text.objects.get(text_code=text_code, language=lang, )
|
2024-04-14 12:39:49 +00:00
|
|
|
except Text.DoesNotExist:
|
|
|
|
legal[text_code] = None
|
|
|
|
|
2024-05-01 08:09:36 +00:00
|
|
|
context = {"rules": rules, }
|
2024-04-14 12:39:49 +00:00
|
|
|
context.update(legal)
|
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()
|
2024-03-25 09:49:56 +00:00
|
|
|
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()
|
2024-03-25 10:04:01 +00:00
|
|
|
return render(request, 'fellchensammlung/forms/form-report.html', {'form': form})
|
2024-03-22 11:45:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
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}
|
|
|
|
|
2024-03-25 10:04:01 +00:00
|
|
|
return render(request, 'fellchensammlung/details/detail-report.html', context)
|
2024-03-22 11:45:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2024-03-23 21:20:31 +00:00
|
|
|
|
|
|
|
|
2024-04-07 07:06:18 +00:00
|
|
|
def member_detail(request, user_id):
|
|
|
|
member = Member.objects.get(id=user_id)
|
2024-03-23 21:20:31 +00:00
|
|
|
context = {"member": member}
|
2024-04-07 07:03:20 +00:00
|
|
|
return render(request, 'fellchensammlung/details/detail-member.html', context=context)
|
|
|
|
|
|
|
|
|
|
|
|
def modqueue(request):
|
|
|
|
open_reports = Report.objects.filter(status=Report.WAITING)
|
|
|
|
context = {"reports": open_reports}
|
|
|
|
return render(request, 'fellchensammlung/modqueue.html', context=context)
|