Просмотр исходного кода

mypy에서 에러 안 나도록 재작성

잉여개발기 (SPDV) 3 лет назад
Родитель
Сommit
564862ab54
6 измененных файлов с 76 добавлено и 66 удалено
  1. 4 4
      route/api_bbs_w_comment.py
  2. 8 8
      route/api_bbs_w_post.py
  3. 54 43
      route/bbs_w_post.py
  4. 7 8
      route/edit_delete_file.py
  5. 2 2
      route/tool/func.py
  6. 1 1
      version.json

+ 4 - 4
route/api_bbs_w_comment.py

@@ -1,12 +1,12 @@
 from .tool.func import *
 
-def api_bbs_w_comment(sub_code : str = '') -> str:
-    conn : typing.Union[sqlite3.Connection, pymysql.connections.Connection, None]
+def api_bbs_w_comment(sub_code : str = '') -> flask.Response:
+    conn : typing.Union[sqlite3.Connection, pymysql.connections.Connection]
     with get_db_connect() as conn:
-        curs : typing.Union[sqlite3.Cursor, pymysql.cursors.Cursor, None] = conn.cursor()
+        curs : typing.Union[sqlite3.Cursor, pymysql.cursors.Cursor] = conn.cursor()
 
         curs.execute(db_change('select set_name, set_data, set_code, set_id from bbs_data where (set_name = "comment" or set_name = "comment_date" or set_name = "comment_user_id") and set_id = ? order by set_code + 0 asc'), [sub_code])
-        db_data : typing.Union[list[typing.Tuple[str, str, str]], None] = curs.fetchall()
+        db_data : typing.Union[list[tuple[str, str, str]], None] = curs.fetchall()
         if not db_data:
             return flask.jsonify({})
         else:

+ 8 - 8
route/api_bbs_w_post.py

@@ -1,16 +1,16 @@
 from .tool.func import *
 
-def api_bbs_w_post(sub_code : str = '') -> str:
-    sub_code : list[str] = sub_code.split('-')
-    if len(sub_code) < 2:
-        sub_code = ['', '']
+def api_bbs_w_post(sub_code : str = '') -> flask.Response:
+    sub_code_split : list[str] = sub_code.split('-')
+    if len(sub_code_split) < 2:
+        sub_code_split = ['', '']
 
-    conn : typing.Union[sqlite3.Connection, pymysql.connections.Connection, None]
+    conn : typing.Union[sqlite3.Connection, pymysql.connections.Connection]
     with get_db_connect() as conn:
-        curs : typing.Union[sqlite3.Cursor, pymysql.cursors.Cursor, None] = conn.cursor()
+        curs : typing.Union[sqlite3.Cursor, pymysql.cursors.Cursor] = conn.cursor()
 
-        curs.execute(db_change('select set_name, set_data, set_code from bbs_data where set_id = ? and set_code = ?'), [sub_code[0], sub_code[1]])
-        db_data : typing.Union[list[typing.Tuple[str, str, str]], None] = curs.fetchall()
+        curs.execute(db_change('select set_name, set_data, set_code from bbs_data where set_id = ? and set_code = ?'), [sub_code_split[0], sub_code_split[1]])
+        db_data : typing.Union[list[tuple[str, str, str]], None] = curs.fetchall()
         if not db_data:
             return flask.jsonify({})
         else:

+ 54 - 43
route/bbs_w_post.py

@@ -29,7 +29,7 @@ def bbs_w_post_make_thread(user_id : str, date : str, data : str, code : str, co
         </table>
     '''
 
-def bbs_w_post_comment(user_id : str, sub_code : str, comment_num : str) -> list[str, str, int, int]:
+def bbs_w_post_comment(user_id : str, sub_code : str, comment_num : str) -> tuple[str, str, int, int]:
     comment_data : str = ''
     comment_select : str = ''
 
@@ -70,28 +70,28 @@ def bbs_w_post_comment(user_id : str, sub_code : str, comment_num : str) -> list
         comment_select += '<option value="' + sub_code_check + '" ' + comment_default + '>' + sub_code_check + '</option>'
         comment_data += '<hr class="main_hr">'
 
-        temp_data : list[str, str, int, int] = bbs_w_post_comment(user_id, sub_code + '-' + temp_dict['code'], comment_num)
+        temp_data : tuple[str, str, int, int] = bbs_w_post_comment(user_id, sub_code + '-' + temp_dict['code'], comment_num)
 
         comment_data += temp_data[0]
         comment_select += temp_data[1]
         comment_add_count += temp_data[3]
 
-    return [comment_data, comment_select, comment_count, comment_add_count]
+    return (comment_data, comment_select, comment_count, comment_add_count)
 
-def bbs_w_post(bbs_num : typing.Union[int, str] = '', post_num : typing.Union[int, str] = '', do_type : str = ''):
-    conn : typing.Union[sqlite3.Connection, pymysql.connections.Connection, None]
+def bbs_w_post(bbs_num : typing.Union[int, str] = '', post_num : typing.Union[int, str] = '', do_type : str = '') -> flask.Response:
+    conn : typing.Union[sqlite3.Connection, pymysql.connections.Connection]
     with get_db_connect() as conn:
-        curs : typing.Union[sqlite3.Cursor, pymysql.cursors.Cursor, None] = conn.cursor()
+        curs : typing.Union[sqlite3.Cursor, pymysql.cursors.Cursor] = conn.cursor()
 
         curs.execute(db_change('select set_data from bbs_set where set_id = ? and set_name = "bbs_name"'), [bbs_num])
-        db_data : list[typing.Tuple[str]] = curs.fetchall()
-        if not db_data:
+        db_data_3 : list[tuple[str]] = curs.fetchall()
+        if not db_data_3:
             return redirect('/bbs/main')
         
-        bbs_name : str = db_data[0][0]
+        bbs_name : str = db_data_3[0][0]
 
         curs.execute(db_change('select set_name, set_data, set_code from bbs_data where set_id = ? and set_code = ?'), [bbs_num, post_num])
-        db_data : list[typing.Tuple[str, str, str]] = curs.fetchall()
+        db_data : list[tuple[str, str, str]] = curs.fetchall()
         if not db_data:
             return redirect('/bbs/main')
 
@@ -99,9 +99,19 @@ def bbs_w_post(bbs_num : typing.Union[int, str] = '', post_num : typing.Union[in
         post_num_str : str = str(post_num)
         bbs_comment_acl : int = acl_check(bbs_num_str, 'bbs_comment')
         ip : str = ip_check()
+
+        set_id : str
+        text : str
+        data_preview : str
+        user_id : str
+        bbs_comment_form : str
+        id_data : str
+        data : str
+        date : str
+        temp_dict : dict[str, str]
         
         curs.execute(db_change('select set_data from bbs_set where set_id = ? and set_name = "bbs_type"'), [bbs_num])
-        db_data_2 : list[typing.Tuple[str]] = curs.fetchall()
+        db_data_2 : list[tuple[str]] = curs.fetchall()
         if not db_data_2:
             return redirect('/bbs/main')
         elif db_data_2[0][0] == 'thread':
@@ -114,18 +124,18 @@ def bbs_w_post(bbs_num : typing.Union[int, str] = '', post_num : typing.Union[in
                 else:
                     captcha_post('', 0)
 
-                set_id : str = bbs_num_str + '-' + post_num_str
+                set_id = bbs_num_str + '-' + post_num_str
 
                 curs.execute(db_change('select set_code from bbs_data where set_name = "comment" and set_id = ? order by set_code + 0 desc'), [set_id])
-                db_data : list[typing.Tuple[str]] = curs.fetchall()
-                id_data : str = str(int(db_data[0][0]) + 1) if db_data else '1'
+                db_data_4 : list[tuple[str]] = curs.fetchall()
+                id_data = str(int(db_data_4[0][0]) + 1) if db_data_4 else '1'
 
-                data : str = flask.request.form.get('content', '')
+                data = flask.request.form.get('content', '')
                 if data == '':
                     # re_error로 대체 예정
                     return redirect('/bbs/w/' + bbs_num_str + '/' + post_num_str)
                 
-                date : str = get_time()
+                date = get_time()
 
                 curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment', ?, ?, ?)"), [id_data, set_id, data])
                 curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment_date', ?, ?, ?)"), [id_data, set_id, date])
@@ -138,8 +148,8 @@ def bbs_w_post(bbs_num : typing.Union[int, str] = '', post_num : typing.Union[in
                 if acl_check(bbs_num_str, 'bbs_view') == 1:
                     return re_error('/ban')
 
-                text : str = ''
-                data_preview : str = ''
+                text = ''
+                data_preview = ''
                 if do_type == 'preview':
                     text = flask.request.form.get('content', '')
                     text = text.replace('\r', '')
@@ -150,9 +160,9 @@ def bbs_w_post(bbs_num : typing.Union[int, str] = '', post_num : typing.Union[in
                         data_in = 'from'
                     )
                 
-                temp_dict : dict[str, str] = json.loads(api_bbs_w_post(bbs_num_str + '-' + post_num_str).data)
+                temp_dict = json.loads(api_bbs_w_post(bbs_num_str + '-' + post_num_str).data)
 
-                data : str = ''
+                data = ''
                 data += '<h2>' + html.escape(temp_dict['title']) + '</h2>'
                 data += bbs_w_post_make_thread(
                     ip_pas(temp_dict['user_id']),
@@ -167,7 +177,7 @@ def bbs_w_post(bbs_num : typing.Union[int, str] = '', post_num : typing.Union[in
                 )
                 data += '<hr class="main_hr">'
 
-                user_id : str = temp_dict['user_id']
+                user_id = temp_dict['user_id']
                 count : int = 1
 
                 thread_data : list[dict[str, str]] = json.loads(api_bbs_w_comment(bbs_num_str + '-' + post_num_str).data)
@@ -191,7 +201,7 @@ def bbs_w_post(bbs_num : typing.Union[int, str] = '', post_num : typing.Union[in
                     )
                     data += '<hr class="main_hr">'
 
-                bbs_comment_form : str = ''
+                bbs_comment_form = ''
                 if bbs_comment_acl == 0:
                     bbs_comment_form += '''                        
                         <textarea name="content" id="opennamu_edit_textarea" class="opennamu_textarea_100">''' + html.escape(text) + '''</textarea>
@@ -228,34 +238,35 @@ def bbs_w_post(bbs_num : typing.Union[int, str] = '', post_num : typing.Union[in
                     captcha_post('', 0)
                 
                 select : str = flask.request.form.get('comment_select', 'default')
-                select : str = '' if select == 'default' else select
+                select = '' if select == 'default' else select
                 if select != '':
-                    select : list[str] = select.split('-')
-                    if len(select) < 2:
-                        curs.execute(db_change('select set_code from bbs_data where set_name = "comment" and set_id = ? and set_code = ? limit 1'), [bbs_num_str + '-' + post_num_str, select[0]])
+                    select_split : list[str] = select.split('-')
+                    if len(select_split) < 2:
+                        curs.execute(db_change('select set_code from bbs_data where set_name = "comment" and set_id = ? and set_code = ? limit 1'), [bbs_num_str + '-' + post_num_str, select_split[0]])
                         if not curs.fetchall():
-                            return ''
+                            # re_error로 변경 예정
+                            return redirect('/bbs/w/' + bbs_num_str + '/' + post_num_str)
                         else:
-                            set_id : str = bbs_num_str + '-' + post_num_str + '-' + select[0]
+                            set_id = bbs_num_str + '-' + post_num_str + '-' + select_split[0]
                     else:
-                        curs.execute(db_change('select set_code from bbs_data where set_name = "comment" and set_id = ? and set_code = ? limit 1'), [bbs_num_str + '-' + post_num_str + '-' + '-'.join(select[0:len(select) - 1]), select[len(select) - 1]])
+                        curs.execute(db_change('select set_code from bbs_data where set_name = "comment" and set_id = ? and set_code = ? limit 1'), [bbs_num_str + '-' + post_num_str + '-' + '-'.join(select_split[0:len(select) - 1]), select_split[len(select_split) - 1]])
                         if not curs.fetchall():
-                            return ''
+                            return redirect('/bbs/w/' + bbs_num_str + '/' + post_num_str)
                         else:
-                            set_id : str = bbs_num_str + '-' + post_num_str + '-' + '-'.join(select)
+                            set_id = bbs_num_str + '-' + post_num_str + '-' + '-'.join(select_split)
                 else:
-                    set_id : str = bbs_num_str + '-' + post_num_str
+                    set_id = bbs_num_str + '-' + post_num_str
 
                 curs.execute(db_change('select set_code from bbs_data where set_name = "comment" and set_id = ? order by set_code + 0 desc'), [set_id])
-                db_data : list[typing.Tuple[str]] = curs.fetchall()
-                id_data : str = str(int(db_data[0][0]) + 1) if db_data else '1'
+                db_data_5 : list[tuple[str]] = curs.fetchall()
+                id_data = str(int(db_data_5[0][0]) + 1) if db_data_5 else '1'
 
-                data : str = flask.request.form.get('content', '')
+                data = flask.request.form.get('content', '')
                 if data == '':
                     # re_error로 대체 예정
                     return redirect('/bbs/w/' + bbs_num_str + '/' + post_num_str)
 
-                date : str = get_time()
+                date = get_time()
 
                 curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment', ?, ?, ?)"), [id_data, set_id, data])
                 curs.execute(db_change("insert into bbs_data (set_name, set_code, set_id, set_data) values ('comment_date', ?, ?, ?)"), [id_data, set_id, date])
@@ -272,9 +283,9 @@ def bbs_w_post(bbs_num : typing.Union[int, str] = '', post_num : typing.Union[in
                 if acl_check(bbs_num_str, 'bbs_view') == 1:
                     return re_error('/ban')
                     
-                text : str = ''
+                text = ''
                 comment_num : str = ''
-                data_preview : str = ''
+                data_preview = ''
                 if do_type == 'preview':
                     text = flask.request.form.get('content', '')
                     text = text.replace('\r', '')
@@ -287,9 +298,9 @@ def bbs_w_post(bbs_num : typing.Union[int, str] = '', post_num : typing.Union[in
                         data_in = 'from'
                     )
 
-                temp_dict : dict[str, str] = json.loads(api_bbs_w_post(bbs_num_str + '-' + post_num_str).data)
+                temp_dict = json.loads(api_bbs_w_post(bbs_num_str + '-' + post_num_str).data)
 
-                data : str = ''
+                data = ''
                 data += '<h2>' + html.escape(temp_dict['title']) + '</h2>'
                 data += bbs_w_post_make_thread(
                     ip_pas(temp_dict['user_id']),
@@ -303,7 +314,7 @@ def bbs_w_post(bbs_num : typing.Union[int, str] = '', post_num : typing.Union[in
                     color = 'red'
                 )
 
-                user_id : str = temp_dict['user_id']
+                user_id = temp_dict['user_id']
                 comment_data : str = ''
 
                 comment_select : str = '<hr class="main_hr"><select name="comment_select">'
@@ -312,7 +323,7 @@ def bbs_w_post(bbs_num : typing.Union[int, str] = '', post_num : typing.Union[in
                 comment_count : int = 0
                 comment_add_count : int = 0
 
-                temp_data : list[typing.Tuple[str, str]] = bbs_w_post_comment(user_id, bbs_num_str + '-' + post_num_str, comment_num)
+                temp_data : tuple[str, str, int, int] = bbs_w_post_comment(user_id, bbs_num_str + '-' + post_num_str, comment_num)
 
                 comment_data += temp_data[0]
                 comment_select += temp_data[1]
@@ -329,7 +340,7 @@ def bbs_w_post(bbs_num : typing.Union[int, str] = '', post_num : typing.Union[in
                     data += load_lang('reply') + ' : ' + str(comment_add_count) + '<hr class="main_hr">'
                     data += comment_data
 
-                bbs_comment_form : str = ''
+                bbs_comment_form = ''
                 if bbs_comment_acl == 0:
                     bbs_comment_form += '''
                         ''' + comment_select + '''

+ 7 - 8
route/edit_delete_file.py

@@ -4,25 +4,24 @@ from .edit_delete import edit_delete
 
 # 처음으로 차세대 코드 방법론 적용
 # 앞으로 다 이렇게 작성할 예정
-def edit_delete_file(name : str = 'test.jpg') -> str:
-    conn : typing.Union[sqlite3.Connection, pymysql.connections.Connection, None]
+def edit_delete_file(name : str = 'test.jpg') -> flask.Response:
+    conn : typing.Union[sqlite3.Connection, pymysql.connections.Connection]
     with get_db_connect() as conn:
-        curs : typing.Union[sqlite3.Cursor, pymysql.cursors.Cursor, None] = conn.cursor()
+        curs : typing.Union[sqlite3.Cursor, pymysql.cursors.Cursor] = conn.cursor()
 
         ip : str = ip_check()
         if admin_check() == 0:
             return re_error('/ban')
 
-        mime_type : typing.Union[re.Match, None] = re.search(r'([^.]+)$', name)
+        mime_type : typing.Optional[re.Match] = re.search(r'([^.]+)$', name)
+        mime_type_str : str = 'jpg'
         if mime_type:
-            mime_type = mime_type.group(1).lower()
-        else:
-            mime_type = 'jpg'
+            mime_type_str = mime_type.group(1).lower()
 
         file_name : str = re.sub(r'\.([^.]+)$', '', name)
         file_name = re.sub(r'^file:', '', file_name)
 
-        file_all_name : str = sha224_replace(file_name) + '.' + mime_type
+        file_all_name : str = sha224_replace(file_name) + '.' + mime_type_str
         file_directory : str = os.path.join(load_image_url(), file_all_name)
 
         if not os.path.exists(file_directory):

+ 2 - 2
route/tool/func.py

@@ -194,7 +194,7 @@ class get_db_connect:
     
 
 class class_check_json:
-    def do_check_set_json():
+    def do_check_set_json(self):
         if os.getenv('NAMU_DB') or os.getenv('NAMU_DB_TYPE'):
             set_data = {}
             set_data['db'] = os.getenv('NAMU_DB') if os.getenv('NAMU_DB') else 'data'
@@ -249,7 +249,7 @@ class class_check_json:
 
         return data_db_set
 
-    def do_check_mysql_json(data_db_set):
+    def do_check_mysql_json(self, data_db_set):
         if os.path.exists(os.path.join('data', 'mysql.json')):
             db_set_list = ['user', 'password', 'host', 'port']
             with open(os.path.join('data', 'mysql.json'), encoding = 'utf8') as file_data:

+ 1 - 1
version.json

@@ -1,6 +1,6 @@
 {
     "beta" : {
-        "r_ver" : "v3.4.6-RC3-dev189",
+        "r_ver" : "v3.4.6-RC3-dev190",
         "c_ver" : "3500361",
         "s_ver" : "3500111"
     }