scratch-site/scratch_show/views.py

45 lines
1.5 KiB
Python
Raw Normal View History

2022-05-13 03:04:13 +02:00
from django.shortcuts import get_object_or_404
from django.utils import timezone
2022-05-13 05:11:35 +02:00
from django.views.generic.base import TemplateView
2022-05-13 03:04:13 +02:00
from django.views.generic.detail import DetailView
2022-05-13 05:11:35 +02:00
from django.views.generic.edit import CreateView
from django.core.exceptions import ValidationError
2022-05-13 03:04:13 +02:00
from . import forms, models
2022-05-13 05:11:35 +02:00
class IndexView(TemplateView):
template_name = "scratch_show/index.html"
2022-05-13 03:04:13 +02:00
2022-05-13 05:11:35 +02:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
today = timezone.now().date()
context['current_event'] = models.Event.objects.filter(date=today, accept_projects=True).first()
context['past_events'] = models.Event.objects.exclude(date=today, accept_projects=True).order_by('-date')
2022-05-13 05:11:35 +02:00
return context
2022-05-13 03:04:13 +02:00
class ScratchProjectAddView(CreateView):
2022-05-13 05:11:35 +02:00
model = models.ScratchProject
fields = ['name', 'author_name', 'project_url']
2022-05-13 03:04:13 +02:00
def dispatch(self, request, *args, **kwargs):
self.event = get_object_or_404(models.Event, date=timezone.now().date(), accept_projects=True)
return super().dispatch(request, *args, **kwargs)
def form_valid(self, form):
form.instance.event = self.event
return super().form_valid(form)
class ScratchProjectDetailView(DetailView):
model = models.ScratchProject
2022-05-13 05:11:35 +02:00
class EventDetailView(DetailView):
model = models.Event
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['projects'] = models.ScratchProject.objects.filter(event=context['object'].pk)
return context