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 @@