def accepted(self): # Handles the pagination in a really simple way startkey = request.GET.get('startkey') endkey = request.GET.get('endkey') if startkey: c.images = list(Image.by_day(self.db, descending=True, startkey=startkey, limit=16)) elif endkey: c.images = list(Image.by_day(self.db, descending=False, startkey=endkey, limit=16)) c.images.reverse() else: c.images = list(Image.by_day(self.db, descending=True, limit=16)) c.nextkey = c.prevkey = None if c.images: # If it's 16 elements long, save the key to get the next images # and remove the last element (which serves just to see the next key) if len(c.images) == 16: c.nextkey = day_to_str(c.images[-1].day) c.images = c.images[:-1] # If the image is not the first one, then save the key to get the # previous images if c.images[0].id != list(Image.by_day(self.db, descending=True, limit=1))[0].id: c.prevkey = day_to_str(c.images[0].day) return render('/admin/accepted.mako')
def show(self, day): if str_to_day(day) > datetime.utcnow(): abort(404) else: """Shows a single image""" c.image = list(Image.by_day(self.db, startkey=day))[0] if (c.image.day != str_to_day(day)): abort(404) # Gets the older image (if there is one), the startkey is # the day of the image and the list is in descending order olders = list(Image.by_day(self.db, descending=True, limit=2, startkey=day)) # If there is one store it if len(olders) > 1: c.older = day_to_str(olders[1].day) # Same thing, but the list is in ascending order for the # newer images newers = list(Image.by_day(self.db, limit=2, startkey=day)) # We check that the newer image is not in a future date if (len(newers) > 1) and (datetime.utcnow() >= newers[1].day): c.newer = day_to_str(newers[1].day) session['return_to'] = url(controller='images', action='show', day=day) return render('/images/show.mako')
def months(self): # Of course we start from the present day (since there could be # image scheduled for future days c.images = Image.by_day(self.db, descending=True, startkey=day_to_str(datetime.utcnow())) return render('/images/months.mako')
def index(self): """ Feed with the last images """ feed = Atom1Feed( title="troppotardi.com Image Feed", link=url.current(qualified=True), description="The last 10 entries from troppotardi.com", language="en", ) images = Image.by_day(self.db, startkey=day_to_str(datetime.utcnow()), descending=True, limit=10) for image in images: feed.add_item(title=image.author + ", " + day_to_str(image.day), link=url(controller='images', action='show', day=day_to_str(image.day), qualified=True), description="Image of the day by " + (image.author_url and ("<a href=\"" + image.author_url + "\">" + image.author + "</a>") or image.author), date=image.day, ) response.content_type = 'application/atom+xml' return feed.writeString('utf-8')
def last(self): # We get the last image from the present day. day = list(Image.by_day(self.db, limit=1, startkey=day_to_str(datetime.utcnow()), descending=True))[0].day redirect(url(controller='images', action='show', day=day_to_str(day)))
def validate_python(self, field_dict, state): # 'change_day' is a hidden input that sinals that # the user can edit the date. This is necessary because # if the image is still pending there is no need to check, # since there is no day field to edit at all if 'change_day' in field_dict: day = day_to_str(datetime(year=int(field_dict['year']), month=int(field_dict['month']), day=int(field_dict['day']))) imgs = list(Image.by_day(tmpl_context.db, startkey=day, endkey=day)) # Checks that there are no different images from this with # the same day. if imgs and imgs[0].id != field_dict['id']: raise formencode.Invalid( 'The day you entered already exists.', field_dict, state)
def authors(self): c.images = Image.by_day(self.db) return render('/admin/authors.mako')
def xml_list(self): c.images = Image.by_day(self.db, descending=True, startkey=day_to_str(datetime.utcnow())) return render('/images/xml_list.mako')