Last active
November 26, 2018 15:50
-
-
Save BlackFoxgamingstudio/562a41883988da29ea59b547be8c1c10 to your computer and use it in GitHub Desktop.
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
from bookshelf import get_model, oauth2, storage | |
from flask import Blueprint, current_app, redirect, render_template, request, \ | |
session, url_for | |
import random | |
#changing first blueprint from crud to books | |
books = Blueprint('books', __name__) | |
#adding second blueprint workflows | |
workflows = Blueprint('workflows',__name__) | |
# [START upload_image_file] | |
def upload_image_file(file): | |
""" | |
Upload the user-uploaded file to Google Cloud Storage and retrieve its | |
publicly-accessible URL. | |
""" | |
if not file: | |
return None | |
public_url = storage.upload_file( | |
file.read(), | |
file.filename, | |
file.content_type | |
) | |
current_app.logger.info( | |
"Uploaded file %s as %s.", file.filename, public_url) | |
return public_url | |
# [END upload_image_file] | |
# [START books list] | |
@books.route("/") | |
def list(): | |
token = request.args.get('page_token', None) | |
if token: | |
token = token.encode('utf-8') | |
books, next_page_token = get_model().Crud.factory("Book").list(cursor=token) | |
return render_template( | |
"list.html", | |
books=books, | |
next_page_token=next_page_token | |
) | |
# [END list] | |
# [START books_list_mine] | |
@books.route("/books_mine") | |
@oauth2.required | |
def list_books_mine(): | |
token = request.args.get('page_token', None) | |
if token: | |
token = token.encode('utf-8') | |
books, next_page_token = get_model().Crud.factory("Book").list_by_user( | |
user_id=session['profile']['id'], | |
cursor=token) | |
return render_template( | |
"list.html", | |
books=books, | |
next_page_token=next_page_token | |
) | |
# [END books_list_mine] | |
@books.route('/<id>') | |
def view(id): | |
book = get_model().Crud.factory("Book").read(id) | |
return render_template("view.html", book=book) | |
# [START add] | |
@books.route('/add', methods=['GET', 'POST']) | |
def add(): | |
if request.method == 'POST': | |
data = request.form.to_dict(flat=True) | |
# If an image was uploaded, update the data to point to the new image. | |
# [START image_url] | |
image_url = upload_image_file(request.files.get('image')) | |
# [END image_url] | |
# [START image_url2] | |
if image_url: | |
data['imageUrl'] = image_url | |
# [END image_url2] | |
# If the user is logged in, associate their profile with the new book. | |
if 'profile' in session: | |
data['createdBy'] = session['profile']['displayName'] | |
data['createdById'] = session['profile']['id'] | |
book = get_model().Crud.factory("Book").create(data) | |
return redirect(url_for('.view', id=book['id'])) | |
return render_template("form.html", action="Add", book={}) | |
# [END add] | |
@books.route('/<id>/edit', methods=['GET', 'POST']) | |
def edit(id): | |
book = get_model().Crud.factory("Book").read(id) | |
if request.method == 'POST': | |
data = request.form.to_dict(flat=True) | |
image_url = upload_image_file(request.files.get('image')) | |
if image_url: | |
data['imageUrl'] = image_url | |
book = get_model().Crud.factory("Book").update(data, id) | |
return redirect(url_for('.view', id=book['id'])) | |
return render_template("form.html", action="Edit", book=book) | |
@books.route('/<id>/delete') | |
def delete(id): | |
get_model().Crud.factory("Book").delete(id) | |
return redirect(url_for('.list')) | |
# [START workflow list] | |
@workflows.route("/") | |
def list(): | |
token = request.args.get('workflow_next_page', None) | |
if token: | |
token = token.encode('utf-8') | |
#adding Worklfows | |
flows, workflow_next_page_token = get_model().Crud.factory('Workflow').list(cursor=token) | |
return render_template( | |
"workflow_list.html", | |
flows=flows, | |
workflow_next_page_token=workflow_next_page_token | |
) | |
# [END list] | |
# [START list_mine] | |
@workflows.route("/workflows_mine") | |
@oauth2.required | |
def list_workflow_mine(): | |
token = request.args.get('workflow_next_page', None) | |
if token: | |
token = token.encode('utf-8') | |
#adding Worklfows | |
flows, workflow_next_page_token = get_model().Crud.factory('Workflow').list_by_user( | |
user_id=session['profile']['id'], | |
cursor=token) | |
return render_template( | |
"workflow_list.html", | |
flows=flows, | |
workflow_next_page_token=workflow_next_page_token | |
) | |
# [END workflow_list_mine] | |
@workflows.route('/<id>') | |
def workflow_view(id): | |
workflow = get_model().Crud.factory('Workflow').workflow_read(id) | |
return render_template("workflow_view.html", workflow=workflow) | |
# [START add] | |
@workflows.route('/add', methods=['GET', 'POST']) | |
def workflow_add(): | |
if request.method == 'POST': | |
data = request.form.to_dict(flat=True) | |
# If an image was uploaded, update the data to point to the new image. | |
# [START image_url] | |
image_url = upload_image_file(request.files.get('image')) | |
# [END image_url] | |
# If the user is logged in, associate their profile with the new book. | |
if 'profile' in session: | |
data['createdBy'] = session['profile']['displayName'] | |
data['createdById'] = session['profile']['id'] | |
# [START image_url2] | |
if image_url: | |
data['imageUrl'] = image_url | |
# [END image_url2] | |
workflow = get_model().Crud.factory('Workflow').workflow_create(data) | |
return redirect(url_for('.workflow_view', id=workflow['id'])) | |
return render_template("workflow_form.html", action="Add", workflow={}) | |
# [END add] | |
@workflows.route('/<id>/edit', methods=['GET', 'POST']) | |
def workflow_edit(id): | |
workflow = get_model().Crud.factory('Workflow').workflow_read(id) | |
if request.method == 'POST': | |
data = request.form.to_dict(flat=True) | |
image_url = upload_image_file(request.files.get('image')) | |
if image_url: | |
data['imageUrl'] = image_url | |
workflow = get_model().Crud.factory('Workflow').workflow_update(data, id) | |
return redirect(url_for('.view', id= workflow['id'])) | |
return render_template("workflow_form.html", action="Edit", workflow=workflow) | |
@workflows.route('/<id>/delete') | |
def workflow_delete(id): | |
get_model().Crud.factory('Workflow').workflow_delete(id) | |
return redirect(url_for('.list')) |
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
from bson.objectid import ObjectId | |
from flask_pymongo import PyMongo | |
builtin_list = list | |
workflow_builtin_list = list | |
mongo = None | |
def _id(id): | |
if not isinstance(id, ObjectId): | |
return ObjectId(id) | |
return id | |
# [START from_mongo] | |
def from_mongo(data): | |
""" | |
Translates the MongoDB dictionary format into the format that's expected | |
by the application. | |
""" | |
if not data: | |
return None | |
data['id'] = str(data['_id']) | |
return data | |
# [END from_mongo] | |
def from_mongo_for_workflow(data): | |
""" | |
Translates the MongoDB dictionary format into the format that's expected | |
by the application. | |
""" | |
if not data: | |
return None | |
data['id'] = str(data['_id']) | |
return data | |
def init_app(app): | |
global mongo | |
mongo = PyMongo(app) | |
mongo.init_app(app) | |
# [START list] | |
class Crud(object): | |
def factory(type): | |
if type =="Book": | |
return Book() | |
if type =="Workflow": | |
return Workflow() | |
assert 0, "bad Machanic creation: "+ type | |
factory = staticmethod(factory) | |
class Book(Crud): | |
def list(self, limit=10, cursor=None): | |
self.cursor = int(cursor) if cursor else 0 | |
self.results = mongo.db.books.find(skip=self.cursor, limit=10).sort('title') | |
self.books = builtin_list(map(from_mongo, self.results)) | |
self.next_page = cursor + limit if len(self.books) == limit else None | |
return (self.books, self.next_page) | |
# [END list] | |
# [START read] | |
def read(self, id): | |
self.result = mongo.db.books.find_one({'_id': _id(id)}) | |
return from_mongo(self.result) | |
# [END read] | |
# [START create] | |
def create(self, data): | |
self.result = mongo.db.books.insert_one(data) | |
return self.read(self.result.inserted_id) | |
# [END create] | |
# [START update] | |
def update(self, data, id): | |
mongo.db.books.replace_one({'_id': _id(id)}, data) | |
return self.read(id) | |
# [END update] | |
def delete(self, id): | |
mongo.db.books.delete_one({'_id': _id(id)}) | |
def list_by_user(self, user_id, limit=10, cursor=None): | |
self.cursor = int(cursor) if cursor else 0 | |
self.results = mongo.db.books\ | |
.find({'createdById': user_id}, skip=self.cursor, limit=10)\ | |
.sort('title') | |
self.books = builtin_list(map(from_mongo, self.results)) | |
self.next_page = cursor + limit if len(self.books) == limit else None | |
return (self.books, self.next_page) | |
class Workflow(Crud): | |
# [START worflow_list] | |
def list(self, limit=10, cursor=None): | |
self.cursor = int(cursor) if cursor else 0 | |
self.results = mongo.db.workflows.find(skip=self.cursor, limit=10).sort('name') | |
self.flows = workflow_builtin_list(map(from_mongo, self.results)) | |
self.workflow_next_page = cursor + limit if len(self.flows) == limit else None | |
return (self.flows, self.workflow_next_page) | |
# [START worflow_read] | |
def workflow_read(self, id): | |
self.result = mongo.db.workflows.find_one({'_id': _id(id)}) | |
return from_mongo_for_workflow(self.result) | |
# [END read] | |
# [START worflow_create] | |
def workflow_create(self, data): | |
self.result = mongo.db.workflows.insert_one(data) | |
return self.workflow_read(self.result.inserted_id) | |
# [END create] | |
# [START worflow_update] | |
def workflow_update(self, data, id): | |
mongo.db.workflows.replace_one({'id': id}, data) | |
return self.workflow_read(id) | |
# [END update] | |
def workflow_delete(self, id): | |
mongo.db.workflows.delete_one({'_id': _id(id)}) | |
def list_by_user(self, user_id, limit=10, cursor=None): | |
self.cursor = int(cursor) if cursor else 0 | |
self.results = mongo.db.workflows\ | |
.find({'createdById': user_id}, skip=self.cursor, limit=10)\ | |
.sort('title') | |
self.workflows = builtin_list(map(from_mongo, self.results)) | |
self.next_page = cursor + limit if len(self.workflows) == limit else None | |
return (self.workflows, self.next_page) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment