コード例 #1
0
ファイル: base.py プロジェクト: jmichault/motioneye.eo
 def get(self, *args, **kwargs):
     raise HTTPError(404, 'not found')
コード例 #2
0
 def do_error(self, msg, code=500):
     raise HTTPError(code, msg)
コード例 #3
0
ファイル: tasks.py プロジェクト: drewsherlock/flower
    def post(self, taskname):
        """
Execute a task by name and wait results

**Example request**:

.. sourcecode:: http

  POST /api/task/apply/tasks.add HTTP/1.1
  Accept: application/json
  Accept-Encoding: gzip, deflate, compress
  Content-Length: 16
  Content-Type: application/json; charset=utf-8
  Host: localhost:5555

  {
      "args": [1, 2]
  }

**Example response**:

.. sourcecode:: http

  HTTP/1.1 200 OK
  Content-Length: 71
  Content-Type: application/json; charset=UTF-8

  {
      "state": "SUCCESS",
      "task-id": "c60be250-fe52-48df-befb-ac66174076e6",
      "result": 3
  }

:query args: a list of arguments
:query kwargs: a dictionary of arguments
:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
:statuscode 404: unknown task
        """
        args, kwargs, options = self.get_task_args()
        logger.debug("Invoking a task '%s' with '%s' and '%s'", taskname, args,
                     kwargs)

        try:
            task = self.capp.tasks[taskname]
        except KeyError:
            raise HTTPError(404, "Unknown task '%s'" % taskname)

        try:
            self.normalize_options(options)
        except ValueError:
            raise HTTPError(400, 'Invalid option')

        result = task.apply_async(args=args, kwargs=kwargs, **options)
        response = {'task-id': result.task_id}

        # In tornado for not blocking event loop we must return results
        # from other thread by self.finish()
        th = Thread(target=self.wait_results, args=(
            result,
            response,
        ))
        th.start()
コード例 #4
0
 def wrap(self, *args, **kwargs):
     if self.valid_user():
         result = method(self, *args, **kwargs)
         return result
     else:
         raise HTTPError(500, '用户验证出错')
コード例 #5
0
ファイル: backstage.py プロジェクト: thezawad/Vulpix
 def wrapper(self, *args, **kwargs):
     if self.current_user:
         if self.current_user.admin:
             return method(self, *args, **kwargs)
     raise HTTPError(404)
コード例 #6
0
 def get(self):
     raise HTTPError(400, "Oops")
コード例 #7
0
 def prepare(self):
     super(ErrorHandler, self).prepare()
     raise HTTPError(404)
コード例 #8
0
 def default_filter(handler: RequestHandler) -> None:
     if not mngr.apply_profile(
             'default', handler, http_proxy, with_referer=with_referer):
         raise HTTPError(403, reason="Unauthorized profile")
コード例 #9
0
async def mock_gateway_request(url, **kwargs):
    method = 'GET'
    if kwargs['method']:
        method = kwargs['method']

    request = HTTPRequest(url=url, **kwargs)

    endpoint = str(url)

    # Fetch all kernelspecs
    if endpoint.endswith('/api/kernelspecs') and method == 'GET':
        response_buf = StringIO(json.dumps(kernelspecs))
        response = await maybe_future(
            HTTPResponse(request, 200, buffer=response_buf))
        return response

    # Fetch named kernelspec
    if endpoint.rfind('/api/kernelspecs/') >= 0 and method == 'GET':
        requested_kernelspec = endpoint.rpartition('/')[2]
        kspecs = kernelspecs.get('kernelspecs')
        if requested_kernelspec in kspecs:
            response_buf = StringIO(
                json.dumps(kspecs.get(requested_kernelspec)))
            response = await maybe_future(
                HTTPResponse(request, 200, buffer=response_buf))
            return response
        else:
            raise HTTPError(404,
                            message='Kernelspec does not exist: %s' %
                            requested_kernelspec)

    # Create kernel
    if endpoint.endswith('/api/kernels') and method == 'POST':
        json_body = json.loads(kwargs['body'])
        name = json_body.get('name')
        env = json_body.get('env')
        kspec_name = env.get('KERNEL_KSPEC_NAME')
        assert name == kspec_name  # Ensure that KERNEL_ env values get propagated
        model = generate_model(name)
        running_kernels[model.get(
            'id')] = model  # Register model as a running kernel
        response_buf = StringIO(json.dumps(model))
        response = await maybe_future(
            HTTPResponse(request, 201, buffer=response_buf))
        return response

    # Fetch list of running kernels
    if endpoint.endswith('/api/kernels') and method == 'GET':
        kernels = []
        for kernel_id in running_kernels.keys():
            model = running_kernels.get(kernel_id)
            kernels.append(model)
        response_buf = StringIO(json.dumps(kernels))
        response = await maybe_future(
            HTTPResponse(request, 200, buffer=response_buf))
        return response

    # Interrupt or restart existing kernel
    if endpoint.rfind('/api/kernels/') >= 0 and method == 'POST':
        requested_kernel_id, sep, action = endpoint.rpartition(
            '/api/kernels/')[2].rpartition('/')

        if action == 'interrupt':
            if requested_kernel_id in running_kernels:
                response = await maybe_future(HTTPResponse(request, 204))
                return response
            else:
                raise HTTPError(404,
                                message='Kernel does not exist: %s' %
                                requested_kernel_id)
        elif action == 'restart':
            if requested_kernel_id in running_kernels:
                response_buf = StringIO(
                    json.dumps(running_kernels.get(requested_kernel_id)))
                response = await maybe_future(
                    HTTPResponse(request, 204, buffer=response_buf))
                return response
            else:
                raise HTTPError(404,
                                message='Kernel does not exist: %s' %
                                requested_kernel_id)
        else:
            raise HTTPError(404, message='Bad action detected: %s' % action)

    # Shutdown existing kernel
    if endpoint.rfind('/api/kernels/') >= 0 and method == 'DELETE':
        requested_kernel_id = endpoint.rpartition('/')[2]
        running_kernels.pop(
            requested_kernel_id
        )  # Simulate shutdown by removing kernel from running set
        response = await maybe_future(HTTPResponse(request, 204))
        return response

    # Fetch existing kernel
    if endpoint.rfind('/api/kernels/') >= 0 and method == 'GET':
        requested_kernel_id = endpoint.rpartition('/')[2]
        if requested_kernel_id in running_kernels:
            response_buf = StringIO(
                json.dumps(running_kernels.get(requested_kernel_id)))
            response = await maybe_future(
                HTTPResponse(request, 200, buffer=response_buf))
            return response
        else:
            raise HTTPError(404,
                            message='Kernel does not exist: %s' %
                            requested_kernel_id)
コード例 #10
0
 def get(self, project_id):
     if project_id not in FAKE_DB["projects"]:
         raise HTTPError(404)
     self.write(FAKE_DB["projects"][project_id])
コード例 #11
0
 def get(self, user_id):
     if user_id not in FAKE_DB["users"]:
         raise HTTPError(404)
     self.write(FAKE_DB["users"][user_id])
コード例 #12
0
ファイル: swiftfs.py プロジェクト: edina/SwiftContentsManager
 def do_error(self, msg, code=500):
     self.log.error(msg)
     raise HTTPError(code, msg)
コード例 #13
0
ファイル: util.py プロジェクト: IceForgTW/AstroBox
	def get(self, path, include_body=True):
		if self._access_validation is not None:
			self._access_validation(self.request)

		path = self.parse_url_path(path)
		abspath = os.path.abspath(os.path.join(self.root, path))
		# os.path.abspath strips a trailing /
		# it needs to be temporarily added back for requests to root/
		if not (abspath + os.path.sep).startswith(self.root):
			raise HTTPError(403, "%s is not in root static directory", path)
		if os.path.isdir(abspath) and self.default_filename is not None:
			# need to look at the request.path here for when path is empty
			# but there is some prefix to the path that was already
			# trimmed by the routing
			if not self.request.path.endswith("/"):
				self.redirect(self.request.path + "/")
				return
			abspath = os.path.join(abspath, self.default_filename)
		if not os.path.exists(abspath):
			raise HTTPError(404)
		if not os.path.isfile(abspath):
			raise HTTPError(403, "%s is not a file", path)

		stat_result = os.stat(abspath)
		modified = datetime.datetime.fromtimestamp(stat_result[stat.ST_MTIME])

		self.set_header("Last-Modified", modified)

		mime_type, encoding = mimetypes.guess_type(abspath)
		if mime_type:
			self.set_header("Content-Type", mime_type)

		cache_time = self.get_cache_time(path, modified, mime_type)

		if cache_time > 0:
			self.set_header("Expires", datetime.datetime.utcnow() +
									   datetime.timedelta(seconds=cache_time))
			self.set_header("Cache-Control", "max-age=" + str(cache_time))

		self.set_extra_headers(path)

		# Check the If-Modified-Since, and don't send the result if the
		# content has not been modified
		ims_value = self.request.headers.get("If-Modified-Since")
		if ims_value is not None:
			date_tuple = email.utils.parsedate(ims_value)
			if_since = datetime.datetime.fromtimestamp(time.mktime(date_tuple))
			if if_since >= modified:
				self.set_status(304)
				return

		if not include_body:
			assert self.request.method == "HEAD"
			self.set_header("Content-Length", stat_result[stat.ST_SIZE])
		else:
			with open(abspath, "rb") as file:
				while True:
					data = file.read(LargeResponseHandler.CHUNK_SIZE)
					if not data:
						break
					self.write(data)
					self.flush()
コード例 #14
0
ファイル: sample_template.py プロジェクト: tanaes/qiita
def sample_template_handler_patch_request(user,
                                          req_op,
                                          req_path,
                                          req_value=None,
                                          req_from=None):
    """Patches the sample template

    Parameters
    ----------
    user: qiita_db.user.User
        The user performing the request
    req_op : str
        The operation to perform on the sample template
    req_path : str
        The path to the attribute to patch
    req_value : str, optional
        The new value
    req_from : str, optional
        The original path of the element

    Returns
    -------

    Raises
    ------
    HTTPError
        400 If the path parameter doens't follow the expected format
        400 If the given operation is not supported
    """
    req_path = [v for v in req_path.split('/') if v]
    # At this point we know the path should be at least length 2
    if len(req_path) < 2:
        raise HTTPError(400, reason='Incorrect path parameter')

    study_id = int(req_path[0])
    # Check if the current user has access to the study and if the sample
    # template exists
    sample_template_checks(study_id, user, check_exists=True)

    if req_op == 'remove':
        # Path format
        # column: study_id/columns/column_name
        # sample: study_id/samples/sample_id
        if len(req_path) != 3:
            raise HTTPError(400, reason='Incorrect path parameter')

        attribute = req_path[1]
        attr_id = req_path[2]

        qiita_plugin = Software.from_name_and_version('Qiita', 'alpha')
        cmd = qiita_plugin.get_command('delete_sample_or_column')
        params = Parameters.load(cmd,
                                 values_dict={
                                     'obj_class': 'SampleTemplate',
                                     'obj_id': study_id,
                                     'sample_or_col': attribute,
                                     'name': attr_id
                                 })
        job = ProcessingJob.create(user, params, True)
        # Store the job id attaching it to the sample template id
        r_client.set(SAMPLE_TEMPLATE_KEY_FORMAT % study_id,
                     dumps({'job_id': job.id}))
        job.submit()
        return {'job': job.id}
    elif req_op == 'replace':
        # WARNING: Although the patch operation is a replace, is not a full
        # true replace. A replace is in theory equivalent to a remove + add.
        # In this case, the replace operation doesn't necessarily removes
        # anything (e.g. when only new columns/samples are being added to the)
        # sample information.
        # Path format: study_id/data
        # Forcing to specify data for extensibility. In the future we may want
        # to use this function to replace other elements of the sample
        # information
        if len(req_path) != 2:
            raise HTTPError(400, reason='Incorrect path parameter')

        attribute = req_path[1]

        if attribute == 'data':
            # Update the sample information
            if req_value is None:
                raise HTTPError(400,
                                reason="Value is required when updating "
                                "sample information")

            # Check if the file exists
            fp_rsp = check_fp(study_id, req_value)
            if fp_rsp['status'] != 'success':
                raise HTTPError(404, reason='Filepath not found')
            filepath = fp_rsp['file']

            qiita_plugin = Software.from_name_and_version('Qiita', 'alpha')
            cmd = qiita_plugin.get_command('update_sample_template')
            params = Parameters.load(cmd,
                                     values_dict={
                                         'study': study_id,
                                         'template_fp': filepath
                                     })
            job = ProcessingJob.create(user, params, True)

            # Store the job id attaching it to the sample template id
            r_client.set(SAMPLE_TEMPLATE_KEY_FORMAT % study_id,
                         dumps({'job_id': job.id}))

            job.submit()
            return {'job': job.id}
        else:
            raise HTTPError(404, reason='Attribute %s not found' % attribute)

    else:
        raise HTTPError(400,
                        reason='Operation %s not supported. Current '
                        'supported operations: remove, replace' % req_op)
コード例 #15
0
    def post(self, _id):
        data = {}
        question = self.db.Question.find_one({'_id': ObjectId(_id)})
        if not question:
            raise HTTPError(404)

        url = self.reverse_url('admin_question_pictures', str(question['_id']))

        def delete_old():
            for p in (self.db.QuestionPicture.find({
                    'question': question['_id'],
                    'index': {
                        '$gt': 0
                    }
            })):
                p.delete()

        if self.get_argument('delete', False):
            delete_old()
            self.redirect(url)
            return

        iterations = int(self.get_argument('iterations'))
        effect = int(self.get_argument('effect', 0))

        delete_old()

        original, = self.db.QuestionPicture.find({
            'question': question['_id'],
            'index': 0
        })
        original_picture = original.fs.get_last_version('original')
        type_ = original_picture.content_type
        c = 1

        function = self.get_argument('function')

        if function == 'blurrer':
            func = lambda x, y, z: picture_factory.blurrer(
                x, y, z, effect=effect)
        elif function == 'tileshift':
            func = picture_factory.tileshift
        else:
            raise NotImplementedError(function)

        for (payload, format) in func(original_picture, (700, 700),
                                      iterations):
            qp = self.db.QuestionPicture()
            qp['question'] = question['_id']
            qp['index'] = c
            qp.save()
            with qp.fs.new_file('original') as f:
                f.content_type = type_
                payload.save(f, format)

            c += 1

        url = self.reverse_url('admin_question_pictures', str(question['_id']))
        url += '?iterations=%s&effect=%s' % (iterations, effect)
        url += '&function=%s' % function
        self.redirect(url)
コード例 #16
0
 def _fail(msg):
     self.log.exception(msg)
     raise HTTPError(400, msg)
コード例 #17
0
ファイル: service.py プロジェクト: py-prj-code/jzsouservice
    def get(self, city):

        query_dict = {'city_label': city, 'status': 'show'}
        pos = self.get_argument('pos', None)
        if pos:
            lat, lon = pos.split(',')
            query_dict['_location'] = {'$maxDistance': 0.091, '$near': [float(lon), float(lat)]}

        condition = self.get_argument('q')
        if ':' in condition:
            field, value = condition.split(':')
        else:
            raise HTTPError(400, "condition's format field:value")

        # process functions
        def do_tag(tag):
            query_dict['tags'] = tag
            return db.Entry.find(query_dict)

        def do_key(data):
            rqs = [e.lower() for e in re.split('\s+', data) if e]
            regex = re.compile(r'%s' % '|'.join(rqs), re.IGNORECASE)
            query_dict['$or'] = [{'title': regex}, {'brief': regex},
                    {'desc': regex}, {'tags': {'$in': rqs}}] 
            return db.Entry.find(query_dict)

        handle_q = {
                'tag': do_tag, 
                'key': do_key,
                }

        if field in handle_q:
            q = QDict(
                    q=condition,
                    v=value,
                    start=int(self.get_argument('st')),
                    num=int(self.get_argument('qn')),
                    )
            cur_entry = handle_q[field](q.v)


            # composite the results collection
            total = cur_entry.count()
            query_dict = {
                    'q': utf8(q.q),
                    'st': q.start,
                    'qn': q.num,
                    }

            entries = cur_entry.skip(q.start).\
                                limit(q.num)
                                
            entry_collection = {
                    'entries': [self.make_rest(e, 'entries') for e in entries],
                    'total': total,
                    'link': self.full_uri(query_dict),
                    }

            if q.start + q.num < total:
                query_dict['st'] = q.start + q.num
                entry_collection['next'] = self.full_uri(query_dict)

            if q.start > 0:
                query_dict['st'] = max(q.start - q.num, 0)
                entry_collection['prev'] = self.full_uri(query_dict)

            gmt_now = datetime.datetime.utcnow()
            self.set_header('Last-Modified', pretty_time_str(gmt_now))

            # make etag prepare
            self.cur_entries = entry_collection['entries']

        else:
            raise HTTPError(400, "Bad Request, search condtion is not allowed.")

        self.render_json(entry_collection)
コード例 #18
0
ファイル: base.py プロジェクト: jango2015/fruit-1
 def wrapper(self, *args, **kwargs):
     if not self.current_user_id:
         raise HTTPError(401)
     return method(self, *args, **kwargs)
コード例 #19
0
 def no_such_checkpoint(self, path, checkpoint_id):
     raise HTTPError(404, u"Checkpoint does not exist: %s@%s" % (path, checkpoint_id))
コード例 #20
0
ファイル: basic.py プロジェクト: franklyx/IOT_acquisition
 def wrapper(self, *args, **kwargs):
     role = kwargs['c_role']
     if int(role) == 2:
         return method(self, *args, **kwargs)
     else:
         raise HTTPError(**errors.status_29)
コード例 #21
0
ファイル: vamps_handlers.py プロジェクト: mivamo1214/qiita
class VAMPSHandler(BaseHandler):
    @execute_as_transaction
    def display_template(self, preprocessed_data_id, msg, msg_level):
        """Simple function to avoid duplication of code"""
        preprocessed_data_id = int(preprocessed_data_id)
        try:
            preprocessed_data = Artifact(preprocessed_data_id)
        except QiitaDBUnknownIDError:
            raise HTTPError(404, reason="Artifact %d does not exist!" %
                            preprocessed_data_id)
        else:
            user = self.current_user
            if user.level != 'admin':
<<<<<<< HEAD
                raise HTTPError(403, "No permissions of admin, "
                                     "get/VAMPSSubmitHandler: %s!" % user.id)

        prep_template = PrepTemplate(preprocessed_data.prep_template)
        sample_template = SampleTemplate(preprocessed_data.study)
        study = Study(preprocessed_data.study)
=======
                raise HTTPError(403, reason="No permissions of admin, "
                                "get/VAMPSSubmitHandler: %s!" % user.id)
        prep_templates = preprocessed_data.prep_templates
        allow_submission = len(prep_templates) == 1
        msg_list = ["Submission to EBI disabled:"]
        if not allow_submission:
            msg_list.append(
                "Only artifacts with a single prep template can be submitted")
        # If allow_submission is already false, we technically don't need to
        # do the following work. However, there is no clean way to fix this
コード例 #22
0
ファイル: basic.py プロジェクト: franklyx/IOT_acquisition
 def put(self, *args, **kwargs):
     raise HTTPError(**errors.status_0)
コード例 #23
0
ファイル: handlers.py プロジェクト: daemondev/kubecicd
 def prepare(self):
     raise HTTPError(
         status_code=404,
         reason=
         "Invalid resource path.<br/>Please use this endpoints:<br/><b><br/>/greetings<br/>/square/[0-9]+</b>"
     )
コード例 #24
0
ファイル: basic.py プロジェクト: franklyx/IOT_acquisition
 def delete(self, *args, **kwargs):
     raise HTTPError(**errors.status_0)
コード例 #25
0
 async def get(self):
     """
     Overrides the upstream get handler which is not allowed in LTI 1.3
     """
     raise HTTPError(400, "OAuth callback request not allowed")
コード例 #26
0
    def post(self):
        post_data = djangolike_request_dict(self.request.arguments)
        if self.request.files:
            post_data.update(djangolike_request_dict(self.request.files))
        if 'alternatives' in post_data:
            post_data['alternatives'] = ['\n'.join(post_data['alternatives'])]
        form = QuestionForm(post_data,
                            categories=self.categories,
                            locations=self.locations)
        if form.validate():
            question = self.db.Question()
            question['author'] = self.get_current_user()['_id']
            question['text'] = form.text.data
            question['correct'] = form.correct.data
            question['alternatives'] = [
                x.strip() for x in form.alternatives.data.splitlines()
                if x.strip()
            ]
            question['alternatives_sorted'] = form.alternatives_sorted.data
            category = (self.db.Category.find_one(
                {'_id': ObjectId(form.category.data)}))
            assert category
            question['category'] = category['_id']
            question['points_value'] = int(form.points_value.data)
            question['published'] = form.published.data
            question['notes'] = form.notes.data.strip()
            question['seconds'] = int(form.seconds.data)
            question['didyouknow'] = form.didyouknow.data.strip()
            location = (self.db.Location.find_one(
                {'_id': ObjectId(form.location.data)}))
            assert location
            question['location'] = location['_id']
            question.save()
            if form.picture.data:
                picture = self.db.QuestionPicture()
                picture['question'] = question['_id']
                picture.save()

                try:
                    ok = False
                    image = form.picture.data
                    if not any([
                            image['filename'].lower().endswith(x)
                            for x in ('.png', '.jpg', '.jpeg')
                    ]):
                        raise HTTPError(400)
                    assert isinstance(image['body'], str), type(image['body'])
                    type_, __ = mimetypes.guess_type(image['filename'])
                    with picture.fs.new_file('original') as f:
                        f.content_type = type_
                        f.write(image['body'])
                    ok = True
                finally:
                    if not ok:
                        picture.delete()
                        self.push_flash_message(
                            'Picture upload failed',
                            text='Check that the requirements for the picture '
                            'is correct',
                            type_='error')

            count = (self.db.Question.find({
                'category': category['_id'],
                'location': location['_id']
            }).count())
            if count < 10:
                msg = ("%s more questions and people will be able to work as "
                       "a %s in %s" % (11 - count, category['name'], location))
            else:
                msg = ("There are now %s questions of this category in %s" %
                       (count, location))
            self.push_flash_message("Question added!", msg, type_='success')

            if self.get_argument('addanother', False):
                url = self.reverse_url('admin_add_question')
            else:
                url = self.reverse_url('admin_questions')
            self.redirect(url)
        else:
            self.get(form=form)
コード例 #27
0
ファイル: tasks.py プロジェクト: drewsherlock/flower
    def get(self, taskid):
        """
Get a task info

**Example request**:

.. sourcecode:: http

  GET /api/task/info/91396550-c228-4111-9da4-9d88cfd5ddc6 HTTP/1.1
  Accept: */*
  Accept-Encoding: gzip, deflate, compress
  Host: localhost:5555


**Example response**:

.. sourcecode:: http

  HTTP/1.1 200 OK
  Content-Length: 575
  Content-Type: application/json; charset=UTF-8

  {
      "args": "[2, 2]",
      "client": null,
      "clock": 25,
      "eta": null,
      "exception": null,
      "exchange": null,
      "expires": null,
      "failed": null,
      "kwargs": "{}",
      "name": "tasks.add",
      "received": 1400806241.970742,
      "result": "'4'",
      "retried": null,
      "retries": null,
      "revoked": null,
      "routing_key": null,
      "runtime": 2.0037889280356467,
      "sent": null,
      "started": 1400806241.972624,
      "state": "SUCCESS",
      "succeeded": 1400806243.975336,
      "task-id": "91396550-c228-4111-9da4-9d88cfd5ddc6",
      "timestamp": 1400806243.975336,
      "traceback": null,
      "worker": "celery@worker1"
  }

:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
:statuscode 404: unknown task
        """

        task = tasks.get_task_by_id(self.application.events, taskid)
        if not task:
            raise HTTPError(404, "Unknown task '%s'" % taskid)

        response = task.as_dict()
        if task.worker is not None:
            response['worker'] = task.worker.hostname

        self.write(response)
コード例 #28
0
    def post(self, _id):
        data = {}
        question = self.db.Question.find_one({'_id': ObjectId(_id)})
        data['question'] = question
        post_data = djangolike_request_dict(self.request.arguments)
        if 'alternatives' in post_data:
            post_data['alternatives'] = ['\n'.join(post_data['alternatives'])]
        if self.request.files:
            post_data.update(djangolike_request_dict(self.request.files))
        form = QuestionForm(post_data,
                            categories=self.categories,
                            locations=self.locations)
        if form.validate():
            question['text'] = form.text.data
            question['correct'] = form.correct.data
            question['alternatives'] = [
                x.strip() for x in form.alternatives.data.splitlines()
                if x.strip()
            ]
            question['alternatives_sorted'] = form.alternatives_sorted.data
            category = (self.db.Category.find_one(
                {'_id': ObjectId(form.category.data)}))
            assert category
            question['category'] = category['_id']
            question['points_value'] = int(form.points_value.data)
            question['published'] = form.published.data
            question['notes'] = form.notes.data.strip()
            question['seconds'] = int(form.seconds.data)
            question['didyouknow'] = form.didyouknow.data.strip()
            location = (self.db.Location.find_one(
                {'_id': ObjectId(form.location.data)}))
            assert location
            question['location'] = location['_id']
            question.save()

            if form.picture.data:
                if question.has_picture():
                    picture = question.get_picture()
                    picture.delete()
                picture = self.db.QuestionPicture()
                picture['question'] = question['_id']
                picture.save()

                try:
                    ok = False
                    image = form.picture.data
                    if not any([
                            image['filename'].lower().endswith(x)
                            for x in ('.png', '.jpg', '.gif', '.jpeg')
                    ]):
                        raise HTTPError(400)
                    assert isinstance(image['body'], str), type(image['body'])
                    type_, __ = mimetypes.guess_type(image['filename'])
                    with picture.fs.new_file('original') as f:
                        f.content_type = type_
                        f.write(image['body'])
                    ok = True
                finally:
                    if not ok:
                        picture.delete()
                        self.push_flash_message(
                            'Picture upload failed',
                            text=(
                                'Check that the requirements for the picture '
                                'is correct'),
                            type_='error')

            self.redirect(self.reverse_url('admin_questions'))
        else:
            self.get(_id, form=form)
コード例 #29
0
ファイル: schema.py プロジェクト: biothings/discovery-app
    async def get(self):
        """
        {
            "total": 6,
            "context": {
                "schema": "http://schema.org/",
                "bts": "http://discovery.biothings.io/bts/",
                ...
            },
            "hits": [
                {
                    "prefix": "bts",
                    "label": "DataSamples",
                    ...
                },
                ...
            ]
        }
        """
        try:
            doc = None
            if self.args.url:
                # load doc from url
                response = await AsyncHTTPClient().fetch(
                    self.args.url, ca_certs=certifi.where())
                doc = response.body
            elif self.request.body:
                # load doc from request body
                doc = self.request.body
            if doc:
                doc = json.loads(doc)
                validator_options = {
                    "validation_merge": False,
                    "raise_on_validation_error": False
                }
                if self.args.ns:
                    if self.args.ns == 'schema.org':
                        # do no load any base schemas
                        schema = SchemaAdapter(
                            doc,
                            base_schema=[],
                            validator_options=validator_options)
                    elif self.args.ns == 'bioschemas':
                        # do not load bioschemas, only schema.org
                        schema = SchemaAdapter(
                            doc,
                            base_schema=['schema.org'],
                            validator_options=validator_options)
                    else:
                        schema = SchemaAdapter(
                            doc, validator_options=validator_options)
                else:
                    schema = SchemaAdapter(doc,
                                           validator_options=validator_options)
            else:
                self.finish({})
                return
        except Exception as exc:  # TODO
            raise HTTPError(400, reason=str(exc))

        if schema.has_validation_error():
            classes = []
            validation = {
                "valid": False,
                "errors": schema.get_validation_errors()
            }
        else:
            classes = schema.get_classes()
            validation = {
                "valid": True,
                "errors": schema.get_validation_errors(
                )  # could still be some warnings even "valid" is True
            }

        response = {
            "total": len(classes),
            "context": schema.context,
            "hits": classes,
            "validation": validation
        }

        self.finish(response)
コード例 #30
0
ファイル: base.py プロジェクト: jmichault/motioneye.eo
 def post(self, *args, **kwargs):
     raise HTTPError(400, 'method not allowed')