Bladeren bron

코드 좀 정리

잉여개발기 (SPDV) 5 jaren geleden
bovenliggende
commit
e9268b76fd
2 gewijzigde bestanden met toevoegingen van 34 en 42 verwijderingen
  1. 32 37
      route/login_register.py
  2. 2 5
      route/user_tool.py

+ 32 - 37
route/login_register.py

@@ -23,53 +23,54 @@ def login_register_2(conn):
         else:
             captcha_post('', 0)
 
-        if flask.request.form.get('id', None) == '' or flask.request.form.get('pw', None) == '':
+        user_id = flask.request.form.get('id', '')
+        user_pw = flask.request.form.get('pw', '')
+        user_repeat = flask.request.form.get('pw2', '')
+        if user_id == '' or user_pw == '':
             return re_error('/error/27')
 
-        if flask.request.form.get('pw', None) != flask.request.form.get('pw2', None):
+        if user_pw != user_repeat:
             return re_error('/error/20')
 
-        if re.search(r'(?:[^A-Za-zㄱ-힣0-9])', flask.request.form.get('id', None)):
+        if re.search(r'(?:[^A-Za-zㄱ-힣0-9])', user_id):
             return re_error('/error/8')
 
         curs.execute(db_change('select html from html_filter where kind = "name"'))
         set_d = curs.fetchall()
         for i in set_d:
             check_r = re.compile(i[0], re.I)
-            if check_r.search(flask.request.form.get('id', None)):
+            if check_r.search(user_id):
                 return re_error('/error/8')
 
-        if len(flask.request.form.get('id', None)) > 32:
+        if len(user_id) > 32:
             return re_error('/error/7')
 
-        curs.execute(db_change("select id from user where id = ?"), [flask.request.form.get('id', None)])
+        curs.execute(db_change("select id from user where id = ?"), [user_id])
         if curs.fetchall():
             return re_error('/error/6')
 
-        hashed = pw_encode(flask.request.form.get('pw', None))
+        hashed = pw_encode(user_pw)
+        ans_q = flask.request.form.get('approval_question_answer', '')
 
         curs.execute(db_change('select data from other where name = "requires_approval"'))
         requires_approval = curs.fetchall()
         requires_approval = requires_approval and requires_approval[0][0] == 'on'
         requires_approval = None if admin == 1 else requires_approval
-
-        approval_question = ''
         if requires_approval:
             curs.execute(db_change('select data from other where name = "approval_question"'))
             approval_question = curs.fetchall()
-            if approval_question and approval_question[0][0]:
-                approval_question = approval_question[0][0]
-            else:
-                approval_question = ''
+            approval_question = approval_question[0][0] if approval_question and approval_question[0][0] else ''
+        else:
+            approval_question = ''
 
         curs.execute(db_change('select data from other where name = "email_have"'))
         sql_data = curs.fetchall()
         if sql_data and sql_data[0][0] != '' and admin != 1:
-            flask.session['c_id'] = flask.request.form.get('id', None)
+            flask.session['c_id'] = user_id
             flask.session['c_pw'] = hashed
-            flask.session['c_key'] = ''.join(random.choice("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") for i in range(16))
+            flask.session['c_key'] = ''.join(random.choice("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") for i in range(64))
             if requires_approval:
-                flask.session['c_ans'] = flask.request.form.get('approval_question_answer')
+                flask.session['c_ans'] = flask.request.form.get('approval_question_answer', '')
                 flask.session['c_question'] = approval_question
 
             return redirect('/need_email')
@@ -80,7 +81,7 @@ def login_register_2(conn):
             curs.execute(db_change("select id from user limit 1"))
             if not curs.fetchall():
                 curs.execute(db_change("insert into user (id, pw, acl, date, encode) values (?, ?, 'owner', ?, ?)"), [
-                    flask.request.form.get('id', None), 
+                    user_id, 
                     hashed, 
                     get_time(), 
                     db_data[0][0]
@@ -93,12 +94,12 @@ def login_register_2(conn):
                     curs.execute(db_change(
                         "insert into user_application (id, pw, date, encode, question, answer, token, ip, ua, email) values (?, ?, ?, ?, ?, ?, ?, ?, ?, '')"
                     ), [
-                        flask.request.form.get('id', None), 
+                        user_id, 
                         hashed, 
                         get_time(), 
                         db_data[0][0], 
                         approval_question, 
-                        flask.request.form.get('approval_question_answer', None), 
+                        ans_q, 
                         application_token, 
                         ip_check(), 
                         flask.request.headers.get('User-Agent')
@@ -107,33 +108,27 @@ def login_register_2(conn):
                     
                     return redirect('/application_submitted')
                 else:
-                    curs.execute(db_change("insert into user (id, pw, acl, date, encode) values (?, ?, 'user', ?, ?)"), [flask.request.form.get('id', None), hashed, get_time(), db_data[0][0]])
+                    curs.execute(db_change("insert into user (id, pw, acl, date, encode) values (?, ?, 'user', ?, ?)"), [user_id, hashed, get_time(), db_data[0][0]])
 
                 first = 0
 
             ip = ip_check()
             agent = flask.request.headers.get('User-Agent')
 
-            curs.execute(db_change("insert into ua_d (name, ip, ua, today, sub) values (?, ?, ?, ?, '')"), [flask.request.form.get('id', None), ip, agent, get_time()])
+            curs.execute(db_change("insert into ua_d (name, ip, ua, today, sub) values (?, ?, ?, ?, '')"), [user_id, ip, agent, get_time()])
 
-            flask.session['id'] = flask.request.form.get('id', None)
+            flask.session['id'] = user_id
             flask.session['head'] = ''
 
             conn.commit()
 
-            if first == 0:
-                return redirect('/change')
-            else:
-                return redirect('/setting')
+            return redirect('/change') if first == 0 else redirect('/setting')
     else:
-        contract = ''
-
         curs.execute(db_change('select data from other where name = "contract"'))
         data = curs.fetchall()
-        if data and data[0][0] != '':
-            contract = data[0][0] + '<hr class=\"main_hr\">'
+        contract = (data[0][0] + '<hr class="main_hr">') if data and data[0][0] != '' else ''
 
-        http_warring = '<hr class=\"main_hr\"><span>' + load_lang('http_warring') + '</span>'
+        http_warring = '<hr class="main_hr"><span>' + load_lang('http_warring') + '</span>'
         approval_question = ''
         
         curs.execute(db_change('select data from other where name = "requires_approval"'))
@@ -145,11 +140,11 @@ def login_register_2(conn):
             data = curs.fetchall()
             if data and data[0][0] != '':
                 approval_question = '''
-                    <hr class=\"main_hr\">
+                    <hr class="main_hr">
                     <span>''' + load_lang('approval_question') + ' : ' + data[0][0] + '''<span>
-                    <hr class=\"main_hr\">
+                    <hr class="main_hr">
                     <input placeholder="''' + load_lang('approval_question') + '''" name="approval_question_answer" type="text">
-                    <hr class=\"main_hr\">
+                    <hr class="main_hr">
                 '''
 
         return easy_minify(flask.render_template(skin_check(),
@@ -158,11 +153,11 @@ def login_register_2(conn):
                 <form method="post">
                     ''' + contract + '''
                     <input placeholder="''' + load_lang('id') + '''" name="id" type="text">
-                    <hr class=\"main_hr\">
+                    <hr class="main_hr">
                     <input placeholder="''' + load_lang('password') + '''" name="pw" type="password">
-                    <hr class=\"main_hr\">
+                    <hr class="main_hr">
                     <input placeholder="''' + load_lang('password_confirm') + '''" name="pw2" type="password">
-                    <hr class=\"main_hr\">
+                    <hr class="main_hr">
                     ''' + approval_question + '''
                     ''' + captcha_get() + '''
                     <button type="submit">''' + load_lang('save') + '''</button>

+ 2 - 5
route/user_tool.py

@@ -13,11 +13,8 @@ def user_tool_2(conn, name):
 
     if admin_check(1) == 1:
         curs.execute(db_change("select block from rb where block = ? and ongoing = '1'"), [name])
-        if curs.fetchall():
-            ban_name = load_lang('release')
-        else:
-            ban_name = load_lang('ban')
-
+        ban_name = load_lang('release') if curs.fetchall() else load_lang('ban')
+        
         data += '''
             <h2>''' + load_lang('admin') + '''</h2>
             <ul>