def on_post(self): try: ordering = self.get_argument("ordering", None) data = tornado.escape.json_decode(ordering) new_ordering = [] for item in data.items(): new_ordering.append(item) sorted_order = sorted(new_ordering, key=lambda tup: tup[1]) l = len(sorted_order) #We iterate through the preferences and we update the next #and previous nugget for each nugget. The first one has the last #one as its previous and the last one has next the first one. for i, nugget_tuple in enumerate(sorted_order): n = Nugget.objects(id=nugget_tuple[0]).get() if i==0: #Update the first nugget of this section s = Section.objects(id=n.section).get() s.first_nugget = str(n.id) s.save() n.next_nugget = sorted_order[i+1][0] if i<l-1 else sorted_order[0][0] n.previous_nugget = sorted_order[i-1][0] n.save() except Exception, e: print e
def on_post(self): try: title = self.get_argument("title-inp", None) content = self.get_argument("content-inp", None) img_name = self.get_argument("img-name-inp", None) sid = self.get_argument("section", None) s = Section.objects(id=sid).get() n = Nugget() n.title = title n.content = content n.img = img_name s.nuggets.append(n) s.save() except Exception, e: print e
def on_get(self): sid = self.get_argument("sid", None) nuggets = Nugget.objects(section=sid) nuggets = [(str(nugget.id), nugget.title) for nugget in nuggets] return (nuggets,)