Ejemplo n.º 1
0
    def test_quoted_json(self):
        attr = JSONAttribute()
        serialized = attr.serialize('\\t')
        assert attr.deserialize(serialized) == '\\t'

        serialized = attr.serialize('"')
        assert attr.deserialize(serialized) == '"'
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
 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
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
 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)
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
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])
Ejemplo n.º 11
0
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()
Ejemplo n.º 12
0
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}
Ejemplo n.º 13
0
class CharacterModel(Model):
    class Meta:
        table_name = 'ff14-character'
        region = 'us-west-2'

    c_id = NumberAttribute(hash_key=True)
    name = UnicodeAttribute()
    levels = JSONAttribute()
Ejemplo n.º 14
0
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)
Ejemplo n.º 15
0
class EventModel(BaseNoSQLModel):
    event_date = DateTimeAttribute()
    appointment_id = DjangoPrimaryKeyAttribute()
    name = UnicodeAttribute()
    attributes = JSONAttribute(default={})

    class Meta(BaseNoSQLModel.Meta):
        table_name = "event"
Ejemplo n.º 16
0
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
Ejemplo n.º 17
0
class ConfigModel(Model):
    class Meta:
        table_name = "PongConfiguration"
        region = "us-west-2"

    Id = UnicodeAttribute(hash_key=True)
    Config = JSONAttribute()
    UpdatedAt = UTCDateTimeAttribute()
    CreatedAt = UTCDateTimeAttribute()
Ejemplo n.º 18
0
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)
        ]
Ejemplo n.º 19
0
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()
Ejemplo n.º 20
0
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()
Ejemplo n.º 21
0
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)
Ejemplo n.º 22
0
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())
Ejemplo n.º 23
0
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={})
Ejemplo n.º 24
0
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()
Ejemplo n.º 25
0
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()
Ejemplo n.º 26
0
    def test_quoted_json(self):
        attr = JSONAttribute()
        serialized = attr.serialize('\\t')
        assert attr.deserialize(serialized) == '\\t'

        serialized = attr.serialize('"')
        assert attr.deserialize(serialized) == '"'
Ejemplo n.º 27
0
    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), '"')
Ejemplo n.º 28
0
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()
Ejemplo n.º 29
0
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)
Ejemplo n.º 30
0
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
Ejemplo n.º 31
0
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)
Ejemplo n.º 32
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)
Ejemplo n.º 33
0
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])
Ejemplo n.º 34
0
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)
Ejemplo n.º 35
0
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)
Ejemplo n.º 36
0
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()
Ejemplo n.º 37
0
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()
Ejemplo n.º 38
0
    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)