JourneyJoker/frontend/views.py

83 lines
3.1 KiB
Python

from django.views.generic import TemplateView, View
from django.conf import settings
from django.http.response import HttpResponse, JsonResponse
from django.contrib import messages
from django.utils.formats import get_format
from .models import InspirationRegion, Inspiration
import django_countries
class HomeView(TemplateView):
template_name = "frontend/index.html"
class Error404View(TemplateView):
template_name = "frontend/404.html"
class DemoTemplateView(TemplateView):
template_name = "partners/calendar.html"
class ImpressumView(TemplateView):
template_name = "frontend/impressum.html"
class PrivacyNoticeView(TemplateView):
template_name = "frontend/privacy.html"
class TOSView(TemplateView):
template_name = "frontend/terms.html"
class InspirationsView(TemplateView):
template_name = "frontend/inspirations.html"
class InspirationsCountryAPIView(View):
def get(self, request, *args, **kwargs):
countries = [{"code": country[0], "name": django_countries.countries.name(country[0])} for country in InspirationRegion.country_set.all()]
return JsonResponse(countries, safe=False)
class InspirationsRegionAPIView(View):
def get(self, request, *args, **kwargs):
regions = [{"id": region.id, "name": region.name, "state": region.is_state} for region in InspirationRegion.objects.filter(country=kwargs["country"])]
return JsonResponse(regions, safe=False)
class InspirationsAPIView(View):
def get(self, request, *args, **kwargs):
inspirations = [
{
"id": inspiration.id,
"title": inspiration.title,
"subtitle": inspiration.subtitle,
"image": inspiration.image.url,
"sponsor": {
"id": inspiration.sponsor.id,
"name": inspiration.sponsor.name,
"image": inspiration.sponsor.image.url
},
"content": inspiration.content,
"destination": {
"name": inspiration.destination_name,
"coords": {
"lat": inspiration.destination_coords.y,
"lon": inspiration.destination_coords.x
}
}
} for inspiration in Inspiration.objects.filter(region__id=kwargs["region"])]
return JsonResponse(inspirations, safe=False)
class LanguageChoiceView(View):
def get(self, request, *args, **kwargs):
response = HttpResponse()
for language, _ in settings.LANGUAGES:
if language.startswith(kwargs["code"]):
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, language)
if request.user.is_authenticated:
request.user.language = language
request.user.save()
return response
class LocaleVariableView(View):
def get(self, request, *args, **kwargs):
variables = {
"date_format": get_format('SHORT_DATE_FORMAT', use_l10n=True).replace("Y", "yyyy")
}
return JsonResponse(variables)