<commit_before>from common import *

def randstring():
    return "".join(chr(randrange(33,127)) for i in range(8))

def valid_password(password, account):
    all_hashed = [account.hashed] + [pr.hashed for pr in account.passwordreset_set.all()]
    return any(bcrypt.hashpw(password, hashed) == hashed for hashed in all_hashed)

@all_renderable(ACCOUNTS)
class Root:
    @unrestricted
    def login(self, message="", **params):
        if "email" in params:
            try:
                account = Account.objects.get(email__iexact = params["email"])
                PasswordReset.objects.filter(generated__lt = datetime.now() - timedelta(days = 7)).delete()
                if not valid_password(params["password"], account):
                    message = "Incorrect password"
            except Account.DoesNotExist:
                message = "No account exists for that email address"
            
            if not message:
                cherrypy.session["account_id"] = account.id
                cherrypy.session["csrf_token"] = uuid4().hex
                raise HTTPRedirect("homepage")
        
        return {
            "message": message,
            "email":   params.get("email", "")
        }
    
    @unrestricted
    def homepage(self, message=""):
        if not cherrypy.session.get("account_id"):
            raise HTTPRedirect("login?message={}", "You are not logged in")
        return {"message": message}
    
    @unrestricted
    def logout(self):
        for key in ["account_id", "csrf_token"]:
            cherrypy.session.pop(key, None)
        raise HTTPRedirect("login?message={}", "You have been logged out")
    
    @unrestricted
    def reset(self, message="", email=None):
        if email is not None:
            account = Account.objects.filter(email=email)
            if not account:
                message = "No account exists for email address {!r}".format(email)
            else:
                account = account[0]
                password = <PASSWORD>()
                PasswordReset.objects.create(account=account, hashed=bcrypt.hashpw(password, bcrypt.gensalt()))
                data = {
                    "name": account.name,
                    "password":  password
                }
                send_email(ADMIN_EMAIL, account.email, "MAGFest Admin Password Reset",
                           render("accounts/reset_email.txt", data))
                raise HTTPRedirect("login?message={}", "Your new password has been emailed to you")
        
        return {
            "email":   email,
            "message": message
        }
    
    @unrestricted
    def change_password(self, message="", old_password=None, new_password=None, csrf_token=None):
        if not cherrypy.session.get("account_id"):
            raise HTTPRedirect("login?message={}", "You are not logged in")<commit_msg>made password resets case insensitive<commit_after>from common import *

def randstring():
    return "".join(chr(randrange(33,127)) for i in range(8))

def valid_password(password, account):
    all_hashed = [account.hashed] + [pr.hashed for pr in account.passwordreset_set.all()]
    return any(bcrypt.hashpw(password, hashed) == hashed for hashed in all_hashed)

@all_renderable(ACCOUNTS)
class Root:
    @unrestricted
    def login(self, message="", **params):
        if "email" in params:
            try:
                account = Account.objects.get(email__iexact = params["email"])
                PasswordReset.objects.filter(generated__lt = datetime.now() - timedelta(days = 7)).delete()
                if not valid_password(params["password"], account):
                    message = "Incorrect password"
            except Account.DoesNotExist:
                message = "No account exists for that email address"
            
            if not message:
                cherrypy.session["account_id"] = account.id
                cherrypy.session["csrf_token"] = uuid4().hex
                raise HTTPRedirect("homepage")
        
        return {
            "message": message,
            "email":   params.get("email", "")
        }
    
    @unrestricted
    def homepage(self, message=""):
        if not cherrypy.session.get("account_id"):
            raise HTTPRedirect("login?message={}", "You are not logged in")
        return {"message": message}
    
    @unrestricted
    def logout(self):
        for key in ["account_id", "csrf_token"]:
            cherrypy.session.pop(key, None)
        raise HTTPRedirect("login?message={}", "You have been logged out")
    
    @unrestricted
    def reset(self, message="", email=None):
        if email is not None:
            account = Account.objects.filter(email__iexact=email)
            if not account:
                message = "No account exists for email address {!r}".format(email)
            else:
                account = account[0]
                password = <PASSWORD>()
                PasswordReset.objects.create(account=account, hashed=bcrypt.hashpw(password, bcrypt.gensalt()))
                data = {
                    "name": account.name,
                    "password":  password
                }
                send_email(ADMIN_EMAIL, account.email, "MAGFest Admin Password Reset",
                           render("accounts/reset_email.txt", data))
                raise HTTPRedirect("login?message={}", "Your new password has been emailed to you")
        
        return {
            "email":   email,
            "message": message
        }
    
    @unrestricted
    def change_password(self, message="", old_password=<PASSWORD>, new_password=<PASSWORD>, csrf_token=None):
        if not cherrypy.session.get("account_id"):
            raise HTTPRedirect("login?message={}", "You are not logged in")
{'fraction_non_alphanumeric': 0.11434511434511435, 'fraction_numerical': 0.0031185031185031187, 'mean_word_length': 2.088817549491707, 'pattern_counts': {'":': 14, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7026485', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1555, 'n_words': 383}