1
0
Fork 0
mirror of https://github.com/Findus23/acronomy.git synced 2024-09-19 15:33:45 +02:00
acronomy/acros/views.py

117 lines
3.2 KiB
Python
Raw Normal View History

2020-07-06 19:54:36 +02:00
from datetime import date
2020-06-01 11:03:21 +02:00
from django.contrib.auth.mixins import LoginRequiredMixin
2020-06-13 22:36:46 +02:00
from django.contrib.messages.views import SuccessMessageMixin
2020-06-13 21:18:58 +02:00
from django.http import HttpResponse
2020-06-01 11:03:21 +02:00
from django.views import generic
from rest_framework import viewsets, filters
from acros.forms import EditForm, AddForm
2020-07-06 19:54:36 +02:00
from acros.models import Acronym, Tag, AcroOfTheDay
2020-06-01 11:03:21 +02:00
from acros.serializers import AcronymSerializer, AcronymListSerializer, TagSerializer
2020-06-13 21:18:58 +02:00
from acros.utils.assets import get_css
2020-06-01 11:03:21 +02:00
2020-06-15 21:02:50 +02:00
handler404 = 'acros.views.PageNotFoundView'
2020-06-01 11:03:21 +02:00
2020-07-06 19:54:36 +02:00
2020-06-01 11:03:21 +02:00
class IndexView(generic.TemplateView):
template_name = "acros/index.html"
2020-07-06 19:54:36 +02:00
def get_context_data(self, **kwargs):
data = super().get_context_data(**kwargs)
aotd = AcroOfTheDay.objects.filter(date=date.today()).select_related('acronym')
data['acronyms_of_the_day'] = aotd
data['num_acronyms'] = Acronym.objects.all().count()
return data
2020-06-15 21:02:50 +02:00
class PageNotFoundView(generic.TemplateView):
template_name = "404.html"
2020-06-01 11:03:21 +02:00
class OverView(generic.ListView):
template_name = "acros/overview.html"
model = Acronym
context_object_name = 'acros'
ordering = "name"
class DetailView(generic.DetailView):
template_name = 'acros/detail.html'
context_object_name = 'acro'
model = Acronym
2020-06-15 21:02:50 +02:00
class EditView(LoginRequiredMixin, SuccessMessageMixin, generic.UpdateView):
2020-06-01 11:03:21 +02:00
template_name = 'acros/edit.html'
context_object_name = 'acro'
model = Acronym
# fields = ['name', 'full_name', "description_md", "tags"]
form_class = EditForm
2020-06-15 21:02:50 +02:00
success_message = 'Acronym "%(name)s" was edited successfully'
2020-06-01 11:03:21 +02:00
2020-06-13 22:36:46 +02:00
class AddView(LoginRequiredMixin, SuccessMessageMixin, generic.CreateView):
2020-06-01 11:03:21 +02:00
template_name = "acros/add.html"
form_class = AddForm
model = Acronym
2020-06-13 22:36:46 +02:00
success_message = 'Acronym "%(name)s" was created successfully'
2020-06-01 11:03:21 +02:00
class TagListView(generic.ListView):
template_name = "acros/taglist.html"
model = Tag
context_object_name = 'tags'
ordering = "name"
2020-06-15 20:30:17 +02:00
queryset = Tag.objects.exclude(acronyms=None)
2020-06-01 11:03:21 +02:00
class TagAcroView(generic.ListView):
template_name = "acros/tagacro.html"
context_object_name = 'acros'
ordering = "name"
2020-06-15 21:02:50 +02:00
allow_empty = False
2020-06-01 11:03:21 +02:00
def get_queryset(self):
return Acronym.objects.filter(tags__slug__exact=self.kwargs['slug'])
#### API Views ####
class AcronymViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint for viewing Acronyms
"""
queryset = Acronym.objects.all().order_by('name')
serializer_class = AcronymSerializer
serializer_classes = {
'list': AcronymListSerializer,
'retrieve': AcronymSerializer,
}
default_serializer_class = AcronymListSerializer
filter_backends = [filters.SearchFilter]
search_fields = ['name']
def get_serializer_class(self):
return self.serializer_classes.get(self.action, self.default_serializer_class)
class TagViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Tag.objects.all().order_by("name")
serializer_class = TagSerializer
2020-06-13 21:18:58 +02:00
##### DEBUG views #####
def debug_css(request):
css, source_map = get_css(debug=True)
return HttpResponse(css, content_type="text/css")
def debug_css_sourcemap(request):
css, source_map = get_css(debug=True)
return HttpResponse(source_map, content_type="application/json")