-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
11f7b2b
commit 1d32d7c
Showing
6 changed files
with
228 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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">×关闭</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 %} |