Fixes #4076: Recommendations from collections.
[wolnelektury.git] / src / sponsors / widgets.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from django.conf import settings
5 from django import forms
6 from django.utils.safestring import mark_safe
7
8 from sponsors import models
9
10
11 class SponsorPageWidget(forms.Textarea):
12     class Media:
13         js = (
14             '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js',
15             '//code.jquery.com/ui/1.12.1/jquery-ui.min.js',
16             settings.STATIC_URL + 'sponsors/js/jquery.json.min.js',
17             settings.STATIC_URL + 'sponsors/js/footer_admin.js',
18         )
19         css = {
20             'all': (settings.STATIC_URL + 'sponsors/css/footer_admin.css',),
21         }
22
23     def render(self, name, value, attrs=None, renderer=None):
24         output = [super(SponsorPageWidget, self).render(name, value, attrs, renderer)]
25         sponsors = [(str(obj), obj.pk, obj.logo.url) for obj in models.Sponsor.objects.all().iterator()]
26         sponsors_js = ', '.join('{name: "%s", id: %d, image: "%s"}' % sponsor for sponsor in sponsors)
27         output.append('<script type="text/javascript">$(function(e) {')
28         # TODO: "id_" is hard-coded here. This should instead use the correct
29         # API to determine the ID dynamically.
30         output.append('$("#id_%s").sponsorsFooter({sponsors: [%s]}); });</script>\n' % (name, sponsors_js))
31         return mark_safe(''.join(output))