Beispiel #1
0
def _get_job(experiment, job_sequence):
    try:
        job = ExperimentJob.objects.get(experiment=experiment, sequence=job_sequence)
    except (ExperimentJob.DoesNotExist, ValidationError):
        logger.info('Job with experiment:`{}` sequence:`{}` does not exist'.format(
            experiment.unique_name, job_sequence))
        raise exceptions.NotFound('Experiment was not found')

    if not job.is_running:
        logger.info('Job with experiment:`{}` sequence:`{}` is not currently running'.format(
            experiment.unique_name, job_sequence))
        raise exceptions.NotFound('Job was not running')

    return job
Beispiel #2
0
def _get_job(experiment, job_id):
    try:
        job = ExperimentJob.objects.get(experiment=experiment, id=job_id)
    except (ExperimentJob.DoesNotExist, ValidationError):
        _logger.info('Job with experiment:`%s` id:`%s` does not exist',
                     experiment.unique_name, job_id)
        raise exceptions.NotFound('Experiment was not found')

    if not job.is_running:
        _logger.info(
            'Job with experiment:`%s` id:`%s` is not currently running',
            experiment.unique_name, job_id)
        raise exceptions.NotFound('Job was not running')

    return job
Beispiel #3
0
 async def get(self, _, id: str):
     with db_session:
         try:
             c = Community[id]
             return response.json(c.to_dict())
         except (ObjectNotFound, ValueError) as _:
             pass  # try it as a slug
         try:
             c = Community.get(slug=id)
             if c is None:
                 raise exceptions.NotFound(
                     "Community {} not found".format(id))
             return response.json(c.to_dict())
         except ObjectNotFound:
             raise exceptions.NotFound("Community {} not found".format(id))
Beispiel #4
0
    def _handle_request(self, request, req_path):
        """Listen to get requests"""
        css = self._web_compile(request.path)
        if css is None:
            raise exceptions.NotFound(f"{req_path} not found.", False)

        return response.text(css, content_type="text/css ")
Beispiel #5
0
    async def wrapper(request, *args, **kwargs):
        user = await User.find_one(dict(name=kwargs["user"].lower()))
        if not user:
            raise exceptions.NotFound("User not found")

        kwargs["user"] = user
        return await handler(request, *args, **kwargs)
Beispiel #6
0
 async def patch(self, request, id):
     with db_session:
         try:
             c = Community[id]
             if 'slug' in request.json:
                 c.slug = request.json['slug']
             if 'name' in request.json:
                 c.name = request.json['name']
             if 'description' in request.json:
                 c.description = request.json['description']
             if 'icon' in request.json:
                 c.icon = request.json['icon']
             if 'owner' in request.json:
                 c.owner = request.json['owner']
             if 'tags' in request.json:
                 c.tags.clear()
                 for tag_name in request.json['tags']:
                     try:
                         tag = Tag[tag_name]
                     except ObjectNotFound:
                         tag = Tag(tag=tag_name)
                     c.tags.add(tag)
             commit()
             return response.json(c.to_dict())
         except ObjectNotFound:
             raise exceptions.NotFound("Community {} not found".format(id))
Beispiel #7
0
def _get_validated_experiment(project, experiment_sequence):
    experiment = _get_experiment(project, experiment_sequence)
    if not experiment.is_running:
        _logger.info('Experiment project `%s` num `%s` is not currently running',
                     project.name, experiment.sequence)
        raise exceptions.NotFound('Experiment was not running')

    return experiment
Beispiel #8
0
    async def wrapper(request, *args, **kwargs):

        data = await Outbox.find_one(dict(user_id=kwargs["user"].name, _id=kwargs["entity"]))
        if not data:
            raise exceptions.NotFound("Object not found")

        kwargs["entity"] = data
        return await handler(request, *args, **kwargs)
Beispiel #9
0
def _get_running_experiment(project, experiment_id):
    experiment = _get_experiment(project, experiment_id)
    if not experiment.is_running:
        _logger.info('Experiment project `%s` is not currently running',
                     experiment.unique_name)
        raise exceptions.NotFound('Experiment was not running')

    return experiment
Beispiel #10
0
async def remove_command(request):
    command_name = request.json['name']
    try:
        await db.delete(command_name)
    except KeyError:
        raise exceptions.NotFound('Command not found')

    return response.json({'success': True})
Beispiel #11
0
 async def delete(self, _, id):
     with db_session:
         try:
             c = Community[id]
             c.delete()
             commit()
         except ObjectNotFound:
             raise exceptions.NotFound("Community {} not found".format(id))
         return response.text('', status=204)
Beispiel #12
0
async def delete_todo_item(request, todo_pk):
    try:
        # Try to fetch the item from collection to assert it exists.
        assert collection[uuid.UUID(todo_pk)]
        request.app.loop.call_later(
            randint(50, 5000) / 1000, _update_data, uuid.UUID(todo_pk), None)
    except KeyError:
        raise exceptions.NotFound("Task was not found")

    return response.text('', status=HTTPStatus.ACCEPTED)
Beispiel #13
0
async def get_image_route(request, img_id):
    try:
        indexed_image = await IndexedImage.load_from_index_async(
            app.index_redis, img_id)
    except KeyError as e:
        if e.args[0].startswith("No image"):
            raise exceptions.NotFound("No image " + str(img_id) + " in index")
        else:
            raise e

    img_path = await load_indexed_image(app, indexed_image)
    _, ext = osp.splitext(img_path)

    return await response.file_stream(img_path, mime_type=image_types[ext[1:]])
Beispiel #14
0
    async def handler(self, request, server, sid, tid):
        if tid not in self.handlers or tid in self.disable_transports:
            raise exceptions.NotFound("SockJS transport handler not found.")

        create, transport = self.handlers[tid]

        # session
        manager = self.manager
        if not manager.started:
            manager.start()

        if not sid or "." in sid or "." in server:
            raise exceptions.NotFound("SockJS bad route")

        try:
            session = manager.get(sid, create, request=request)
        except KeyError:
            return response.HTTPResponse(None,
                                         status=404,
                                         headers=session_cookie(request))

        t = transport(manager, session, request)
        try:
            return await t.process()
        except asyncio.CancelledError:
            raise
        except exceptions.SanicException as exc:
            msg = "Server Exception in Transport handler: %s" % str(exc)
            log.exception(msg)
            raise
        except Exception:
            msg = "Exception in transport: %s" % tid
            log.exception(msg)
            if manager.is_acquired(session):
                await manager.release(session)
            raise exceptions.ServerError(msg)
Beispiel #15
0
async def get_todo_item(request, todo_pk):
    # Initialize the output schema.
    output_schema = TaskView()

    # Get the item.
    try:
        item = collection[uuid.UUID(todo_pk)]
    except KeyError:
        raise exceptions.NotFound("Task was not found")

    return response.json(output_schema.dump({
        **item,
        'url':
        get_absolute_url(request, 'get_todo_item', todo_pk=item['uuid']),
    }),
                         status=HTTPStatus.OK,
                         escape_forward_slashes=False)
Beispiel #16
0
async def get_index_data(request, img_id):
    try:
        indexed_image = await IndexedImage.load_from_index_async(
            app.index_redis, img_id)
    except KeyError as e:
        if e.args[0].startswith("No image"):
            raise exceptions.NotFound("No image " + str(img_id) + " in index")
        else:
            raise e

    data = {
        "img_id": str(indexed_image.img_id),
        "imhash": base64.b64encode(indexed_image.imhash),
        "cache_path": "image/" + indexed_image.cache_filename,
    }
    data.update(attr.asdict(indexed_image.queued_img_data))

    return response.json(data)
Beispiel #17
0
async def token_get(request):
    user = await User.find_one(
        dict(name=request.json["username"].lower().lstrip('@')))
    if not user:
        raise exceptions.NotFound("User not found")

    if not check_password_hash(user.password, request.json["password"]):
        return response.json({"error": "password incorrect"}, status=401)

    token = getattr(user, "token")
    if not token:
        token = random_object_id()
        #TODO make token expire
        await User.update_one({'name': request.json["username"]},
                              {'$set': {
                                  'token': token
                              }})

    return response.json({'access_token': token})
Beispiel #18
0
async def set_todo_item_status(request, todo_pk):
    # Initialize command schema.
    command_schema = Command()
    # Initialize output schema.
    output_schema = TaskView()

    try:
        item = collection[uuid.UUID(todo_pk)]
    except KeyError:
        raise exceptions.NotFound("Task was not found")

    command = command_schema.load(request.json).get('command')
    # Populate `data` variable to ensure it exists, should
    # the command handling conditional logic be skipped.
    data = item

    if command == 'complete' and not item['is_completed']:
        data = {**item, 'is_completed': True, 'completed_at': datetime.now()}
    elif command == 'revert' and item['is_completed']:
        data = {
            **item,
            'is_completed': False,
            'completed_at': None,
        }

    if item != data:
        # Data was modified. Store it.
        request.app.loop.call_later(
            randint(50, 5000) / 1000, _update_data, data['uuid'], data)

        return response.json(output_schema.dump({
            **data,
            'url':
            get_absolute_url(request, 'get_todo_item', todo_pk=data['uuid']),
        }),
                             status=HTTPStatus.ACCEPTED,
                             escape_forward_slashes=False)
    else:
        # Return empty response, since nothing was modified.
        return response.text('', status=HTTPStatus.NO_CONTENT)
Beispiel #19
0
async def update_todo_item(request, todo_pk):
    # Initialize the input schema.
    input_schema = Task(exclude=('created_at', 'completed_at', 'uuid',
                                 'is_completed'))
    store_schema = Task()
    # Initialize the output schema.
    output_schema = TaskView()

    try:
        # Validate the data.
        data = input_schema.load(request.json)

        item = collection[uuid.UUID(todo_pk)]

        data = store_schema.dump({
            **item,
            **data,
        })
        data = store_schema.load(data)
    except KeyError:
        raise exceptions.NotFound("Task was not found")

    if data != item:
        # Data was modified. Store it.
        request.app.loop.call_later(
            randint(50, 5000) / 1000, _update_data, item['uuid'], data)

        return response.json(output_schema.dump({
            **data,
            'url':
            get_absolute_url(request, 'get_todo_item', todo_pk=data['uuid']),
        }),
                             status=HTTPStatus.ACCEPTED,
                             escape_forward_slashes=False)
    else:
        # Return empty response, since nothing was modified.
        return response.text('', status=HTTPStatus.NO_CONTENT)
Beispiel #20
0
def _get_experiment(project, experiment_sequence):
    try:
        return Experiment.objects.get(project=project,
                                      sequence=experiment_sequence)
    except (Experiment.DoesNotExist, ValidationError):
        raise exceptions.NotFound('Experiment was not found')
Beispiel #21
0
def _get_project(username, project_name):
    try:
        return Project.objects.get(name=project_name, user__username=username)
    except Project.DoesNotExist:
        raise exceptions.NotFound('Project was not found')
Beispiel #22
0
async def handle_path(request, path):
    # make sure we aren't serving up paths from outside of the current directory
    # eg prevents curl 'http://127.0.0.1:8000/%2e%2e/' serving up the parent directory
    current_directory = os.path.realpath('.')
    full_path = os.path.realpath(os.path.join(current_directory, path))
    if os.path.commonprefix([full_path, current_directory
                             ]) != current_directory:
        raise exceptions.Forbidden(
            "Can only serve files from within the current directory")

    if os.path.exists(full_path) is False:
        raise exceptions.NotFound("Path does not exist")

    current_user = auth.current_user(request)
    is_logged_in = (current_user is not None)

    if os.path.isdir(full_path) is True:
        # serve an html representation of the directory
        html = list_directory(full_path, path, is_logged_in)

        # prevent any sort of browser caching
        headers = {
            'Cache-Control': 'private, max-age=0, no-cache, no-store',
        }

        return response.html(html, headers=headers)
    else:
        chunk_size = 2 * (1024**2)  # 2mb
        file_size = os.path.getsize(full_path)

        range_object = None
        range_header = request.headers.get('Range')
        if range_header is not None:
            range_object = types.SimpleNamespace()
            range_object.start = 0
            range_object.total = file_size
            range_object.end = range_object.total - 1

            range_ = range_header.split('=')[1]
            requested_start, requested_end = range_.split('-')

            if requested_start is not '':
                range_object.start = int(requested_start)

            if requested_end is not '':
                range_object.end = int(requested_end)

            range_object.end = min(range_object.end,
                                   range_object.start + chunk_size - 1)
            range_object.size = range_object.end - range_object.start + 1

        headers = {
            'Accept-Ranges': 'Accept-Ranges: bytes',
            'Monkey-Patch-Content-Length': file_size,
        }

        return await response.file_stream(
            full_path,
            headers=headers,
            chunk_size=chunk_size,
            _range=range_object,
        )
Beispiel #23
0
async def update_hotel(request):
    logger = logging.getLogger(__name__)
    try:
        body = request.json
    except exceptions.InvalidUsage:
        body = None
    if not body or not isinstance(body, dict):
        logger.warning(f"Invalid request body: {request.body}")
        raise exceptions.InvalidUsage({
            "status": 400,
            "errmsg": "Invalid request body"
        })

    oid = body.get("_id")
    if not oid:
        logger.warning('_id is required!')
        raise exceptions.InvalidUsage({
            "status": 400,
            "errmsg": "_id is required!"
        })
    db = databases("scripture")
    doc = await db.statics.hotels.relux.rooms.find_one({"_id": ObjectId(oid)},
                                                       {"rooms_cn": 1})
    if not doc:
        logger.warning(f'oid:{oid} Corresponding Hotel not found')
        raise exceptions.NotFound({
            "status": 404,
            "errmsg": "Hotel not found!"
        })
    ori_rooms_cn = doc.get("rooms_cn")
    if not ori_rooms_cn:
        logger.warning(f'oid:{oid} Corresponding Hotel rooms_cn not found')
        raise exceptions.NotFound({
            "status": 404,
            "errmsg": "Hotel rooms_cn not found!"
        })
    rooms_cn = body.get("rooms_cn")
    if not rooms_cn:
        logger.warning(f'oid:{oid} Corresponding rooms_cn is required')
        raise exceptions.InvalidUsage({
            "status": 400,
            "errmsg": "rooms_cn is required!"
        })

    futures = []
    for room, ori_room in zip(rooms_cn, ori_rooms_cn):
        room_id = room["id"]
        if room_id != ori_room["id"]:
            logger.error(f"The order of the rooms is out of order: "
                         f"room({room}), ori_doc({doc})")
            continue
        if room == ori_room:
            continue

        for plan, ori_plan in zip(room.get("plans", []),
                                  ori_room.get("plans", [])):
            if plan["id"] != ori_plan["id"]:
                logger.error(f"The order of the plans is out of order: "
                             f"plan({plan}), ori doc({doc})")
                continue
            if plan == ori_plan:
                continue
            if plan["name"] != ori_plan["name"]:
                plan["ori_name"] = ori_plan["name"]
            if plan["feature"] != ori_plan["feature"]:
                plan["ori_feature"] = ori_plan["feature"]
        future = asyncio.ensure_future(
            db.statics.hotels.relux.rooms.update_one(
                {
                    "_id": ObjectId(oid),
                    "rooms_cn.id": room_id,
                    "rooms_cn.plans": ori_room["plans"]
                }, {"$set": {
                    "rooms_cn.$.plans": room["plans"]
                }}))
        extra = {
            "oid": oid,
            "ori_plan": ori_room["plans"],
            "new_plan": room["plans"]
        }
        future.add_done_callback(partial(callback, extra=extra))
        futures.append(future)

    if not futures:
        logger.warning(
            f'ori_room["plans"]:{ori_room["plans"]},room["plans"]:{room["plans"]},'
            f' No difference or rooms order is wrong')
        raise exceptions.InvalidUsage({
            "status":
            400,
            "errmsg":
            "No difference or rooms order is wrong"
        })
    logger.info(f'oid:{oid} update_relux_plan success')
    return response.json({"status": 200, "data": {"count": len(futures)}})
Beispiel #24
0
async def publish(request, principal):
    request.principal = principal
    try:
        return await publisher(request, root)
    except ResolveError:
        raise exceptions.NotFound('Resource not found.')
Beispiel #25
0
async def get_character_images_route(request, character):
    page = 0
    count = 100

    character = character.lower()

    if "page" in request.args:
        try:
            page = int(request.args["page"][0])
        except ValueError:
            raise exceptions.InvalidUsage("Page argument must be an integer")

    if "count" in request.args:
        try:
            count = int(request.args["count"][0])
        except ValueError:
            raise exceptions.InvalidUsage("Count argument must be an integer")

    start_index = page * count

    if not (await app.index_redis.exists("index:characters:" + character) > 0):
        raise exceptions.NotFound("No character " + character +
                                  " found in index")

    filter_sets = []

    if "tag" in request.args:
        filter_sets.extend("index:tags:merged:" + t
                           for t in request.args["tag"])

    if "author" in request.args:
        filter_sets.extend("index:authors:" + a
                           for a in request.args["author"])

    if "site" in request.args:
        filter_sets.extend("index:sites:" + s for s in request.args["site"])

    if "rating" in request.args:
        filter_sets.extend("index:rating:" + r for r in request.args["rating"])

    if len(filter_sets) > 0:
        filter_sets.insert(0, "index:characters:" + character)

        h = hashlib.sha1()
        for s in filter_sets:
            h.update(s.encode("utf-8"))
        dest_key = "tmp_query:" + h.hexdigest()

        if not (await app.index_redis.exists(dest_key)):
            await app.index_redis.zinterstore(dest_key,
                                              *filter_sets,
                                              aggregate="max")

        await app.index_redis.expire(dest_key, 15 * 60)

        total = await app.index_redis.zcard(dest_key)
        ids = await app.index_redis.zrange(dest_key,
                                           start_index,
                                           start_index + count,
                                           encoding="utf-8")
    else:
        total = await app.index_redis.zcard("index:characters:" + character)
        ids = await app.index_redis.zrange(
            "index:characters:" + character,
            start_index,
            start_index + count,
            encoding="utf-8",
        )

    resp = []
    for img_id in ids:
        indexed_image = await IndexedImage.load_from_index_async(
            app.index_redis, int(img_id))

        index_data = {
            "img_id": str(indexed_image.img_id),
            "imhash": base64.b64encode(indexed_image.imhash),
            "cache_path": "image/" + indexed_image.cache_filename,
        }
        index_data.update(attr.asdict(indexed_image.queued_img_data))

        resp.append(index_data)

    return response.json(resp, headers={"X-Total-Items": total})