def _validate(arg_name, validator, required_args): if isinstance(validator, ArgumentValidator): validator.application = current_app try: if not len(request.form) and not len( request.args) and request.data: from werkzeug.urls import url_decode args = url_decode(request.data) if arg_name not in args: raise KeyError(arg_name) value = args[arg_name] else: value = request.form[ arg_name] if arg_name in request.form else request.args[ arg_name] if not validator.validate(value): raise errors.InvalidParameterValue(arg_name) except KeyError: if validator.required: raise errors.MissingRequiredParameter(arg_name) value = validator.default_value required_args[arg_name] = validator.get_value( value) if value is not None else None else: raise ValueError( "Invalid parameter in argument validator description: %s" % str(validator))
def wrapper(): required_args = {} for arg_name, validator in arguments_dict.items(): _validate(arg_name, validator, required_args) for validator in arguments_list: if isinstance(validator, AtLeastOneOfValidator): if not validator.arguments_dict: raise ValueError( "Invalid parameter in argument validator description: AtLeastOneOfValidator has no items." ) required_args_part = {} first_arg_name = u"" for arg_name, single_validator in validator.arguments_dict.items( ): single_validator.required = False single_validator.default = None if not first_arg_name: first_arg_name = arg_name _validate(arg_name, single_validator, required_args_part) if not required_args_part: raise errors.MissingRequiredParameter(first_arg_name) required_args.update(required_args_part) return wrapped(**required_args)
def get_car_insurance_branches(region=None, limit=None, offset=None, **kwargs): if 'id' not in kwargs: raise errors.MissingRequiredParameter('id') id_arg = kwargs['id'] if region: query = CarAssuranceBranch.query.filter_by( car_assurance_id=id_arg, region=region).order_by( CarAssuranceBranch.region).order_by(CarAssuranceBranch.title) else: query = CarAssuranceBranch.query.filter_by( car_assurance_id=id_arg).order_by( CarAssuranceBranch.region).order_by(CarAssuranceBranch.title) total = query.count() query = query.limit(limit).offset(offset) count = query.count() result = { 'total': total, 'count': count, 'branches': [{ "id": i.id, "title": i.title, "phone": i.phone, "address": i.address, "region": i.region } for i in query] } return {'result': result}
def get_car_insurances(search=None, limit=100, offset=0, **kwargs): if 'type' not in kwargs: raise errors.MissingRequiredParameter('type') type_arg = kwargs['type'] if type_arg != 'osago': raise errors.InvalidParameterValue('type') if search: query = CarAssurance.query.filter_by(full_name=search).order_by( CarAssurance.full_name) else: query = CarAssurance.query.filter().order_by(CarAssurance.short_name) total = query.count() query = query.limit(limit).offset(offset) count = query.count() result = { 'total': total, 'count': count, 'insurances': [{ "id": i.id, "short_name": i.short_name, "full_name": i.full_name } for i in query] } return {'result': result}
def get_ifns(**kwargs): id_val = kwargs.get('id', None) if not id_val: raise errors.MissingRequiredParameter('id') item = IfnsCatalogObject.query.filter_by(id=id_val).first() if not item: raise errors.IfnsNotFound() return db_ifns_to_api(item)
def confirm_account(code=None, user_id=None): if len(code) != current_app.config['max_activation_link_length'] and \ len(code) != current_app.config['digital_activation_link_length']: raise errors.InvalidParameterValue('code') if len(code) == current_app.config['digital_activation_link_length'] and not user_id: raise errors.MissingRequiredParameter('code') link_type = ConfirmationLinkTypeEnum.CLT_MOBILE if ( len(code) == current_app.config['digital_activation_link_length']) else ConfirmationLinkTypeEnum.CLT_EMAIL user = UserManager.confirm_email_or_mobile(code, user_id if user_id else None, link_type) if not user: raise errors.UserNotFound() data = get_user_api_structure(user) return {"result": data}
def signup(email=None, access_token=None, password=None, social_network=None): if "X-Forwarded-For" in request.headers and request.headers.getlist("X-Forwarded-For"): ip = request.headers.getlist("X-Forwarded-For")[0] elif "X-Real-Ip" in request.headers and request.headers.getlist("X-Real-Ip"): ip = request.headers.getlist("X-Real-Ip")[0] else: ip = request.remote_addr if ip in current_app.config['OFFICE_IP']: is_test_user = True else: is_test_user = False if not email and not access_token: raise errors.MissingRequiredParameter('email') if not EmailAddressValidator().validate(email) and not access_token: raise errors.InvalidParameterValue('email') if access_token: password = '' if social_network and social_network not in SocialServiceEnum.TAG_ALL: raise errors.InvalidParameterValue('social_network') if not PasswordValidator().validate(password) and not access_token: raise errors.MissingRequiredParameter('password') if current_user and not current_user.is_anonymous: if not current_user.temporal: raise errors.InvalidParameterValue('email') new_user = UserManager.promote_temp_user(current_user, access_token, None, email, u"", u"", u"", password, social_network) new_user.is_tester = is_test_user else: new_user = UserManager.register_user(access_token, None, email, u"", u"", u"", password, social_network) new_user.is_tester = is_test_user google_client_id = request.cookies.get('_ga_cid') if google_client_id and not new_user.temporal: metrics.update_user_info(new_user, google_client_id=google_client_id) new_user.email = new_user.email.lower() if new_user.email else u"" data = get_user_api_structure(new_user) result = {"result": data} if not email and access_token: login_user(new_user) my_resp = MyResp() current_app.session_interface.save_session(current_app, flask.session, my_resp) return result user = UserManager.login_user(email, password) if user: login_user(user) my_resp = MyResp() current_app.session_interface.save_session(current_app, flask.session, my_resp) user.last_login_date = datetime.utcnow() return result
def request_bank_partner(bank_id=None, batch_id=None, bank_contact_phone_general_manager=False, bank_contact_phone="", send_private_data=None): if not bank_contact_phone_general_manager and not bank_contact_phone: raise errors.MissingRequiredParameter('bank_contact_phone') batch = DocumentBatchDbObject.query.filter_by( id=batch_id, _owner=current_user, deleted=False, batch_type=DocumentBatchTypeEnum.DBT_NEW_LLC).scalar() if not batch or not batch.data: raise errors.BatchNotFound() current_batch = DocumentBatch.db_obj_to_field(batch) partner = BankPartnersObject.query.filter_by(id=bank_id).first() if not partner: raise errors.InvalidParameterValue('partner_id') svc_data = BankPartnersServiceObject.query.filter_by( bank_partner_id=partner.id).first() if not svc_data: raise errors.ServerError() current_bank_request = BankPartnerRequestObject.query.filter_by( bank_partner_id=partner.id, batch_id=batch_id).first() if current_bank_request and current_bank_request.status in ('sending', 'success'): struct = current_batch.get_api_structure() return {'result': struct} if current_bank_request and abs( (datetime.utcnow() - current_bank_request.sent_date).total_seconds()) > 60: BankPartnerRequestObject.query.filter_by( id=current_bank_request.id).delete() sqldb.session.commit() current_bank_request = None svc_type = svc_data.type fields = svc_data.fields extra_context = { 'bank_contact_phone_general_manager': bank_contact_phone_general_manager, 'bank_contact_phone': bank_contact_phone, 'send_private_data': send_private_data, 'bank_title': partner.title } field_list = BatchManager.make_fields_from_data( batch_id, fields, current_app.config, extra_context=extra_context) context = {} errors_list = [] for name in field_list: field = field_list[name] try: if not field.initialized: if field.required: raise MissingRequiredFieldException(name) else: field.validate() except (InvalidFieldValueException, MissingRequiredFieldException), ex: if hasattr(field, "suppress_validation_errors"): suppress_validation_errors = field.suppress_validation_errors if isinstance(suppress_validation_errors, dict): suppress_validation_condition = Condition( suppress_validation_errors) context = copy.copy(batch.data) context.update(extra_context) suppress_validation_errors = suppress_validation_condition.check( context) if suppress_validation_errors: continue if getattr(ex, 'ext_data', None): err_list = error_tree_to_list(ex.ext_data) error_field_paths = [{ 'field': name + '.' + i['field'], 'error_code': i['error_code'] } for i in err_list] errors_list.extend(error_field_paths) else: errors_list.append({ 'field': name, 'error_code': ex.ERROR_CODE }) current_app.logger.exception(u"Field %s validation error" % name) continue if field_list[name].initialized: context[name] = field_list[name]