migrated art to blueprint

This commit is contained in:
aj 2019-02-21 22:36:15 +00:00
parent 3e1fa1863d
commit 3b2815c87e
6 changed files with 97 additions and 27 deletions

1
sarsoo/art/__init__.py Normal file
View File

@ -0,0 +1 @@
from .art import art_print

30
sarsoo/art/art.py Normal file
View File

@ -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('/<tag>')
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)

45
sarsoo/art/db.py Normal file
View File

@ -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

View File

@ -0,0 +1,17 @@
{% extends 'base.html' %}
{% block title %}dev{% endblock %}
{% block content %}
{% for row in tags|batch(2) %}
<div class="row">
{% for tag in row %}
<div class="col-pad-6 card">
<h1>{{ tag.name }}</h1>
<p>{{ tag.description }}</p>
<a href="/{{ urlprefix }}/{{ tag.doc_name }}" style="width:100%;" class="button">view</a>
</div>
{% endfor %}
</div>
{% endfor %}
{% endblock %}

View File

@ -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')