def test_quoted_json(self): attr = JSONAttribute() serialized = attr.serialize('\\t') assert attr.deserialize(serialized) == '\\t' serialized = attr.serialize('"') assert attr.deserialize(serialized) == '"'
def test_control_chars(self): """ JSONAttribute with control chars """ attr = JSONAttribute() item = {"foo\t": "bar\n", "bool": True, "number": 3.141} encoded = six.u(json.dumps(item)) self.assertEqual(attr.deserialize(encoded), item)
def test_json_deserialize(self): """ JSONAttribute.deserialize """ attr = JSONAttribute() item = {'foo': 'bar', 'bool': True, 'number': 3.141} encoded = six.u(json.dumps(item)) self.assertEqual(attr.deserialize(encoded), item)
def test_json_deserialize(self): """ JSONAttribute.deserialize """ attr = JSONAttribute() item = {"foo": "bar", "bool": True, "number": 3.141} encoded = six.u(json.dumps(item)) self.assertEqual(attr.deserialize(encoded), item)
def test_control_chars(self): """ JSONAttribute with control chars """ attr = JSONAttribute() item = {'foo\t': 'bar\n', 'bool': True, 'number': 3.141} encoded = six.u(json.dumps(item)) self.assertEqual(attr.deserialize(encoded), item)
def test_json_serialize(self): """ JSONAttribute.serialize """ attr = JSONAttribute() item = {'foo': 'bar', 'bool': True, 'number': 3.141} assert attr.serialize(item) == six.u(json.dumps(item)) assert attr.serialize({}) == six.u('{}') assert attr.serialize(None) is None
def test_json_serialize(self): """ JSONAttribute.serialize """ attr = JSONAttribute() item = {"foo": "bar", "bool": True, "number": 3.141} self.assertEqual(attr.serialize(item), six.u(json.dumps(item))) self.assertEqual(attr.serialize({}), six.u("{}")) self.assertEqual(attr.serialize(None), None)
def test_json_serialize(self): """ JSONAttribute.serialize """ attr = JSONAttribute() item = {'foo': 'bar', 'bool': True, 'number': 3.141} self.assertEqual(attr.serialize(item), six.u(json.dumps(item))) self.assertEqual(attr.serialize({}), six.u('{}')) self.assertEqual(attr.serialize(None), None)
class Poll(Model): """ A DynamoDB User """ class Meta: table_name = DYNAMO_DB_TABLE_NAME key = UnicodeAttribute(hash_key=True) question = UnicodeAttribute() author = JSONAttribute() # option_id is index of the option in the list options = ListAttribute(default=list) votes = MapAttribute(default=dict) # user_id -> option_id users = MapAttribute(default=dict) # user_id -> data # see https://pynamodb.readthedocs.io/en/latest/optimistic_locking.html version = VersionAttribute() # information about poll message in telegram telegram_version = NumberAttribute() telegram_datetime = UTCDateTimeAttribute() def get_users_by_option_id(self): users_by_option_id = {} for user_id in self.votes: option_id = self.votes[user_id] users_by_option_id.setdefault(option_id, []) users_by_option_id[option_id].append(self.users[user_id]) return users_by_option_id
class DynamoDBEventModel(Model): """ A DynamoDB Event model """ class Meta: region = os.environ.get('AWS_REGION', 'ap-southeast-2') table_name = os.environ.get("EVENT_TABLE", "events") host = os.environ.get('DYNAMODB_HOST', None) aggregate_name = UnicodeAttribute(hash_key=True) aggregate_key = UnicodeAttribute(range_key=True) event_type = UnicodeAttribute() created_at = UTCDateTimeAttribute() event_data = JSONAttribute() @classmethod def _conditional_operator_check(cls, conditional_operator): pass @property def aggregate_id(self): return self.aggregate_key.split(AGGREGATE_KEY_DELIMITER)[0] @property def version(self): return int(self.aggregate_key.split(AGGREGATE_KEY_DELIMITER)[1])
class TableWithRange(Model): Meta = make_meta(table_name='with_range') key1 = UnicodeAttribute(hash_key=True) key2 = UnicodeAttribute(range_key=True) date_attr = UTCDateTimeAttribute(null=True) json_attr = JSONAttribute()
class Website(Model): class Meta: table_name = "websites" region = "us-east-2" url = UnicodeAttribute(hash_key=True) current_code = UnicodeAttribute(null=True) contacts = JSONAttribute(null=True) # {contact_location:frequency}
class CharacterModel(Model): class Meta: table_name = 'ff14-character' region = 'us-west-2' c_id = NumberAttribute(hash_key=True) name = UnicodeAttribute() levels = JSONAttribute()
class MetaAttribute(MapAttribute): num_threads = NumberAttribute(default=0) num_arecs = NumberAttribute(default=0) entrypoint = UnicodeAttribute(null=True) stopped = ListAttribute(default=list) exe = MapAttribute(null=True) result = JSONAttribute(null=True) broken = BooleanAttribute(default=False)
class EventModel(BaseNoSQLModel): event_date = DateTimeAttribute() appointment_id = DjangoPrimaryKeyAttribute() name = UnicodeAttribute() attributes = JSONAttribute(default={}) class Meta(BaseNoSQLModel.Meta): table_name = "event"
class BlindCredential(Model): class Meta: table_name = settings.DYNAMODB_TABLE if settings.DYNAMODB_URL: host = settings.DYNAMODB_URL region = settings.AWS_DEFAULT_REGION connection_cls = DDBConnection session_cls = DDBSession id = UnicodeAttribute(hash_key=True) revision = NumberAttribute() data_type = UnicodeAttribute() data_type_date_index = DataTypeDateIndex() name = UnicodeAttribute() credential_pairs = JSONAttribute() credential_keys = NonNullUnicodeSetAttribute(default=set, null=True) enabled = BooleanAttribute(default=True) data_key = JSONAttribute() cipher_version = NumberAttribute() cipher_type = UnicodeAttribute() metadata = JSONAttribute(default=dict, null=True) modified_date = UTCDateTimeAttribute(default=datetime.now) modified_by = UnicodeAttribute() documentation = UnicodeAttribute(null=True) def equals(self, other_cred): if self.name != other_cred.name: return False if self.credential_pairs != other_cred.credential_pairs: return False if self.credential_keys != other_cred.credential_keys: return False if self.enabled != other_cred.enabled: return False if self.data_key != other_cred.data_key: return False if self.cipher_version != other_cred.cipher_version: return False if self.cipher_type != other_cred.cipher_type: return False if self.metadata != other_cred.metadata: return False if self.documentation != other_cred.documentation: return False return True
class ConfigModel(Model): class Meta: table_name = "PongConfiguration" region = "us-west-2" Id = UnicodeAttribute(hash_key=True) Config = JSONAttribute() UpdatedAt = UTCDateTimeAttribute() CreatedAt = UTCDateTimeAttribute()
class PynamoLogger(Model): ''' log changes to pynamo member ''' class Meta: table_name = None region = 'ap-southeast-1' appName = UnicodeAttribute(hash_key=True) timestamp = NumberAttribute(range_key=True) logMessage = UnicodeAttribute() requestObject = JSONAttribute(default={'noValue': 'noValue'}) responseObject = JSONAttribute(default={'noValue': 'noValue'}) def to_dict(self): res = {} for k in [ 'appName', 'timestamp', 'logMessage', 'requestObject', 'responseObject' ]: res[k] = getattr(self, k) return res @classmethod def log(cls, message: str, appName='mockapp', requestObject={'request': "none"}, responseObject={'message': 'not specified'}): dynamoLogger = cls(appName=appName, timestamp=dt.now().timestamp(), logMessage=message, responseObject=responseObject) saveResult = dynamoLogger.save() return {'logId': dynamoLogger.timestamp, 'saveResult': saveResult} @classmethod def checkLog(cls, appName='mockapp', logId='', logType='write', limit=10): return [ log.to_dict() for log in cls.query(hash_key=appName, range_key_condition=cls.timestamp == float(logId) if logId else None, limit=limit) ]
class UnvalidatedGlobalKeyRequests(Model): """Global key requests that have not been validated yet.""" class Meta: table_name = "magiccap_unvalidated_global_key_requests" region = "eu-west-2" read_capacity_units = 1 write_capacity_units = 1 key = UnicodeAttribute(hash_key=True) data = JSONAttribute()
class ResourceModel(Model): class Meta: table_name = 'fairhealth-resource' name = UnicodeAttribute(hash_key=True) hours = UnicodeAttribute(null=True) address = UnicodeAttribute(null=True) zip_code = UnicodeAttribute(null=True) phone = UnicodeAttribute(null=True) specialties = JSONAttribute()
class ConnectedVehicleTable(Model): class Meta: region = "us-east-1" request_key = UnicodeAttribute(hash_key=True) event_datetime = NumberAttribute(range_key=True) programcode = UnicodeAttribute() timestamp = UTCDateTimeAttribute() referenceid = UnicodeAttribute(default="0") vin = UnicodeAttribute(null=True) latitude = NumberAttribute(null=True) longitude = NumberAttribute(null=True) language = UnicodeAttribute(null=True) msisdn = UnicodeAttribute(null=True) activationtype = UnicodeAttribute(null=True) brand = UnicodeAttribute(null=True) modelname = UnicodeAttribute(null=True) modelyear = UnicodeAttribute(null=True) modelcode = UnicodeAttribute(null=True) modeldesc = UnicodeAttribute(null=True) market = UnicodeAttribute(null=True) odometer = NumberAttribute(null=True) odometerscale = UnicodeAttribute(null=True) headingdirection = UnicodeAttribute(null=True) countrycode = UnicodeAttribute(null=True) eventid = UnicodeAttribute(null=True) servicetype = UnicodeAttribute(null=True) altitude = UnicodeAttribute(null=True) vehicletype = UnicodeAttribute(null=True) enginestatus = UnicodeAttribute(null=True) positiondirection = UnicodeAttribute(null=True) vehiclespeed = NumberAttribute(null=True) callreason = UnicodeAttribute(null=True) calltrigger = UnicodeAttribute(null=True) mileage = NumberAttribute(null=True) mileageunit = UnicodeAttribute(null=True) calltype = UnicodeAttribute(null=True) flowid = UnicodeAttribute(null=True) correlationid = UnicodeAttribute(null=True) calldate = UnicodeAttribute(null=True) calltime = UnicodeAttribute(null=True) phonenumber = UnicodeAttribute(null=True) customerfirstname = UnicodeAttribute(null=True) customerlastname = UnicodeAttribute(null=True) modelcolor = UnicodeAttribute(null=True) srnumber = UnicodeAttribute(null=True) locationaddress = UnicodeAttribute(null=True) locationcity = UnicodeAttribute(null=True) locationstate = UnicodeAttribute(null=True) locationpostalcode = UnicodeAttribute(null=True) locationconfidence = UnicodeAttribute(null=True) locationtrueness = UnicodeAttribute(null=True) cruisingrange = UnicodeAttribute(null=True) ismoving = BooleanAttribute(null=True) JSONData = JSONAttribute(null=True)
class FutureAttribute(MapAttribute): resolved = BooleanAttribute(default=False) continuations = ListAttribute(default=list) chain = NumberAttribute(null=True) value = JSONAttribute(null=True) def serialize(self, value): return super().serialize(value.serialise()) def deserialize(self, value): return Future.deserialise(super().deserialize(value).as_dict())
class DeviceModel(AuditModel): class Meta: table_name = generate_table_name("iot_devices") region = IOT_AWS_REGION host = DATABASE_HOST device_id = UnicodeAttribute(hash_key=True) description = UnicodeAttribute(null=True) device_type = UnicodeAttribute(default="default") device_group = UnicodeAttribute(default="default") settings = JSONAttribute(default={})
class PynamoTestTable(Model): '''a table to store the list of sensitive columns which was last read in pynamodb''' class Meta: table_name = 'speed-test-table' region = 'us-east-1' hashedPhone = UnicodeAttribute(hash_key=True) arn = UnicodeAttribute() name = UnicodeAttribute() versionId = UnicodeAttribute() sensitiveColumn = JSONAttribute()
class BlindCredential(Model): class Meta: table_name = app.config.get('DYNAMODB_TABLE') if app.config.get('DYNAMODB_URL'): host = app.config.get('DYNAMODB_URL') region = app.config.get('AWS_DEFAULT_REGION') id = UnicodeAttribute(hash_key=True) revision = NumberAttribute() data_type = UnicodeAttribute() data_type_date_index = DataTypeDateIndex() name = UnicodeAttribute() credential_pairs = JSONAttribute() credential_keys = UnicodeSetAttribute(default=set([]), null=True) enabled = BooleanAttribute(default=True) data_key = JSONAttribute() cipher_version = NumberAttribute() cipher_type = UnicodeAttribute() metadata = JSONAttribute(default={}, null=True) modified_date = UTCDateTimeAttribute(default=datetime.now) modified_by = UnicodeAttribute()
def test_quoted_json(self): attr = JSONAttribute() serialized = attr.serialize('\\t') self.assertEqual(attr.deserialize(serialized), '\\t') serialized = attr.serialize('"') self.assertEqual(attr.deserialize(serialized), '"')
class AttributeTestModel(Model): class Meta: host = 'http://localhost:8000' table_name = 'test' binary_attr = BinaryAttribute() binary_set_attr = BinarySetAttribute() number_attr = NumberAttribute() number_set_attr = NumberSetAttribute() unicode_attr = UnicodeAttribute() unicode_set_attr = UnicodeSetAttribute() datetime_attr = UTCDateTimeAttribute() bool_attr = BooleanAttribute() json_attr = JSONAttribute()
class SnapshotModel(Model): class Meta: table_name = 'EventStore' region = 'ap-northeast-1' max_retry_attempts = 8 base_backoff_ms = 297 item_id = UnicodeAttribute(hash_key=True) version = NumberAttribute(range_key=True) from_version = NumberAttribute() name = UnicodeAttribute() state = JSONAttribute() saved_at = UnicodeAttribute() order_id = UnicodeAttribute(null=True)
class BingSearch(Model): class Meta: aws_access_key_id = settings.AWS_ACCESS_KEY_ID aws_secret_access_key = settings.AWS_SECRET_ACCESS_KEY table_name = "dynamodb-bingsearch" id = UnicodeAttribute(hash_key=True, null=True) type = JSONAttribute(range_key=True, null=True) query_context = JSONAttribute(null=True) instrumentation = JSONAttribute(null=True) web_pages = JSONAttribute(null=True) entities = JSONAttribute(null=True) images = JSONAttribute(null=True) news = JSONAttribute(null=True) related_searches = JSONAttribute(null=True) videos = JSONAttribute(null=True) def save(self): if not BingSearch.exists(): BingSearch.create_table(read_capacity_units=1, write_capacity_units=1, wait=True) else: print('already created') super(BingSearch, self).save() def convert(self): data = { 'id': self.id, 'type': self.type, 'query_context': self.query_context, 'instrumentation': self.instrumentation, 'web_pages': self.web_pages, 'entities': self.entities, 'images': self.images, 'news': self.news, 'related_searches': self.related_searches, 'videos': self.videos } return data
class MemberModel(Model): class Meta: table_name = 'members' region = 'us-west-2' read_capacity_units = 5 write_capacity_units = 5 uid = UnicodeAttribute(hash_key=True) last_logged_in_stamp = NumberAttribute(default=time.time()) last_logged_in = UnicodeAttribute(default='') username = UnicodeAttribute(default='') location = UnicodeAttribute(default='') pics = UnicodeSetAttribute(default=set()) msgs = JSONAttribute(default=[]) phone = UnicodeAttribute(default='') last_updated = NumberAttribute(default=time.time()) last_full_updated = NumberAttribute(default=0)
class OrganizationModel(BaseModel): class Meta(BaseModel.Meta): simple_name = 'organization' id = UnicodeAttribute(hash_key=True) name = UnicodeAttribute() search_name = UnicodeAttribute() email = UnicodeAttribute() phone = UnicodeAttribute() administrator_id = UnicodeAttribute() address = JSONAttribute(null=True) is_verified = BooleanAttribute(default=False) search_name_index = SearchNameIndex() def save(self, conditional_operator=None, **expected_values): self.search_name = self.name.lower() super().save(conditional_operator, **expected_values)
class ScreenModel(Model): class Meta: table_name = 'fairhealth-screen' slug = UnicodeAttribute(hash_key=True) created_at = UTCDateTimeAttribute() params = JSONAttribute() phone = UnicodeAttribute(null=True) email = UnicodeAttribute(null=True) event = UnicodeAttribute(null=True) visits = NumberAttribute(default=0) @staticmethod def make_slug(): firstword = settings.SLUG_WORDS[random.randint(0, 1501)] num = str(random.randint(1, 100)) return ''.join([firstword, num])
class CredentialBase(Model): id = UnicodeAttribute(hash_key=True) revision = NumberAttribute() data_type = UnicodeAttribute() name = UnicodeAttribute() credential_pairs = UnicodeAttribute() enabled = BooleanAttribute(default=True) data_key = BinaryAttribute() # TODO: add cipher_type cipher_version = NumberAttribute(null=True) metadata = JSONAttribute(default=dict, null=True) modified_date = UTCDateTimeAttribute(default=datetime.now) modified_by = UnicodeAttribute() documentation = UnicodeAttribute(null=True) # Classification info (eg: FINANCIALLY_SENSITIVE) tags = ListAttribute(default=list) last_decrypted_date = UTCDateTimeAttribute(null=True) last_rotation_date = UTCDateTimeAttribute(null=True)
class Attribute(Model): """ DynamoDB data model for a property """ class Meta(object): table_name = os.getenv('AWS_DB_PREFIX', 'demo_actingweb') + "_attributes" read_capacity_units = 26 write_capacity_units = 2 region = os.getenv('AWS_DEFAULT_REGION', 'us-west-1') host = os.getenv('AWS_DB_HOST', None) id = UnicodeAttribute(hash_key=True) bucket_name = UnicodeAttribute(range_key=True) bucket = UnicodeAttribute() name = UnicodeAttribute() data = JSONAttribute(null=True) timestamp = UTCDateTimeAttribute(null=True)
class OrganizationModel(Model): class Meta: region = 'ca-central-1' simple_name = 'organization' id = UnicodeAttribute(hash_key=True) name = UnicodeAttribute() search_name = UnicodeAttribute() email = UnicodeAttribute() phone = UnicodeAttribute() administrator_id = UnicodeAttribute() address = JSONAttribute(null=True) is_verified = BooleanAttribute(default=False) search_name_index = SearchNameIndex() created_at = UTCDateTimeAttribute() created_by = UnicodeAttribute() updated_at = UTCDateTimeAttribute() updated_by = UnicodeAttribute()
class Tweet(Model): class Meta: table_name = 'tweets' read_capacity_units = 10 write_capacity_units = 10 id = UnicodeAttribute(hash_key=True) screen_name = UnicodeAttribute(range_key=True) search_term = UnicodeAttribute(range_key=True) text = UnicodeAttribute() lang = UnicodeAttribute() timestamp = NumberAttribute() retweet_count = NumberAttribute() in_reply_to_screen = UnicodeAttribute(range_key=True) image = UnicodeAttribute() metadata = JSONAttribute() by_user = ScreenNameIndex() by_search = SearchTermIndex()
class MyNotification(Model): message_id = UnicodeAttribute(hash_key=True) from_user = UnicodeAttribute() to_user = UnicodeAttribute() created_at = UTCDateTimeAttribute(default=datetime.now, range_key=True) status = UnicodeAttribute() data = JSONAttribute(null=True) message = UnicodeAttribute() message_type = UnicodeAttribute() reference_link = UnicodeAttribute(null=True) exec_status = UnicodeAttribute(null=True) is_deleted = BooleanAttribute(default=False) class Meta: table_name = 'Notification' @staticmethod def setup_model(model, table_name): setattr(model.Meta, 'table_name', table_name)