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 65 66
|
from flask import Flask,abort from flask import jsonify from flask import request
from pymongo import MongoClient
app = Flask(__name__)
client = MongoClient("mongodb://localhost:27017/") client.myblog.authenticate("user","password") database = client["myblog"] collection = database["article"]
query = {}
@app.route('/login', methods=['GET']) def get_all_users(): star = collection.find(query) output = [] for s in star: output.append({'name' : s['name']}) return jsonify({'result' : output})
@app.route('/register', methods=['POST']) def add_user(): star = mongo.db.userInfo name = request.json['name'] pwd = request.json['pwd'] star_id = star.insert({'name': name, 'pwd': pwd}) new_star = star.find_one({'_id': star_id }) output = {'name' : new_star['name'], 'pwd' : new_star['pwd']} return jsonify({'result' : output})
@app.route('/modify/<string:name>', methods=['PUT']) def update_user(name): user = mongo.db.userInfo.find({"name":name}) output = [] for s in user: output.append({'name': s['name'], 'pwd': s['pwd']}) if len(output) == 0: abort(404) mongo.db.userInfo.update({"name":name},{'$set':{"name":"LZ111"}}) return jsonify({'result': output})
@app.route('/delete/<string:name>', methods=['DELETE']) def delete_user(name): user = mongo.db.userInfo.find({"name": name}) output = [] for s in user: output.append({'name': s['name'], 'pwd': s['pwd']}) if len(output) == 0: abort(404) mongo.db.userInfo.remove({'name': name}) return jsonify({'result': True})
if __name__ == '__main__': app.run()
|