<commit_before># -*- coding:utf-8 -*-

import json

import tornado
import tornado.escape
import tornado.web
import wtforms.validators
from wtforms.fields import StringField
from wtforms.validators import Required
from wtforms_tornado import Form

import config
from torcms.core import tools
from torcms.core.base_handler import BaseHandler
from torcms.core.tool.send_email import send_mail
from torcms.model.user_model import MUser
from torcms.core.tools import logger


class SumForm(Form):
    user_name = StringField('user_name', validators=[Required()])
    user_pass = StringField('user_pass', validators=[Required()])
    user_email = StringField('user_email', validators=[Required(), wtforms.validators.Email()])


class SumForm_info(Form):
    user_email = StringField('user_email', validators=[Required(), wtforms.validators.Email()])


class SumForm_pass(Form):
    user_pass = StringField('user_pass', validators=[Required()])


class UserHandler(BaseHandler):
    def initialize(self):
        super(UserHandler, self).initialize()

    def get(self, *args, **kwargs):

        url_str = args[0]
        url_arr = self.parse_url(url_str)

        if url_str == 'regist':
            if self.get_current_user():
                self.redirect('/')
            else:
                self.__to_register__()
        elif url_str == 'login':
            self.to_login()
        elif url_str == 'info':
            self.show_info()
        elif url_str == 'logout':
            self.logout()
        elif url_str == 'reset-password':
            self.to_reset_password()
        elif url_str == 'changepass':
            self.changepass()

        elif url_str == 'changeinfo':
            self.change_info()
        elif url_str == 'reset-passwd':
            if self.gen_passwd():
                pass
            else:
                self.redirect(config.SITE_CFG['site_url'])
        elif url_arr[0] == 'changerole':
            self.change_role(url_arr[1])
        elif url_str == 'find':

            self.to_find()

        elif url_arr[0] == 'find':

            self.find(url_arr[1])
        elif url_arr[0] == 'delete_user':
            self.delete(url_arr[1])

    def post(self, url_str):

        url_arr = self.parse_url(url_str)

        if url_str == 'regist':
            self.register()
        elif url_str == 'j_regist':
            self.json_register()
        elif url_str == 'j_changeinfo':

            self.json_changeinfo()
        elif url_str == 'j_changepass':

            self.json_changepass()
        elif url_str == 'login':
            self.login()
        elif url_str == 'changepass':
            self.changepassword()
        elif url_arr[0] == 'changepass':
            self.p_changepassword()
        elif url_str == 'changeinfo':
            self.changeinfo()
        elif url_arr[0] == 'changeinfo':
            self.p_changeinfo()
        elif url_str == 'find':
            self.post_find()
        elif url_arr[0] == 'find':
            self.find(url_arr[1])
        elif url_str == 'reset-password':
            self.reset_password()
        elif url_arr[0] == 'changerole':
            self.changerole(url_arr[1])

    @tornado.web.authenticated
    def p_changepassword(self):

        post_data = self.get_post_data()

        usercheck = MUser.check_user(self.userinfo.uid, post_data['rawpass'])
        if usercheck == 1:
            MUser.update_pass(self.userinfo.uid, post_data['user_pass'])
            output = {'changepass ': usercheck}
        else:
            output = {'changepass ': 0}
        return json.dump(output, self)

    @tornado.web.authenticated
    def p_changeinfo(self):
        '''
        Change Infor via Ajax.
        :return:
        '''
        post_data = self.get_post_data()
        usercheck = MUser.check_user(self.userinfo.uid, post_data['rawpass'])
        if usercheck == 1:
            MUser.update_info(self.userinfo.uid, post_data['user_email'])
            output = {'changeinfo ': usercheck}
        else:
            output = {'changeinfo ': 0}
        return json.dump(output, self)

    @tornado.web.authenticated
    def changepassword(self):
        post_data = self.get_post_data()

        uu = MUser.check_user(self.userinfo.uid, post_data['rawpass'])
        if uu == 1:
            MUser.update_pass(self.userinfo.uid, post_data['user_pass'])
            self.redirect('/user/info')
        else:
            kwd = {
                'info': '原密码输入错误，请重新输入',
                'link': '/user/changepass',
            }
            self.render('misc/html/404.html',
                        kwd=kwd,
                        userinfo=self.userinfo)

    @tornado.web.authenticated
    def changeinfo(self):

        post_data = self.get_post_data()

        uu = MUser.check_user(self.userinfo.uid, post_data['rawpass'])

        if uu == 1:
            MUser.update_info(self.userinfo.uid, post_data['user_email'])
            self.redirect(('/user/info'))
        else:
            kwd = {
                'info': '密码输入错误。',
                'link': '/user/changeinfo',
            }
            self.render('misc/html/404.html',
                        kwd=kwd,
                        userinfo=self.userinfo)

    @tornado.web.authenticated
    def changerole(self, xg_username):
        post_data = self.get_post_data()

        # if self.tmpl_router == "user":
        MUser.update_role(xg_username, post_data['role'])
        self.redirect('/user/info')
        # else:
        #     if MUser.update_role(xg_username, post_data['role']):
        #         output = {'del_category ': 1}
        #     else:
        #         output = {'del_category ': 0}
        #     return json.dump(output, self)

    @tornado.web.authenticated
    def logout(self):
        self.clear_all_cookies()
        self.redirect('/')

    @tornado.web.authenticated
    def changepass(self):

        self.render(self.wrap_tmpl('user/{sig}user_changepass.html'),
                    userinfo=self.userinfo)

    @tornado.web.authenticated
    def change_info(self):
        self.render(self.wrap_tmpl('user/{sig}user_changeinfo.html'),
                    userinfo=self.userinfo)

    @tornado.web.authenticated
    def change_role(self, xg_username):
        self.render('user/user_changerole.html',
                    userinfo=MUser.get_by_name(xg_username))

    @tornado.web.authenticated
    def to_find(self, ):
        kwd = {
            'pager': '',
        }
        self.render(self.wrap_tmpl('user/{sig}user_find_list.html'),
                    cfg=config.CMS_CFG,
                    kwd=kwd,
                    view=MUser.get_by_keyword(""),
                    userinfo=self.userinfo)

    @tornado.web.authenticated
    def show_info(self):
        self.render(self.wrap_tmpl('user/{sig}user_info.html'),
                    userinfo=self.userinfo)

    def to_reset_password(self):
        self.render('user/user_reset_password.html',
                    userinfo=self.userinfo, )

    def to_login(self):
        next_url = self.get_argument("next", "/")
        if self.get_current_user():
            self.redirect(next_url)
        else:
            kwd = {
                'pager': '',
                'next_url': next_url,
            }
            self.render('user/user_login.html',
                        kwd=kwd,
                        userinfo=None)

    def __check_valid(self, post_data):
        user_create_status = {'success': False, 'code': '00'}

        if not tools.check_username_valid(post_data['user_name']):
            user_create_status['code'] = '11'
            return user_create_status
        elif not tools.check_email_valid(post_data['user_email']):
            user_create_status['code'] = '21'
            return user_create_status
        elif MUser.get_by_name(post_data['user_name']):
            user_create_status['code'] = '12'
            return user_create_status
        elif MUser.get_by_email(post_data['user_email']):
            user_create_status['code'] = '22'
            return user_create_status

        user_create_status['success'] = True
        return user_create_status

    def __check_valid_info(self, post_data):
        user_create_status = {'success': False, 'code': '00'}

        if not tools.check_email_valid(post_data['user_email']):
            user_create_status['code'] = '21'
            return user_create_status
        elif MUser.get_by_email(post_data['user_email']):
            user_create_status['code'] = '22'
            return user_create_status

        user_create_status['success'] = True
        return user_create_status

    def __check_valid_pass(self, post_data):
        user_create_status = {'success': False, 'code': '00'}
        #    user_create_status['code'] = '31'
        #    return user_create_status


        user_create_status['success'] = True
        return user_create_status

    def register(self):
        post_data = self.get_post_data()

        form = SumForm(self.request.arguments)
        ckemail = MUser.get_by_email(post_data['user_email'])
        if ckemail is None:
            pass
        else:
            kwd = {
                'info': '邮箱已经存在，请更换邮箱。',
                'link': '/user/regist',
            }
            self.set_status(400)
            self.render('misc/html/404.html',
                        cfg=config.CMS_CFG,
                        kwd=kwd,
                        userinfo=None)
        if form.validate():
            res_dic = MUser.create_user(post_data)
            if res_dic['success']:
                self.redirect('/user/login')
            else:
                kwd = {
                    'info': '注册不成功',
                    'link': '/user/regist',
                }
                self.set_status(400)
                self.render('misc/html/404.html',
                            cfg=config.CMS_CFG,
                            kwd=kwd,<commit_msg>修改用户注册，添加重复用户名提示。<commit_after># -*- coding:utf-8 -*-

import json

import tornado
import tornado.escape
import tornado.web
import wtforms.validators
from wtforms.fields import StringField
from wtforms.validators import Required
from wtforms_tornado import Form

import config
from torcms.core import tools
from torcms.core.base_handler import BaseHandler
from torcms.core.tool.send_email import send_mail
from torcms.model.user_model import MUser
from torcms.core.tools import logger


class SumForm(Form):
    user_name = StringField('user_name', validators=[Required()])
    user_pass = StringField('user_pass', validators=[Required()])
    user_email = StringField('user_email', validators=[Required(), wtforms.validators.Email()])


class SumForm_info(Form):
    user_email = StringField('user_email', validators=[Required(), wtforms.validators.Email()])


class SumForm_pass(Form):
    user_pass = StringField('user_pass', validators=[Required()])


class UserHandler(BaseHandler):
    def initialize(self):
        super(UserHandler, self).initialize()

    def get(self, *args, **kwargs):

        url_str = args[0]
        url_arr = self.parse_url(url_str)

        if url_str == 'regist':
            if self.get_current_user():
                self.redirect('/')
            else:
                self.__to_register__()
        elif url_str == 'login':
            self.to_login()
        elif url_str == 'info':
            self.show_info()
        elif url_str == 'logout':
            self.logout()
        elif url_str == 'reset-password':
            self.to_reset_password()
        elif url_str == 'changepass':
            self.changepass()

        elif url_str == 'changeinfo':
            self.change_info()
        elif url_str == 'reset-passwd':
            if self.gen_passwd():
                pass
            else:
                self.redirect(config.SITE_CFG['site_url'])
        elif url_arr[0] == 'changerole':
            self.change_role(url_arr[1])
        elif url_str == 'find':

            self.to_find()

        elif url_arr[0] == 'find':

            self.find(url_arr[1])
        elif url_arr[0] == 'delete_user':
            self.delete(url_arr[1])

    def post(self, url_str):

        url_arr = self.parse_url(url_str)

        if url_str == 'regist':
            self.register()
        elif url_str == 'j_regist':
            self.json_register()
        elif url_str == 'j_changeinfo':

            self.json_changeinfo()
        elif url_str == 'j_changepass':

            self.json_changepass()
        elif url_str == 'login':
            self.login()
        elif url_str == 'changepass':
            self.changepassword()
        elif url_arr[0] == 'changepass':
            self.p_changepassword()
        elif url_str == 'changeinfo':
            self.changeinfo()
        elif url_arr[0] == 'changeinfo':
            self.p_changeinfo()
        elif url_str == 'find':
            self.post_find()
        elif url_arr[0] == 'find':
            self.find(url_arr[1])
        elif url_str == 'reset-password':
            self.reset_password()
        elif url_arr[0] == 'changerole':
            self.changerole(url_arr[1])

    @tornado.web.authenticated
    def p_changepassword(self):

        post_data = self.get_post_data()

        usercheck = MUser.check_user(self.userinfo.uid, post_data['rawpass'])
        if usercheck == 1:
            MUser.update_pass(self.userinfo.uid, post_data['user_pass'])
            output = {'changepass ': usercheck}
        else:
            output = {'changepass ': 0}
        return json.dump(output, self)

    @tornado.web.authenticated
    def p_changeinfo(self):
        '''
        Change Infor via Ajax.
        :return:
        '''
        post_data = self.get_post_data()
        usercheck = MUser.check_user(self.userinfo.uid, post_data['rawpass'])
        if usercheck == 1:
            MUser.update_info(self.userinfo.uid, post_data['user_email'])
            output = {'changeinfo ': usercheck}
        else:
            output = {'changeinfo ': 0}
        return json.dump(output, self)

    @tornado.web.authenticated
    def changepassword(self):
        post_data = self.get_post_data()

        uu = MUser.check_user(self.userinfo.uid, post_data['rawpass'])
        if uu == 1:
            MUser.update_pass(self.userinfo.uid, post_data['user_pass'])
            self.redirect('/user/info')
        else:
            kwd = {
                'info': '原密码输入错误，请重新输入',
                'link': '/user/changepass',
            }
            self.render('misc/html/404.html',
                        kwd=kwd,
                        userinfo=self.userinfo)

    @tornado.web.authenticated
    def changeinfo(self):

        post_data = self.get_post_data()

        uu = MUser.check_user(self.userinfo.uid, post_data['rawpass'])

        if uu == 1:
            MUser.update_info(self.userinfo.uid, post_data['user_email'])
            self.redirect(('/user/info'))
        else:
            kwd = {
                'info': '密码输入错误。',
                'link': '/user/changeinfo',
            }
            self.render('misc/html/404.html',
                        kwd=kwd,
                        userinfo=self.userinfo)

    @tornado.web.authenticated
    def changerole(self, xg_username):
        post_data = self.get_post_data()

        # if self.tmpl_router == "user":
        MUser.update_role(xg_username, post_data['role'])
        self.redirect('/user/info')
        # else:
        #     if MUser.update_role(xg_username, post_data['role']):
        #         output = {'del_category ': 1}
        #     else:
        #         output = {'del_category ': 0}
        #     return json.dump(output, self)

    @tornado.web.authenticated
    def logout(self):
        self.clear_all_cookies()
        self.redirect('/')

    @tornado.web.authenticated
    def changepass(self):

        self.render(self.wrap_tmpl('user/{sig}user_changepass.html'),
                    userinfo=self.userinfo)

    @tornado.web.authenticated
    def change_info(self):
        self.render(self.wrap_tmpl('user/{sig}user_changeinfo.html'),
                    userinfo=self.userinfo)

    @tornado.web.authenticated
    def change_role(self, xg_username):
        self.render('user/user_changerole.html',
                    userinfo=MUser.get_by_name(xg_username))

    @tornado.web.authenticated
    def to_find(self, ):
        kwd = {
            'pager': '',
        }
        self.render(self.wrap_tmpl('user/{sig}user_find_list.html'),
                    cfg=config.CMS_CFG,
                    kwd=kwd,
                    view=MUser.get_by_keyword(""),
                    userinfo=self.userinfo)

    @tornado.web.authenticated
    def show_info(self):
        self.render(self.wrap_tmpl('user/{sig}user_info.html'),
                    userinfo=self.userinfo)

    def to_reset_password(self):
        self.render('user/user_reset_password.html',
                    userinfo=self.userinfo, )

    def to_login(self):
        next_url = self.get_argument("next", "/")
        if self.get_current_user():
            self.redirect(next_url)
        else:
            kwd = {
                'pager': '',
                'next_url': next_url,
            }
            self.render('user/user_login.html',
                        kwd=kwd,
                        userinfo=None)

    def __check_valid(self, post_data):
        user_create_status = {'success': False, 'code': '00'}

        if not tools.check_username_valid(post_data['user_name']):
            user_create_status['code'] = '11'
            return user_create_status
        elif not tools.check_email_valid(post_data['user_email']):
            user_create_status['code'] = '21'
            return user_create_status
        elif MUser.get_by_name(post_data['user_name']):
            user_create_status['code'] = '12'
            return user_create_status
        elif MUser.get_by_email(post_data['user_email']):
            user_create_status['code'] = '22'
            return user_create_status

        user_create_status['success'] = True
        return user_create_status

    def __check_valid_info(self, post_data):
        user_create_status = {'success': False, 'code': '00'}

        if not tools.check_email_valid(post_data['user_email']):
            user_create_status['code'] = '21'
            return user_create_status
        elif MUser.get_by_email(post_data['user_email']):
            user_create_status['code'] = '22'
            return user_create_status

        user_create_status['success'] = True
        return user_create_status

    def __check_valid_pass(self, post_data):
        user_create_status = {'success': False, 'code': '00'}
        #    user_create_status['code'] = '31'
        #    return user_create_status


        user_create_status['success'] = True
        return user_create_status

    def register(self):
        post_data = self.get_post_data()

        form = SumForm(self.request.arguments)
        ckname = MUser.get_by_name(post_data['user_name'])
        ckemail = MUser.get_by_email(post_data['user_email'])
        if ckname is None:
            pass
        else:
            kwd = {
                'info': '用户名已存在，请更换用户名。',
                'link': '/user/regist',
            }
            self.set_status(400)
            self.render('misc/html/404.html',
                        cfg=config.CMS_CFG,
                        kwd=kwd,
                        userinfo=None)
        if ckemail is None:
            pass
        else:
            kwd = {
                'info': '邮箱已经存在，请更换邮箱。',
                'link': '/user/regist',
            }
            self.set_status(400)
            self.render('misc/html/404.html',
                        cfg=config.CMS_CFG,
                        kwd=kwd,
                        userinfo=None)
        if form.validate():
            res_dic = MUser.create_user(post_data)
            if res_dic['success']:
                self.redirect('/user/login')
            else:
                kwd = {
                    'info': '注册不成功',
                    'link': '/user/regist',
                }
                self.set_status(400)
                self.render('misc/html/404.html',
                            cfg=config.CMS_CFG,
                            kwd=kwd,
{'fraction_non_alphanumeric': 0.11908284023668639, 'fraction_numerical': 0.00621301775147929, 'mean_word_length': 1.8832812055729315, 'pattern_counts': {'":': 2, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6865042', 'n_tokens_mistral': 6387, 'n_tokens_neox': 6097, 'n_words': 1005}