-
Notifications
You must be signed in to change notification settings - Fork 21
/
utilities.py
34 lines (27 loc) · 998 Bytes
/
utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from django.conf import settings
from django.core.exceptions import ValidationError
def validate_only_one_instance(obj):
"""
Allow only one instance
"""
model = obj.__class__
if (model.objects.count() > 0 and
obj.id != model.objects.get().id):
raise ValidationError("We're sorry but a %s already exists, only one is allowed." % model.__name__)
def replace_tags(string=None, tags=dict()):
"""
Replace tags from a given string
"""
if string is None:
raise TypeError("string type needed")
else:
for key, value in tags.items():
string = string.replace(key, value)
return string
def has_recaptcha():
"""
Check if the WagtailCaptcha settings are properly set
"""
wagtailcaptcha_public_key = getattr(settings, 'RECAPTCHA_PUBLIC_KEY', None)
wagtailcaptcha_private_key = getattr(settings, 'RECAPTCHA_PRIVATE_KEY', None)
return wagtailcaptcha_public_key and wagtailcaptcha_private_key