Пример #1
0
 def validate_python(self, field_dict, state):
     values = {
         'privatekey': config['recaptcha_privkey'],
         'remoteip': visitor_ip(),
         'challenge': field_dict['recaptcha_challenge_field'],
         'response': field_dict['recaptcha_response_field'],
         }
     req = urllib2.urlopen(config['recaptcha_server'],
                           urllib.urlencode(values))
     if req.read().splitlines()[0] != 'true':
         raise formencode.Invalid(
             'Wrong captcha.',
             field_dict, state)
Пример #2
0
    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