def delete(self, _type, poem_id): if _type == "poem": Poem.deletePoem(poem_id) self.write(json.dumps({"status": "success"})) elif _type == "draft": Poem.deleteDraft(poem_id) self.write(json.dumps({"status": "success"})) else: self.write(json.dumps({"status": "failed"}))
def get(self): poem = Poem.load(Poem.getMaxID()).getData() if poem is None: self.write( json.dumps({ 'status': 'failed', 'reason': 'Poem not found' })) else: poem['status'] = 'success' self.write(json.dumps(poem))
def get(self, poem_id): try: poem_id = int(poem_id) if poem_id > Poem.getMaxID(): self.redirect('/poem/' + str(Poem.getMaxID())) return if poem_id < 1: self.redirect('/poem/1') return opts = load_page_vars(poem_id) opts['is_front_page'] = False self.render('index.html', **opts) except: write_err(traceback.format_exc()) self.redirect('/')
def get(self, page): try: page = max(0, int(page) - 1) except: page = 0 opts = {"page": page + 1, "capitalize": lambda s: s[0].upper() + s[1:]} opts["collection"] = [{ "poem_id": i[0], "title": i[1], "date": i[2], } for i in Poem.getPoemPage(page, 20)] opts["page_range"] = [1, Poem.getMaxID() + 1] lower_limit = max(1, page - 2) upper_limit = min(lower_limit + 4, opts["page_range"][1] / 20 + 1) opts["page_range"] = (lower_limit, upper_limit) self.render('archive.html', **opts)
def load_page_vars(poem_id): if poem_id == 1: prev_page = 1 else: prev_page = poem_id - 1 if poem_id == Poem.getMaxID(): next_page = Poem.getMaxID() else: next_page = poem_id + 1 poem = get_poem(poem_id) if poem is not None and len(poem['poem_title']) == 0: poem['poem_title'] = poem['poem_date'] poem['poem_date'] = '' poem['prev_page'] = prev_page poem['next_page'] = next_page return poem
def get_draft(poem_id): try: if poem_id.isdigit(): return Poem.load_draft(poem_id).getData() else: return None except: write_err(traceback.format_exc()) return None
def get_poem(poem_id): try: poem_id = str(poem_id) if poem_id.isdigit(): return Poem.load(poem_id).getData() else: return None except: write_err(traceback.format_exc()) return None
def get(self, _type, poem_id): print "GET req", poem_id if _type == "draft": poem = get_draft(poem_id) if poem is None: self.redirect('/admin/list/drafts') else: poem['poem_image_uploaded'] = poem['image_url'] is not None poem['page_no'] = (Poem.getMaxDraftID() - poem['poem_id']) / 10 + 1 self.render('edit.html', **poem) elif _type == "poem": poem = get_poem(poem_id) if poem is None: self.redirect('/admin/list/poems') else: poem['poem_image_uploaded'] = poem['image_url'] is not None poem['page_no'] = (Poem.getMaxID() - poem['poem_id']) / 10 + 1 self.render('edit.html', **poem) else: self.redirect('/admin/list/poems')
def get(self, _type, page): PAGE_SIZE = 10 try: page = max(1, int(page)) except: page = 1 opts = { "type": _type, "page": page, "capitalize": lambda s: s[0].upper() + s[1:] } if _type == "poems": opts["collection"] = [{ "poem_id": i[0], "title": i[1], "date": i[2], "preview": avoid_incomplete_tag(i[3])[:30] + '...' } for i in Poem.getPoemPage(page - 1, PAGE_SIZE)] id_range = [1, Poem.getMaxID() - 1] elif _type == "drafts": opts["collection"] = [{ "poem_id": i[0], "title": i[1], "date": i[2], "preview": avoid_incomplete_tag(i[3])[:30] + '...' } for i in Poem.getDraftPage(page - 1, PAGE_SIZE)] id_range = [1, Poem.getMaxDraftID() - 1] else: self.redirect('/admin') opts["page_range"] = page_range(page, 1, 1 + id_range[1] / PAGE_SIZE, 2) self.render('list.html', **opts)
def post(self, _type, poem_id): print "POST req", poem_id poem_id = int(poem_id) _json = { "poem": self.get_argument("poem"), "date": self.get_argument("date"), "title": self.get_argument("title"), "type": self.get_argument("type"), "image": "/img/" + self.get_argument("image") } p = Poem(_json["title"], _json["date"], _json["poem"], _json["image"], _type, new=False, poem_id=poem_id) p.save(_json["type"]) if _json["type"] == "draft": msg = "Poem saved as draft " + datetime.datetime.now().strftime( "%H:%M %m/%d/%Y") elif _json["type"] == "poem": msg = "Poem saved " + datetime.datetime.now().strftime( "%H:%M %m/%d/%Y") status = "success" if _type == _json["type"]: self.set_header("Content-Type", "application/json") self.write(json.dumps({"status": status, "msg": msg})) else: self.write( json.dumps({ "status": "redirect", "msg": "saved", "url": "/admin/edit/" + _json["type"] + "/" + str(p.poem_id) + "?transfer=true" }))
def get(self, _type): opts = {} if _type == "poems": poems = [{ "poem_id": i[0], "title": i[1], "date": i[2], "preview": avoid_incomplete_tag(i[3])[:30] + '...' } for i in Poem.getPoemPage(0, 5)] drafts = [{ "poem_id": i[0], "title": i[1], "date": i[2], "preview": avoid_incomplete_tag(i[3])[:30] + '...' } for i in Poem.getDraftPage(0, 5)] opts = { 'poems': poems, 'more_poems': Poem.getMaxID() > 5, 'drafts': drafts, 'more_drafts': Poem.getMaxDraftID() > 5, } self.render('manage_poems.html', **opts) elif _type == "content": self.render('manage_content.html', **opts)
def post(self, _type, page): original_id = int(self.get_argument('from')) new_id = min(max(int(self.get_argument('to')), 1), Poem.getMaxID()) Poem.movePoem(original_id, new_id) new_page = (Poem.getMaxID() - new_id) / 10 + 1 self.write(json.dumps({"page": str(new_page)}))
def get(self): draft = Poem.new() draft.save() self.redirect('/admin/edit/draft/' + str(draft.poem_id))
def get(self): print Poem.getMaxID() opts = load_page_vars(Poem.getMaxID()) opts['is_front_page'] = True self.render('index.html', **opts)
def get(self): rand_poem = int(round(1. + (Poem.getMaxID() - 1) * random.random())) self.redirect('/poem/' + str(rand_poem))