forked from elbuo8/flask-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_db_mongo.py
More file actions
48 lines (39 loc) · 1.46 KB
/
app_db_mongo.py
File metadata and controls
48 lines (39 loc) · 1.46 KB
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
from flask import Flask, render_template, request, jsonify
from pymongo import MongoClient
from bson.objectid import ObjectId
from sendgrid import Mail, SendGridClient
import os
sg = SendGridClient(os.getenv('SG_USER'), os.getenv('SG_PWD'))
tweets = MongoClient().tweets.tweets
app = Flask(__name__)
app.debug = True
@app.route('/', methods=['GET'])
def view_tweets():
return render_template('index.html', tweets=list(tweets.find()))
@app.route('/tweet', methods=['POST'])
def create_tweet():
return jsonify({'_id': str(tweets.insert({'text': request.form['tweet']}))})
@app.route('/tweet', methods=['GET'])
def all_tweets():
return jsonify({'tweets': list(tweets.find())})
@app.route('/tweet/<string:id>', methods=['DELETE', 'GET', 'PUT'])
def single_tweet(id):
id = ObjectId(id)
tweet = request.form['tweet']
if request.method == 'DELETE':
return jsonify(tweets.remove({'_id': id}))
elif request.method == 'PUT':
return jsonify(tweets.update({'_id': id}, {'$set': {'text': tweet}}))
else:
return jsonify(tweets.find_one({'_id': id}))
@app.route('/emailhook', methods=['POST'])
def create_tweet_email():
return jsonify({'_id': str(tweets.insert({'text': request.form['subject']}))})
@app.route('/tweet/email/<string:email>', methods=['GET'])
def email_twets(email):
mail = Mail(from_email='yamil@sendgrid.com',
to=email, subject='Tweets', text=str(list(tweets.find())))
sg.send(mail)
return jsonify({})
if __name__ == '__main__':
app.run()