From e209d7670f091dfec3a0843feec4fe8426b0ca95 Mon Sep 17 00:00:00 2001 From: javamon117 <124219470+javamon117@users.noreply.github.com> Date: Wed, 13 Sep 2023 17:04:52 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=EB=B8=94=EB=A1=9C=EA=B7=B8=20=EB=A9=94?= =?UTF-8?q?=EC=9D=B8=ED=8E=98=EC=9D=B4=EC=A7=80=20=EC=B5=9C=EA=B7=BC=20?= =?UTF-8?q?=EB=AA=A9=EB=A1=9D=20=EB=B8=94=EB=A1=9C=EA=B7=B8=20=EB=B0=8F=20?= =?UTF-8?q?=EB=9E=9C=EB=8D=A4=20=EB=B8=94=EB=A1=9C=EA=B7=B8=20=EA=B0=80?= =?UTF-8?q?=EC=A0=B8=EC=98=A4=EA=B8=B0=20=EB=B6=80=EB=B6=84=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 31 ++++++++++++++++++++----------- templates/index.html | 4 ++-- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/app.py b/app.py index 8201e2f..282b9c4 100644 --- a/app.py +++ b/app.py @@ -6,6 +6,7 @@ from bs4 import BeautifulSoup from werkzeug.utils import secure_filename import os import uuid +import re from markupsafe import Markup from jinja2 import filters @@ -17,6 +18,11 @@ app.secret_key = 'your secret key' app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER +def remove_html_tags(text): + clean = re.compile('<.*?>') + return re.sub(clean, '', text) + + @app.before_request def load_user(): if 'user_info' in session: @@ -94,20 +100,23 @@ def sql_execute(q, d, is_data=False, is_last_id=False): @app.route('/') def index(): - if 'user_info' in session: # 로그인된 사용자가 있을 경우 - # 외부 공개 안된 글도 포함하여 모든 블로그 포스트를 가져옵니다. - query = "SELECT `id`, `title`, `category`, `thumbnail_img`, `contents`, `add_date` FROM `blog` WHERE `use_yn` = 'Y' ORDER BY `add_date` DESC limit 7;" - random_query = "SELECT `id`, `title`, `category`, `thumbnail_img`, `contents`, `add_date` FROM `blog` WHERE `use_yn` = 'Y' ORDER BY rand();" - else: # 로그인된 사용자가 없을 경우 - # 외부 공개된 블로그 포스트만 가져옵니다. - query = "SELECT `id`, `title`, `category`, `thumbnail_img`, `contents`, `add_date` FROM `blog` WHERE `use_yn` = 'Y' and `public_yn` = 'Y' ORDER BY `add_date` DESC limit 7;" - random_query = "SELECT `id`, `title`, `category`, `thumbnail_img`, `contents`, `add_date` FROM `blog` WHERE `use_yn` = 'Y' and `public_yn` = 'Y' ORDER BY rand();" + if 'user_info' in session: # 로그인된 사용자 + query = "SELECT `id`, `title`, `category`, `thumbnail_img`, `contents`, `add_date` FROM `blog` WHERE `use_yn` = 'Y' ORDER BY `add_date` DESC limit 6;" + r_query = "SELECT `id`, `title`, `category`, `thumbnail_img`, `contents`, `add_date` FROM `blog` WHERE `use_yn` = 'Y' ORDER BY RAND() DESC limit 1;" + else: + query = "SELECT `id`, `title`, `category`, `thumbnail_img`, `contents`, `add_date` FROM `blog` WHERE `use_yn` = 'Y' and `public_yn` = 'Y' ORDER BY `add_date` DESC limit 6;" + r_query = "SELECT `id`, `title`, `category`, `thumbnail_img`, `contents`, `add_date` FROM `blog` WHERE `use_yn` = 'Y' and `public_yn` = 'Y' ORDER BY RAND() DESC limit 1;" r, posts = sql_execute(query, (), is_data=True) - r, random_posts = sql_execute(random_query, (), is_data=True) - # `index.html` 템플릿으로 데이터를 전달합니다. - return render_template('index.html',posts=posts, random_posts=random_posts) + r, random_post = sql_execute(r_query, (), is_data=True) + posts.append(random_post[0]) + + # 태그 제거 후 150자로 제한 + for post in posts: + post['contents'] = remove_html_tags(post['contents'])[:150] + + return render_template('index.html', posts=posts) @app.route('/login', methods=['GET', 'POST']) def login(): diff --git a/templates/index.html b/templates/index.html index c020d04..85ddea9 100644 --- a/templates/index.html +++ b/templates/index.html @@ -36,7 +36,7 @@