def upload(self, package, datastream): object_path = self.get_path(package) LOG.debug('PUT package "%s"', object_path) try: self.client.put_object(self.container, object_path, datastream, headers={'user-agent': USER_AGENT}) except ClientException as e: LOG.error('Failed to store package "%s": %s', object_path, e) raise HTTPInternalServerError() object_meta_path = self.get_meta_path(package) metadata = json.dumps(package.get_metadata()) LOG.debug('PUT package metadata "%s"', object_meta_path) try: self.client.put_object(self.container, object_meta_path, metadata, headers={'user-agent': USER_AGENT}) except ClientException as e: LOG.error('Failed to store package metadata "%s": %s', object_meta_path, e) raise HTTPInternalServerError()
def deploy(self): """ Deploy a package to destination """ logger.debug("======= deploy =======") try: err, pkg_name, dest, email, passwd = \ self.__get_deploy_request_params() if (err): return HTTPBadRequest(err) result = deploy_package(pkg_name, dest, email, passwd) if result['status'] == 202: deployment_id = result['deployment_id'] status_url = 'deployment/{0}'.format(deployment_id) contents = json.dumps(result, sort_keys=True) response = HTTPAccepted() response.headers = { 'Content-Type': 'application/json; charset=UTF-8', 'Location': status_url } response.body = contents return response elif result['status'] == 404: msg = 'Cannot find package "{0}" installed.'.format(pkg_name) return HTTPNotFound(detail=msg) else: return HTTPInternalServerError(detail=result['errors']) except Exception as e: stack_info = traceback.format_exc() logger.exception(stack_info) return HTTPInternalServerError(detail=stack_info)
def post_generate_member_token(request): # get request params content = json.loads(request.body) service = content['service'] email = content['email'] # check email if not email_registered(email): raise HTTPForbidden() # check member token if token_for_email_exists(service, email): raise HTTPForbidden() # generate member token token = generate_token(service, email) if not token: raise HTTPInternalServerError() # save member token if not save_token(service, email, token): raise HTTPInternalServerError() # return member token return { 'token': token }
def gen_error_view(o_error): ''' :param o_error: string or Exception String or Exception with string representation error :return: Error view ''' # Create error page exc_view = HTTPInternalServerError() # Filling error field exc_view.explanation = o_error if isinstance(o_error, basestring) else o_error.message return exc_view
def _almost_equals(self, geom, other_geom): if geom is None and other_geom is None: return True elif geom is not None and other_geom is None: return False elif geom is None and other_geom is not None: return False g1 = None proj1 = None if isinstance(geom, geoalchemy2.WKBElement): g1 = wkb_to_shape(geom) proj1 = geom.srid else: # WKT are used in the tests. split1 = str.split(geom, ';') proj1 = int(str.split(split1[0], '=')[1]) str1 = split1[1] g1 = wkt.loads(str1) g2 = None proj2 = None if isinstance(other_geom, geoalchemy2.WKBElement): g2 = wkb_to_shape(other_geom) proj2 = other_geom.srid else: # WKT are used in the tests. split2 = str.split(other_geom, ';') proj2 = int(str.split(split2[0], '=')[1]) str2 = split2[1] g2 = wkt.loads(str2) # https://github.com/Toblerity/Shapely/blob/ # 8df2b1b718c89e7d644b246ab07ad3670d25aa6a/shapely/geometry/base.py#L673 decimals = None if proj1 != proj2: # Should never occur raise HTTPInternalServerError('Incompatible projections') elif proj1 == 3857: decimals = -0.2 # +- 0.8m = 0.5 * 10^0.2 elif proj1 == 4326: decimals = 7 # +- 1m # 5178564 740093 | gdaltransform -s_srs EPSG:3857 -t_srs EPSG:4326 # 46.5198319099112 6.63349924965325 0 # 5178565 740093 | gdaltransform -s_srs EPSG:3857 -t_srs EPSG:4326 # 46.5198408930641 6.63349924965325 0 # 46.5198408930641 - 46.5198319099112 = 0.0000089 -> 7 digits else: raise HTTPInternalServerError('Bad projection') return g1.almost_equals(g2, decimals)
def home_view(request, input_filepath, output_basename_generator): settings = request.registry.settings converted_path = settings['convertit.converted_path'] input_mimetype = get_input_mimetype(request, input_filepath) output_mimetype = request.GET.get('to', 'application/pdf') output_basename = output_basename_generator(request, output_mimetype) output_filepath = os.path.join(converted_path, output_basename) remove_old_files(request) convert = get_converter(request, input_mimetype, output_mimetype) try: convert(input_filepath, output_filepath) except Exception as e: message = "Sorry, there was an error converting the document. Reason: %s" log.error(message % str(e)) return HTTPInternalServerError(body=message % str(e), content_type='text/plain') return HTTPFound(static_url(output_filepath, request), content_disposition='attachement; filename=%s' % output_basename)
def downloadkml(self): if self.request.method == 'OPTIONS': return Response(status=200) # IE is always URLEncoding the body jsonstring = urllib.unquote_plus(self.request.body) try: spec = json.loads(jsonstring, encoding=self.request.charset) except Exception: raise HTTPBadRequest() kml = spec.get('kml') filename = spec.get('filename') if kml is None or filename is None: raise HTTPBadRequest() make_sure_path_exists(self.tmpdir) delete_old_files(self.tmpdir) # create the file in the tmp dir try: with open(self.tmpdir + '/' + filename, 'w') as file_: file_.write(kml.encode('utf8')) except EnvironmentError as e: raise HTTPInternalServerError(e) return {'url': self.host + '/' + filename}
def status(request): try: request.db.execute("SELECT 1") return {"status": "okay"} except Exception: # pylint:disable=broad-except LOG.exception("Executing a simple database query failed:") raise HTTPInternalServerError("Database connection failed")
def data(self): id, lang = self._validate_id_lang() headers = None if 'Authorization' in self.request.headers: headers = { 'Authorization': self.request.headers.get('Authorization') } url = '%s/%d?l=%s' % (self._API_ROUTE, id, lang) resp, data = call_api(self.settings, url, headers) if resp.status_code != 200: raise HTTPInternalServerError( "An error occurred while loading the document") if data.get('not_authorized', False): self.template_input.update({'not_authorized': True}) else: locales = data['locales'] self.template_input.update({ 'lang': locales[0]['lang'], 'profile': data, 'locale': locales[0], 'geometry': self._get_geometry(data['geometry']['geom']) if data['geometry'] else None }) return self.template_input
def getTopics(params): # Getting all topics subreq = Request.blank('/rest/services') topicresp = params.request.invoke_subrequest(subreq) if topicresp.status_int != 200: raise HTTPInternalServerError('Topics service did not return OK status') # pragma: no cover return json.loads(topicresp.body)['topics']
def __init__(self, request): self.opentrans_api_key = get_current_registry().settings[ 'opentrans_api_key'] # Get API key from config .ini if self.opentrans_api_key == '': raise HTTPInternalServerError( 'The opentrans_api_key has no value, is registeret in .ini') self.ot_api = opentransapi.OpenTrans(self.opentrans_api_key) self.request = request if request.matched_route.name == 'stationboard': id = request.matchdict['id'] if id.isdigit() is False: raise HTTPBadRequest('The id must be an integer.') else: self.id = int(id) self.destination = request.params.get('destination', 'all') limit = request.params.get('limit') if limit: if limit.isdigit(): self.limit = min(int(limit), self.MAX_LIMT) else: raise HTTPBadRequest( 'The limit parameter must be an integer.') else: self.limit = self.DEFAULT_LIMIT
def post(self): """ Unblock the given user. Request: `POST` `/users/unblock` Request body: {'user_id': @user_id@} """ user = _get_user(self.request.validated['user_id']) user.blocked = False # Make sure the rate limiting counter is reset # (it might be the reason the user was blocked): user.ratelimit_times = 0 # unsuspend account in Discourse try: client = get_discourse_client(self.request.registry.settings) client.unsuspend(user.id) except: log.error('Unsuspending account in Discourse failed: %d', user.id, exc_info=True) raise HTTPInternalServerError( 'Unsuspending account in Discourse failed') return {}
def get_multilingual_element(value, language, not_null=True): """ Method that takes a string or dict in order to retrieve the text value of the requested language. Args: values (str or dict): The multilingual values encoded as JSON. language (str): The language in which the multilingual element is to be returned. Returns: String: The equivalent string in the requested language. Raises: HTTPInternalServerError: If default language of requested value is not available. """ multilingual_value = value.get(language, None) if multilingual_value is None and not_null: msg = 'Default language "{language}" is not available in: \n {element}'\ .format(language=language, element=value) log.error(msg) log.info("Call stack of failing get_multilingual_element:") for line in traceback.format_stack(): log.info("{}".format(line)) raise HTTPInternalServerError() return multilingual_value
def mocked_request(*args, **kwargs): # noqa: E811 tmp["retry"] -= 1 if not tmp["retry"]: return mocked_file_response(tmp["json"].name, tmp["http"]) resp = HTTPInternalServerError( ) # internal retry expect at least a 5xx code to retry return resp # will be available on next call (to test retries)
def post(self): request = self.request user = request.validated['user'] user.password = request.validated['password'] # The user was validated by the nonce so we can log in token = log_validated_user_i_know_what_i_do(user, request) if token: settings = request.registry.settings response = token_to_response(user, token, request) try: client = get_discourse_client(settings) r = client.redirect_without_nonce(user) response['redirect_internal'] = r except: # Since only the password is changed, any error with discourse # must not prevent login and validation. log.error('Error logging into discourse for %d', user.id, exc_info=True) user.clear_validation_nonce() try: DBSession.flush() except: log.warning('Error persisting user', exc_info=True) raise HTTPInternalServerError('Error persisting user') return response else: request.errors.status = 403 request.errors.add('body', 'user', 'Login failed') return None
def post(self): user = schema_create_user.objectify(self.request.validated) user.password = self.request.validated['password'] user.update_validation_nonce(Purpose.registration, VALIDATION_EXPIRE_DAYS) # directly create the user profile, the document id of the profile # is the user id lang = user.lang user.profile = UserProfile( categories=['amateur'], locales=[DocumentLocale(lang=lang, title='')]) DBSession.add(user) try: DBSession.flush() except: log.warning('Error persisting user', exc_info=True) raise HTTPInternalServerError('Error persisting user') # also create a version for the profile DocumentRest.create_new_version(user.profile, user.id) # The user needs validation email_service = get_email_service(self.request) nonce = user.validation_nonce settings = self.request.registry.settings link = settings['mail.validate_register_url_template'] % nonce email_service.send_registration_confirmation(user, link) return to_json_dict(user, schema_user)
def _redirect_to_full_url(self): id = self._validate_int('id') is_lang_set = 'lang' in self.request.matchdict lang = self._validate_lang() if is_lang_set \ else self._get_interface_lang() url = '%s/%d/%s/info' % (self._API_ROUTE, id, lang) if log.isEnabledFor(logging.DEBUG): log.debug('API: %s %s', 'GET', url) resp, data = call_api(self.settings, url) if resp.status_code == 404: raise HTTPNotFound() elif resp.status_code == 400: raise HTTPBadRequest("Incorrect document id or lang") elif resp.status_code != 200: raise HTTPInternalServerError( "An error occurred while loading the document") if 'redirects_to' in data: if lang not in data['available_langs']: lang = self._get_best_lang(data['available_langs']) self._redirect(data['redirects_to'], lang, None, is_lang_set) else: locale = data['locales'][0] slug = get_slug(locale, is_route=self._API_ROUTE == 'routes') self._redirect(data['document_id'], locale['lang'], slug, is_lang_set)
def event_type_graph(request): record = get_record(request, _MODEL) nodes = list(record.nodes) res, by_id, parents_cache = {}, {}, {} i, loop, ref = 0, 0, 'A' while nodes: if i >= len(nodes): i = 0 if loop >= len(nodes): raise HTTPInternalServerError( json_body={ "error": "Invalid event graph", "partial_res": res, "remaining_node_ids": [str(node.id) for node in nodes] }) node = nodes[i] parents = parents_cache.get(node.id) if parents is None: parents = [p.id for p in node.parents] loop += 1 i += 1 if not all(p in by_id for p in parents): continue parent_refs = [] up_depth = 0 for parent in parents: parent_ref = by_id[parent] parent_refs.append(parent_ref) parent_depth = res[parent_ref]['depth'] if parent_depth > up_depth: up_depth = parent_depth node_dict = node.as_dict() node_dict['depth'] = up_depth + 1 node_dict['parent_refs'] = parent_refs by_id[node.id] = ref res[ref] = node_dict del nodes[i - 1] loop = 0 new_ref = "" ret = True for c in ref[::-1]: ordc = ord(c) + 1 if ret else ord(c) if ordc > ord('Z'): new_ref = 'A' + new_ref ret = True else: new_ref = chr(ordc) + new_ref ret = False ref = 'A' + new_ref if ret else new_ref list_res = [dict(ref=key, **val) for key, val in res.items()] list_res.sort(key=lambda self: self['depth']) return list_res
def post(self): """ Block the given user. Request: `POST` `/users/block` Request body: {'user_id': @user_id@} """ user = _get_user(self.request.validated['user_id']) user.blocked = True # suspend account in Discourse (suspending an account prevents a login) try: client = get_discourse_client(self.request.registry.settings) block_duration = 99999 # 99999 days = 273 years client.suspend(user.id, block_duration, 'account blocked by moderator') except: log.error('Suspending account in Discourse failed: %d', user.id, exc_info=True) raise HTTPInternalServerError( 'Suspending account in Discourse failed') return {}
def getTimestampsForId(request): """ Returns a json document which contains a a list of messtischblatt tuples which have the same blattnumber like the given object. """ try: log.info('Receive request for timestamps for a given object number.') dbsession = request.db object_id = request.GET.get('id') metadataObj = Metadata.by_id(object_id, dbsession) spatial_relation_objects = VBlattschnittMtb.allForBlattnr( metadataObj.blattnr, dbsession) #create response response = [] for rel_object in spatial_relation_objects: if rel_object.isttransformiert: response.append({ 'id': rel_object.mapid, 'time': rel_object.time, 'extent': Map.getExtent(rel_object.map, dbsession, DATABASE_SRID) }) return {'maps': response} except DBAPIError as e: log.error(e) log.error(traceback.format_exc()) raise HTTPInternalServerError(GENERAL_ERROR_MESSAGE)
def _compute_points(self): """Compute the alt=fct(dist) array""" collection = geojson.loads(self.request.params['feature']) geom = shape(collection['features'][0]['geometry']) continous_functions = { 'mean': mean, 'min': min, 'max': max, 'median': median, 'std': std, 'sum': nsum } if 'layers' in self.request.params: rasters = {} layers = geojson.loads(self.request.params['layers']) for layer in layers: if layer in self.rasters: rasters[layer] = self.rasters[layer] else: raise HTTPNotFound("Layer %s not found" % layer) else: rasters = self.rasters classifications = {} for ref in rasters.keys(): results = [] layer_parameters = self.request.registry.settings[ 'raster_statistics'][ref] coords, exception = self._create_points( geom, layer_parameters['resolution']) if exception is not None: raise HTTPInternalServerError(exception) for coord in coords: value = self._get_raster_value(self.rasters[ref], ref, coord[0], coord[1]) if value: results.append(float(value)) classification = {'results': {}} values = layer_parameters['values'] # Create statistics for param in values: classification['results'][param] = self._round( continous_functions[param](results), layer_parameters['round']) classification['unit'] = layer_parameters['unit'] classification['order'] = layer_parameters['order'] classifications[ref] = classification return rasters.keys(), classifications
def georeferenceGetProcess(request): log.info('Receive request GetGeoreferenceProcess') try: request_data = None if request.method == 'POST': request_data = request.json_body objectid = None if 'objectid' in request_data: validateId(request_data['objectid']) objectid = int(request_data['objectid']) log.debug('Objectid is valide: %s' % request_data) georeferenceid = None if 'georeferenceid' in request_data: georeferenceid = int(request_data['georeferenceid']) if georeferenceid: response = createResponseForSpecificGeoreferenceProcess( objectid, georeferenceid, request) else: response = createGeneralResponse(objectid, request) return json.dumps(response, ensure_ascii=False, encoding='utf-8') except GeoreferenceParameterError as e: message = 'Wrong or missing service parameter - %s' % e.value log.error(message) return HTTPBadRequest(message) except Exception as e: message = 'Problems while creating response for getprocess request - %s' % e log.error(message) return HTTPInternalServerError(message)
def sync(self, h_groups, group_info_params): """ Sync standard data to h for an LTI launch with the provided groups. This will upsert the provided list of groups, the current user and make that user a member of each group. :param h_groups: the list of models.HGroup objects to upsert :param group_info_params: the params to record for these groups in models.GroupInfo :raise HTTPInternalServerError: if we can't sync to h for any reason """ if not self._ai_getter.provisioning_enabled(): return try: self._h_api.execute_bulk(commands=self._yield_commands(h_groups)) except HAPIError as err: raise HTTPInternalServerError(explanation=err.explanation) from err # Keep a note of the groups locally for reporting purposes. for h_group in h_groups: self._group_info_service.upsert( h_group=h_group, consumer_key=self._lti_user.oauth_consumer_key, params=group_info_params, )
def status(request): try: request.db.execute("SELECT 1") except Exception as exc: log.exception(exc) raise HTTPInternalServerError("Database connection failed") return {"status": "okay"}
def error_view(exc, request): # from traceback import format_exc from datetime import datetime capture_exception(getattr(request, "exc_info", None)) return HTTPInternalServerError( explanation="Sorry, Assembl had an internal issue and you have to reload. Please send this to a discussion administrator.", detail=datetime.utcnow().isoformat()+"\n"+repr(request.exception))
def packages_view(req, compression=None): channel = req.matchdict.get('channel', None) index = req.matchdict.get('index', None) arch = req.matchdict.get('arch', None) if not channel or not index or not arch: return HTTPNotFound() arch = arch.lower().strip() indexobj = fetch_index_from_names(channel, index) if not indexobj: return HTTPNotFound() pkgdata = generate_debian_package_index( channel, index, arch, compression=compression) if not pkgdata: return HTTPInternalServerError() contenttype = 'text/plain' if compression == 'gz': contenttype = 'application/gzip' elif compression == 'bz2': contenttype = 'application/x-bzip2' return Response(pkgdata[0], content_type=contenttype, status_code=200)
def signup(context, request): """ Create new user. :param request: Pyramid request object """ domain = 'encode.auth0.com' access_token = request.json.get('accessToken') if not access_token: raise HTTPBadRequest(explanation='Access token required') url = 'https://{domain}/userinfo?access_token={access_token}'.format(domain=domain, access_token=access_token) user_data_request = requests.get(url) if user_data_request.status_code != 200: raise HTTPBadRequest(explanation='Could not get user data') user_data = user_data_request.json() if user_data['email_verified'] is not True: raise HTTPBadRequest(explanation='Unverified email') user_info = _get_user_info(user_data) validate_request(context.type_info.schema, request, user_info) if request.errors: raise ValidationError(', '.join(request.errors)) result = collection_add(context, request, user_info) if not result or result['status'] != 'success': raise HTTPInternalServerError(explanation='attempt to create account was not successful') return HTTPCreated(explanation='User created')
def __call__(self): context = self.context request = self.request root = request.root login = root['login'] logout = root['logout'] if context == logout: authentication_ticket = None else: authentication_ticket = request.authenticated_userid if context != login and not authentication_ticket: request.session['next'] = request.path return HTTPFound(request.resource_url(login)) method = request.method.lower() if method not in self.methods: return HTTPMethodNotAllowed() result = getattr(self, method)(request) if isinstance(result, Response): return result elif isinstance(result, dict): data = dict(title=context.title, username=getattr(authentication_ticket, 'username', None)) data.update(result) return data elif result: return Response(result) else: return HTTPInternalServerError()
def post(self): form_data = self.request.POST.mixed() try: result = AccountSchema().load(form_data) except ValidationError as error: return {'form': self.form(form_data, error.messages)} if self.context.find_login(result['login']): errors = {'login': '******'} elif not recaptcha.verify(self.request, result['captcha_token']): errors = {'captcha': 'Captcha validation failed'} else: errors = None if errors: return {'form': self.form(form_data, errors)} new_account = self.context.register(result) if not new_account: raise HTTPInternalServerError() self.request.override_renderer = 'amnesia:templates/account/register_ok.pt' return {'new_account': new_account}
def perform_action(self, action, request): _ = self.get_ugettext(request) if not request.POST.get('accept', ''): msg = _(u'You must accept the new terms of use to continue logging in') raise self.ActionError(msg) userid = action.user_id version = action.params['version'] user = request.tou_db.get_user_by_id(userid, raise_on_missing=False) logger.debug('Loaded ToUUser {!s} from db'.format(user)) if not user: user = ToUUser(userid=userid, tou=[]) user.tou.add(ToUEvent( version = version, application = 'eduid_tou_plugin', created_ts = datetime.utcnow(), event_id = ObjectId() )) request.tou_db.save(user) logger.debug("Asking for sync of {!s} by Attribute Manager".format(user)) rtask = update_attributes_keep_result.delay('tou', str(user.user_id)) try: result = rtask.get(timeout=10) logger.debug("Attribute Manager sync result: {!r}".format(result)) except Exception, e: logger.exception("Failed Attribute Manager sync request: " + str(e)) message = _('There were problems with your submission. ' 'You may want to try again later, ' 'or contact the site administrators.') request.session.flash(message) raise HTTPInternalServerError()
def make_internal_server_error(message): error = HTTPInternalServerError() error.content_type = "text/plain" error.text = message return error