3bb586bcf8bdaac841f8669b5bb5df56d99dbc09
[wolnelektury.git] / apps / sponsors / widgets.py
1 from django.conf import settings
2 from django import forms
3 from django.utils.safestring import mark_safe
4
5 from sponsors import models
6
7
8 class SponsorPageWidget(forms.Textarea):
9     class Media:
10         js = (
11             'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js',
12             'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js',
13             settings.MEDIA_URL + 'sponsors/js/jquery.json.min.js',
14             settings.MEDIA_URL + 'sponsors/js/footer_admin.js',
15         )
16         css = {
17             'all': (settings.MEDIA_URL + 'sponsors/css/footer_admin.css',),
18         }
19
20     def render(self, name, value, attrs=None):
21         output = [super(SponsorPageWidget, self).render(name, value, attrs)]
22         sponsors = [(unicode(obj), obj.pk) for obj in models.Sponsor.objects.all()]
23         sponsors_js = ', '.join('{name: "%s", id: %d}' % sponsor for sponsor in sponsors)
24         output.append(u'<script type="text/javascript">addEvent(window, "load", function(e) {')
25         # TODO: "id_" is hard-coded here. This should instead use the correct
26         # API to determine the ID dynamically.
27         output.append(u'$("#id_%s").sponsorsFooter({sponsors: [%s]}); });</script>\n' % 
28             (name, sponsors_js))
29         return mark_safe(u''.join(output))