Coverage for /Users/marco/Code/django-simple-captcha/captcha/helpers.py: 93.67%

# Coverage for /Users/marco/Code/django-simple-captcha/captcha/helpers.py : 93.67%

## 65 statements 63 run 2 missing 0 excluded 3 partial

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | # -*- coding: utf-8 -*- import random from captcha.conf import settings import django 5 ↛ 6line 5 didn't jump to line 6, because the condition on line 5 was never trueif django.VERSION < (1, 10): # NOQA from django.core.urlresolvers import reverse # NOQA else: # NOQA from django.urls import reverse # NOQA from six import u, text_type def math_challenge(): operators = ('+', '*', '-',) operands = (random.randint(1, 10), random.randint(1, 10)) operator = random.choice(operators) if operands[0] < operands[1] and '-' == operator: operands = (operands[1], operands[0]) challenge = '%d%s%d' % (operands[0], operator, operands[1]) return '{}='.format( challenge.replace('*', settings.CAPTCHA_MATH_CHALLENGE_OPERATOR) ), text_type(eval(challenge)) def random_char_challenge(): chars, ret = u('abcdefghijklmnopqrstuvwxyz'), u('') for i in range(settings.CAPTCHA_LENGTH): ret += random.choice(chars) return ret.upper(), ret def unicode_challenge(): chars, ret = u('äàáëéèïíîöóòüúù'), u('') for i in range(settings.CAPTCHA_LENGTH): ret += random.choice(chars) return ret.upper(), ret def word_challenge(): fd = open(settings.CAPTCHA_WORDS_DICTIONARY, 'rb') l = fd.readlines() fd.close() while True: word = random.choice(l).strip() 44 ↛ 43line 44 didn't jump to line 43, because the condition on line 44 was never false if len(word) >= settings.CAPTCHA_DICTIONARY_MIN_LENGTH and len(word) <= settings.CAPTCHA_DICTIONARY_MAX_LENGTH: break return word.upper(), word.lower() def huge_words_and_punctuation_challenge(): "Yay, undocumneted. Mostly used to test Issue 39 - http://code.google.com/p/django-simple-captcha/issues/detail?id=39" fd = open(settings.CAPTCHA_WORDS_DICTIONARY, 'rb') l = fd.readlines() fd.close() word = '' while True: word1 = random.choice(l).strip() word2 = random.choice(l).strip() punct = random.choice(settings.CAPTCHA_PUNCTUATION) word = '%s%s%s' % (word1, punct, word2) 60 ↛ 56line 60 didn't jump to line 56, because the condition on line 60 was never false if len(word) >= settings.CAPTCHA_DICTIONARY_MIN_LENGTH and len(word) <= settings.CAPTCHA_DICTIONARY_MAX_LENGTH: break return word.upper(), word.lower() def noise_arcs(draw, image): size = image.size draw.arc([-20, -20, size[0], 20], 0, 295, fill=settings.CAPTCHA_FOREGROUND_COLOR) draw.line([-20, 20, size[0] + 20, size[1] - 20], fill=settings.CAPTCHA_FOREGROUND_COLOR) draw.line([-20, 0, size[0] + 20, size[1]], fill=settings.CAPTCHA_FOREGROUND_COLOR) return draw def noise_dots(draw, image): size = image.size for p in range(int(size[0] * size[1] * 0.1)): draw.point((random.randint(0, size[0]), random.randint(0, size[1])), fill=settings.CAPTCHA_FOREGROUND_COLOR) return draw def noise_null(draw, image): return draw def post_smooth(image): from PIL import ImageFilter return image.filter(ImageFilter.SMOOTH) def captcha_image_url(key): """Return url to image. Need for ajax refresh and, etc""" return reverse('captcha-image', args=[key])