JourneyJoker/frontend/views.py

84 lines
3.1 KiB
Python
Raw Normal View History

from django.views.generic import TemplateView, View
from django.conf import settings
from django.http.response import HttpResponse, JsonResponse
from django.contrib import messages
2021-10-28 07:26:17 +00:00
from django.utils.formats import get_format
2021-03-01 09:56:10 +00:00
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):
2021-10-28 07:26:17 +00:00
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()
2021-10-28 07:26:17 +00:00
return response
class LocaleVariableView(View):
def get(self, request, *args, **kwargs):
variables = {
2021-11-13 13:59:13 +00:00
"date_format": get_format('SHORT_DATE_FORMAT', use_l10n=True).replace("Y", "yyyy")
2021-10-28 07:26:17 +00:00
}
return JsonResponse(variables)