from django.http import HttpResponseRedirect
from django.contrib import messages
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _
from django.views.generic import TemplateView

from events.models import Event


class IndexView(TemplateView):

    def get(self, request, *args, **kwargs):
        data = self.get_context_data()
        try:
            data['event'] = Event.objects.get_actual()
            if not data['event']:
                return HttpResponseRedirect(reverse_lazy('no_data'))

            data['html_sections'] = data['event'].html_sections.all().order_by('order_index')

        except Exception as e:
            HttpResponseRedirect(reverse_lazy('no_data'))

        return self.render_to_response(data)


class NoDataView(TemplateView):
    template_name = 'core/no_data.html'

    def get(self, request, *args, **kwargs):
        data = self.get_context_data()
        return self.render_to_response(data)