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