def from_dict(self, database_manager, data): exclusions = self.local_exclusions + [ 'database_manager', 'local_exclusions', 'confirmation_code', 'ctime', 'mtime' ] self.database_manager = database_manager for key, value in data.items(): if key not in exclusions: setattr(self, key, value) if 'ctime' in data and isinstance(data['ctime'], str): self.ctime = DatabaseObject.get_datetime_from_string( data['ctime'] ) if 'mtime' in data and isinstance(data['mtime'], str): self.mtime = DatabaseObject.get_datetime_from_string( data['mtime'] ) if 'is_email_verified' in data and isinstance(data['is_email_verified'], int): self.is_email_verified = DatabaseObject.get_boolean_from_string( data["is_email_verified"] ) if 'is_suspended' in data and isinstance(data['is_suspended'], int): self.is_suspended = DatabaseObject.get_boolean_from_string( data["is_suspended"] )
def from_dict(self, database_manager, data): exclusions = self.local_exclusions + [ 'database_manager', 'local_exclusions', 'expires', 'ctime', 'mtime' ] self.database_manager = database_manager for key, value in data.items(): if key not in exclusions: setattr(self, key, value) if 'ctime' in data and isinstance(data['ctime'], str): self.ctime = DatabaseObject.get_datetime_from_string(data['ctime']) if 'mtime' in data and isinstance(data['mtime'], str): self.mtime = DatabaseObject.get_datetime_from_string(data['mtime']) if 'expires' in data and isinstance(data['expires'], str): self.mtime = DatabaseObject.get_datetime_from_string( data['expires'])
def to_dict(self): exclusions = self.local_exclusions + [ 'database_manager', 'local_exclusions', 'ctime', 'mtime' ] data = {} for property, value in vars(self).iteritems(): if property not in exclusions: data[property] = value if 'ctime' in data and isinstance(data['ctime'], datetime): self.ctime = DatabaseObject.get_datetime_from_string(data['ctime']) if 'mtime' in data and isinstance(data['mtime'], datetime): self.mtime = DatabaseObject.get_datetime_from_string(data['mtime']) if 'expires' in data and isinstance(data['expires'], datetime): self.mtime = DatabaseObject.get_datetime_from_string( data['expires']) return data
def from_dict(self, database_manager, data): super.from_dict(database_manager, data) if 'is_email_verified' in data: self.is_email_verified = DatabaseObject.get_string_from_boolean( data["is_email_verified"])
def from_dict(self, database_manager, data): super.from_dict(database_manager, data) if 'is_decommisioned' in data: self.is_banned = DatabaseObject.get_boolean_from_string( data["is_decommisioned"])
def handle_get(http_server, database_manager, ip, account, api_key): query_params = http_server.get_query_parameters() if "phone" not in query_params: raise ApiParamNoPhoneProvidedError http_server.print_headers() phone_string = query_params["phone"] matches = re.match( r"^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$", phone_string) if matches is None: raise ApiParamInvalidPhoneError try: phone = phonenumbers.parse(phone_string, "US") formatted_number = phonenumbers.format_number( phone, phonenumbers.PhoneNumberFormat.NATIONAL) except: raise ApiParamInvalidPhoneError area_code_string = re.findall(r"\(([^\)]{3})\)", formatted_number) if len(area_code_string) == 0: raise ApiParamInvalidPhoneError area_code_conditions = [{ "column": "area_code", "equivalence": "=", "value": area_code_string[0] }] area_codes = database_manager.fetch_by(AreaCode(database_manager), area_code_conditions) locations = [] if len(area_codes) > 0: city_ids = [area_code.city_id for area_code in area_codes] state_ids = [area_code.state_id for area_code in area_codes] country_id = area_codes[0].country_id city_conditions = [{ "column": "id", "equivalence": "IN", "value": city_ids }] cities = database_manager.fetch_by(City(database_manager), city_conditions) city_names = set([city.name for city in cities]) state_conditions = [{ "column": "id", "equivalence": "IN", "value": state_ids }] states = database_manager.fetch_by(UsState(database_manager), state_conditions) state_names = set([state.name for state in states]) country_conditions = [{ "column": "id", "equivalence": "=", "value": country_id }] country_info = None country = Country.fetch_by_id(database_manager, area_codes[0].country_id) if country is not None: country_info = {"name": country.name, "code": country.code} for city in cities: city_state = None for state in states: if city.state_id == state.id: city_state = {"name": state.name, "code": state.code} break location = { "is_overlay": DatabaseObject.get_boolean_from_string( area_codes[0].is_overlay), "city": city.name, "state": city_state, "country": country_info } locations.append(location) output = {"locations": locations} http_server.print_json(output)