def read_json_contents(self, body: dict): """ This function is responsible for populating the course with the data found in the JSON. :param body: JSON info """ for section in body: new_section = Section(section['id'], section['name'], self.__download) new_section.read_json_contents(section['modules']) self.__sections.append(new_section)
def init_db(): session = Session() file = open('example.json', 'r', encoding='utf-8') data = json.load(file) file.close() for user in data['Users']: item = User(username=user['username']) session.add(item) for section in data['Sections']: item = Section(title=section['title']) session.add(item) for post in data['Posts']: item = Post(title=post['title'], section_id=post['section_id'], user_id=post['user_id'], description=post['description']) session.add(item) for tag in data['Tags']: item = Tag(name=tag['name'], post_id=tag['post_id']) session.add(item) session.commit() session.close()
def count_chapter(chapter_id): toret = {"crs": 0, "ws": 0, "pgs": 0} sections = Section.query(Section.chapter == chapter_id) for section in sections: extend_dictionary(toret, count_text(section.text)) return toret
def _parse_section(json): section = Section() section.set_name(json[NAME]) section.set_description(json[DESCRIPTION]) for sentence_json in json[SENTENCES]: sentence = _parse_sentence(sentence_json) section.add_sentence(sentence) return section
def get(self): try: chapter_id = self.request.GET['chapter_id'] except: self.redirect("/error?msg=missing key for addition") return user = users.get_current_user() if user: try: chapter = ndb.Key(urlsafe=chapter_id).get() except: self.redirect("/error?msg=key was not found") return num_sections = len( Section.query(Section.chapter == chapter.key.id()).fetch( keys_only=True)) + 1 section = Section() section.chapter = chapter.key.id() section.num = num_sections section.text = "An interesting section." key = model.section.update(section) self.redirect("/sections/modify?section_id=" + key.urlsafe()) else: self.redirect("/") return
def get(self): id = self.request.GET.get('story_id') if not id: self.redirect("/error?msg=Key missing for exporting.") return user = users.get_current_user() if user: user_name = user.nickname() access_link = users.create_logout_url("/") try: story = ndb.Key(urlsafe=id).get() # Collect all chapters chapters = Chapter.query( Chapter.story == story.key.id()).order(Chapter.num) # Collect all sections all_sections = {} for chapter in chapters: # Retrieve all sections sections = Section.query( Section.chapter == chapter.key.id()).order(Section.num).fetch() all_sections[chapter.num] = sections except: self.redirect("/error?msg=Key was not found.") return template_values = { "info": AppInfo, "user_name": user_name, "access_link": access_link, "story": story, "chapters": chapters, "all_sections": all_sections } jinja = jinja2.get_jinja2(app=self.app) self.response.headers["target"] = "_blank" self.response.write(jinja.render_template("export_story.html", **template_values)); else: self.redirect("/")
def get(self): user = users.get_current_user() if user: user_name = user.nickname() try: chapter_id = self.request.GET['chapter_id'] except: self.redirect("/error?msg=Key missing for management.") return try: chapter = ndb.Key(urlsafe=chapter_id).get() story = Story.get_by_id(chapter.story) except: self.redirect("/error?msg=Key was not found.") return sections = Section.query( Section.chapter == chapter.key.id()).order(Section.num) total_stats = count_chapter(chapter.key.id()) access_link = users.create_logout_url("/") template_values = { "info": AppInfo, "user_name": user_name, "access_link": access_link, "story": story, "chapter": chapter, "sections": sections, "total_crs": total_stats["crs"], "total_ws": total_stats["ws"], "total_pgs": total_stats["pgs"] } jinja = jinja2.get_jinja2(app=self.app) self.response.write( jinja.render_template("sections.html", **template_values)) else: self.redirect("/") return
def remove_all_sections_of(chapter_key): sections_keys = Section.query(Section.chapter == chapter_key.id()).fetch( keys_only=True) ndb.delete_multi(sections_keys)