-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
64 lines (48 loc) · 1.65 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
from os.path import join, dirname
from dotenv import load_dotenv
from flask import Flask, render_template, jsonify, request
from pymongo import MongoClient
from datetime import datetime
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
MONGODB_URI = os.environ.get("MONGODB_URI")
DB_NAME = os.environ.get("DB_NAME")
client = MongoClient(MONGODB_URI)
db = client[DB_NAME]
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/diary', methods=['GET'])
def show_diary():
# sample_receive = request.args.get('sample_give')
# print(sample_receive)
articles = list(db.diary.find({}, {'_id': False}))
return jsonify({'articles': articles})
@app.route('/diary', methods=['POST'])
def save_diary():
title_receive = request.form.get('title_give')
content_receive = request.form.get('content_give')
today = datetime.now()
mytime = today.strftime('%Y-%m-%d-%H-%M-%S')
file = request.files['file_give']
extension = file.filename.split('.')[-1]
filename = f'static/post-{mytime}.{extension}'
file.save(filename)
profile = request.files['profile_give']
extension = profile.filename.split('.')[-1]
profilename = f'static/profile-{mytime}.{extension}'
profile.save(profilename)
time = today.strftime('%Y.%m.%d')
doc = {
'file' : filename,
'profile' : profilename,
'title' : title_receive,
'content' : content_receive,
'time' : time,
}
db.diary.insert_one(doc)
return jsonify({'message': 'data was saved'})
if __name__ == '__main__':
app.run('0.0.0.0', port=5000, debug=True)