Add views to genereate badges for the state of interns
This commit is contained in:
		
							parent
							
								
									5555992f06
								
							
						
					
					
						commit
						cfe4c07d84
					
				
					 6 changed files with 60 additions and 2 deletions
				
			
		| 
						 | 
					@ -6,3 +6,9 @@ def environment(request):
 | 
				
			||||||
    return {
 | 
					    return {
 | 
				
			||||||
        'debug': settings.DEBUG
 | 
					        'debug': settings.DEBUG
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def base_url(request):
 | 
				
			||||||
 | 
					    """ Template context processor adding a base URL variable to the context. """
 | 
				
			||||||
 | 
					    return {
 | 
				
			||||||
 | 
					        'base_url': settings.BASE_URL
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,4 +8,5 @@ urlpatterns = [
 | 
				
			||||||
    path('', CityList.as_view(), name='city_list'),
 | 
					    path('', CityList.as_view(), name='city_list'),
 | 
				
			||||||
    path('<city>/', AssociationList.as_view(), name='association_list'),
 | 
					    path('<city>/', AssociationList.as_view(), name='association_list'),
 | 
				
			||||||
    path('<city>/<slug>/', AssociationDetail.as_view(), name='association'),
 | 
					    path('<city>/<slug>/', AssociationDetail.as_view(), name='association'),
 | 
				
			||||||
 | 
					    path('<city>/<slug>/badge.svg', AssociationBadgeView.as_view(), name='association_badge'),
 | 
				
			||||||
]
 | 
					]
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,8 +1,13 @@
 | 
				
			||||||
 | 
					import lxml.html
 | 
				
			||||||
from django import forms
 | 
					from django import forms
 | 
				
			||||||
 | 
					from django.http import Http404, HttpResponse
 | 
				
			||||||
from django.urls import reverse_lazy
 | 
					from django.urls import reverse_lazy
 | 
				
			||||||
 | 
					from django.views import View
 | 
				
			||||||
from django.views.generic import ListView, UpdateView
 | 
					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):
 | 
					class CityList(ListView):
 | 
				
			||||||
| 
						 | 
					@ -55,3 +60,36 @@ class AssociationDetail(UpdateView):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def get_success_url(self):
 | 
					    def get_success_url(self):
 | 
				
			||||||
        return reverse_lazy('dronken:association_list', kwargs={'city': self.get_object().city.slug})
 | 
					        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
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -65,6 +65,7 @@ TEMPLATES = [
 | 
				
			||||||
                'django.contrib.auth.context_processors.auth',
 | 
					                'django.contrib.auth.context_processors.auth',
 | 
				
			||||||
                'django.contrib.messages.context_processors.messages',
 | 
					                'django.contrib.messages.context_processors.messages',
 | 
				
			||||||
                'dronken.context_processors.environment',
 | 
					                'dronken.context_processors.environment',
 | 
				
			||||||
 | 
					                'dronken.context_processors.base_url',
 | 
				
			||||||
            ],
 | 
					            ],
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
| 
						 | 
					@ -121,3 +122,6 @@ USE_TZ = True
 | 
				
			||||||
# https://docs.djangoproject.com/en/2.0/howto/static-files/
 | 
					# https://docs.djangoproject.com/en/2.0/howto/static-files/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
STATIC_URL = '/static/'
 | 
					STATIC_URL = '/static/'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Base URL
 | 
				
			||||||
 | 
					BASE_URL = 'https://isonzeinterndronken.nl'
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1 +1,3 @@
 | 
				
			||||||
Django==2.0
 | 
					Django==2.0
 | 
				
			||||||
 | 
					anybadge==1.1.1
 | 
				
			||||||
 | 
					lxml==4.2.5
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -65,6 +65,11 @@
 | 
				
			||||||
            margin-bottom: 2em;
 | 
					            margin-bottom: 2em;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        .badge img {
 | 
				
			||||||
 | 
					            position: relative;
 | 
				
			||||||
 | 
					            top: 4px;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    </style>
 | 
					    </style>
 | 
				
			||||||
</head>
 | 
					</head>
 | 
				
			||||||
<body>
 | 
					<body>
 | 
				
			||||||
| 
						 | 
					@ -85,6 +90,8 @@
 | 
				
			||||||
    {% else %}
 | 
					    {% else %}
 | 
				
			||||||
        <h1>{{ association.short_name }} heeft helemaal geen intern!</h1>
 | 
					        <h1>{{ association.short_name }} heeft helemaal geen intern!</h1>
 | 
				
			||||||
    {% endif %}
 | 
					    {% endif %}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <p class="badge">Deel een badge!<br /><img src="{% url 'dronken:association_badge' city=association.city.slug slug=association.slug %}" /> <input onClick="this.select();" type="text" readonly value="{{ base_url }}{% url 'dronken:association_badge' city=association.city.slug slug=association.slug %}"/></p>
 | 
				
			||||||
</div>
 | 
					</div>
 | 
				
			||||||
</body>
 | 
					</body>
 | 
				
			||||||
</html>
 | 
					</html>
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue