Example #1
0
class ProfilePictureHandler(BaseHandler):

    @tornado.web.asynchronous
    @tornado.gen.coroutine
    def post(self):

        posted_file = self.request.files['profile'][0]
        original_filename = posted_file['filename']

        ti = ThumbImage()
        result = {}
        try:
            img_cover = ti.process(posted_file['body'], format="binary>json", tags=["delete"],
                                   attrs={"filename": original_filename, "random": random.uniform(0,1)},
                                   template="profile")
        except Exception, e:
            result = {"error": unicode(e)}

        if "error" not in result:
            image_id = yield motor.Op(self.db.images.insert, img_cover)
            self.set_cookie("profile_picture", unicode(image_id))
            result = {"image_id": unicode(image_id), "data": img_cover["data-160x160"]}


        # once a time remove images marked to delete
        if random.randint(0, 9) == 9:
            created_at = datetime.utcnow()-timedelta(minutes=60)
            yield motor.Op(self.db.images.remove, {"tags": {"$in": ["delete"]}, "created_at": {"$lte": created_at}})

        self.set_header("Content-Type", "text/plain")
        self.write(json.dumps(result))
        self.finish()
Example #2
0
    def test_partial_update(self, done):
        """Motor: partial atomic update with save"""
        class Doc(BaseDocument, dot_notation=True):
            foo = Field(str)
            bar = Field(int, required=False)
            moo = Field(list)

        Doc.register(client=MOTOR_CLIENT, db='nanotestdb')

        d = Doc(foo='foo value', bar=42)
        d.moo = []
        yield motor.Op(d.insert)
        del d.bar  # unset
        yield motor.Op(d.save)
        yield AssertEqual(d, Doc.find_one, {'_id': d._id})
        d.foo = 'new foo'
        d['bar'] = 1337
        d.moo = ['moo 0']
        yield motor.Op(d.save, atomic=True)
        yield AssertEqual(d, Doc.find_one, {'foo': 'new foo', 'bar': 1337})
        d.moo = []
        del d['bar']
        yield motor.Op(d.save)
        yield AssertEqual(d, Doc.find_one, {'_id': d._id})
        done()
Example #3
0
    def get_posts(self, slug, callback):
        slug = slug.rstrip('/')
        posts = self.settings['db'].posts
        postdoc = yield motor.Op(posts.find_one,
            {'slug': slug, 'status': 'publish'},
            {'summary': False, 'original': False})

        if not postdoc:
            raise tornado.web.HTTPError(404)

        # Only posts have prev / next navigation, not pages
        if postdoc['type'] == 'post':
            fields = {'summary': False, 'body': False, 'original': False}
            prevdoc = yield motor.Op(posts.find({
                'status': 'publish', 'type': 'post', '_id': {'$lt': postdoc['_id']}
            }, fields).sort([('_id', -1)]).limit(-1).next)

            nextdoc = yield motor.Op(posts.find({
                'status': 'publish', 'type': 'post', '_id': {'$gt': postdoc['_id']}
            }, fields).sort([('_id', 1)]).limit(-1).next)
        else:
            prevdoc, nextdoc = None, None

        # Done
        callback([prevdoc, postdoc, nextdoc], None)
Example #4
0
    def _get(self, *args, **kwargs):
        categorydocs = yield motor.Op(self.get_categories)
        self.categories = categories = [Category(**doc) for doc in categorydocs]

        postdocs = yield motor.Op(self.get_posts, *args, **kwargs)
        self.posts = posts = [
            Post(**doc) if doc else None
            for doc in postdocs]

        mod = max(
            thing.last_modified
            for things in (posts, categories)
            for thing in things if thing)

        if mod:
            # If-Modified-Since header is only good to the second. Truncate
            # our own mod-date to match its precision.
            mod = mod.replace(microsecond=0)
            self.set_header('Last-Modified', mod)

            # Adapted from StaticFileHandler
            ims_value = self.request.headers.get("If-Modified-Since")
            if ims_value is not None:
                date_tuple = email.utils.parsedate(ims_value)
                if_since = models.utc_tz.localize(
                    datetime.datetime.fromtimestamp(time.mktime(date_tuple)))
                if if_since >= mod:
                    # No change since client's last request. Tornado will take
                    # care of the rest.
                    self.set_status(304)
                    self.finish()
                    return

        gen.engine(get)(self, *args, **kwargs)
Example #5
0
def run(*fileids):
    _redis = redis.client.Redis(settings.REDIS_HOST, settings.REDIS_PORT)
    connection = motor.MotorConnection().open_sync()
    db = connection.tiler

    try:
        cursor = db.images.find({'fileid': {'$in': fileids}})
        for document in (yield motor.Op(cursor.to_list)):
            pprint(document)
            yield motor.Op(db.images.update, {'_id': document['_id']},
                           {'$unset': {
                               'cdn_domain': 1
                           }})
            metadata_key = 'metadata:%s' % document['fileid']
            if _redis.get(metadata_key):
                print "Invalidated metadata cache"
                _redis.delete(metadata_key)
            lock_key = 'uploading:%s' % document['fileid']
            # locking it from aws upload for 1 hour
            _redis.setex(lock_key, time.time(), 60 * 60)

            upload_log = os.path.join(ROOT, 'static',
                                      'upload.%s.txt' % document['fileid'])
            if os.path.isfile(upload_log):
                os.remove(upload_log)

            print "\n"

    finally:
        IOLoop.instance().stop()
Example #6
0
 def find_deepsky(self, query):
     if query.startswith('Abell'):
         res = yield motor.Op(
             self.datadb.deepsky.find_one, {
                 '$or': [{
                     'alias': query
                 }, {
                     'object': query
                 }, {
                     'cn_name': query
                 }, {
                     'cn_alias': query
                 }]
             })
     else:
         res = yield motor.Op(
             self.datadb.deepsky.find_one, {
                 '$or': [{
                     'object': query
                 }, {
                     'alias': query
                 }, {
                     'cn_name': query
                 }, {
                     'cn_alias': query
                 }]
             })
     raise tornado.gen.Return(res)
Example #7
0
    def post(self, fileid):
        image = yield motor.Op(self.db.images.find_one, {'fileid': fileid})
        if not image:
            raise tornado.web.HTTPError(404, "File not found")

        yield motor.Op(self.db.images.update, {'_id': image['_id']},
                       {'$unset': {
                           'cdn_domain': 1
                       }})

        lock_key = 'uploading:%s' % fileid
        # locking it from aws upload for 1 hour
        self.redis.setex(lock_key, time.time(), 60 * 60)

        metadata_key = 'metadata:%s' % fileid
        self.redis.delete(metadata_key)

        upload_log = os.path.join(self.application.settings['static_path'],
                                  'upload.%s.txt' % fileid)
        if os.path.isfile(upload_log):
            os.remove(upload_log)
        else:
            print "couldn't remove", upload_log

        url = self.reverse_url('admin_image', fileid)
        self.redirect(url)
Example #8
0
    def get(self, fileid):
        image = yield motor.Op(self.db.images.find_one, {'fileid': fileid})
        if not image:
            raise tornado.web.HTTPError(404, "File not found")

        image_split = (fileid[:1] + '/' + fileid[1:3] + '/' + fileid[3:])

        if image['contenttype'] == 'image/jpeg':
            extension = 'jpg'
        elif image['contenttype'] == 'image/png':
            extension = 'png'
        else:
            raise NotImplementedError

        image['found_tiles'] = self._count_tiles(image)
        _ranges = image.get('ranges')
        if _ranges:
            _ranges = [int(x) for x in _ranges]
        image['ranges'] = _ranges or self._calculate_ranges(image)
        image['expected_tiles'] = self._expected_tiles(image)
        _tiles_before = self.get_argument('before', None)
        if _tiles_before is not None and _tiles_before != image['found_tiles']:
            if image.get('cdn_domain'):
                yield motor.Op(self.db.images.update, {'_id': image['_id']},
                               {'$unset': {
                                   'cdn_domain': 1
                               }})
                image['cdn_domain'] = None
                lock_key = 'uploading:%s' % fileid
                # locking it from aws upload for 1 hour
                self.redis.setex(lock_key, time.time(), 60 * 60)

        data = {
            'image_split': image_split,
            'ranges': image['ranges'],
            'found_tiles_before': _tiles_before,
        }
        data['image'] = image
        _cols = {}
        _rows = {}
        tiles = {}
        root = self.application.settings['static_path']
        for zoom in image['ranges']:
            extra = self.get_extra_rows_cols(zoom)
            tiles[zoom] = {}
            width = 256 * (2**zoom)
            cols = rows = extra + width / 256
            _cols[zoom] = cols
            _rows[zoom] = rows
            for row in range(rows):
                for col in range(cols):
                    key = '%s,%s' % (row, col)
                    filename = os.path.join(root, 'tiles', image_split, '256',
                                            str(zoom), key + '.' + extension)
                    tiles[zoom][key] = os.path.isfile(filename)
        data['rows'] = _rows
        data['cols'] = _cols
        data['tiles'] = tiles

        self.render('admin/tiles.html', **data)
Example #9
0
    def get(self):

        stream_id = self.get_argument("stream_id")

        stream = yield motor.Op(self.db.streams.find_one,
                                {"_id": ObjectId(stream_id)}, {
                                    "reencoding": 1,
                                    "name": 1,
                                    "default_program_id": 1,
                                    "fav_group_id": 1,
                                    "picture": 1,
                                    "_id": 1
                                })

        program = yield motor.Op(
            self.db.programs.find_one,
            {"_id": ObjectId(stream["default_program_id"])}, {
                "groups": 1,
                "selection": 1,
                "_id": 0
            })

        group_id = program["groups"][0]

        stream["_id"] = unicode(stream["_id"])

        self.write({
            "program": program,
            "group_id": group_id,
            "stream": stream
        })
        self.finish()
Example #10
0
    def check_external_portal(self, portid):
        """Check to make sure a portal is okay for external linking.
        (This lives here because a couple of different handlers use it.)
        Returns the portal DB object, or raises an exception.
        """
        portal = yield motor.Op(self.application.mongodb.portals.find_one,
                                {'_id': portid})
        if not portal:
            raise tornado.web.HTTPError(403, 'No such portal')
        if portal['iid'] is not None:
            raise tornado.web.HTTPError(403, 'Portal is not world-level')
        if portal['scid'] == 'same':
            raise tornado.web.HTTPError(403, 'Portal is current-scope')

        plist = yield motor.Op(self.application.mongodb.portlists.find_one,
                               {'_id': portal['plistid']})
        if not plist:
            raise tornado.web.HTTPError(403, 'No such portlist')
        if plist['type'] != 'world':
            raise tornado.web.HTTPError(403, 'Portlist is not world-level')
        if not plist.get('external', False):
            raise tornado.web.HTTPError(
                403, 'Portlist is not available for external linking')

        world = yield motor.Op(self.application.mongodb.worlds.find_one,
                               {'_id': portal['wid']}, {'copyable': 1})
        if not world:
            raise tornado.web.HTTPError(403, 'No such world')
        if not world.get('copyable', False):
            raise tornado.web.HTTPError(403, 'World is not copyable')

        return portal
Example #11
0
 def global_eventloc(loc, all):
     """Send an event message to all players in the given location.
     (Location or key, or the entire realm.)
     """
     ctx = EvalPropContext.get_current_context()
     iid = ctx.loctx.iid
     if ctx.level != LEVEL_EXECUTE:
         raise Exception('Events may only occur in action code')
    
     if isinstance(loc, two.execute.RealmProxy):
         locid = None
     elif isinstance(loc, two.execute.LocationProxy):
         res = yield motor.Op(ctx.app.mongodb.locations.find_one,
                              {'_id':loc.locid, 'wid':ctx.loctx.wid},
                              {'_id':1})
         if not res:
             raise KeyError('No such location')
         locid = loc.locid
     else:
         res = yield motor.Op(ctx.app.mongodb.locations.find_one,
                              {'key':loc, 'wid':ctx.loctx.wid},
                              {'_id':1})
         if not res:
             raise KeyError('No such location: %s' % (loc,))
         locid = res['_id']
         
     if is_typed_dict(all, 'text'):
         all = all.get('text', None)
         subctx = EvalPropContext(ctx.task, parent=ctx, level=LEVEL_MESSAGE)
         val = yield subctx.eval(all, evaltype=EVALTYPE_TEXT)
     else:
         val = str(all)
             
     others = yield ctx.task.find_location_players(iid, locid)
     ctx.task.write_event(others, val)
Example #12
0
    def post(self):

        update = {
            "max_size": int(self.get_argument('max-size')) * 1048576,
            "max_streams": int(self.get_argument('max-streams'))
        }

        confirmed = self.get_argument("confirmed", False)
        if confirmed == "on": confirmed = True
        else: confirmed = False
        update["confirmed"] = confirmed

        yield motor.Op(self.db.users.update,
                       {"_id": ObjectId(self.get_current_user())},
                       {"$set": update},
                       upsert=False,
                       multi=False)

        if self.get_argument("remove") == "1":
            yield motor.Op(self.db.users.remove,
                           {"_id": ObjectId(self.get_current_user())})

            stream = yield motor.Op(self.db.streams.find_one,
                                    {"user_id": self.get_current_user()},
                                    {"_id": 1})
            if stream is not None:
                stream_id = unicode(stream["_id"])
                yield motor.Op(self.db.streams.remove,
                               {"user_id": self.get_current_user()})
                yield motor.Op(self.db.media.remove, {"stream_id": stream_id})
                self.redirect("/admin/user/me/log-as.html")

        self.write({"msg": "User settings updated successfully."})
        self.finish()
    def get(self, stream_id):

        stream = yield motor.Op(self.db.streams.find_one,
                                {"_id": ObjectId(stream_id)}, {
                                    "reencoding": 1,
                                    "name": 1,
                                    "cover_image": 1,
                                    "_id": 1
                                })
        print stream["cover_image"]
        cover_picture = yield motor.Op(
            self.db.images.find_one, {"_id": ObjectId(stream["cover_image"])},
            {
                "tags": 1,
                "_id": 0
            },
            upsert=False,
            multi=False)

        self.clear_cookie("cover_picture")
        self.clear_cookie("cover_stream_id")

        self.template_vars["menu"] = "appearance"
        self.template_vars["submenu"] = "appearance"
        self.template_vars["stream"] = stream
        self.template_vars["stream_id"] = stream_id
        if cover_picture is not None and "uploaded" in cover_picture["tags"]:
            uploaded = True
        else:
            uploaded = False
        self.template_vars["cover_uploaded"] = uploaded

        self.render("admin/appearance.html", **self.template_vars)
Example #14
0
    def get(self, stream_id):

        stream = yield motor.Op(self.db.streams.find_one,
                                {"_id": ObjectId(stream_id)}, {
                                    "reencoding": 1,
                                    "name": 1,
                                    "default_program_id": 1,
                                    "_id": 1
                                })

        program = yield motor.Op(
            self.db.programs.find_one,
            {"_id": ObjectId(stream["default_program_id"])}, {
                "groups": 1,
                "selection": 1,
                "_id": 0
            })

        group_id = program["groups"][0]

        stream["_id"] = unicode(stream["_id"])
        stream["id"] = unicode(stream["_id"])

        self.template_vars["program"] = program
        self.template_vars["group_id"] = group_id
        self.template_vars["stream"] = stream
        self.template_vars["live"] = True
        self.template_vars["menu"] = "streams"
        self.template_vars["submenu"] = "live"
        self.template_vars["stream_id"] = stream_id

        self.render("admin/live.html", **self.template_vars)
Example #15
0
    def test_op(self):
        # motor.Op is deprecated in Motor 0.2, superseded by Tornado 3 Futures.
        # Just make sure it still works.

        collection = self.collection
        doc = {'_id': 'jesse'}

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")

            # Op works.
            _id = yield motor.Op(collection.insert, doc)
            self.assertEqual('jesse', _id)

            # Raised a DeprecationWarning.
            self.assertEqual(1, len(w))
            warning = w[-1]
            self.assertTrue(issubclass(warning.category, DeprecationWarning))
            message = str(warning.message)
            self.assertTrue("deprecated" in message)
            self.assertTrue("insert" in message)

        result = yield motor.Op(collection.find_one, doc)
        self.assertEqual(doc, result)

        # Make sure it works with no args.
        result = yield motor.Op(collection.find_one)
        self.assertTrue(isinstance(result, dict))

        with assert_raises(pymongo.errors.DuplicateKeyError):
            yield motor.Op(collection.insert, doc)
Example #16
0
 def global_location(obj=None):
     """Create a LocationProxy.
     - No argument: the current player's location
     - String argument: the location with the given key
     - Player argument: the location of the given player (if in the current world!)
     """
     if obj is None:
         ctx = EvalPropContext.get_current_context()
         if not ctx.uid:
             raise Exception('No current player')
         if not ctx.loctx.locid:
             return None
         return two.execute.LocationProxy(ctx.loctx.locid)
     
     if isinstance(obj, two.execute.PlayerProxy):
         ctx = EvalPropContext.get_current_context()
         res = yield motor.Op(ctx.app.mongodb.playstate.find_one,
                              {'_id':obj.uid},
                              {'iid':1, 'locid':1})
         if not res:
             raise KeyError('No such player')
         if res['iid'] != ctx.loctx.iid:
             return None
         return two.execute.LocationProxy(res['locid'])
     
     ctx = EvalPropContext.get_current_context()
     if not ctx.loctx.wid:
         raise Exception('No current world')
     res = yield motor.Op(ctx.app.mongodb.locations.find_one,
                          {'wid':ctx.loctx.wid, 'key':obj},
                          {'_id':1})
     if not res:
         raise KeyError('No such location: %s' % (obj,))
     return two.execute.LocationProxy(res['_id'])
Example #17
0
    def resolve_dirty(self, ent):
        """Write back (or delete) a single dirty entry. Then mark it clean.
        """
        dbname = ent.tup[0]
        if dbname not in writable_collections:
            # Maybe we should update the equivalent writable entry here,
            # but we'll just skip it.
            self.log.warning('Unable to update %s entry: %s', dbname, ent.key)
            if ent.mutable:
                ent.origval = deepcopy(ent.val)
            return

        if ent.found:
            # Resolve update.
            try:
                checkwritable(ent.val)
            except TypeError as ex:
                self.log.warning('Unable to update %s entry: %s', ent.key, ex)
                ### drop entry entirely?
                return
            newval = dict(ent.query)
            newval['val'] = ent.val
            yield motor.Op(self.app.mongodb[dbname].update,
                           ent.query, newval,
                           upsert=True)
            if ent.mutable:
                ent.origval = deepcopy(ent.val)
        else:
            # Resolve delete.
            yield motor.Op(self.app.mongodb[dbname].remove,
                           ent.query)
        ent.dirty = False
Example #18
0
    def get(self):
        item = self.get_argument('item', None)
        ruleid = self.get_argument('ruleid', None)
        origin = self.get_argument('origin', None)
        host = self.get_argument('host', None)
        doc = True if self.get_argument('doc', False) else False
        coll = 'documentation' if doc else 'log_logentry'
        body = None
        fmt = None
        reqheaders = {}
        respheaders = {}
        entry = None
        if item:
            collection = self.settings['db'].proxyservice[coll]
            entry = yield motor.Op(collection.find_one,
                                   {'_id': self.get_id(item)})
            if entry:
                reqheaders = entry['request'][
                    'headers'] if 'request' in entry and 'headers' in entry[
                        'request'] and entry['request'][
                            'headers'] else reqheaders
                respheaders = entry['response'][
                    'headers'] if 'response' in entry and 'headers' in entry[
                        'response'] else respheaders
                status = entry['response']['status']

                if 'fileid' in entry['response']:
                    body, ctype = yield self.get_gridfs_body(
                        entry['response']['fileid'], respheaders)
                    fmt = util.get_format(ctype)
                elif 'body' in entry['response']:
                    body = entry['response']['body']

                entry = entry['request']
                entry['status'] = status

        elif ruleid:
            collection = self.settings['db'].proxyservice['log_rules']
            entry = yield motor.Op(collection.find_one,
                                   {'_id': self.get_id(ruleid)})
            body = entry['body']
            reqheaders = entry['reqheaders'] if 'reqheaders' in entry and entry[
                'reqheaders'] else reqheaders
            respheaders = entry[
                'respheaders'] if 'respheaders' in entry else respheaders
            fmt = util.get_format(
                util.get_content_type(
                    self.nice_headers(respheaders))) if respheaders else None

        self.render("ruleadd.html",
                    tryagain=False,
                    item=item,
                    origin=origin,
                    host=host,
                    entry=entry,
                    body=body,
                    fmt=fmt,
                    reqheaders=reqheaders,
                    respheaders=respheaders)
    def get(self, stream_id):

        stream = yield motor.Op(self.db.streams.find_one,
                                {"_id": ObjectId(stream_id)}, {
                                    "reencoding": 1,
                                    "name": 1,
                                    "default_program_id": 1,
                                    "_id": 1
                                })

        program = yield motor.Op(
            self.db.programs.find_one,
            {"_id": ObjectId(stream["default_program_id"])}, {
                "groups": 1,
                "selection": 1,
                "_id": 0
            })
        # basic mode pick first, in extended mode there is different situation where user selecting the group
        group_id = program["groups"][0]

        cursor = self.db.media.find(
            {
                "stream_id": stream_id,
                "groups.id": group_id
            }, {
                "_id": 1,
                "artist": 1,
                "title": 1,
                "tags.duration": 1,
                "groups.weight": 1,
                "file_id": 1
            },
            sort=[('groups.weight', 1)])

        results = yield motor.Op(cursor.to_list, self.batch_size)

        self.template_vars["group_id"] = group_id

        self.template_vars["group"] = yield motor.Op(
            self.db.groups.find_one, {"_id": ObjectId(group_id)}, {
                "fade_in": 1,
                "fade_out": 1,
                "_id": 0
            })

        self.template_vars["program"] = program

        stream["id"] = unicode(stream["_id"])
        self.template_vars["stream"] = stream

        self.template_vars["media"] = results

        self.template_vars["menu"] = "streams"
        self.template_vars["submenu"] = "database"
        self.template_vars["stream_id"] = stream_id

        self.render("admin/database.html", **self.template_vars)
Example #20
0
    def get(self):

        stream_id = self.get_argument("stream_id")

        stream = yield motor.Op(self.db.streams.find_one,
                                {"_id": ObjectId(stream_id)}, {
                                    "reencoding": 1,
                                    "name": 1,
                                    "default_program_id": 1,
                                    "fav_group_id": 1,
                                    "_id": 1
                                })
        stream["_id"] = unicode(stream["_id"])

        program = yield motor.Op(
            self.db.programs.find_one,
            {"_id": ObjectId(stream["default_program_id"])}, {
                "groups": 1,
                "selection": 1,
                "_id": 0
            })
        # basic mode pick first, in extended mode there is different situation where user selecting the group
        group_id = program["groups"][0]

        cursor = self.db.media.find(
            {
                "stream_id": stream_id,
                "groups.id": group_id
            }, {
                "_id": 1,
                "artist": 1,
                "title": 1,
                "tags.duration": 1,
                "groups.weight": 1,
                "file_id": 1
            },
            sort=[('groups.weight', 1)])

        results = yield motor.Op(cursor.to_list, self.batch_size)
        media = self.list_normalize(results)

        group = yield motor.Op(self.db.groups.find_one,
                               {"_id": ObjectId(group_id)}, {
                                   "fade_in": 1,
                                   "fade_out": 1,
                                   "_id": 0
                               })

        self.write({
            "stream": stream,
            "media": media,
            "group_id": group_id,
            "group": group,
            "program": program
        })
        self.finish()
Example #21
0
    def post(self):

        stream = yield motor.Op(self.db.streams.find_one, {"_id":ObjectId(self.get_argument("stream_id"))},
                                {"current":1, "name": 1, "user_id": 1, "_id": 0})

        user = yield motor.Op(self.db.users.find_one, {"_id":ObjectId(stream['user_id'])},
                                {"name": 1, "email": 1, "_id": 0})

        self.write({"stream": stream, "user": user})
        self.finish()
Example #22
0
    def post(self, key):
        res = yield motor.Op(self.application.mongodb.pwrecover.find_one,
                             {'key': key})
        if not res:
            raise tornado.web.HTTPError(404)
        uid = res['_id']

        # Apply canonicalizations to the passwords.
        password = self.get_argument('password', '')
        password = unicodedata.normalize('NFKC', password)
        password = password.encode()  # to UTF8 bytes
        password2 = self.get_argument('password2', '')
        password2 = unicodedata.normalize('NFKC', password2)
        password2 = password2.encode()  # to UTF8 bytes

        formerror = None
        formfocus = 'name'

        if self.twsessionstatus == 'auth':
            formerror = 'You are already signed in!'
        elif (not password):
            formerror = 'You must enter your new password.'
            formfocus = 'password'
        elif (not password2):
            formerror = 'You must enter your new password twice.'
            formfocus = 'password2'
        elif (len(password) < 6):
            formerror = 'Please use at least six characters in your password.'
            formfocus = 'password'
        elif (len(password) > 128):
            formerror = 'Please use no more than 128 characters in your password.'
            formfocus = 'password'
        elif (password != password2):
            formerror = 'The passwords you entered were not the same.'
            password2 = ''
            formfocus = 'password2'

        if formerror:
            self.render('recover2.html', key=key, formerror=formerror)
            return

        try:
            yield self.application.twsessionmgr.change_password(uid, password)
            # Success.
            yield motor.Op(self.application.mongodb.pwrecover.remove,
                           {'key': key})
            formerror = None
        except MessageException as ex:
            formerror = str(ex)

        if formerror:
            self.render('recover2.html', key=key, formerror=formerror)
            return

        self.render('recover2.html', key=key, pwchanged=True)
Example #23
0
    def post(self):
        '''
            Cebus applications post

            Register a app record
        '''
        result = yield gen.Task(check_json, self.request.body)
        struct, error = result.args
        if error:
            self.set_status(400)
            self.finish(error)
            return

        records = yield gen.Task(self.new_app_record, struct)
        app_id, error = records.args

        # WARNING: The complete error module it's going to be re-written
        if error:
            error = str(error)
            system_error = errors.Error(error)
            # Error handling 409?
            self.set_status(400)

        if error and 'Model' in error:
            error = system_error.model('Applications')
            self.finish(error)
            return
        elif error and 'duplicate' in error:
            error = system_error.duplicate('App', 'uniqueid',
                                           struct['uniqueid'])
            self.finish(error)
            return
        elif error:
            self.finish(error)
            return

        if 'accountcode' in struct:
            account = struct['accountcode']
            resource = {
                'account': account,
                'resource': 'applications',
                'id': app_id
            }

            exist = yield motor.Op(self.check_exist, account)

            if exist:
                # update the account data set the app_id on applications resources
                update = yield motor.Op(self.new_resource, resource)

        # Set status
        self.set_status(201)

        # Return the new application id
        self.finish({'id': app_id})
Example #24
0
    def global_setfocus(symbol, player=None):
        """Set the given player's focus to this symbol. (If player is None,
        the current player; if a location, then every player in it; if
        the realm, then every player in the instance.)
        """
        ctx = EvalPropContext.get_current_context()
        if ctx.level != LEVEL_EXECUTE:
            raise Exception('setfocus: may only occur in action code')
        if type(symbol) is not str:
            raise TypeError('setfocus: symbol must be string')

        uid = None
        query = None
        
        if player is None:
            if not ctx.uid:
                raise Exception('No current player')
            uid = ctx.uid
        elif isinstance(player, two.execute.PlayerProxy):
            uid = player.uid
        elif isinstance(player, two.execute.RealmProxy):
            query = { 'iid':ctx.loctx.iid }
        elif isinstance(player, two.execute.LocationProxy):
            query = { 'iid':ctx.loctx.iid, 'locid':player.locid }
        else:
            raise TypeError('setfocus: must be player, location, realm, or None')
        if uid is not None:
            # Focus exactly one player.
            res = yield motor.Op(ctx.app.mongodb.playstate.find_one,
                                 {'_id':uid},
                                 {'iid':1, 'focus':1})
            if not res:
                raise KeyError('No such player')
            if res['iid'] != ctx.loctx.iid:
                raise Exception('Player is not in this instance')
            yield motor.Op(ctx.app.mongodb.playstate.update,
                           {'_id':uid},
                           {'$set':{'focus':symbol}})
            ctx.task.set_dirty(uid, DIRTY_FOCUS)
        elif query is not None:
            # Focus all matching players
            uids = []
            cursor = ctx.app.mongodb.playstate.find(query,
                                                    {'_id':1})
            while (yield cursor.fetch_next):
                player = cursor.next_object()
                uids.append(player['_id'])
            # cursor autoclose
            if uids:
                yield motor.Op(ctx.app.mongodb.playstate.update,
                               query,
                               {'$set':{'focus':symbol}}, multi=True)
                ctx.task.set_dirty(uids, DIRTY_FOCUS)
Example #25
0
    def post(self):
        """
        Delete everything in the collection
        """
        db = self.settings['motor_db']
        yield motor.Op(db.chirps.drop)
        chirps.clear()
        yield motor.Op(db.create_collection, 'chirps', size=10000, capped=True)
        logging.info('Created capped collection "chirps" in database "test"')

        self.settings['cursor_manager'].emit('cleared', {})
        self.finish()
Example #26
0
    def get(self):

        session = self.get_argument("session", default=False)

        confirm_key = self.get_argument('confirm-key', None)
        user_id = self.get_argument('user-id', None)
        confirmed = None
        try:
            if not confirm_key is None and not user_id is None:
                user = yield motor.Op(self.db.users.find_one, {
                    "_id": ObjectId(user_id),
                    "confirm_key": confirm_key
                }, {
                    "confirmed": 1,
                    "_id": 0,
                    "waiting_email": 1
                })
                if user is not None:
                    if user["confirmed"]:
                        yield motor.Op(self.db.users.update,
                                       {"_id": ObjectId(user_id)}, {
                                           "$set": {
                                               "confirmed": True,
                                               "email": user["waiting_email"]
                                           }
                                       },
                                       upsert=False,
                                       multi=False)
                    else:
                        yield motor.Op(self.db.users.update,
                                       {"_id": ObjectId(user_id)},
                                       {"$set": {
                                           "confirmed": True
                                       }},
                                       upsert=False,
                                       multi=False)

                        yield self.new_session(unicode(user_id))

                    confirmed = True
                else:
                    confirmed = False
        except:
            confirmed = False

        self.write({
            "session_file_no": 0,
            "user": self.current_user if self.current_user else False,
            "genres": genres,
            "session": session,
            "confirmed": confirmed
        })
        self.finish()
Example #27
0
    def post(self, fileid):
        image = yield motor.Op(self.db.images.find_one, {'fileid': fileid})
        if not image:
            raise tornado.web.HTTPError(404, "File not found")

        featured = image.get('featured', True)
        yield motor.Op(self.db.images.update, {'_id': image['_id']},
                       {'$set': {
                           'featured': not featured
                       }})

        url = self.reverse_url('admin_image', fileid)
        self.redirect(url)
Example #28
0
    def setUp(self):
        super(TestTrackingClientWithMockPusher, self).setUp()

        logging.basicConfig(
            level=logging.DEBUG,
            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

        self.pusher = MockPusher()
        self.server.application.pusher = self.pusher

        # Cleaning test database after tests
        motor.Op(self.server.application.mongo.trackings.remove)
        motor.Op(self.server.application.mongo.tdespatch_cabdriver.remove)
def run(*args):
    _redis = redis.client.Redis(settings.REDIS_HOST, settings.REDIS_PORT)
    connection = motor.MotorConnection().open_sync()
    db = connection.tiler

    try:
        cursor = (db.images.find())
        image = yield motor.Op(cursor.next_object)
        while image:
            _redis.incr('bytes_downloaded', image['size'])
            image = yield motor.Op(cursor.next_object)

    finally:
        IOLoop.instance().stop()
Example #30
0
    def monitor_mongo_status(self):
        """Check the status of the MongoDB connection. If the server has
        died, close the socket. If the socket is closed (or has never been
        opened), try to open it.

        This is called once when the app launches, to open the initial
        connection, and every few seconds thereafter.

        The mongotimerbusy flag protects us from really slow connection
        attempts. Not sure why that would happen, but if it does, we'll
        avoid piling up multiple attempts. On the down side, if the function
        throws an uncaught exception, the flag will be stuck forever.
        (So don't do that.)
        """
        if (self.mongotimerbusy):
            self.log.warning('monitor_mongo_status: already in flight; did a previous call jam?')
            return
        if (self.app.caughtinterrupt):
            self.log.warning('monitor_mongo_status: shutting down, never mind')
            return
        self.mongotimerbusy = True
        
        if (self.mongoavailable):
            try:
                res = yield motor.Op(self.mongo.admin.command, 'ping')
                if (not res):
                    self.log.error('Mongo client not alive')
                    self.mongoavailable = False
            except Exception as ex:
                self.log.error('Mongo client not alive: %s', ex)
                self.mongoavailable = False
            if (not self.mongoavailable):
                self.mongo_disconnect()
            
        if (not self.mongoavailable):
            try:
                self.mongo = motor.MotorClient(tz_aware=True)
                res = yield motor.Op(self.mongo.open)
                ### maybe authenticate to a database?
                self.mongoavailable = True
                self.app.mongodb = self.mongo[self.app.twopts.mongo_database]
                self.log.info('Mongo client open')
                # Schedule a callback to load up the localization data.
                tornado.ioloop.IOLoop.instance().add_callback(self.load_localization)
            except Exception as ex:
                self.mongoavailable = False
                self.app.mongodb = None
                self.log.error('Mongo client not open: %s', ex)
        
        self.mongotimerbusy = False