Add views to genereate badges for the state of interns

This commit is contained in:
Kevin Alberts 2018-09-13 14:47:21 +02:00
parent 5555992f06
commit cfe4c07d84
Signed by: Kurocon
GPG key ID: BCD496FEBA0C6BC1
6 changed files with 60 additions and 2 deletions

View file

@ -6,3 +6,9 @@ def environment(request):
return {
'debug': settings.DEBUG
}
def base_url(request):
""" Template context processor adding a base URL variable to the context. """
return {
'base_url': settings.BASE_URL
}

View file

@ -8,4 +8,5 @@ urlpatterns = [
path('', CityList.as_view(), name='city_list'),
path('<city>/', AssociationList.as_view(), name='association_list'),
path('<city>/<slug>/', AssociationDetail.as_view(), name='association'),
path('<city>/<slug>/badge.svg', AssociationBadgeView.as_view(), name='association_badge'),
]

View file

@ -1,8 +1,13 @@
import lxml.html
from django import forms
from django.http import Http404, HttpResponse
from django.urls import reverse_lazy
from django.views import View
from django.views.generic import ListView, UpdateView
from dronken.models import City, Association, ExtraState
import anybadge
from dronken.models import City, Association
class CityList(ListView):
@ -55,3 +60,36 @@ class AssociationDetail(UpdateView):
def get_success_url(self):
return reverse_lazy('dronken:association_list', kwargs={'city': self.get_object().city.slug})
class AssociationBadgeView(View):
def get(self, *args, **kwargs):
city = kwargs['city']
slug = kwargs['slug']
try:
city_obj = City.objects.get(slug=city)
except City.DoesNotExist:
raise Http404
try:
association = Association.objects.get(city=city_obj, slug=slug)
except Association.DoesNotExist:
raise Http404
image = self.generate_badge(association)
return HttpResponse(image, content_type="image/svg+xml")
STATE_TO_COLOR = {
'nuchter': 'lightgrey',
'dronken': 'green',
'brak': 'yellowgreen',
}
def generate_badge(self, association):
badge = anybadge.Badge(lxml.html.fromstring(association.short_name).text_content(),
association.get_state_display(),
default_color=AssociationBadgeView.STATE_TO_COLOR.get(association.state, 'yellow'))
return badge.badge_svg_text