def get(self): """Fetches 10 shouts and returns them as JSON. Use *offset* query string parameter for pagination.""" try: offset = int(self.get_argument('offset', 0)) except: offset = 0 shouts = Shout.objects().order_by('-created_at')[offset:offset + 10] response = [] for shout in shouts: response.append({ 'id': unicode(shout.id), 'text': shout.text, 'created_at': self.shout_date(shout.created_at), 'user': { 'nick': shout.user.nick, 'avatar_url': shout.user.avatar_url, 'url': shout.user.url } }) self.write({ 'shouts': response })
def post(self): """Saves a shout by a current user.""" if self.current_user == None: self.send_error(403) else: current_shout = Shout.objects(user=self.current_user).first() if current_shout != None: self.send_error(409) else: shout = Shout(user=self.current_user, text=xhtml_escape(self.get_argument('text'))) try: shout.save() except: logging.exception('Failed to save shout in DB.') self.send_error() else: self.write({ 'status': 'ok' })
def get(self): shouts_count = Shout.objects().count() self.render('../templates/index.html', shouts_count=shouts_count, ga_id=config.GOOGLE_ANALYTICS_ID)