def like(): if ml.request.method == 'POST': if ml.request.is_json: data = ml.request.get_json() print(data) vlike = data['likes'] vdislike = data['dislikes'] id = data['id'] print(vlike, vdislike) # tempID = [str(temp['_id']) for temp in ml.config.article.find({},{'_id':1})] postID = ml.config.article.find_one({'_id': ml.ObjectId(id)}) if id == str(postID['_id']): query = {'$set': {'likes': vlike, 'dislikes': vdislike}} ml.config.article.update({"_id": ml.ObjectId(id)}, query) return ml.jsonify({ 'status': 200, 'message': 'like and dislike updated', 'error': '' }) else: return ml.jsonify({ 'status': 404, 'message': 'post not found', 'error': '_id not found' })
def deletepost(): if ml.request.method == 'DELETE': if ml.request.is_json: data = ml.request.get_json() id = data['id'] else: id = ml.request.form.get('id') # postID = ml.config.article.find_one({'_id': ml.ObjectId(id)}) if postID: ml.config.article.remove({'_id': ml.ObjectId(id)}, { '_id': 1, 'post_title': 1, 'post_desc': 1, 'author': 1, 'likes': 1, 'dislikes': 1, 'created_at': 1 }) return ml.jsonify({ "status": 200, "message": "Post Deleted Successfully", 'error': '' }) else: return ml.jsonify({ 'status': 404, 'message': 'post not found', 'error': '_id not found' }) return ml.render_template('account/deletepost.html', title='MobileApp-Delete')
def postdetails(): if ml.request.method == 'POST': if ml.request.is_json: data = ml.request.get_json() id = data['id'] else: data = ml.request.form id = data.get('id') print(id) authorpost = [] tempID = [ str(temp['_id']) for temp in ml.config.article.find({}, {'_id': 1}) ] print(tempID) postID = ml.config.article.find({'_id': ml.ObjectId(id)}) if id in tempID: for x in postID: authorpost.append(x) print(authorpost) authorpost[0]['_id'] = id return ml.make_response( ml.jsonify({ 'status': 200, 'message': 'author post fetched', 'data': authorpost, 'error': '' })) else: return ml.jsonify({ 'status': 404, 'message': 'post not found', 'error': '_id not found' }) return ml.render_template('account/postdetails.html', title='post details')
def createpost(): if ml.request.method == 'POST': if ml.request.is_json: data = ml.request.get_json() post_title = data['post_title'] post_desc = data['post_desc'] author = data['author'] else: data = ml.request.form post_title = data.get('post_title') post_desc = data.get('post_desc') author = data.get('author') print(post_desc, post_title, author) # insert post data into db ml.config.article.insert_one({ 'post_title': post_title, 'post_desc': post_desc, 'author': author, 'likes': 0, 'dislikes': 0, 'created_at': ml.currentdate }) # return data return ml.jsonify({ 'status': 200, 'message': 'Post Created Successfully', 'error': '' }) return ml.render_template('account/createpost.html', title='createpost')
def editpost(): if ml.request.method == 'POST': if ml.request.is_json: data = ml.request.get_json() id = data['id'] post_title = data['post_title'] post_desc = data['post_desc'] author = data['author'] else: id = ml.request.form.get('id') post_title = ml.request.form.get('post_title') post_desc = ml.request.form.get('post_desc') author = ml.request.form.get('author') postID = ml.config.article.find_one({'_id':ml.ObjectId(id)}) print('put') if postID: query = {'$set': {'post_title':post_title, 'post_desc':post_desc, 'author':author ,"created_at":ml.currentdate}} ml.config.article.update_one({"_id":ml.ObjectId(id)},query) return ml.jsonify({'status':200, 'message':'Post Updated Successfully', 'error':''}) else: return ml.jsonify({'status':404, 'message':'post not found', 'error':'_id not found'}) return ml.render_template('account/editpost.html', title='MobileAPP-Edit')
def allpost(): if ml.request.method == 'GET': allpost = [] data = ml.config.article.find({}, { '_id': 1, 'post_title': 1, 'post_desc': 1, 'likes': 1, 'dislikes': 1 }) for x in data: x['_id'] = str(x['_id']) allpost.append(x) return ml.jsonify({ "status": 200, "message": "All Posts", "data": allpost, 'error': '' })