Exemple #1
0
 def post(self):
     name = self.get_argument('name')
     password = self.get_argument('password')
     user = db.User.authenticate(self.session, name, password, inet_aton(self.request.remote_ip))
     if user:
         self.set_secure_cookie('s', struct.pack('<I', user.id))
         self.redirect('/')
     else:
         self.flash.error.append('Failed to login with that username/password')
         self.redirect('/signup')
Exemple #2
0
    def post(self):
        name = self.get_argument('name')
        if not NAME_RE.match(name):
            self.flash.error.append('Username contains characters that are not allowed')
            self.redirect('/signup')
            return
        if name.lower() == 'anonymous':
            self.flash.error.append('That username is reserved.')
            self.redirect('/signup')
            return
        if len(name) < 2:
            self.flash.error.append('Username must be at least two characters long.')
            self.redirect('/signup')
            return
        if len(name) > 32:
            self.flash.error.append('Username is too long; restrict to at most 32 characters.')
            self.redirect('/signup')
            return

        password = self.get_argument('password')
        email = self.get_argument('email', None)
        location = self.get_argument('location', None)

        user = db.User.create(
            self.session,
            name = name,
            password = password,
            email = email,
            location = location,
            remote_ip = inet_aton(self.request.remote_ip)
            )
        if isinstance(user, basestring):
            self.flash.error.append(user)
            self.redirect('/signup')
        else:
            self.session.commit()
            self.set_secure_cookie('s', struct.pack('<I', user.id))
            self.redirect('/user/' + url_escape(name))
Exemple #3
0
    def post(self):
        try:
            img_fields = self.request.files['img']
        except KeyError:
            self.send_error(httplib.BAD_REQUEST)
            return
        assert len(img_fields) == 1
        img_fields = img_fields[0] # XXX: why?
        body_hash = hashlib.sha1(img_fields['body']).hexdigest()
        img_file = StringIO.StringIO(img_fields['body'])
        content_type = img_fields['content_type'].lower()
        if content_type == 'application/octet-stream':
            name = img_fields.get('filename', '').lower()
            if name.endswith('.jpg'):
                content_type = 'image/jpeg'
            elif name.endswith('.png'):
                content_type = 'image/png'
        img = PIL.Image.open(img_file)

        do_exif = True
        try:
            raw_info = img._getexif()
        except AttributeError:
            do_exif = False
            lat, lng, make, model, dt, sensor = None, None, None, None, None, None
        if do_exif:
            raw_exif = img.info['exif']
            info = dict((TAGS.get(k, k), v) for k, v in raw_info.iteritems())
            if 'Orientation' in info:
                orientation = info['Orientation']
                if orientation == 1:
                    pass
                elif orientation == 2:
                    img = img.transpose(PIL.Image.FLIP_LEFT_RIGHT)
                elif orientation == 3:
                    img = img.transpose(PIL.Image.ROTATE_180)
                elif orientation == 4:
                    img = img.transpose(PIL.Image.FLIP_TOP_BOTTOM)
                elif orientation == 5:
                    img = img.transpose(PIL.Image.ROTATE_90)
                    img = img.transpose(PIL.Image.FLIP_LEFT_RIGHT)
                elif orientation == 6:
                    img = img.transpose(PIL.Image.ROTATE_270)
                elif orientation == 7:
                    img = img.transpose(PIL.Image.ROTATE_270)
                    img = img.transpose(PIL.Image.FLIP_LEFT_RIGHT)
                elif orientation == 8:
                    img = img.transpose(PIL.Image.ROTATE_90)
            if 'GPSInfo' in info:
                lat, lng, geohash = self.decode_gps(info['GPSInfo'])
                sensor = True
            else:
                lat, lng, geohash = None, None, None
                sensor = None
            dt = info.get('DateTime')
            if dt is not None:
                dt = datetime.datetime.strptime(dt, '%Y:%m:%d %H:%M:%S')
            make = info.get('Make')
            model = info.get('Model')

        fsid = os.urandom(16).encode('hex')
        fspath = construct_path(fsid, content_type, makedirs=True)

        if content_type == 'image/jpeg':
            pil_type = 'JPEG'
        elif content_type == 'image/png':
            pil_type = 'PNG'
        else:
            pil_type = None

        if do_exif:
            with open(fspath + '.exif', 'wb') as f:
                f.write(raw_exif)
        self.save_versions(img, pil_type, fspath)

        row = db.Photo.create(
            self.session,
            body_hash = body_hash,
            content_type = content_type,
            fsid = fsid,
            latitude = lat,
            longitude = lng,
            geohash = geohash,
            make = make,
            model = model,
            photo_time = dt,
            photo_width = img.size[0],
            photo_height = img.size[1],
            remote_ip = inet_aton(self.request.remote_ip),
            sensor = sensor,
            user_id = self.user.id if self.user else None
            )
        self.session.commit()
        self.redirect('/photo/' + row.encid)