def add_side_comment(self, id): """ adding a side comment for this article / paragraph """ try: m = Model() current_article = m.find_by_id(id) data = self.request.body print(data) data = json.loads(data) print() section_id = data["sectionId"] data.pop("sectionId") if str(section_id) in current_article.side_comments: #there are already comments for this section current_article.side_comments[str(section_id)].append(data) else: # first section comment current_article.side_comments[str(section_id)] = [data] current_article.upsert() print(current_article.side_comments) except Exception as e: self.error(message="Error adding comment. No such aricle id ??: " + str(id) + "msg: " + str(e), data=None, status="500")
def upvote(self, id=None): """ vote the article one up """ m = Model() current_article = m.find_by_id(id) current_article.votes += 1 try: ip = self.request.remote_ip if ip not in current_article.voter_ips: current_article.voter_ips.append(ip) except: self.application.log_request( message="Error: Article.upvote: Could not get remote_ip: " + self.request.remote_ip) if current_article.votes > 10000: self.success(pure=True, format="json", data={"votes": ">10k"}, message="successfully upvoted") else: current_article.upsert() self.application.log_request( self, message="Article.upvote: remote_ip: " + self.request.remote_ip) self.success(pure=True, format="json", data={"votes": current_article.votes}, message="successfully upvoted")
def show(self, id=None): m = Model() article = m.find_by_id(id) #adict = self.get_author(article.author_id) self.success(message="article show", data=article, curr_user=self.current_user)
def update(self, id=None): data_json = self.request.body m = Model() m.init_from_json(data_json) res = m.find_by_id(m.id) res.init_from_json(data_json) # # update all author dicts in articles of this author # art = Article() author_articles = art.find(art.where("author_id") == res.id, as_generator=True) for elem in author_articles: elem.author = res.to_dict() elem.upsert() print("updated author information for article: {}".format( str(elem.id))) try: #res.tags= res.tags.split(",") res.upsert() self.success(message="author, successfully updated " + str(res.id), data=res, format="json") except Exception as e: self.error(message="author, error updating: " + str(m.id) + "msg: " + str(e), data=data_json, format="json")
def blog_simple(self): """ shows a simple blog styled article list """ m = Model() res = m.find_all() self.success(message="blog simple", data=res, curr_user=self.current_user)
def edit(self, id=None): m = Model() try: print(" .. GET Edit Data (ID): " + id) res = m.find_by_id(id) self.success(message="article, edit id: " + str(id), data=res) except Exception as e: self.error(message="article, edit id: " + str(id) + "msg: " + str(e), data=None)
def blog_medium(self): """ show the featured blog view """ m = Model() res = m.find_all() self.success(message="blog medium", data=res, curr_user=self.current_user, template="index_medium.bs4")
def destroy(self, id=None): try: data_json = self.request.body print(" .. DELETE Data: ID:" + str(data_json)) m = Model() m.init_from_json(data_json) res = m.find_by_id(m.id) res.delete() self.success(message="todo, destroy id: " + str(m.id)) except Exception as e: self.error(message="todo, destroy id: " + str(e))
def blog_main(self): """ Blog main => show the blog, iterate over all articles for now without a paging limit """ a = Article() res = a.find_all() curr_user=self.current_user #if user: # curr_user=user.login #else: # curr_user=None self.success(message="blog_main()", data=list(res), model=a, template="index_medium.bs4", curr_user=curr_user)
def list_author_articles(self, id=None): """ list all articles of author with given id """ if id: a = Author() m = Model() author = a.find_by_id(id) author_articles = m.find(m.where("author_id") == author.id, as_generator=True) self.success(data=author_articles, curr_user=self.current_user, author=author) else: self.error(message="article, edit id: " + str(id) + "msg: " + str(e), data=None, status="500")
def update(self, id=None): data_json = self.request.body m = Model() res = m.find_by_id(id) res.init_from_json(data_json, simple_conversion=True) print(res.side_comments) print(type(res.side_comments)) try: #res.tags= res.tags.split(",") res.upsert() self.success(message="article, successfully updated " + str(res.id), data=res, format="json") except Exception as e: self.error(message="article, error updating: " + str(m.id) + "msg: " + str(e), data=data_json, format="json", raw_data=True)
def create(self): try: data_json = self.request.body m = Model() m.init_from_json(data_json, simple_conversion=True) auth_dict = self.current_user.to_dict() auth_dict.pop("login") auth_dict.pop("password") m.author = auth_dict m.author_id = auth_dict["id"] m.published_date = datetime.datetime.utcnow() m.upsert() self.success(message="article, successfully created " + str(m.id), data=m, format="json") except Exception as e: self.error(message="article, error creating " + str(m.id) + "msg: " + str(e), data=m, format="json")
def page(self, page=0): m = Model() res = m.page(page=int(page), page_size=myapp["page_size"]) self.success(message="article page: #" + str(page), data=res)
def list(self): m = Model() res = m.get_all() self.success(message="article, index", data=res, curr_user=self.current_user)
# # create 10 dummy articles # from medium.models.tinydb.article import Article from medium.models.tinydb.author import Author def get_author(author_id): """ returns a dict with the author information for the given author_id """ a=Author() author = a.find_by_id(author_id) adict=author.to_dict() adict.pop("password") adict.pop("login") return adict if __name__ == "__main__": a=Author() admin=get_author("f74c8108-55d1-4a61-8fa3-88b81e987e9d") for x in range(0,9): a = Article() a.title ="I am Article #" + str(x) a.author=admin a.author_id=admin["id"] a.teaser="Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est" a.upsert()