def _check_profile_data(self, data, update=False): name_pattern = re.compile("^[\S-]{2,50}$") price_pattern = re.compile("^[0-9]+$") mandatories = { 'name': { "type": str, "regex": name_pattern }, 'price': { "type": int, "regex": price_pattern }, } for mandatory, specs in mandatories.items(): if not update: if mandatory not in data or data[mandatory] is None: raise InvalidData("Missing value %s" % mandatory) else: if mandatory not in data: continue value = data[mandatory] if "type" in specs and not isinstance(value, specs["type"]): raise InvalidData("Invalid type %s" % mandatory) if "regex" in specs and isinstance( value, str) and not re.match(specs["regex"], value): raise InvalidData("Invalid value %s" % mandatory)
def _check_profile_data(self, data, update=False): name_pattern = re.compile("^[\S-]{2,50}$") email_pattern = re.compile( "^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$") mandatories = { 'firstname': { "type": str, "regex": name_pattern }, 'lastname': { "type": str, "regex": name_pattern }, 'email': { "type": str, "regex": email_pattern } } for mandatory, specs in mandatories.items(): if not update: if mandatory not in data or data[mandatory] is None: raise InvalidData("Missing value %s" % mandatory) else: if mandatory not in data: continue value = data[mandatory] if "type" in specs and not isinstance(value, specs["type"]): raise InvalidData("Invalid type %s" % mandatory) if "regex" in specs and isinstance( value, str) and not re.match(specs["regex"], value): raise InvalidData("Invalid value %s" % mandatory)
def _check_profile_data(self, data: dict, update=False): mandatories = { 'date': { "type": str }, 'start_time': { "type": int }, 'end_time': { "type": int }, 'level': { "type": str } } if (('start_time' in data == True) and ('end_time' in data == True)): if data['start_time'] > data['end_time']: raise InvalidData("End time must be superior than start time") for mandatory, specs in mandatories.items(): if not update: if mandatory not in data or data[mandatory] is None: raise InvalidData("Missing value %s" % mandatory) else: if mandatory not in data: continue value = data[mandatory] if "type" in specs and not isinstance(value, specs["type"]): raise InvalidData("Invalid type %s" % mandatory)
def decode(cls, data): if len(data) != 15: raise InvalidData("Input to short") if data[0] != cls.msgtype: raise InvalidData("Wrong msgtype") _msgtype, userid, nonce = unpack_from('>BBQBB', data) return cls(userid, nonce)
def decode(cls, data): if len(data) < 2: raise InvalidData("Input to short") if data[0] != cls.msgtype: raise InvalidData("Wrong msgtype") _msgtype, command = unpack_from('>BB', data) return cls(command)
def decode(cls, data): if len(data) < calcsize(cls.packformat) + 6: raise InvalidData("Input to short") if data[0] != cls.msgtype: raise InvalidData("Wrong msgtype") _msgtype, = unpack_from('>B', data) return cls(data[1:7])
def decode(cls, data): if len(data) != 15: raise InvalidData("Input to short") if data[0] != ConnectionInfoMessage.msgtype: raise InvalidData("Wrong msgtype") _msgtype, userid, remote_session_nonce, _unknown, bootloader, application = unpack_from( '>BBQBBB', data) return cls(userid, remote_session_nonce, bootloader, application)
def decode(cls, data): if len(data) < calcsize(cls.packformat): raise InvalidData("Input to short") if data[0] != cls.msgtype: raise InvalidData("Wrong msgtype") _msgtype, year, month, day, hours, minutes, second = unpack_from( '>BBBBBBB', data) date = datetime(year + 2000, month - 1, day, hours, minutes, second) return cls(date)
def _check_profile_data(self, data, update=False): mandatories = {'username': {"type": str}, 'password': {"type": str}} for mandatory, specs in mandatories.items(): if not update: if mandatory not in data or data[mandatory] is None: raise InvalidData("Missing value %s" % mandatory) else: if mandatory not in data: continue value = data[mandatory] if "type" in specs and not isinstance(value, specs["type"]): raise InvalidData("Invalid type %s" % mandatory)
def _check_data(self, data, specs, update=False): for mandatory, specs in specs.items(): if not update: if mandatory not in data or data[mandatory] is None: raise InvalidData("Missing value %s" % mandatory) else: if mandatory not in data: continue value = data[mandatory] if "type" in specs and not isinstance(value, specs["type"]): raise InvalidData("Invalid type %s" % mandatory) if "regex" in specs and isinstance(value, str) and not re.match(specs["regex"], value): raise InvalidData("Invalid value %s" % mandatory)
def __init__(self, userid, encrypted_pair_key, security_counter, authentication): self.userid = userid self.encrypted_pair_key = encrypted_pair_key self.security_counter = security_counter self.authentication = authentication if len(self.encrypted_pair_key) < 16: raise InvalidData("Encrypted pair key < 16") if len(self.encrypted_pair_key) > 22: raise InvalidData("Encrypted pair key > 22") length = len(self.encrypted_pair_key) if length < 22: self.encrypted_pair_key.extend(b'\x00' * (22 - length))
def new_loan_entry_is_valid(active_loans, csv_entry): for cdp in active_loans: if csv_entry['wallet_address'] == cdp.wallet_address: raise InvalidData( 'Trying to create a loan that already exists:{}'.format( cdp.wallet_address)) return True
def decode(cls, data): if len(data) < 29: raise InvalidData("Input to short") if data[0] != cls.msgtype: raise InvalidData("Wrong msgtype") head = calcsize('>BB') tail = head + 22 _msgtype, userid = unpack_from('>BB', data) encrypted_pair_key = data[head:tail] security_counter, = unpack_from('>H', data, tail) tail += 2 authentication = data[tail:tail + 4] return cls(userid, encrypted_pair_key, security_counter, authentication)
def validate_data(self): data = request.form or request.json # get data title = data.get('title') review = data.get('review') error_messages = {} if not title: # if the title field is empty error_messages[ 'title'] = 'This field may not be blank.' # add an error message if not review: # if the review field is empty error_messages[ 'review'] = 'This field may not be blank.' # add an error message if error_messages: # if the error message is not empty raise InvalidData( error_messages ) # raise the exception if the error message is not empty return data # return validated data
def create_person(self, data, person_type=None): logging.info("Create member with data %s" % str(data)) self._check_person_data(data) try: with self._database_engine.new_session() as session: # Save member in database dao = PersonDAO(session) person = session.query(Person).filter_by( email=data.get('email')).all() if len(person) > 0: raise InvalidData("Mail already existing") member = dao.create(data) member_data = member.to_dict() return member_data except Error as e: # log error logging.error("An Error occured (%s)" % str(e)) raise e
def __init__(self, fragmentid): # uint8 if fragmentid > 255: raise InvalidData("fragmentid does not fit into a byte") self.fragmentid = fragmentid
def decode(cls, data): if data[0] != FragmentAck.msgtype: raise InvalidData("Wrong msgtype") _msgtype, fragmentid = unpack_from('>BB', data) return cls(fragmentid)
def __init__(self, answer): # uint8 if answer: raise InvalidData("answer does not fit into a byte") self.answer = answer
def decode(cls, data): if data[0] != AnswerWithoutSecurity.msgtype: raise InvalidData("Wrong msgtype") _msgtype, answer = unpack_from('>BB', data) return cls(answer)