diff --git a/MANIFEST.in b/MANIFEST.in index 11b7d77..ef41f52 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -6,3 +6,5 @@ recursive-include compte_agglo_montpellier/apps/feed_plugin/templates *.html recursive-include compte_agglo_montpellier/apps/feed_plugin/locale *.po *.mo include local_settings.py.example include requirements.txt +include MANIFEST.in +include VERSION diff --git a/compte_agglo_montpellier/apps/feed_plugin/cms_plugins.py b/compte_agglo_montpellier/apps/feed_plugin/cms_plugins.py index c9684d9..403aa19 100644 --- a/compte_agglo_montpellier/apps/feed_plugin/cms_plugins.py +++ b/compte_agglo_montpellier/apps/feed_plugin/cms_plugins.py @@ -13,6 +13,10 @@ from cms.plugin_pool import plugin_pool from . import models from . import forms + +logger = logging.getLogger(__name__) + + class SelectUserFeedPlugin(CMSPluginBase): model = models.SelectUserFeed name = _('select user feeds') @@ -45,6 +49,7 @@ class ShowUserFeedPlugin(CMSPluginBase): def get_feeds(self, instance, user): entries = [] + logger.debug('loading RSS feeds of user %s', user.username) for pref in models.FeedPreference.objects.filter(user=user): feed = feedparser.parse(pref.feed.url) for entry in feed.entries: @@ -55,6 +60,7 @@ class ShowUserFeedPlugin(CMSPluginBase): break entries.append((date, entry.title, entry.link, pref.feed.name, pref.feed.color_hex, pref.feed.css_classes)) + logger.debug('loading finished of %s entries', len(entries)) entries.sort(reverse=True) entries = entries[:instance.limit] return [dict(title=title, link=link, feed_name=feed_name, diff --git a/compte_agglo_montpellier/apps/feed_plugin/forms.py b/compte_agglo_montpellier/apps/feed_plugin/forms.py index c7ab439..5506fd4 100644 --- a/compte_agglo_montpellier/apps/feed_plugin/forms.py +++ b/compte_agglo_montpellier/apps/feed_plugin/forms.py @@ -1,8 +1,10 @@ from django import forms from django.utils.translation import ugettext as _ -import models +from . import models +from . import widgets class FeedForm(forms.Form): feeds = forms.ModelMultipleChoiceField(queryset=models.Feed.objects.all(), - label=_('Your feeds'), widget=forms.CheckboxSelectMultiple) + label=_('Your feeds'), widget=widgets.CheckboxMultipleSelect, + required=False) diff --git a/compte_agglo_montpellier/apps/feed_plugin/templates/feed_plugin/select_user_feed.html b/compte_agglo_montpellier/apps/feed_plugin/templates/feed_plugin/select_user_feed.html index cc6de55..41ec1aa 100644 --- a/compte_agglo_montpellier/apps/feed_plugin/templates/feed_plugin/select_user_feed.html +++ b/compte_agglo_montpellier/apps/feed_plugin/templates/feed_plugin/select_user_feed.html @@ -1,6 +1,15 @@ {% load i18n %} -
+ {% csrf_token %} - {{ form.feeds }} +
diff --git a/compte_agglo_montpellier/apps/feed_plugin/widgets.py b/compte_agglo_montpellier/apps/feed_plugin/widgets.py new file mode 100644 index 0000000..30a7262 --- /dev/null +++ b/compte_agglo_montpellier/apps/feed_plugin/widgets.py @@ -0,0 +1,104 @@ +from itertools import chain + +from django.forms.widgets import SubWidget, SelectMultiple +from django.forms.util import flatatt +from django.utils.html import conditional_escape +from django.utils.encoding import StrAndUnicode, force_unicode +from django.utils.safestring import mark_safe + +class CheckboxInput(SubWidget): + """ + An object used by CheckboxRenderer that represents a single + . + """ + def __init__(self, name, value, attrs, choice, index): + self.name, self.value = name, value + self.attrs = attrs + self.choice_value = force_unicode(choice[0]) + self.choice_label = force_unicode(choice[1]) + self.index = index + + def __unicode__(self): + return self.render() + + def render(self, name=None, value=None, attrs=None, choices=()): + name = name or self.name + value = value or self.value + attrs = attrs or self.attrs + + if 'id' in self.attrs: + label_for = ' for="%s_%s"' % (self.attrs['id'], self.index) + else: + label_for = '' + choice_label = conditional_escape(force_unicode(self.choice_label)) + return mark_safe(u'%s %s' % (label_for, self.tag(), choice_label)) + + def is_checked(self): + return self.choice_value in self.value + + def tag(self): + if 'id' in self.attrs: + self.attrs['id'] = '%s_%s' % (self.attrs['id'], self.index) + final_attrs = dict(self.attrs, type='checkbox', name=self.name, value=self.choice_value) + if self.is_checked(): + final_attrs['checked'] = 'checked' + return mark_safe(u'' % flatatt(final_attrs)) + +class CheckboxRenderer(StrAndUnicode): + def __init__(self, name, value, attrs, choices): + self.name, self.value, self.attrs = name, value, attrs + self.choices = choices + + def __iter__(self): + for i, choice in enumerate(self.choices): + yield CheckboxInput(self.name, self.value, self.attrs.copy(), choice, i) + + def __getitem__(self, idx): + choice = self.choices[idx] # Let the IndexError propogate + return CheckboxInput(self.name, self.value, self.attrs.copy(), choice, idx) + + def __unicode__(self): + return self.render() + + def render(self): + """Outputs a