Permalink
Browse files

You can set recaptcha nad akismet keys by settings now

  • Loading branch information...
maccesch committed Oct 28, 2010
1 parent c8aac40 commit 5e402ca9cdc2d4cdb09444852087a54139e6aa94
View
@@ -8,7 +8,7 @@ Spam protection is done through `glamkit-stopspam <http://github.com/maccesch/gl
If you want to use ReCAPTCHA you have to get a Public and Private Key from http://www.google.com/recaptcha. You can get them for free.
-The Akismet spam protection method requires an Aksimet API Key which is obtainable from http://akismet.com/ For private persons this is free, too.
+The Akismet spam protection method requires an Akismet API Key which is obtainable from http://akismet.com/ For private persons this is free, too.
For more Information on the spam protection methods see the glamkit-stopspam link below.
@@ -46,3 +46,15 @@ Unzip the file you downloaded. Then go in your terminal and ``cd`` into the unpa
Put "cmsplugin_contact" in your ``INSTALLED_APPS`` section in settings.py. Don't forget to syncdb your database.
+Settings
+========
+
+RECAPTCHA_PUBLIC_KEY and RECAPTCHA_PRIVATE_KEY
+----------------------------------------------
+
+If you don't want to enter the ReCATPCHA keys in the admin interface you can provide them through these settings.
+
+AKISMET_API_KEY
+---------------
+
+The same as for ReCAPTCHA goes fo Akismet.
View
@@ -1,4 +1,4 @@
-from django.forms import ModelForm, Field, CharField
+from django.forms import ModelForm, Field, CharField, HiddenInput
from django.forms.util import ErrorList
from django.core.exceptions import ValidationError
from django.contrib.sites.models import Site
@@ -15,15 +15,12 @@ def validate(self, value):
class ContactAdminForm(ModelForm):
akismet_api_key = KeyField(max_length=255, label=_("Akismet API Key"), help_text=_('Get a Wordpress Key from http://akismet.com/'))
- recaptcha_public_key = getattr(settings, "RECAPTCHA_PUBLIC_KEY", \
- KeyField(max_length=255, label=_("ReCAPTCHA Public Key"), help_text=_('Get this from http://www.google.com/recaptcha')))
- recaptcha_private_key = getattr(settings, "RECAPTCHA_PRIVATE_KEY", \
- KeyField(max_length=255, label=_("ReCAPTCHA Private Key"), help_text=_('Get this from http://www.google.com/recaptcha')))
+ recaptcha_public_key = KeyField(max_length=255, label=_("ReCAPTCHA Public Key"), help_text=_('Get this from http://www.google.com/recaptcha'))
+ recaptcha_private_key = KeyField(max_length=255, label=_("ReCAPTCHA Private Key"), help_text=_('Get this from http://www.google.com/recaptcha'))
class Meta:
model = Contact
-
def _add_error(self, field_name, error):
if not field_name in self._errors:
self._errors[field_name] = ErrorList()
@@ -38,16 +35,19 @@ def add_error(error):
except ImportError:
self._add_error('spam_protection_method', _('Akismet library is not installed. Use "easy_install akismet" or "pip install akismet".'))
- api_key = self.cleaned_data['akismet_api_key']
- if not api_key:
- add_error(Field.default_error_messages['required'])
- else:
- ak = Akismet(
- key = api_key,
- blog_url = 'http://%s/' % Site.objects.get(pk=settings.SITE_ID).domain
- )
- if not ak.verify_key():
- add_error(_('The API Key is not valid.'))
+ api_key = getattr(settings, "AKISMET_API_KEY", \
+ self.cleaned_data['akismet_api_key'])
+
+ if not hasattr(settings, "AKISMET_API_KEY"):
+ if not api_key:
+ add_error(Field.default_error_messages['required'])
+ else:
+ ak = Akismet(
+ key = api_key,
+ blog_url = 'http://%s/' % Site.objects.get(pk=settings.SITE_ID).domain
+ )
+ if not ak.verify_key():
+ add_error(_('The API Key is not valid.'))
def _check_recaptcha(self):
@@ -27,7 +27,7 @@ class ContactPlugin(CMSPluginBase):
def create_form(self, instance, request):
if instance.get_spam_protection_method_display() == 'Akismet':
- AkismetContactForm.aksimet_api_key = instance.aksimet_api_key
+ AkismetContactForm.aksimet_api_key = instance.akismet_api_key
FormClass = AkismetContactForm
elif instance.get_spam_protection_method_display() == 'ReCAPTCHA':
RecaptchaContactForm.recaptcha_public_key = getattr(settings, "RECAPTCHA_PUBLIC_KEY", \
@@ -81,7 +81,9 @@ def render(self, context, instance, placeholder):
def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
context.update({
- 'spam_protection_method': obj.spam_protection_method if obj else 0
+ 'spam_protection_method': obj.spam_protection_method if obj else 0,
+ 'recaptcha_settings': hasattr(settings, "RECAPTCHA_PUBLIC_KEY"),
+ 'akismet_settings': hasattr(settings, "AKISMET_API_KEY"),
})
return super(ContactPlugin, self).render_change_form(request, context, add, change, form_url, obj)
@@ -8,7 +8,9 @@
$('div.spam_protection_method').parent().children('.form-row').each(function(index, row) {
var visible = false;
$.each($(row).attr('class').split(/\s+/), function(index, class) {
- if (class == 'spam_protection_method' || class.indexOf(class_prefix) >= 0) {
+ if (class == 'spam_protection_method' || (class.indexOf(class_prefix) >= 0
+ {% if recaptcha_settings %} && class.indexOf("recaptcha_public_key") < 0 && class.indexOf("recaptcha_private_key") < 0{% endif %}
+ {% if akismet_settings %} && class.indexOf("akismet_api_key") < 0{% endif %})) {
visible = true;
}
});
@@ -43,13 +45,13 @@
margin-left: 13em;
}
- {% if spam_protection_method != 1 %}
+ {% if spam_protection_method != 1 or akismet_settings %}
div.form-row.akismet_api_key {
display: none;
}
{% endif %}
- {% if spam_protection_method != 2 %}
+ {% if spam_protection_method != 2 or recaptcha_settings %}
div.form-row.recaptcha_public_key {
display: none;
}

1 comment on commit 5e402ca

@powderflask

This comment has been minimized.

Show comment
Hide comment
@powderflask

powderflask Oct 30, 2010

Awesome - that works great. Thanks a ton!

Awesome - that works great. Thanks a ton!

Please sign in to comment.