Created
February 8, 2019 05:45
-
-
Save TBD/1a1c0bfb8de486d0d67c4f223d237a95 to your computer and use it in GitHub Desktop.
refactor of a flask python app for displaying nested text status entries
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
import json | |
class Toot(object): | |
def __init__(self, id, author_id, parent_id, text): | |
super(Toot, self).__init__() | |
self.id = id | |
self.author_id = author_id | |
self.parent_id = parent_id | |
self.text = text | |
self._children = [] | |
@property | |
def children(self): | |
return self._children | |
@property | |
def author(self): | |
return authors.find_by_id(self.author_id) | |
def add_children(self, toot): | |
self.children.append(toot) | |
def html(self): | |
output = [""" | |
<li><a href="/author/{author_name}">{author_name}</a> {text}</li> | |
""".format( | |
author_name=self.author.name, | |
text=self.text, | |
parent_id=self.parent_id, | |
id=self.id, | |
)] | |
for toot in self.children: | |
output.append("<ul>") | |
output.append(toot.html()) | |
output.append("</ul>") | |
return "".join(output) | |
def __repr__(self): | |
return "%r" % (self.__dict__) | |
class TootsManager(object): | |
def __init__(self): | |
super(TootsManager, self).__init__() | |
self.toots = [] | |
def add(self, toot): | |
if toot.parent_id is None: | |
self.toots.append(toot) | |
else: | |
parent_toot = self.find_by_id(self.toots, toot.parent_id) | |
if parent_toot is None: | |
raise RuntimeError('cannot find %s ' % toot.parent_id) | |
parent_toot.add_children(toot) | |
def load_json(self, json_file): | |
with open(json_file) as toots_json_file: | |
toots_json = json.load(toots_json_file) | |
for toot_json in toots_json: | |
self.add(Toot(**toot_json)) | |
def find_by_id(self, list, id): | |
for toot in list: | |
found_toot = self.find_by_id(toot.children, id) | |
if found_toot: | |
return found_toot | |
if (toot.id == id): | |
return toot | |
def find_by_author(self, list, author, toots=[]): | |
for toot in list: | |
toots.append(toot) | |
found_toot = self.find_by_author(toot.children, author, toots) | |
# if found_toot: | |
# toots.append(found_toot) | |
if (toot.author_id != author.id) & (len(toots) > 0): | |
toots.pop() | |
def render_all_html(self): | |
output = [] | |
for toot in self.toots: | |
output.append(toot.html()) | |
return "".join(output) | |
def render_author_html(self, author_name): | |
author = authors.find_by_name(author_name) | |
if author: | |
output = [] | |
toots = [] | |
self.find_by_author(self.toots, author, toots) | |
for toot in toots: | |
output.append(toot.html()) | |
return "".join(output) | |
else: | |
return "N/A" | |
def __repr__(self): | |
return "%s" % self.toots | |
class Author(object): | |
def __init__(self, id, name): | |
super(Author, self).__init__() | |
self.id = id | |
self.name = name | |
def __repr__(self): | |
return "%r" % (self.__dict__) | |
class AuthorsManager(object): | |
def __init__(self): | |
super(AuthorsManager, self).__init__() | |
self.authors = [] | |
def add(self, author): | |
self.authors.append(author) | |
def load_json(self, json_file): | |
with open(json_file) as authors_json_file: | |
authors_json = json.load(authors_json_file) | |
for author_json in authors_json: | |
self.add(Author(**author_json)) | |
def find_by_id(self, id): | |
return self.authors[id] | |
def find_by_name(self, name): | |
for author in self.authors: | |
if (author.name == name): | |
return author | |
return None | |
def __repr__(self): | |
return "%s - %s" % (len(self.authors), self.authors.values()) | |
# --- global init | |
toots = TootsManager() | |
toots.load_json('data/toots.json') | |
authors = AuthorsManager() | |
authors.load_json('data/authors.json') | |
# --- web app init | |
from flask import Flask | |
from flask import render_template | |
app = Flask(__name__) | |
# --- routes | |
@app.route('/') | |
def index(): | |
return render_template('toots.html', toots=toots.render_all_html()) | |
@app.route('/author/<author_name>') | |
def toot_threads_by_author(author_name): | |
return render_template('toots.html', toots=toots.render_author_html(author_name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
just the main code without: