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 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 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 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 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 store(self, db, image_file=None): # Record the date of submission and the ip of the submitter if not self.submitted: self.submitted = datetime.utcnow() self.author_ip = visitor_ip() if self.accepted: if self.day: # We check that that's the only image we have that day days = Image.by_day(db, descending=False, startkey=day_to_str(self.day), endkey=day_to_str(self.day), ) # If there is, schedule it, and warn who edited it if days and (list(days)[0].id != self.id): flash("The day you selected was already taken, so the photo has been scheduled automatically") self.schedule(db) else: self.schedule(db) # Saves the email only if the image is accepted # and if we have an email, of course if self.accepted and self.author_email: # If there is no previous day, then it means that we are scheduling # the image for the first time. if not self.prev_day: # Saves the email tmpl_context.day = self.day tmpl_context.author = self.author tmpl_context.image_url = pylons_url(str(self.url), qualified=True) email = Email(text=render('/emails/accepted.mako'), subject='troppotardi.com', recipients=[self.author_email], ) email.store(db) # Else, we are rescheduling it. elif self.prev_day != self.day: # Saves the reschedule email tmpl_context.day = self.day tmpl_context.author = self.author tmpl_context.image_url = pylons_url(str(self.url), qualified=True) email = Email(text=render('/emails/accepted_again.mako'), subject='troppotardi.com', recipients=[self.author_email], ) email.store(db) # Now we can set the prev_day to the present day self.prev_day = self.day # If there is a user in the session, store it in the revision if 'user' in session: self.revised_by = session['user'].id # Store it in the database. super(Image, self).store(db) # Save the image file. We do it afterwards storing the image # because we use the id as a filename, and we need to store the image # first to get an id. if image_file: self.store_file(image_file, self.id, db) return self
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')