|
| 1 | +from django.forms import PasswordInput |
| 2 | +from django.utils.safestring import mark_safe |
| 3 | +from django.utils.translation import gettext as _ |
| 4 | + |
| 5 | +# The PasswordStrengthInput and PasswordConfirmationInput widgets come from the |
| 6 | +# django-password-strength project, https://pypi.org/project/django-password-strength/ |
| 7 | +# |
| 8 | +# Original license: |
| 9 | +# |
| 10 | +# Copyright © 2015 A.J. May and individual contributors. All rights reserved. |
| 11 | +# |
| 12 | +# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the |
| 13 | +# following conditions are met: |
| 14 | +# |
| 15 | +# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following |
| 16 | +# disclaimer. |
| 17 | +# |
| 18 | +# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the |
| 19 | +# following disclaimer in the documentation and/or other materials provided with the distribution. |
| 20 | +# |
| 21 | +# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote |
| 22 | +# products derived from this software without specific prior written permission. |
| 23 | +# |
| 24 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 25 | +# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 26 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 | +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 28 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 29 | +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 30 | +# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +# |
| 32 | + |
| 33 | +class PasswordStrengthInput(PasswordInput): |
| 34 | + """ |
| 35 | + Form widget to show the user how strong his/her password is. |
| 36 | + """ |
| 37 | + |
| 38 | + def render(self, name, value, attrs=None, renderer=None): |
| 39 | + strength_markup = """ |
| 40 | + <div style="margin-top: 10px;"> |
| 41 | + <div class="progress" style="margin-bottom: 10px;"> |
| 42 | + <div class="progress-bar progress-bar-warning password_strength_bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="5" style="width: 0%%"></div> |
| 43 | + </div> |
| 44 | + <p class="text-muted password_strength_info hidden"> |
| 45 | + <span class="label label-danger"> |
| 46 | + %s |
| 47 | + </span> |
| 48 | + <span style="margin-left:5px;"> |
| 49 | + %s |
| 50 | + </span> |
| 51 | + </p> |
| 52 | + </div> |
| 53 | + """ % ( |
| 54 | + _("Warning"), |
| 55 | + _( |
| 56 | + 'This password would take <em class="password_strength_time"></em> to crack.' |
| 57 | + ), |
| 58 | + ) |
| 59 | + |
| 60 | + try: |
| 61 | + self.attrs["class"] = "%s password_strength".strip() % self.attrs["class"] |
| 62 | + except KeyError: |
| 63 | + self.attrs["class"] = "password_strength" |
| 64 | + |
| 65 | + return mark_safe( |
| 66 | + super(PasswordInput, self).render(name, value, attrs, renderer) |
| 67 | + + strength_markup |
| 68 | + ) |
| 69 | + |
| 70 | + class Media: |
| 71 | + js = ( |
| 72 | + "ietf/js/zxcvbn.js", |
| 73 | + "ietf/js/password_strength.js", |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +class PasswordConfirmationInput(PasswordInput): |
| 78 | + """ |
| 79 | + Form widget to confirm the users password by letting him/her type it again. |
| 80 | + """ |
| 81 | + |
| 82 | + def __init__(self, confirm_with=None, attrs=None, render_value=False): |
| 83 | + super(PasswordConfirmationInput, self).__init__(attrs, render_value) |
| 84 | + self.confirm_with = confirm_with |
| 85 | + |
| 86 | + def render(self, name, value, attrs=None, renderer=None): |
| 87 | + if self.confirm_with: |
| 88 | + self.attrs["data-confirm-with"] = "id_%s" % self.confirm_with |
| 89 | + |
| 90 | + confirmation_markup = """ |
| 91 | + <div style="margin-top: 10px;" class="hidden password_strength_info"> |
| 92 | + <p class="text-muted"> |
| 93 | + <span class="label label-danger"> |
| 94 | + %s |
| 95 | + </span> |
| 96 | + <span style="margin-left:5px;">%s</span> |
| 97 | + </p> |
| 98 | + </div> |
| 99 | + """ % ( |
| 100 | + _("Warning"), |
| 101 | + _("Your passwords don't match."), |
| 102 | + ) |
| 103 | + |
| 104 | + try: |
| 105 | + self.attrs["class"] = ( |
| 106 | + "%s password_confirmation".strip() % self.attrs["class"] |
| 107 | + ) |
| 108 | + except KeyError: |
| 109 | + self.attrs["class"] = "password_confirmation" |
| 110 | + |
| 111 | + return mark_safe( |
| 112 | + super(PasswordInput, self).render(name, value, attrs, renderer) |
| 113 | + + confirmation_markup |
| 114 | + ) |
0 commit comments