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 _dodeleted(self): to_delete = request.POST.getall('delete') for id in to_delete: Image.load(self.db, id).delete_permanently(self.db) redirect(url(controller='admin', action='deleted'))
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 _doaccepted(self): to_delete = request.POST.getall('delete') # Delete them for id in to_delete: Image.load(self.db, id).delete(self.db) redirect(url(controller='admin', action='accepted'))
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 _dopending(self): # These are all the images ticked with accept to_accept = request.POST.getall('accept') # Accept all of them for id in to_accept: image = Image.load(self.db, id) image.state = 'accepted' image.store(self.db) # These are the ones to delete to_delete = request.POST.getall('delete') # Delete them for id in to_delete: Image.load(self.db, id).delete(self.db) redirect(url(controller='admin', action='pending'))
def _dosubmit(self): image = Image() image.author = self.form_result.get('author') image.author_url = self.form_result.get('author_url') image.author_email = self.form_result.get('email') image.text = self.form_result.get('text') image_file = self.form_result.get('image_file').file image.store(self.db, image_file=image_file) flash('Image submitted correctly') return_to(url('last'))
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 _doedit(self, id): image = Image.load(self.db, id) image.author = request.params.getone('author') image.author_email = request.params.getone('author_email') image.author_url = request.params.getone('author_url') image.text = request.params.getone('text') # "change_day" is a hidden field that indicates that the date # is exposed in the form. we don't change the day if we are # setting the image to pending, since this creates problems # when detecting if the image has changed date if ('change_day' in self.form_result) and request.params.getone('state') == 'accepted': image.day = datetime(year=int(self.form_result.get('year')), month=int(self.form_result.get('month')), day=int(self.form_result.get('day'))) image.state = self.form_result.get('state') image.store(self.db) flash('Image successfully edited') redirect(url(controller='admin', action='edit', id=id))
def deleted(self): c.images = Image.deleted_by_time(self.db, descending=True) return render('/admin/deleted.mako')
def pending(self): c.images = Image.pending_by_time(self.db, descending=True) return render('/admin/pending.mako')
def authors(self): c.images = Image.by_day(self.db) return render('/admin/authors.mako')
def edit(self, id): c.image = Image.load(self.db, id) return render('/admin/edit.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')