From 160a5db5001b5fd836914112576791333a9a87fe Mon Sep 17 00:00:00 2001 From: bkfox Date: Fri, 8 Jul 2016 01:17:30 +0200 Subject: [PATCH] add missing files --- cms/forms.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ cms/utils.py | 22 ++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 cms/forms.py create mode 100644 cms/utils.py diff --git a/cms/forms.py b/cms/forms.py new file mode 100644 index 0000000..51d1879 --- /dev/null +++ b/cms/forms.py @@ -0,0 +1,47 @@ +import django.forms as forms +from django.utils.translation import ugettext as _, ugettext_lazy +from django.core.exceptions import ValidationError + +from honeypot.decorators import verify_honeypot_value + +import aircox.cms.models as models + + +class CommentForm(forms.ModelForm): + class Meta: + model = models.Comment + fields = ['author', 'email', 'url', 'content'] + localized_fields = '__all__' + widgets = { + 'author': forms.TextInput(attrs={ + 'placeholder': _('your name'), + }), + 'email': forms.TextInput(attrs={ + 'placeholder': _('your email (optional)'), + }), + 'url': forms.URLInput(attrs={ + 'placeholder': _('your website (optional)'), + }), + 'comment': forms.TextInput(attrs={ + 'placeholder': _('your lovely comment'), + }) + } + + def __init__(self, *args, **kwargs): + self.request = kwargs.pop('request', None) + self.thread = kwargs.pop('object', None) + super().__init__(*args, **kwargs) + + def clean(self): + super().clean() + if self.request: + if verify_honeypot_value(self.request, 'hp_website'): + raise ValidationError(_('You are a bot, that is not cool')) + + if not self.object: + raise ValidationError(_('No publication found for this comment')) + + + + + diff --git a/cms/utils.py b/cms/utils.py new file mode 100644 index 0000000..f86cfdb --- /dev/null +++ b/cms/utils.py @@ -0,0 +1,22 @@ +import aircox.cms.routes as routes + +def tags_to_html(model, tags, sep = ', '): + """ + Render tags as string of HTML urls. `self` can be a class, but in + this case, it `tags` must be provided. + + tags must be an iterator on taggit's Tag models (or similar) + + """ + website = model._website + r = [] + for tag in tags: + url = website.reverse(model, routes.TagsRoute, tags = tag.slug) + if url: + r.append('{name}'.format( + url = url, name = tag.name) + ) + else: + r.append(tag.name) + return sep.join(r) +