def __init__(self, req, *args, **kwargs): self.request = req post_vars = variable_decode(self.request.params) url_vars = variable_decode(self.request.urlvars) self.kwargs = kwargs.copy() self.kwargs.update(url_vars) self.kwargs.update(post_vars)
def get_params(params=None): """ Returns the request's form parameters. If params is not ``None``, it is decoded and returned. Else if ``request.json`` is available, that is returned unmodified. Otherwise, if ``request.method`` is POST or PUT, ``request.form`` and ``request.files`` are decoded, combined and returned. If ``request.method`` is neither POST nor PUT, ``request.args`` is decoded and returned. Note that this will hide potential multivalued keys in :class:`werkzeug.datastructures.MultiDict`, used for ``request.args``, ``request.form`` and ``request.files``. That is, if the client sends two values for the key 'name', only the first will be used. Additionally, if there is a key present in both ``request.form`` and ``request.files``, the key from ``request.files`` will be used. For the way to submit multivalued fields to formencode, see: http://www.formencode.org/en/latest/Validator.html#http-html-form-input :param params: If provided, this object will be passed through :func:`variable_decode` and returned. Defaults to ``None``. """ if params is not None: return variable_decode(params) if request.json is not None: return request.json if request.method in ('POST', 'PUT'): params = variable_decode(request.form) files = variable_decode(request.files) params.update(files) return params else: return variable_decode(request.args)
def wrapped(*args, **kwargs): if request.json is not None: g.api_data = request.json else: try: g.api_data = variabledecode.variable_decode(request.form) except ValueError: abort(400) file_data = {} for name, upload in request.files.items(): file_data[name] = upload file_data = variabledecode.variable_decode(file_data) g.api_data.update(file_data) try: resp = f(*args, **kwargs) except InvalidSchema as e: resp = api_error(e.unpack_errors()) code = 200 if isinstance(resp, tuple): resp, code = resp if hasattr(resp, 'api_view'): resp = resp.api_view() try: return jsonify(**resp), code # dict response except TypeError: try: return jsonify(resp), code # else string, list except Exception: msg = 'API view response value failed to serialize. Value: {0}' current_app.logger.error(msg.format(resp)) abort(500)
def index(context, request): queryMap = variable_decode(request.params) query = context.config.convertToQuery(queryMap) return { "query": simplejson.dumps(query), 'filters': simplejson.dumps(context.config.getFilterSelection()) }
def wrapper(func, self, *args, **kwargs): """Decorator Wrapper function""" request = pylons.request._current_obj() errors = {} # Skip the validation if on_get is False and its a GET if not on_get and request.environ['REQUEST_METHOD'] == 'GET': return func(self, *args, **kwargs) # If they want post args only, use just the post args if post_only: params = request.POST else: params = request.params is_unicode_params = isinstance(params, UnicodeMultiDict) params = params.mixed() if variable_decode: log.debug("Running variable_decode on params") decoded = variabledecode.variable_decode(params, dict_char, list_char) else: decoded = params if schema: log.debug("Validating against a schema") try: self.form_result = schema.to_python(decoded, state) except formencode.Invalid, e: errors = e.unpack_errors(variable_decode, dict_char, list_char)
def validate(self): """ Runs validation and returns True/False whether form is valid. This will check if the form should be validated (i.e. the request method matches) and the schema/validators validate. Validation will only be run once; subsequent calls to validate() will have no effect, i.e. will just return the original result. The errors and data values will be updated accordingly. """ assert self.schema or self.validators if self.is_validated: return not bool(self.errors) if self.method and self.method != self.request.method: return False if self.method == 'POST': params = self.request.POST.mixed() else: params = self.request.params.mixed() if self.variable_decode: self.data_raw = variabledecode.variable_decode( params, self.dict_char, self.list_char) else: self.data_raw = params self.data.update(params) if self.schema: try: self.data = self.schema.to_python( self.data_raw, self.state) except Invalid, e: self.errors = e.unpack_errors( self.variable_decode, self.dict_char, self.list_char)
def configure(**kwargs): """ Given a dictionary of config values, creates DataStores and saves them by name """ from .datastore import DataStore from formencode.variabledecode import variable_decode from formencode import schema, validators class AuthenticateSchema(schema.Schema): name = validators.UnicodeString(not_empty=True) password = validators.UnicodeString(not_empty=True) class DatastoreSchema(schema.Schema): master = validators.UnicodeString(if_missing=None, if_empty=None) slave = validators.UnicodeString(if_missing=None, if_empty=None) database = validators.UnicodeString(not_empty=True) authenticate = AuthenticateSchema(if_missing=None) connect_retry = validators.Number(if_missing=3, if_empty=0) use_gevent = validators.Bool(if_missing=False) # pymongo network_timeout = validators.Number(if_missing=None, if_empty=None) tz_aware = validators.Bool(if_missing=False) slave_okay = validators.Bool(if_missing=False) max_pool_size = validators.Int(if_missing=10) config = variable_decode(kwargs) datastores = {} for name, datastore in config["ming"].items(): args = DatastoreSchema.to_python(datastore) datastores[name] = DataStore(**args) Session._datastores = datastores # bind any existing sessions for name, session in Session._registry.items(): session.bind = datastores.get(name, None)
def configure(**kwargs): """ Given a (flat) dictionary of config values, creates DataStores and saves them by name """ config = variable_decode(kwargs) configure_from_nested_dict(config['ming'])
def wrapper(func, self, *args, **kwargs): """Decorator Wrapper function""" request = pylons.request._current_obj() errors = {} # Skip the validation if on_get is False and its a GET if not on_get and request.environ["REQUEST_METHOD"] == "GET": return func(self, *args, **kwargs) # If they want post args only, use just the post args if post_only: params = request.POST else: params = request.params is_unicode_params = isinstance(params, UnicodeMultiDict) params = params.mixed() if variable_decode: log.debug("Running variable_decode on params") decoded = variabledecode.variable_decode(params, dict_char, list_char) else: decoded = params if schema: log.debug("Validating against a schema") try: self.form_result = schema.to_python(decoded, state) except formencode.Invalid, e: errors = e.unpack_errors(variable_decode, dict_char, list_char)
def crawler_update(): if request.method == 'POST': spiderbotids = [] postvars = variabledecode.variable_decode(request.form, dict_char='_') for ids, values in postvars.items(): selected = True if "selected" in values and values["selected"] == "on" else False id = int(ids) if ids.isnumeric() else None if id: if selected: if 'delete_button' in request.form: flash('Delete crawler...') sclogic.crawlerDelete(id) flash('Delete crawler finished!') elif 'filter_button' in request.form: for spiderbot in models.Spiderbot.query.filter_by(crawlerid=id).all(): spiderbotids.append(spiderbot.spiderbotid) elif 'save_button' in request.form: print("itt: "+values["maxprice"]) crawler = models.Crawler.query.filter_by(crawlerid=id).first() if crawler: crawler.active = True if ("active" in values and values["active"] == "on") else False crawler.name = values["name"] crawler.notes = values["notes"] crawler.runcadence = float(values["runcadence"]) crawler.maxprice = float(values["maxprice"]) if values["maxprice"] and values["maxprice"] != '' and float(values["maxprice"]) !=0 else None crawler.minprice = float(values["minprice"]) if values["minprice"] and values["minprice"] != '' and float(values["minprice"]) !=0 else None db.session.commit() if len(spiderbotids)>0: showhidden = True if 'showhidden' in request.args else False onlysaved = True if 'onlysaved' in request.args else False return index_engine(spiderbotids=spiderbotids, showhidden=showhidden, onlysaved=onlysaved) return redirect('/')
def validate_form(self, schema): """ Validates a form post against schema in :doc:`forms`. If no form was posted, returns `False`. If the form was posted and it is invalid, returns `False` and sets `self.form_error`, a dictionary with input names and their corresponding error messages. If the form validated correctly, returns `True` and sets `self.form_result` to the validated results. """ if self.request.method != 'POST': return False try: # Convert fields with more than one value into lists form_vars = multidict_to_dict(self.request.form) form_vars.update(multidict_to_dict(self.request.files)) state = FormState(self.currentObj, self.case, self.urls) self.form_result = schema.to_python(variable_decode(form_vars), state) return True except Invalid, e: self.form_error = e.unpack_errors(encode_variables=True) return False
def decode_request_data(request): if request.method == "POST": params = request.POST else: params = request.params return variabledecode.variable_decode(params)
def wrapper(func, self, *args, **kwargs): """Decorator Wrapper function""" request = self._py_object.request errors = {} # Skip the validation if on_get is False and its a GET if not on_get and request.environ['REQUEST_METHOD'] == 'GET': return func(self, *args, **kwargs) # If they want post args only, use just the post args if post_only: params = request.POST else: params = request.params params = params.mixed() if variable_decode: decoded = variabledecode.variable_decode(params, dict_char, list_char) else: decoded = params if schema: try: self.form_result = schema.to_python(decoded, state) except formencode.Invalid, e: errors = e.unpack_errors(variable_decode, dict_char, list_char)
def show_logs(): logs_date_from = get_logs_date_from() logs_date_to = get_logs_date_to() developer_mode = get_developer_mode() if request.method == 'POST': postvars = variabledecode.variable_decode(request.form, dict_char='_') logs_date_from = postvars['dateFrom'] logs_date_to = postvars['dateTo'] if (('developerMode' in postvars) and postvars['developerMode'] == 'on'): developer_mode = True else: developer_mode = False set_logs_date_from(logs_date_from) set_logs_date_to(logs_date_to) set_developer_mode(developer_mode) return render_template('logs.html', title='Логи', dateFrom=logs_date_from, dateTo=logs_date_to, devMode=developer_mode) else: logs_date_from = datetime.datetime.now().strftime('%Y-%m-%d') logs_date_to = datetime.datetime.now().strftime('%Y-%m-%d') developer_mode = False set_logs_date_from(logs_date_from) set_logs_date_to(logs_date_to) set_developer_mode(developer_mode) return render_template('logs.html', title='Логи', dateFrom=logs_date_from, dateTo=logs_date_to, devMode=developer_mode)
def validate(self, schema, params, json=False, error_handler=None, htmlfill=None, variable_decode=None): ''' Performs validation against a schema for any passed params, including support for ``JSON``. :param schema: A ``formencode`` ``Schema`` object to validate against. :param params: The dictionary of parameters to validate. :param json: A boolean, indicating whether or not the validation should validate against JSON content. :param error_handler: The path to a controller which will handle errors. If not specified, validation errors will raise a ``ValidationException``. :param htmlfill: Specifies whether or not to use htmlfill. :param variable_decode: Indicates whether or not to decode variables when using htmlfill. ''' try: to_validate = params if json: to_validate = loads(request.body) if variable_decode is not None: to_validate = variabledecode.variable_decode(to_validate, **variable_decode) params = schema.to_python(to_validate) except Invalid, e: kwargs = {} if variable_decode is not None: kwargs['encode_variables'] = True kwargs.update(variable_decode) request.pecan['validation_errors'] = e.unpack_errors(**kwargs) if error_handler is not None: raise ValidationException()
def validate(self): """ Runs validation and returns True/False whether form is valid. This will check if the form should be validated (i.e. the request method matches) and the schema/validators validate. Validation will only be run once; subsequent calls to validate() will have no effect, i.e. will just return the original result. The errors and data values will be updated accordingly. """ assert self.schema or self.validators if self.is_validated: return not bool(self.errors) if self.method and self.method != self.request.method: return False if self.method == 'POST': params = self.request.POST.mixed() else: params = self.request.params.mixed() if self.variable_decode: self.data_raw = variabledecode.variable_decode( params, self.dict_char, self.list_char) else: self.data_raw = params self.data.update(params) if self.schema: try: self.data = self.schema.to_python(self.data_raw, self.state) except Invalid, e: self.errors = e.unpack_errors(self.variable_decode, self.dict_char, self.list_char)
def register(): form = variable_decode(request.form) s = get_session() try: user_id = form.pop('user_id') except KeyError: abort(400) user = s.query(User).get(user_id) next_url = form.pop('next', None) if user is None or user.status != UserStatus.unregistered: abort(400) try: data = UserRegistrationSchema().to_python(form) except formencode.Invalid as e: return render_template('user/register.html', formdata=request.form, formerror=e.error_dict) user.status = UserStatus.active user.login = data['login'] user.name = data['name'] user.created_at = datetime.datetime.now(tzutc()) s.commit() flask.ext.login.login_user(user) if not next_url: next_url = resource_url(user) return redirect(next_url)
def handle_validation(view_func, success_func, schema = None, validators = None, variable_decode=False, dict_char='.', list_char='-', post_only=True, use_htmlfill = True, state = None, on_get=False): """ A more flexible, DRY way of form handling than using @validate. Using a Formencode schema or validator dict, processes request parameters. Returns view_func if form not posted or contains errors, and success_func if form is valid. view_func takes one parameter, errors (may be a dict, or None); success_func takes one parameter, result (a dict). Code canabalized from pylons @validate decorator. Example usage: class NewsController(BaseController): @authorize(RemoteUser()) def edit(self, id): newsitem = model.NewsItem.get(id) def view(errors): c.errors = errors c.newsitem = newsitem return render_response('news/edit.html') def update(result): newsitem.title = result['title'] newsitem.content = result['content'] newsitem.flush() redirect_to(action = 'view', id = id) return handle(view, update, forms.NewsItemSchema()) Taken from http://pylonshq.com/pasties/325 """ if not on_get and not request.method == 'POST': return view_func(None) if post_only: params = request.POST.copy() else: params = request.params.copy() if variable_decode: decoded = variabledecode.variable_decode(params, dict_char, list_char) else: decoded = params errors = {} result = {} state = None if callable(state): state = state() if schema: try: result = schema.to_python(decoded, state) except api.Invalid, e: errors = e.unpack_errors(variable_decode, dict_char, list_char)
def getCharacters(self): schema = GetCharactersForm() values = variabledecode.variable_decode(request.params) try: result = schema.to_python(values) except Invalid, e: result = {'valid': False, 'errors': e.unpack_errors()}
def validate_python(self, value, state): post_vars = variable_decode(request.POST) if 'mailForwardingAddress' not in post_vars and h.asbool(post_vars['ForwardingOnly']): raise Invalid( self.message('not_enough_forwards', state), value, state )
def post(self): request = self.request user = request.user if not user.SuperUser: self._security_failure() model_state = request.model_state model_state.schema = DomainMapSchema() model_state.form.variable_decode = True if model_state.validate(): domains = ET.Element('Domains') for domain in model_state.value('domain') or []: if not any(domain.values()): continue el = ET.SubElement(domains, 'Domain') for key, val in domain.iteritems(): if isinstance(val, bool): ET.SubElement(el, key).text = unicode(int(val)) if val: ET.SubElement(el, key).text = unicode(val) args = [request.dboptions.MemberID, user.Mod, ET.tostring(domains)] with request.connmgr.get_connection('admin') as conn: sql = ''' DECLARE @ErrMsg as nvarchar(500), @RC as int EXECUTE @RC = dbo.sp_GBL_View_DomainMap_Analytics_u ?,?, ?, @ErrMsg=@ErrMsg OUTPUT SELECT @RC as [Return], @ErrMsg AS ErrMsg ''' cursor = conn.execute(sql, args) result = cursor.fetchone() cursor.close() if not result.Return: self.request.dboptions._invalidate() msg = _('The Google Analytics Configuration successfully updated.', request) self._go_to_route('admin_ganalytics', _query=[('InfoMsg', msg)]) else: ErrMsg = _('There were validation errors.') edit_info = self._get_edit_info()._asdict() edit_info['ErrMsg'] = ErrMsg model_state.form.data = variabledecode.variable_decode(request.POST) title = _('Manage Google Analytics Configuration', request) return self._create_response_namespace(title, title, edit_info, no_index=True)
def dictionary(self, input_keys=None, output_keys=None): """Calculate dictionary for selected keys and input text. """ schema = CorrespondForm() try: form_result = schema.to_python(variable_decode(request.params)) except formencode.Invalid, error: error_message = unicode(error) return error_message
def app_factory(global_config, **local_settings): settings = dict(global_config) settings.update(local_settings) update_settings_from_environ(settings) settings = variabledecode.variable_decode(settings) settings['patterns'] = compile_patterns(settings['patterns']) config = Configurator(settings=settings) config.add_view(root) return config.make_wsgi_app()
def validate(self, force_validate=False, params=None): """ Runs validation and returns True/False whether form is valid. This will check if the form should be validated (i.e. the request method matches) and the schema/validators validate. Validation will only be run once; subsequent calls to validate() will have no effect, i.e. will just return the original result. The errors and data values will be updated accordingly. `force_validate` : will run validation regardless of request method. `params` : dict or MultiDict of params. By default will use **request.json_body** (if JSON body), **request.POST** (if HTTP POST) or **request.params**. """ assert self.schema or self.validators, \ "validators and/or schema required" if self.is_validated: return not(self.errors) if not force_validate: if self.method and self.method != self.request.method: return False if params is None: if hasattr(self.request, 'json_body') and self.request.json_body: params = self.request.json_body elif self.method == "POST": params = self.request.POST else: params = self.request.params if self.variable_decode and not (hasattr(self.request, 'json_body') and self.request.json_body): decoded = variabledecode.variable_decode( params, self.dict_char, self.list_char) else: decoded = params if hasattr(decoded, "mixed"): decoded = decoded.mixed() self.data.update(params) if self.schema: try: self.data = self.schema.to_python(decoded, self.state) except Invalid, e: self.errors = e.unpack_errors(self.variable_decode, self.dict_char, self.list_char)
def post(self): request = self.request user = request.user if not user.cic.SuperUser or not request.dboptions.UseOfflineTools: self._security_failure() model_state = request.model_state model_state.schema = OfflineToolSchema() model_state.form.variable_decode = True if model_state.validate(): #success machines = model_state.value('machine') root = ET.Element('Data') for machine in machines: machine_id = unicode(machine['MachineID']) for sl in machine['SecurityLevels']: ET.SubElement(root, 'MachineSL', MachineID=machine_id, SL_ID=unicode(sl)) args = [user.Agency, ET.tostring(root)] with request.connmgr.get_connection('admin') as conn: sql = ''' DECLARE @ErrMsg as nvarchar(500), @RC as int EXECUTE @RC = dbo.sp_CIC_Offline_Machines_u ?, ?, @ErrMsg=@ErrMsg OUTPUT SELECT @RC as [Return], @ErrMsg AS ErrMsg ''' cursor = conn.execute(sql, *args) result = cursor.fetchone() cursor.close() if not result.Return: msg = _('The Offline Machines were successfully updated.', request) self._go_to_route('admin_offlinetools', _query=[('InfoMsg', msg)]) else: ErrMsg = _('There were validation errors.') edit_info = self._get_edit_info()._asdict() edit_info['ErrMsg'] = ErrMsg if not model_state.validate(): edit_info.data = variabledecode.variable_decode(model_state.form.data) title = _('Manage Offline Machines', request) return self._create_response_namespace(title, title, edit_info, no_index=True)
def configure(**kwargs): from datastore import DataStore config = variable_decode(kwargs) datastores = dict( (name, DataStore(**value)) for name, value in config['ming'].iteritems()) Session._datastores = datastores # bind any existing sessions for name, session in Session._registry.iteritems(): session.bind = datastores.get(name, None)
def match_update(): if request.method == 'POST': postvars = variabledecode.variable_decode(request.form, dict_char='_') for k, v in postvars.items(): match = models.Match.query.filter_by(matchid=int(k)).first() if match: match.hide = True if ("hide" in v and v["hide"] == "on") else False match.saved = True if ("saved" in v and v["saved"] == "on") else False db.session.commit() return redirect('/')
def repositories2(): local_repo.get_remotes() global ssh_path if request.method == 'POST': postvars = variabledecode.variable_decode(request.form, dict_char=',') repositories_action(postvars) # get, send, change setting repos_packed = pack_remotes(local_repo) return render_template('newrepository.html', local_repo=local_repo, repos_packed=repos_packed)
def default(self, o): if isinstance(o, decimal.Decimal): if o % 1 > 0: return float(o) else: return int(o) elif isinstance(o, MultiDict): return vd.variable_decode(o) else: return super(DataApiEncoder, self).default(o)
def parse(self, stream, media_type=None, parser_context=None): result = super().parse(stream, media_type=media_type, parser_context=parser_context) data = variable_decode(result.data) qdict = QueryDict('', mutable=True) qdict.update(data) data_and_files = parsers.DataAndFiles(qdict, result.files) return data_and_files
def validate_form(self): values = variable_decode(self.request.params) schema_id = values.get('type') if not schema_id: raise HTTPNotImplemented() try: resp = self.validate_values(values) except Invalid, error: log.error(error.error_dict) self.result['values'][schema_id] = error.value or {} self.result['errors'][schema_id] = error.error_dict or {} self.request.response.status_int = 401
def is_valid(self, params): self.set_values(params, use_ids=True) params = self.get_values() result = True try: params = variabledecode.variable_decode(params, '.', '--') form_result = self.schema.to_python(params) self.set_values(form_result) except Invalid, e: self.set_errors(e.unpack_errors()) result = False
def get_perl_cgi(params_dict): params_dict = variable_decode(params_dict) cgi = g.perl.eval('$cgi = new CGI;') cgi.charset("UTF-8") for key, val in params_dict.iteritems(): if key in updatable_attributes: if isinstance(val, list): cgi.param(key, '\n'.join(val)) else: cgi.param(key, str(val)) return cgi
def validate(self, force_validate=False, params=None): """ Runs validation and returns True/False whether form is valid. This will check if the form should be validated (i.e. the request method matches) and the schema/validators validate. Validation will only be run once; subsequent calls to validate() will have no effect, i.e. will just return the original result. The errors and data values will be updated accordingly. `force_validate` : will run validation regardless of request method. `params` : dict or MultiDict of params. By default will use **request.json_body** (if JSON body), **request.POST** (if HTTP POST) or **request.params**. """ assert self.schema or self.validators, \ "validators and/or schema required" if self.is_validated: return not (self.errors) if not force_validate: if self.method and self.method != self.request.method: return False if params is None: if hasattr(self.request, 'json_body') and self.request.json_body: params = self.request.json_body elif self.method == "POST": params = self.request.POST else: params = self.request.params if self.variable_decode and not (hasattr(self.request, 'json_body') and self.request.json_body): decoded = variabledecode.variable_decode(params, self.dict_char, self.list_char) else: decoded = params self.data.update(params) if self.schema: try: self.data = self.schema.to_python(decoded, self.state) except Invalid, e: self.errors = e.unpack_errors(self.variable_decode, self.dict_char, self.list_char)
def index_member(context, request): news = GetNewsFeedProc(request) if news: news = news.NewsFeed if news: news = news[:10] else: news = [] else: news = [] queryMap = variable_decode(request.params) query = context.config.convertToQuery(queryMap) return {"query": simplejson.dumps(query), 'filters': simplejson.dumps(context.config.getFilterSelection()), 'newsfeed': news}
def table(): skus = request.args.get('skus') wk_range = request.args.get('wk_range') cat = request.args.get('cat') wk_start = (datetime.today() - timedelta(days=28)).date() wk_end = (datetime.today() + timedelta(days=63)).date() if request.method == "POST": postvars = variabledecode.variable_decode(request.form, dict_char='_') overrides = {"skus": postvars} for sku in overrides['skus'].keys(): for date, val in overrides['skus'][sku].items(): data.loc[date][sku] = val #data.to_csv('data.csv') #data.to_csv('https://s3.amazonaws.com/flaskapi/data.csv') #push data to s3 csv_buffer = StringIO() data.to_csv(csv_buffer) s3_resource = boto3.resource('s3') s3_resource.Object(s3_bucket, s3_key).put(Body=csv_buffer.getvalue()) #return jsonify(overrides) return redirect(request.referrer) #return render_template('table4.html', result = output) if cat: skus = cats[cat] filter_df = data.loc[wk_start:wk_end][skus] wk_totals = data.loc[wk_start:wk_end][skus].sum(axis=1) filter_df = pd.concat( [filter_df, wk_totals.rename('Category')], axis=1) filter_df = filter_df.append( filter_df.sum(numeric_only=True).rename('Q1')) output = filter_df.T.to_dict(orient='split') return render_template('table4.html', result=output) if skus: skus = skus.split(',') filter_df = data.loc[wk_start:wk_end][skus] wk_totals = data.loc[wk_start:wk_end][skus].sum(axis=1) filter_df = pd.concat( [filter_df, wk_totals.rename('Category')], axis=1) filter_df = filter_df.append( filter_df.sum(numeric_only=True).rename('Q1')) output = filter_df.T.to_dict(orient='split') return render_template('table4.html', result=output)
def put(request): params = variable_decode(request.GET) metadata = V.message_schema.to_python(params, request) data = request.json after = datetime.utcnow() + timedelta(metadata['delay']) msg = M.HTTPMessage.new(data=data, tags=metadata['tags'], timeout=metadata['timeout'], after=after, q=request.matchdict['qname'], pri=metadata['priority']) request.response.status_int = 201 return msg.url(request)
def show_users(): if request.method == 'POST': try: postvars = variabledecode.variable_decode(request.form, dict_char='_') users.query.filter( users.phone_number == postvars['phoneNumber']).update( {'role': postvars['role']}) db.session.commit() except Exception: flash('Пользователь не зарегистрирован!', 'danger') return render_template('users.html', title='Пользователи')
def authenticate_ajax(self): """Checks whether username and password match any records in the User table. Returns a stringified JSON object. """ response.headers['Content-Type'] = 'application/json' schema = LoginForm() values = variabledecode.variable_decode(request.params) try: result = schema.to_python(values) except Invalid, e: result = {'valid': False, 'errors': e.unpack_errors()} return json.dumps(result)
def variable_decode(remainder, params): '''Best-effort formencode.variabledecode on the params before validation If any exceptions are raised due to invalid parameter names, they are silently ignored, hopefully to be caught by the actual validator. Note that this decorator will *add* parameters to the method, not remove. So for instnace a method will move from {'foo-1':'1', 'foo-2':'2'} to {'foo-1':'1', 'foo-2':'2', 'foo':['1', '2']}. ''' try: new_params = variabledecode.variable_decode(params) params.update(new_params) except: pass
def validate_schema(self, schema, variable_decode=True, dict_char='.', list_char='-'): if variable_decode: log.debug("Running variable_decode on params") decoded = variabledecode.variable_decode(self.request.params, dict_char, list_char) errors = {} form_result = {} log.debug("Validating against a schema") try: form_result = schema.to_python(decoded, FormEncodeState(self.request)) except formencode.Invalid, e: errors = e.unpack_errors(variable_decode, dict_char, list_char)
def salesorderUpload(): SessionID = request.cookies.get("SessionID") username = currentuser.Sessions[SessionID]['username'] if request.method == 'POST': parsed = variabledecode.variable_decode(request.form, dict_char='_') SONumber = parsed['Available'] Pallets = [ x['Pallet'] for x in [parsed[str(x)] for x in range(1, 10)] if x['Pallet'] != '' and x['Quantity'] != '' ] Quantities = [ x['Quantity'] for x in [parsed[str(x)] for x in range(1, 10)] if x['Pallet'] != '' and x['Quantity'] != '' ] Timestamp = str(datetime.now()) datalen = len(Pallets) df = pd.DataFrame({ 'SalesOrderNumber': [SONumber] * datalen, 'Pallet': Pallets, 'Quantity': Quantities, 'Timestamp': [Timestamp] * datalen, 'UploadUsername': [username] * datalen }) df.to_sql(name='shipments', con=bdp_sqlserver.alchemy_engine(), schema="data", if_exists="append", method="multi", index=False) flash('Upload successful', 'success') PalletHolders = [{ 'Pallet': str(x) + '_Pallet', 'Quantity': str(x) + '_Quantity', 'ItemNumber': str(x) + '_ItemNumber' } for x in range(1, 10)] query = "SELECT SONumber FROM dbo.a_PlasticsPotentialASNOrders ORDER BY Posted, SONumber" OrdersInGP = bhprd_sqlserver.get_rows(query) query = "SELECT DISTINCT SalesOrderNumber FROM data.shipments" Used = bdp_sqlserver.get_rows(query) Availables = [{ 'SONumber': x } for x in [x[0] for x in OrdersInGP] if x not in [x[0] for x in Used]] return (render_template('salesorder.upload.html', Availables=Availables, PalletHolders=PalletHolders, username=username))
def put(request): params = variable_decode(request.GET) metadata = V.message_schema.to_python(params, request) data = request.json after = datetime.utcnow() + timedelta(metadata['delay']) msg = M.HTTPMessage.new( data=data, tags=metadata['tags'], timeout=metadata['timeout'], after=after, q=request.matchdict['qname'], pri=metadata['priority']) request.response.status_int = 201 return msg.url(request)
async def set_prefs(request): data = await request.post() data = variabledecode.variable_decode(data) logging.warning(data) os.environ['STUN_SERVER'] = data['stun_server'] os.environ['TURN_SERVER'] = data['turn_server'] os.environ['TURN_USERNAME'] = data['turn_username'] os.environ['TURN_PASSWORD'] = data['turn_password'] username = data['server_username'] password = data['server_password'] if ((username != "" and password != "") and (username != 'Not Applicable' or username != 'Not Applicable')): os.environ['username'] = data['server_username'] os.environ['password'] = data['server_password'] return web.HTTPFound('/preferences')
def dispatch(self, path, action, parms): # print 'DISPATCH', path, action, parms if action in self._dispatch: version = parms.pop('Version') for obj in self._dispatch[action]: if version in obj.versions: break else: return self.error(404) parms = variabledecode.variable_decode(parms, dict_char='-', list_char='.') try: parms = obj.schema.to_python(parms) return obj.invoke(**parms) except Invalid, e: return self.error(400) except:
def validate_form(self, schema): """ Validates a form post against schema. If no form was posted, return False. If form was posted and it is invalid, return False and set self.form_error. If form validated correctly, return True and set self.form_result """ if self.request.method != 'POST': return False try: # Convert fields with more than one value into lists form_vars = multidict_to_dict(self.request.form) self.user_posted = form_vars form_vars.update(multidict_to_dict(self.request.files)) self.form_result = schema.to_python(variable_decode(form_vars)) return True except Invalid, e: self.form_error = e.unpack_errors(encode_variables=True) return False
def settings(): if request.method == 'POST': postvars = variabledecode.variable_decode(request.form, dict_char='_') for k, v in postvars.items(): if ('param' in k): t_id = v['id'] if (params.query.filter(params.id == t_id) != None): param = params.query.filter(params.id == t_id)[0] if param.param_value != v['paramvalue']: params.query.filter(params.id == param.id).update( {'param_value': v['paramvalue']}) db.session.commit() settings = params.query.order_by(params.id.asc()).all() return render_template('settings.html', settings=settings)
def repositories(): local_repo.get_remotes() global ssh_path if request.method == 'POST': postvars = variabledecode.variable_decode(request.form, dict_char=',') """print(request.form) print(postvars) repositories_action(postvars) # get, send, change settings""" path = request.form['path'] path = os.path.expanduser(path) name = request.form['remote_name'] if path and name: return repositories_add_remote(path, name) repos_packed = pack_remotes(local_repo) return render_template('newrepository.html', local_repo=local_repo, repos_packed=repos_packed)
def index_member(context, request): news = GetNewsFeedProc(request) if news: news = news.NewsFeed if news: news = news[:10] else: news = [] else: news = [] queryMap = variable_decode(request.params) query = context.config.convertToQuery(queryMap) return { "query": simplejson.dumps(query), 'filters': simplejson.dumps(context.config.getFilterSelection()), 'newsfeed': news }
def table(): skus = request.args.get('skus') wk_range = request.args.get('wk_range') cat = request.args.get('cat') wk_start = (datetime.today() - timedelta(days=28)).date() wk_end = (datetime.today() + timedelta(days=63)).date() if request.method == "POST": postvars = variabledecode.variable_decode(request.form, dict_char='_') overrides = {"skus": postvars} for sku in overrides['skus'].keys(): for date, val in overrides['skus'][sku].items(): data.loc[date][sku] = val data.to_csv('data.csv') #return jsonify(overrides) return redirect(request.referrer) if cat: skus = cats[cat] filter_df = data.loc[wk_start:wk_end][skus] wk_totals = data.loc[wk_start:wk_end][skus].sum(axis=1) filter_df = pd.concat( [filter_df, wk_totals.rename('Category')], axis=1) filter_df = filter_df.append( filter_df.sum(numeric_only=True).rename('Q1')) output = filter_df.T.to_dict(orient='split') return render_template('table4.html', result=output) if skus: skus = skus.split(',') filter_df = data.loc[wk_start:wk_end][skus] wk_totals = data.loc[wk_start:wk_end][skus].sum(axis=1) filter_df = pd.concat( [filter_df, wk_totals.rename('Category')], axis=1) filter_df = filter_df.append( filter_df.sum(numeric_only=True).rename('Q1')) output = filter_df.T.to_dict(orient='split') return render_template('table4.html', result=output)
def processView(self): # If we logged in then go to dashboard if self.request.method == "GET": data = {} else: safe = check_csrf_token(self.request, raises=False) if not safe: return HTTPNotFound() data = variable_decode(self.request.POST) if data["user_pass"] != "": if data["user_pass"] == data["user_pass2"]: # Load connected plugins and check if they modify the registration of an user continue_registration = True for plugin in p.PluginImplementations(p.IAuthorize): ( continue_with_registration, error_message, ) = plugin.before_register(self.request, data) if not continue_with_registration: self.errors.append(error_message) continue_registration = False if continue_registration: added, error_message = register_user( self.request, data) if not added: self.errors.append(error_message) else: # Load connected plugins so they perform actions after the login is performed for plugin in p.PluginImplementations( p.IAuthorize): plugin.after_register(self.request, data) loginData = data["user_id"] headers = remember(self.request, loginData) return HTTPFound( location=self.request.route_url("dashboard"), headers=headers, ) else: self.errors.append( self._("The password and its retype are not the same")) else: self.errors.append(self._("The password cannot be empty")) return {"next": next, "data": data}