Skip to content

Commit

Permalink
add tag .
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeaktian committed Dec 10, 2018
1 parent 11f7b2b commit 1d32d7c
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 14 deletions.
83 changes: 83 additions & 0 deletions app/controllers/writer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env python
#coding:utf-8
"""
file:.py
date:2018/12/10 15:58
author: peak
description:
"""

from flask import Flask, Blueprint, render_template, request, flash, session, redirect, url_for, g
import datetime, os
from app.models import db, User, Comment, Post, Tag, tags
from app.markdown_html import switch_html
from werkzeug.utils import secure_filename

writer = Blueprint(
'writer',
__name__
)

@writer.route('/writer/<int:uid>', methods=['GET', 'POST'])
def write(uid):
if request.method == 'POST':
new_title = request.form.get("title")
new_cover = request.files['cover']
new_markdown = request.files['markdown']
new_tag = request.form.get("tag")
# 查询是否title重名
if Post.query.filter(Post.Title == new_title and Post.User_Id == uid).first() is not None:
flash(u"文章标题已经存在,换个标题吧! -_-", category="warning")
return render_template('writer.html')
# 第一次提交数据库
post_forsql = Post()
post_forsql.Title = new_title
post_forsql.User_Id = uid
# 处理tag
tag_list = new_tag.split()
tag_object = []
for i in tag_list:
if Tag.query.filter(Tag.Title == i).first() is None:
# 若是新的tag首先创建新的tag
tag_forsql = Tag()
tag_forsql.Title = i
db.session.add(tag_forsql)
db.session.commit()
tag_object.append(Tag.query.filter(Tag.Title == i).first())
post_forsql.tags = tag_object
db.session.add(post_forsql)
db.session.commit()

# 获取初始文件名
new_cover_name = new_cover.filename
new_cover_point = new_cover_name.rindex(".")
new_markdown_name = new_markdown.filename
new_markdown_point = new_markdown_name.rindex(".")

# 查询ID并改文件名
post_checksql = Post.query.filter(Post.Title == new_title and Post.User_Id == uid).first()
pid = post_checksql.Id
new_cover_name = str(pid) + new_cover_name[new_cover_point:]
new_markdown_name = str(pid) + new_markdown_name[new_markdown_point:]

# 保存文件
basepath = os.path.abspath(os.path.dirname(__file__)) # 当前文件所在目录
parentdir = os.path.dirname(basepath) # 父级目录
upload_path = os.path.join(parentdir, 'static/Upload_Files/img', secure_filename(new_cover_name))
new_cover.save(upload_path)
upload_path = os.path.join(parentdir, 'static/Upload_Files/img', secure_filename(new_markdown_name))
new_cover.save(upload_path)




# 第二次提交数据库
post_checksql.Cover_Picture_Name = new_cover_name
post_checksql.Content_Name = new_markdown_name
db.session.add(post_checksql)
db.session.commit()

flash(u"提交成功! -_-", category="success")

return render_template('writer.html')

3 changes: 2 additions & 1 deletion app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class User(db.Model):
Register_Date = db.Column(db.DateTime, default=datetime.datetime.now)

tags = db.Table('Post_Tags',
db.Column('post_id', db.Integer, db.ForeignKey('Post.Id')),
db.Column('Id', db.Integer, primary_key=True),
db.Column('post_id', db.Integer, db.ForeignKey('Post.Id'), ),
db.Column('tag_id', db.Integer, db.ForeignKey('Tag.Id'))

)
Expand Down
24 changes: 14 additions & 10 deletions app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
border-radius:9px;
padding-top: 90px;
padding-bottom: 90px;
box-shadow: 0 1px 7px rgba(0, 0, 0, 0.18);
box-shadow: 0 1px 7px rgba(224, 219, 219, 0.37);
transition: all 0.6s;/*所有属性变化在0.6秒内执行动画*/
}
.post-card-container:hover{
Expand Down Expand Up @@ -73,10 +73,14 @@
<div class="container-fluid" style="{% block photo %}{% endblock photo %} background-repeat: no-repeat;background-size: cover">
<div class="row">
<div class="col-md-auto">
<a href="/">
<button type="button" class="btn btn-default" aria-label="Left Align" onmouseover="func3()" onmouseout="func4()" style="background-color: inherit">
<i id="home" class="fas fa-home" style="font-size: 24px; color: white"></i>
</button>
<a href="/" title="主页" id="home" onmouseover="func3()" onmouseout="func4()" style="color: white;text-decoration: none;" >
{# <button type="button" class="btn btn-secondary" aria-label="Left Align" onmouseover="func3()" onmouseout="func4()" style="background-color: inherit;border: 0px">#}
{# <i id="home" class="fas fa-home" style="font-size: 24px; color: white"></i>#}
{# <label style="color: white;font-size: inherit">主页</label>#}
{# <img href="/static/img/logo.jpg">#}
{# </button>#}
<img src="/static/img/logo.jpg" style="width: 40px;height: 40px;border-radius:20px">
主页
</a>
</div>
<div class="col-md-auto">
Expand All @@ -92,11 +96,11 @@
</div>
</div>
</div>
<div class="col-md-auto" style="text-align: center">
<a href="/messagewall">
<button class="btn btn-secondary" style="background-color: inherit;border: 0px;">留言</button>
</a>
</div>
{# <div class="col-md-auto" style="text-align: center">#}
{# <a href="/messagewall">#}
{# <button class="btn btn-secondary" style="background-color: inherit;border: 0px;">留言</button>#}
{# </a>#}
{# </div>#}
</div>
<!-- 标题 -->
{% block title %}
Expand Down
4 changes: 2 additions & 2 deletions app/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h1 class="text-center" style="color: white;font-size: 60px; padding-top: 116px;
{% endblock title %}

{% block content %}
<div class="container-fluid" style="background-color: #3B3F45;padding-top: 70px;padding-bottom: 80px;">
<div class="container-fluid" style="background-color: #2c2a2a;padding-top: 70px;padding-bottom: 80px;">
<div class="text-center" >
<div id="firstpage" class="firstpage" >
{% if lenth != 0 %}
Expand Down Expand Up @@ -143,7 +143,7 @@ <h2 class="posttitle" id="title"></h2>
<div class="text-center" >
<footer style="padding-top: 20px;padding-bottom: 1px">
<label style="color: #b0b0b0;font-size: 12px;margin-right: 60px">友情链接: </label>
<a href="http://git.careyou.xin" style="color: #1abc9c; margin-right: 15px; font-size: 13px">GitHub</a>
<a href="http://git.careyou.xin" style="color: #1abc9c; margin-right: 15px; font-size: 13px">MyGitHub</a>
<a href="http://rooins.careyou.xin" style="color: #1abc9c; margin-right: 15px; font-size: 13px;">• 袋鼠共享打印</a>
<a href="https://me.csdn.net/ha_weii" style="color: #1abc9c; margin-right: 15px; font-size: 13px;">• Minza的博客</a>
<a href="https://me.csdn.net/salove_y" style="color: #1abc9c; margin-right: 15px; font-size: 13px;">• 携手凡生的博客</a>
Expand Down
2 changes: 1 addition & 1 deletion app/templates/tag_details.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ <h1 class="text-center" style="color: white;font-size: 60px; padding-top: 116px;
{% endblock title %}

{% block content %}
<div class="container-fluid" style="background-color: #3B3F45;padding-top: 70px;padding-bottom: 80px;">
<div class="container-fluid" style="background-color: #2c2a2a;padding-top: 70px;padding-bottom: 80px;">
<div class="text-center" >
<div id="firstpage">
{% if lenth != 0 %}
Expand Down
126 changes: 126 additions & 0 deletions app/templates/writer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
{% extends 'base.html' %}

{% block style %}
p{
font-family: Arial, '华文中宋';
font-size: 18px;
line-height: 38px;
}
.message-card-container{
width: 689px;
margin-right: auto;
margin-left: auto;
}
input{
height: 2rem;
line-height: 1rem;
display: inline-block;
padding: .438rem .525rem;
border: 0;
width: 226px;
height: 35px;
border-radius: .3125rem;
background: #343232;
color: #fff;
}
.text-area{
display: inline-block;
padding: .438rem .525rem;
border: 0;
border-radius: .3125rem;
background: #343232;
color: #fff;
overflow-y: visible;
margin-top: .625rem;
width: 100%;
height: 100px;
vertical-align: top;
resize: none;
}
.mysubmit{
cursor: pointer;
/* background: #343232 repeating-linear-gradient(-45deg,#2c2a2a,#2c2a2a 1.125rem,transparent 1.125rem,transparent 2.25rem); */
color: #9e9e9e;
width: 689px;
border: .0625rem solid #c8c8c8;
margin: 0 auto;
border-radius: .3125rem;
display: block;
padding: 0 1rem;
height: 2.2rem;
font-weight: 500;
font-family: inherit;
-webkit-transition: all .5s ease-out;
-moz-transition: all .5s ease-out;
-ms-transition: all .5s ease-out;
-o-transition: all .5s ease-out;
transition: all .5s ease-out;
}
{% endblock style %}

{% block photo %}
background: url(../static/img/header2.jpg) center;
{% endblock photo %}

{% block title %}
<h1 class="text-center" style="color: white;font-size: 60px; padding-top: 116px; padding-bottom: 116px; margin-bottom: 0px">作者主页</h1>
{% endblock title %}

{% block content %}
<div class="container-fluid" style="background-color: #2c2a2a; padding-bottom: 100px">
<br>
<br>
<div class="message-card-container">
<div>
<h4 style="color: #1abc9c">添加新文章</h4>
</div>
<form method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-4">
<input type="text" name="title" placeholder="标题" required="required">
</div>
<div class="col-md-8">
<input type="text" name="tag" required="required" style="width: 452px" placeholder="标签 (多个标签用空格隔开)">
</div>
</div>
<br>
<div>
<label style="color: #1abc9c;">封面图 :</label> <input type="file" name="cover" required="required">
</div>
<div>
<label style="color: #1abc9c;">markdown文档 :</label> <input type="file" name="markdown" required="required">
</div>


<br>
<br>
<input type="submit" value="提交" class="mysubmit" id="submit" onmouseover="comein()" onmouseout="goout()" style="">
</form>
<br>
<div>
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="alert alert-{{ category }} alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true" style="color: orangered; font-size: 60%; font-weight: 600">&times;关闭</span></button>
<div class="text-center" style="font-weight: 600">{{ message }}</div>
</div>
{% endfor %}
{% endif %}
{% endwith %}

</div>
</div>
<div class="message-card-container">

</div>
</div>
<script>
function comein() {
$("#submit").css("color", "#1abc9c");
}
function goout() {
$("#submit").css("color", "#9e9e9e");
}
</script>
{% endblock content %}

0 comments on commit 1d32d7c

Please sign in to comment.