From 3b2815c87e409761baad313c19c5f267ca70eaa0 Mon Sep 17 00:00:00 2001 From: aj Date: Thu, 21 Feb 2019 22:36:15 +0000 Subject: [PATCH] migrated art to blueprint --- sarsoo/art/__init__.py | 1 + sarsoo/art/art.py | 30 +++++++++++++ sarsoo/art/db.py | 45 +++++++++++++++++++ .../art.html => art/templates/art/all.html} | 0 sarsoo/art/templates/art/index.html | 17 +++++++ sarsoo/sarsoo.py | 31 ++----------- 6 files changed, 97 insertions(+), 27 deletions(-) create mode 100644 sarsoo/art/__init__.py create mode 100644 sarsoo/art/art.py create mode 100644 sarsoo/art/db.py rename sarsoo/{templates/art.html => art/templates/art/all.html} (100%) create mode 100644 sarsoo/art/templates/art/index.html diff --git a/sarsoo/art/__init__.py b/sarsoo/art/__init__.py new file mode 100644 index 0000000..3b76085 --- /dev/null +++ b/sarsoo/art/__init__.py @@ -0,0 +1 @@ +from .art import art_print diff --git a/sarsoo/art/art.py b/sarsoo/art/art.py new file mode 100644 index 0000000..0f8d315 --- /dev/null +++ b/sarsoo/art/art.py @@ -0,0 +1,30 @@ +from flask import Blueprint, render_template, abort +from jinja2 import TemplateNotFound + +from google.cloud import firestore + +from .db import getTagDicts, getPopulatedTagDict, getPopulatedTagDicts + +staticbucketurl = 'https://storage.googleapis.com/sarsooxyzstatic/' +urlprefix = 'art' + +art_print = Blueprint('art', __name__, template_folder='templates') +#db = firestore.Client() + +@art_print.route('/') +def root(): + tags = getTagDicts() + print(tags) + return render_template('art/index.html', tags = tags, urlprefix = urlprefix) + +@art_print.route('/') +def tag_view(tag): + tags = getPopulatedTagDict(tag) + return render_template('art/all.html', staticroot = staticbucketurl, tags = [tags]) + +@art_print.route('/all') +def all(): + + sections = getPopulatedTagDicts() + + return render_template('art/all.html', staticroot = staticbucketurl, tags=sections) diff --git a/sarsoo/art/db.py b/sarsoo/art/db.py new file mode 100644 index 0000000..f717f30 --- /dev/null +++ b/sarsoo/art/db.py @@ -0,0 +1,45 @@ +from google.cloud import firestore + +staticbucketurl = 'https://storage.googleapis.com/sarsooxyzstatic/' + +fs = firestore.Client() + +def getTagDicts(): + + art_tags_collection = fs.collection(u'art_tags') + + try: + tags = art_tags_collection.get() + except google.cloud.exceptions.NotFound: + return 'no such document' + + dicts = [] + for tag in tags: + dicts.append(tag.to_dict()) + + return dicts + +def getPopulatedTagDict(name): + + name_doc = fs.document(u'art_tags/{}'.format(name)) + + tag_dicts = name_doc.get().to_dict() + + image_list = [] + for image in tag_dicts['art']: + image_list.append(image.get().to_dict()) + tag_dicts['images'] = image_list + + return tag_dicts + +def getPopulatedTagDicts(): + + tag_dicts = getTagDicts() + + for tag in tag_dicts: + image_list = [] + for image in tag['art']: + image_list.append(image.get().to_dict()) + tag['images'] = image_list + + return tag_dicts diff --git a/sarsoo/templates/art.html b/sarsoo/art/templates/art/all.html similarity index 100% rename from sarsoo/templates/art.html rename to sarsoo/art/templates/art/all.html diff --git a/sarsoo/art/templates/art/index.html b/sarsoo/art/templates/art/index.html new file mode 100644 index 0000000..ae9612e --- /dev/null +++ b/sarsoo/art/templates/art/index.html @@ -0,0 +1,17 @@ +{% extends 'base.html' %} + +{% block title %}dev{% endblock %} + +{% block content %} + {% for row in tags|batch(2) %} +
+ {% for tag in row %} +
+

{{ tag.name }}

+

{{ tag.description }}

+ view +
+ {% endfor %} +
+ {% endfor %} +{% endblock %} diff --git a/sarsoo/sarsoo.py b/sarsoo/sarsoo.py index fce591d..c908887 100644 --- a/sarsoo/sarsoo.py +++ b/sarsoo/sarsoo.py @@ -2,11 +2,15 @@ from flask import Flask, render_template from google.cloud import firestore import os +from .art import art_print + # Project ID is determined by the GCLOUD_PROJECT environment variable db = firestore.Client() app = Flask(__name__, static_folder=os.path.join(os.path.dirname(__file__), '..', 'static'), template_folder = "templates") +app.register_blueprint(art_print, url_prefix='/art') + staticbucketurl = 'https://storage.googleapis.com/sarsooxyzstatic/' @app.route('/') @@ -30,33 +34,6 @@ def main(): def music(): return render_template('music.html') -@app.route('/art') -def art(): - art_collection = db.collection(u'art') - art_tags_collection = db.collection(u'art_tags') - - try: - tags = art_tags_collection.get() - except google.cloud.exceptions.NotFound: - return 'no such document' - - sections = [] - - for tag in tags: - tag_dict = tag.to_dict() - query_ref = art_collection.where(u'tag', u'==', u'{}'.format(tag_dict['name'])) - query = query_ref.get() - - image_list = [] - - for image in query: - image_list.append(image.to_dict()) - - #sections.append({tag_dict['name']: image_list}) - sections.append({'images': image_list, 'name': tag_dict['name'], 'description': tag_dict['description'], 'index': tag_dict['index']}) - - return render_template('art.html', staticroot = staticbucketurl, tags=sections) - @app.route('/dev') def dev(): return render_template('dev.html')