Esempio n. 1
0
def newResetPass(request, response):
    """
    View for the newer api's, doesn't expect .json|.xml in the url, but rather a middleware to provide the 
    response object based on the 'Accept' header
    
    """
    username = request.POST.get('username')
    if not username:
        username = request.POST.get('email')
        
    if not username:
        return response.send(errors='Username or email required.', status=404)
    #email address of the first entry in the ADMINS tuple (you should set it to something meaningful)
    try:
        sent_from = settings.ADMINS[0][1]
    except IndexError:
        return response.send(errors="Please supply an ADMIN email address in settings.py.", status=500) 
    
    try:
        user = User.objects.get(username=username)
    except User.DoesNotExist:
        if is_valid_email(username):
            try:
                user = User.objects.get(email=username)
            except User.DoesNotExist:
                return response.send(errors='user_not_found', status=404)
        else:
            return response.send(errors='user_not_found', status=404)
    
    if not user.email:
        return response.send(errors="no_email_for_user")
    
    newPassword = generateNewPassword()
    user.set_password(newPassword)
    user.save()
    send_mail('Password Reset',
              """The password for the login: "******" has been successfully reset, your new password is "%s",  
              please change this as soon as possible for your security.\n""" % (user.username, newPassword),
              '%s' % sent_from, [user.email, ])
    
    request.session['RESET_PASS'] = True
    return response.send()
Esempio n. 2
0
def validate_field(field, supplied):
    value = None
    if not supplied:
        if field.required:
            return None, "%s is a required field" % field.name
        else:
            return supplied, ''
    
    if field.max_length and field.max_length < len(supplied):
        return None, "%s must be less than %s characters." % (field.name, field.max_length)
    
    if field.type == 'email' and not utils.is_valid_email(supplied):
        return None, "%s is not valid" % field.name
    
    if field.type in ['datetime', 'date'] and supplied:
        value = utils.default_time_parse(supplied)
        if not value:
            return None, "%s is invalid" % field.name
    
    value = value or supplied
    return value, ''