def get(self, id=None, page=0, json=None): offset = 0 p = 0 if page: offset = 10*int(page) p = page template_values = {} if not id: competition = Competition.all().order("-dateCreated").get() else: competition = Competition.get_by_id(int(id)) photo = competition.photoKey # get photo instance showPagination = False captions = Caption.all().filter('competitionKey =',competition).filter('approved =',True).fetch(limit=100) if len(captions) == 11: showPagination = True p = p+1 captions.pop(10) if json == 'json': caps = [] for c in captions: caps.append({'text' : "%s" % c.text, "author" : "%s" % c.author, "comp" : "%s" % c.competitionKey, "caption" : "%s" % c.key().id(), "dateCreated" : "%s" % c.dateCreated}) self.response.headers['Content-Type'] = "application/json" self.response.out.write(simplejson.dumps([caps, showPagination, p])) else: caps = [] for c in captions: caps.append({'text' : c.text, "author" : c.author, "comp" : c.competitionKey, "caption" : c.key().id(), "dateCreated" : c.dateCreated}) template_values['img'] = "/photo/%s" % photo.key().id() # get id from photo instance, to pass later in URL template_values['title'] = photo.title template_values['description'] = photo.description template_values['competitionId'] = competition.key().id() template_values['complete'] = competition.complete template_values['captions'] = caps template_values['showPagination'] = showPagination template_values['page'] = p template_values['divWidth'] = len(captions) * 437 path = os.path.join(os.path.dirname(__file__), 'templates/competition.html') self.response.out.write(template.render(path, template_values))
def post(self): try: template_values = {} competitionId = self.request.get('competitionId') competition = Competition.get_by_id(int(competitionId)).key() captionText = cgi.escape(self.request.get('caption')) authorText = cgi.escape(self.request.get('author')) if len(captionText) > 140 or len(authorText) > 140: template_values = { "msg" : "too many characters" } path = os.path.join(os.path.dirname(__file__), 'templates/oops.html') self.response.out.write(template.render(path, template_values)) else: caption = Caption(text=captionText, author=authorText, competitionKey=competition, dateCreated=datetime.datetime.now()) caption.put() self.redirect("/success"); except: self.redirect("/oops");
def get(self, id): template_values = {} competition = Competition.get_by_id(int(id)) competition.complete = True competition.put() self.redirect('/admin/competitions')