def store_entry(): # Store entry. incoming_json = request.get_json() # dict incoming_entry = Entry(incoming_json) incoming_entry.modification_date = now() db_entry = mongo.dede.entries.find_one(id_query_from_obj(incoming_json)) if db_entry is None: incoming_entry.creation_date = now() mongo.dede.entries.insert(incoming_entry.json_dict()) else: mongo.dede.entries.update(id_query_from_obj(incoming_json), incoming_entry.json_dict()) # Add it to the list of entries of its page (if not already there). page_name = extract_page_name(incoming_json) db_page = mongo.dede.pages.find_one(name_query(page_name)) if db_page is not None: page = Page(db_page) if incoming_entry._id not in page.entry_ids: page.entry_ids.append(incoming_entry._id) mongo.dede.pages.update(id_query_from_obj(db_page), page.json_dict()) else: print "Page {0} was not found. Not updating page entries".format(page_name) return 'ok'
def get_page(page_id): db_pages = mongo.dede.pages.find() pages = [Page(db_page) for db_page in db_pages if Page(db_page).is_shown] # cache page_map = {page._id: page for page in pages } # stupid. cache assembled view pages instead. requested_page = page_map[page_id] return json.dumps(assemble_view_page(requested_page))
def get_page_names(): db_pages = mongo.dede.pages.find() names = [] for db_page in db_pages: page = Page(db_page) names.append(page.name) return json.dumps(names)
def _parse(self, data): """Load pages to be generated""" self.templates = [os.path.join(self.root, template) for template in data.get("templates", [])] for t in self.templates: print("Found template %s" % t) self.environment = Environment( loader=FileSystemLoader(self.templates), autoescape=select_autoescape(["html", "xml"]) ) self.overrides = data.get("overrides", {}) processed_pages = self.process_wildcards(data.get("pages", [])) for page_dict in processed_pages: page = Page(self, **page_dict) self.pages.append(page) path, file = page.target_url_parts try: self.page_reference[path].append((file, page)) except KeyError: self.page_reference[path] = [(file, page), ] processed_resources = self.process_wildcards(data.get("resources", [])) self.resources = [ResourceSelector.select(self, **resource) for resource in processed_resources]
def get_page_ids_and_names(): db_pages = mongo.dede.pages.find() pages = [Page(db_page) for db_page in db_pages] sorted_pages = sorted(pages, key=lambda k: k.creation_date) # I too once used dict comprehensions. # ids_and_names = { page._id: page.name for page in sorted_pages if page.is_shown } ids_and_names = [{ 'id': page._id, 'name': page.name } for page in sorted_pages if page.is_shown] return json.dumps(ids_and_names)
def get_entry_names(page_name): db_page = mongo.dede.pages.find_one(name_query(page_name)) entry_names = [] if db_page is not None: page = Page(db_page) for entry_id in page.entry_ids: db_entry = mongo.dede.entries.find_one(id_query(entry_id)) # Cry on every find. if db_entry is not None: entry = Entry(db_entry) entry_names.append(entry.name) else: print "Page {0} was not found.".format(page_name) return json.dumps(entry_names)
def store_page(): incoming_json = request.get_json() # dict incoming_page = Page(incoming_json) incoming_page.modification_date = now() db_page = mongo.dede.pages.find_one(id_query_from_obj(incoming_json)) if db_page is None: incoming_page.creation_date = now() mongo.dede.pages.insert(incoming_page.json_dict()) else: mongo.dede.pages.update(id_query_from_obj(incoming_json), incoming_page.json_dict()) return 'ok'