<commit_before>import sqlalchemy

from raggregate.models import DBSession
from raggregate.models import User
from raggregate.models import Ban

from raggregate import queries

from pyramid.view import view_config
from pyramid.httpexceptions import HTTPFound
from pyramid.httpexceptions import HTTPNotFound

from raggregate.login_adapters import fb
from raggregate.login_adapters import LoginAdapterExc

from datetime import timedelta

@view_config(renderer='login.mak', route_name='login')
def login(request):
    #@FIXME: this uses a request handling method with success with which I was experimenting
    # it is not used elsewhere and is a pain to read and write
    # success = False causes a page to stop drawing and "error out"
    # some error conditions therefore don't set success to false because it's more convenient
    # to draw the rest of the page.
    #
    # someone should adapt this to be less success-centric and read less branchy.
    s = request.session

    success = True

    # check for facebook login, provided by Facebook's JS SDK
    try:
        fb_cookie = fb.extract_from_cookie(request)
        try:
            u = queries.get_user_by_name(fb_cookie['local_username'])
        except sqlalchemy.orm.exc.NoResultFound:
            u = fb.create_local_user(fb_cookie['info'], fb_cookie['local_username'], request = request)
        try:
            queries.login_user(request, u, None, bypass_password = True)
        except LoginAdapterExc:
            pass
    except LoginAdapterExc:
        pass

    if 'logout' in request.session['safe_params']:
        if 'logged_in' in s:
            del s['logged_in']
            del s['users.id']
            if 'u_fbgraph' in s:
                del s['u_fbgraph']
                del s['u_fbinfo']
            if 'u_twit' in s:
                del s['u_twit']
            s['message'] = "You have been logged out, thanks."
            success = True
        else:
            s['message'] = "You are not logged in."
            success = True
    else:
        logged_in = False
        if 'logged_in' in s:
            s['message'] = "You are already logged in."
            logged_in = True
        else:
            if 'message' not in s:
                if 'last_login_status' in s:
                    s['message'] = s['last_login_status']
                    del s['last_login_status']
                else:
                    s['message'] = "Please log in."
        p = request.session['safe_post']
        prm = request.session['safe_params']
        username = None
        if 'username' in prm:
            username = queries.strip_all_html(prm['username'])
        if p:
            dbsession = DBSession()
            if request.session['safe_get']['act'] == 'register':
                if logged_in:
                    try:
                        u = queries.get_user_by_id(s['users.id'])
                        if u.temporary:
                            queries.create_user(temp_to_perm = True, extant_id = s['users.id'], username = username, password = p['password'], origination = 'site')
                            s['message'] = "Your anonymous profile has been converted, thanks."
                        else:
                            s['message'] = "You can't register while you're logged in."
                    except sqlalchemy.exc.IntegrityError:
                        s['message'] = "This username is already registered, sorry."
                        dbsession.rollback()
                else:
                    try:
                        queries.create_user(username = username, password = p['password'], origination = 'site')
                        s['message'] = "Successfully registered."
                        success = True
                    except sqlalchemy.exc.IntegrityError:
                        s['message'] = "This username is already registered, sorry."
                        success = False
                        dbsession.rollback()
            elif request.session['safe_get']['act'] == 'update_pw':
                if p['new_password'] != p['new_password_confirm']:
                    s['message'] = 'New password doesn\'t match confirmation, please try again.'
                else:
                    u = queries.get_user_by_id(s['users.id'])
                    if u.verify_pw(p['old_password']):
                        u.password = u.hash_pw(p['new_password'])
                        dbsession.add(u)
                        s['message'] = 'Password updated.'
                        success = True
                    else:
                        s['message'] = 'Old password invalid.'
            else:
                try:
                    u = queries.get_user_by_name(username)
                    try:
                        queries.login_user(request, u, p['password'])
                        s['message'] = "Good, logged in"
                        success = True
                    except LoginAdapterExc:
                        s['message'] = "Incorrect password."
                        success = False
                except sqlalchemy.orm.exc.NoResultFound:
                    s['message'] = "Sorry, I don't know you."<commit_msg>Automatically redirect to main page on successful login<commit_after>import sqlalchemy

from raggregate.models import DBSession
from raggregate.models import User
from raggregate.models import Ban

from raggregate import queries

from pyramid.view import view_config
from pyramid.httpexceptions import HTTPFound
from pyramid.httpexceptions import HTTPNotFound

from raggregate.login_adapters import fb
from raggregate.login_adapters import LoginAdapterExc

from datetime import timedelta

@view_config(renderer='login.mak', route_name='login')
def login(request):
    #@FIXME: this uses a request handling method with success with which I was experimenting
    # it is not used elsewhere and is a pain to read and write
    # success = False causes a page to stop drawing and "error out"
    # some error conditions therefore don't set success to false because it's more convenient
    # to draw the rest of the page.
    #
    # someone should adapt this to be less success-centric and read less branchy.
    s = request.session

    success = True

    # check for facebook login, provided by Facebook's JS SDK
    try:
        fb_cookie = fb.extract_from_cookie(request)
        try:
            u = queries.get_user_by_name(fb_cookie['local_username'])
        except sqlalchemy.orm.exc.NoResultFound:
            u = fb.create_local_user(fb_cookie['info'], fb_cookie['local_username'], request = request)
        try:
            queries.login_user(request, u, None, bypass_password = True)
        except LoginAdapterExc:
            pass
    except LoginAdapterExc:
        pass

    if 'logout' in request.session['safe_params']:
        if 'logged_in' in s:
            del s['logged_in']
            del s['users.id']
            if 'u_fbgraph' in s:
                del s['u_fbgraph']
                del s['u_fbinfo']
            if 'u_twit' in s:
                del s['u_twit']
            s['message'] = "You have been logged out, thanks."
            success = True
        else:
            s['message'] = "You are not logged in."
            success = True
    else:
        logged_in = False
        if 'logged_in' in s:
            s['message'] = "You are already logged in."
            logged_in = True
        else:
            if 'message' not in s:
                if 'last_login_status' in s:
                    s['message'] = s['last_login_status']
                    del s['last_login_status']
                else:
                    s['message'] = "Please log in."
        p = request.session['safe_post']
        prm = request.session['safe_params']
        username = None
        if 'username' in prm:
            username = queries.strip_all_html(prm['username'])
        if p:
            dbsession = DBSession()
            if request.session['safe_get']['act'] == 'register':
                if logged_in:
                    try:
                        u = queries.get_user_by_id(s['users.id'])
                        if u.temporary:
                            queries.create_user(temp_to_perm = True, extant_id = s['users.id'], username = username, password = p['password'], origination = 'site')
                            s['message'] = "Your anonymous profile has been converted, thanks."
                        else:
                            s['message'] = "You can't register while you're logged in."
                    except sqlalchemy.exc.IntegrityError:
                        s['message'] = "This username is already registered, sorry."
                        dbsession.rollback()
                else:
                    try:
                        queries.create_user(username = username, password = p['password'], origination = 'site')
                        s['message'] = "Successfully registered."
                        success = True
                    except sqlalchemy.exc.IntegrityError:
                        s['message'] = "This username is already registered, sorry."
                        success = False
                        dbsession.rollback()
            elif request.session['safe_get']['act'] == 'update_pw':
                if p['new_password'] != p['new_password_confirm']:
                    s['message'] = 'New password doesn\'t match confirmation, please try again.'
                else:
                    u = queries.get_user_by_id(s['users.id'])
                    if u.verify_pw(p['old_password']):
                        u.password = u.hash_pw(p['new_password'])
                        dbsession.add(u)
                        s['message'] = 'Password updated.'
                        success = True
                    else:
                        s['message'] = 'Old password invalid.'
            else:
                try:
                    u = queries.get_user_by_name(username)
                    try:
                        queries.login_user(request, u, p['password'])
                        s['message'] = "Good, logged in"
                        success = True
                        return HTTPFound(request.route_url('post'))
                    except LoginAdapterExc:
                        s['message'] = "Incorrect password."
                        success = False
                except sqlalchemy.orm.exc.NoResultFound:
                    s['message'] = "Sorry, I don't know you."
{'fraction_non_alphanumeric': 0.09160812606797038, 'fraction_numerical': 0.0, 'mean_word_length': 1.607027963375402, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6505767', 'n_tokens_mistral': 2768, 'n_tokens_neox': 2604, 'n_words': 772}