Beispiel #1
0
class ErrorModel(Model):
    """
    DynamoDB Error record
    """
    class Meta:
        table_name = "error"
        read_capacity_units = 1
        write_capacity_units = 1

    url = UnicodeAttribute(hash_key=True)
    status_code = NumberAttribute()
    headers = UnicodeAttribute(null=True)
    body = UnicodeAttribute(null=True)
    ttl = NumberAttribute()
Beispiel #2
0
class OrderModel(Model):

    class Meta:
        table_name = ddb_table_name
        region = 'ap-northeast-1'
        read_capacity_units = 1
        write_capacity_units = 1

    id = UnicodeAttribute(hash_key=True)
    status = UnicodeAttribute(null=False)
    transactionStatus = UnicodeAttribute(null=False)
    paymentId = UnicodeAttribute(null=False)
    createdAt = UnicodeAttribute(null=False)

    itemId = NumberAttribute(null=False)
    title = UnicodeAttribute(null=False)
    subtitle = UnicodeAttribute(null=False)
    price = NumberAttribute(null=False)
    


    def __init__(self, **kwargs):
        super().__init__(**kwargs)
    
    def init(self):
        now = datetime.datetime.utcnow().replace(tzinfo=pytz.utc).astimezone(local_tz).strftime('%Y-%m-%d %H:%M:%S')

        self.id = str(uuid.uuid4())
        self.status = ORDER_STATUS_INITIALIZED
        self.transactionStatus = TRANSACTION_STATUS_INITIALIZED
        self.createdAt = now
    
    def setOrderStatusSucceeded(self):
        self.status = ORDER_STATUS_SUCCEEDED
        self.transactionStatus = TRANSACTION_STATUS_BEING_PROCESSED

    def setOrderStatusFailed(self):
        self.status = ORDER_STATUS_FAILED
    
    def setOrderTransctionDone(self):
        self.transactionStatus = TRANSACTION_STATUS_DONE

    def to_dict(self):
        rval = {}
        for key in self.attribute_values:
            rval[key] = self.__getattribute__(key)
        return rval

    def save(self):
        super().save()
Beispiel #3
0
class Thread(Model):
    class Meta:
        read_capacity_units = 1
        write_capacity_units = 1
        table_name = "Thread"
        host = "http://localhost:8000"

    forum_name = UnicodeAttribute(hash_key=True)
    subject = UnicodeAttribute(range_key=True)
    views = NumberAttribute(default=0)
    replies = NumberAttribute(default=0)
    answered = NumberAttribute(default=0)
    tags = UnicodeSetAttribute()
    last_post_datetime = UTCDateTimeAttribute(null=True)
Beispiel #4
0
class BetsHistory(Model):
    """
    BetsHistory model.
    """
    class Meta:
        table_name = os.environ.get("BETS_HISTORY_DYNAMODB_TABLE",
                                    "com.baebot.dev.bets_history")

    bet_id = UnicodeAttribute(hash_key=True)
    event_id = NumberAttribute()
    user_id = UnicodeAttribute()
    bet_amount = NumberAttribute()
    result = UnicodeAttribute()
    bet_processed = BooleanAttribute()
Beispiel #5
0
class AlermsModel(Model):
    class Meta:
        table_name = "Alerms"
        aws_access_key_id = env.AWS_ACCESS_KEY_ID
        aws_secret_access_key = env.AWS_SECRET_ACCESS_KEY
        region = env.AWS_REGION

    AlarmId = NumberAttribute(hash_key=True)
    LineUserId = NumberAttribute(null=False)
    AlermDateTime = UTCDateTimeAttribute(null=False)

    def __iter__(self):
        for name, attr in self.get_attributes().items():
            yield name, attr.serialize(getattr(self, name))
Beispiel #6
0
class BetModel(Model):
    """
    A bet identified by bet_id
    """
    class Meta:
        table_name = 'BetsTable'
        region = 'us-east-1'

    bet_id = UnicodeAttribute(hash_key=True)
    match_id = NumberAttribute()
    user_id = UnicodeAttribute()
    bet_amount = NumberAttribute()
    bet_for = UnicodeAttribute()
    bet_paid_status = NumberAttribute()
Beispiel #7
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)
Beispiel #8
0
class PredictionModel(Model):
    """DynamoDB table model relying on pynamodb."""
    class Meta:
        table_name = os.environ['DYNAMO_TABLE_NAME']
        region = os.environ['DYNAMO_REGION']
        write_capacity_units = os.environ['DYNAMO_WCU']
        read_capacity_units = os.environ['DYNAMO_RCU']

    id = UnicodeAttribute()
    age = NumberAttribute()
    gender = BooleanAttribute()
    osteoporosis = BooleanAttribute()
    work_activity_level = BooleanAttribute()
    tear_width = NumberAttribute()
    tear_retraction = NumberAttribute()
    full_thickness = BooleanAttribute()
    fatty_infiltration = BooleanAttribute()
    ip_address = UnicodeAttribute()
    date = UTCDateTimeAttribute()
    diebold_likelihood = NumberAttribute()
    kwon_likelihood = NumberAttribute()
    utah_likelihood = NumberAttribute()
    keener_likelihood = NumberAttribute()
    combined_likelihood = NumberAttribute()

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def mint(self, ip_address):
        self.id = str(uuid.uuid4())
        self.ip_address = ip_address
        self.date = datetime.datetime.now()
Beispiel #9
0
class Fixture(Model):

    class Meta:
        table_name = 'gfg-fixtures'
        region = Session().get_config_variable('region')

    fixture_id = NumberAttribute(hash_key=True)
    home = UnicodeAttribute()
    away = UnicodeAttribute()
    start_time = NumberAttribute()
    status = UnicodeAttribute()
    status_index = StatusIndex()

    result = Result(null=True)
    league = NumberAttribute()
Beispiel #10
0
class SupervillainModel(GenericModel):
    class Meta:
        table_name = 'd-supervillains'
        host = 'http://localhost:8000'

    columns = (
        ('id', UnicodeAttribute(hash_key=True)),
        ('active', BooleanAttribute()),
        ('name', UnicodeAttribute()),
        ('powers', UnicodeAttribute(null=True)),
        ('num_minions', NumberAttribute(null=True)),
        ('score', NumberAttribute(null=True)),
    )
    for column in columns:
        locals()[column[0]] = column[1]
Beispiel #11
0
class Restaurant(Model):  # inheritance
    class Meta:
        table_name = "Restaurant"
        region = 'us-east-1'

    resName = UnicodeAttribute(hash_key=True)
    city = UnicodeAttribute()
    category1 = UnicodeAttribute()
    category2 = UnicodeAttribute()
    rating = NumberAttribute()
    image_url = UnicodeAttribute()
    priceRange = UnicodeAttribute()
    address = UnicodeAttribute()
    lat = NumberAttribute()
    lng = NumberAttribute()
Beispiel #12
0
class ARecAttribute(MapAttribute):
    ref_count = NumberAttribute()
    function = ListAttribute(null=True)
    dynamic_chain = NumberAttribute(null=True)
    vmid = NumberAttribute(null=True)
    call_site = NumberAttribute(null=True)
    bindings = MapAttribute(default=dict)
    deleted = BooleanAttribute(default=False)

    def serialize(self, value):
        return super().serialize(value.serialise())

    def deserialize(self, value):
        return ActivationRecord.deserialise(
            super().deserialize(value).as_dict())
Beispiel #13
0
class SuperModel(Model):
  id_ = UnicodeAttribute(hash_key=True)
  data = SchemaAttribute(schemaUrl='https://gist.githubusercontent.com/thanakijwanavit/e2720d091ae0cef710a49b57c0c9cd4c/raw/ed2d322eac4900ee0f95b431d0f9067a40f3e0f0/squirrelOpenApiV0.0.3.yaml', null=True)
  lastEdited = NumberAttribute()
  creationTime = NumberAttribute()

  def __repr__(self):
    return json.dumps(vars(self)['attribute_values'])

  @classmethod
  def fromDict(cls, inputDict:dict):
    return cls(data = inputDict)

  #### saving ####
  def pullOutKeys(self):
    '''
    update the keys with data: please override this function by pulling out keys

    for example
    self.orderId = self.data['orderId']
    self.ownerId = self.data['ownerId']
    self.basketId = self.data['basketId']
    '''
    print('please dont foreget to override the pullOutKeys function if needed')
    self.id_ = self.data['id']

  def recordTime(self):
    '''record last edited and creation time'''
    self.lastEdited = datetime.now().timestamp() # record last edited
    if not self.creationTime: # record creation time
      self.creationTime = datetime.now().timestamp()

  def save(self):
    '''
    please override pullOutKeys function
    see docs
    '''
    self.recordTime()
    self.pullOutKeys()

    try:
      super().save()
      return next(self.query(self.id_))
    except ValidationError as e:
      raise ValidationError(f'failed validation \n {e}')

    except Exception as e:
      raise Exception(f'error saving id {self.id_} {e}')
Beispiel #14
0
class Photo(Model):
    """
    Photo table for DynamoDB
    """
    class Meta:
        table_name = 'Photo'
        region = AWS_REGION

    user_id = UnicodeAttribute(hash_key=True)
    id = UnicodeAttribute(range_key=True)
    tags = UnicodeAttribute(null=True)
    desc = UnicodeAttribute(null=True)
    filename_orig = UnicodeAttribute(null=True)
    filename = UnicodeAttribute(null=True)
    filesize = NumberAttribute(null=True)
    geotag_lat = UnicodeAttribute(null=True)
    geotag_lng = UnicodeAttribute(null=True)
    upload_date = UTCDateTimeAttribute(default=datetime.now(get_localzone()))
    taken_date = UTCDateTimeAttribute(null=True)
    make = UnicodeAttribute(null=True)
    model = UnicodeAttribute(null=True)
    width = UnicodeAttribute(null=True)
    height = UnicodeAttribute(null=True)
    city = UnicodeAttribute(null=True)
    nation = UnicodeAttribute(null=True)
    address = UnicodeAttribute(null=True)
Beispiel #15
0
class Sequence(Model):
    """
    シーケンスモデル
    各モデルのインクリメントidを管理する
    """
    class Meta:
        table_name = 'sequence'

        region = 'ap-northeast-1'
        aws_access_key_id = environ.get("AWS_ACCESS_KEY")
        aws_secret_access_key = environ.get("AWS_SECRETE_KEY")

    # スクレイピング対象のURL
    target_name = UnicodeAttribute(hash_key=True, null=False)
    current_id = NumberAttribute(null=False)

    @classmethod
    def next_sequence(cls, class_name):
        record = cls.create_or_increment(class_name)
        return record.current_id

    @classmethod
    def create_or_increment(cls, class_name):
        """ 対象のクラスのレコード数(id)を登録 """
        try:
            # 既に1レコード以上ある場合
            record = cls.get(class_name)
            record.update(actions=[cls.current_id.set(record.current_id + 1)])
            return record
        except cls.DoesNotExist:
            # レコードが無い場合
            record = cls(class_name, current_id=1)
            record.save()
            return record
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 BtcAddresses(Model):
    """
    Class representing information relevant to a single bitcoin wallet address.
    Each field is explained below

    address: wallet address (string)
    node_id: id of node associated with address (after initial clustering based in common inputs) (string)
    used_as_input: set of tx_hash objects where the address was used an input
        in corresponding transaction (set)
    used_as_output: set of tx_hash objects where the address was used as output
        in corresponding transaction (set)
    """
    class Meta:
        table_name = 'btc_addresses_bt'
        read_capacity_units = 25
        write_capacity_units = 50

    address = UnicodeAttribute(hash_key=True)

    addr_index = AddrIdentifierIndex()
    identifier = NumberAttribute()

    neighbor_addrs = JSONAttribute(default=json.dumps([]))
    used_as_input = JSONAttribute(default=json.dumps([]))
    used_as_output = JSONAttribute(default=json.dumps([]))
Beispiel #18
0
class SlipModel(Model):
    class Meta:
        table_name = os.environ['DYNAMODB_TABLE']  # just use table name here
        if 'ENV' in os.environ:
            host = 'http://localhost:8000'
        else:
            region = 'us-east-1'
            # change to own link
            # host = 'https://dynamodb.eu-central-1.amazonaws.com'

    id = UnicodeAttribute(hash_key=True, null=False)
    number = NumberAttribute(hash_key=False, range_key=False, null=None)
    current_boat = UnicodeAttribute(hash_key=False, null=True)
    arrival_date = UnicodeAttribute(hash_key=False, null=True)
    e_type = EntityType()
    entity_type = UnicodeAttribute(default='slip')

    # departure_history - EC. Do later

    def save(self, conditional_operator=None, **expected_values):
        self.updatedAt = datetime.now()
        super(SlipModel, self).save()

    def __iter__(self):
        for name, attr in self._get_attributes().items():
            yield name, attr.serialize(getattr(self, name))
Beispiel #19
0
class ViewIndex(GlobalSecondaryIndex):
    class Meta:
        read_capacity_units = 2
        write_capacity_units = 1
        projection = AllProjection()

    view = NumberAttribute(default=0, hash_key=True)
Beispiel #20
0
class AppUser(Model):
    """
    A user of the Hubmetrix app
    """
    class Meta:
        table_name = 'hubmetrix-user'
        region = 'us-west-1'

    bc_store_hash = UnicodeAttribute(hash_key=True)
    bc_email = UnicodeAttribute()
    bc_id = NumberAttribute(range_key=True)
    bc_store_id = UnicodeAttribute(null=True)
    bc_access_token = UnicodeAttribute()
    bc_scope = UnicodeAttribute()
    bc_webhooks_registered = BooleanAttribute(default=False)
    bc_webhook_ids = ListAttribute(null=True)
    bc_deleted = BooleanAttribute(default=False)
    hs_refresh_token = UnicodeAttribute(null=True)
    hs_access_token = UnicodeAttribute(null=True)
    hs_expires_in = UnicodeAttribute(null=True)
    hs_app_id = UnicodeAttribute(null=True)
    hs_hub_domain = UnicodeAttribute(null=True)
    hs_hub_id = UnicodeAttribute(null=True)
    hs_token_type = UnicodeAttribute(null=True)
    hs_user = UnicodeAttribute(null=True)
    hs_user_id = UnicodeAttribute(null=True)
    hs_scopes = ListAttribute(null=True)
    hs_properties_exist = BooleanAttribute(default=False)
    hs_access_token_timestamp = UnicodeAttribute(null=True)
    cb_subscription_id = UnicodeAttribute(null=True)
    hm_last_sync_timestamp = UnicodeAttribute(null=True)
Beispiel #21
0
class Guest(Model, UserMixin):
    class Meta:
        table_name = os.getenv("DYNAMO_TABLE")
        region = os.getenv("AWS_REGION")
        host = os.getenv("AWS_ENDPOINT_URL", None)

    id = UUIDAttribute(hash_key=True, default=uuid.uuid4)
    email = UnicodeAttribute()
    name = UnicodeAttribute()
    food_allergies = UnicodeAttribute(null=True)
    favourite_music = UnicodeAttribute(null=True)
    last_viewed = UTCDateTimeAttribute(null=True)
    last_responded = UTCDateTimeAttribute(null=True)
    number_of_guests = NumberAttribute(default=0)
    notes = UnicodeAttribute(null=True)
    will_attend = BooleanAttribute(null=True)
    filled_by_admin = BooleanAttribute(null=True)

    def get_id(self):
        return str(self.id)

    @staticmethod
    def find(guest_id: str):
        try:
            return Guest.get(guest_id)
        except DoesNotExist:
            return None

    @staticmethod
    def find_multi_id(guest_ids: list) -> iter:
        try:
            return Guest.batch_get(guest_ids)
        except DoesNotExist:
            return []
Beispiel #22
0
 def test_number_serialize(self):
     """
     NumberAttribute.serialize
     """
     attr = NumberAttribute()
     self.assertEqual(attr.serialize(3.141), '3.141')
     self.assertEqual(attr.serialize(1), '1')
Beispiel #23
0
class BoatModel(Model):
    class Meta:
        table_name = os.environ['DYNAMODB_TABLE']
        if 'ENV' in os.environ:
            host = 'http://localhost:8000'
        else:
            region = 'us-east-1'

    id = UnicodeAttribute(hash_key=True, null=False)
    name = UnicodeAttribute(null=False)
    type = UnicodeAttribute(hash_key=False, null=False)
    length = NumberAttribute(hash_key=False,
                             range_key=False,
                             null=None,
                             default=0)
    at_sea = BooleanAttribute(hash_key=False,
                              range_key=False,
                              null=None,
                              default=True)
    e_type = EntityType()
    entity_type = UnicodeAttribute(default='boat')

    def save(self, conditional_operator=None, **expected_values):
        self.updatedAt = datetime.now()
        super(BoatModel, self).save()

    def __iter__(self):
        for name, attr in self._get_attributes().items():
            yield name, attr.serialize(getattr(self, name))
Beispiel #24
0
class Movie(Model):
    class Meta:
        table_name = 'movies'
        host = "http://localhost:8000"
        write_capacity_units = 5
        read_capacity_units = 25

    title = UnicodeAttribute(hash_key=True)
    format = UnicodeAttribute()
    length = NumberAttribute()
    release_year = NumberAttribute(range_key=True)
    rating = NumberAttribute()

    def __iter__(self):
        for name, attr in self._get_attributes().items():
            yield name, attr.serialize(getattr(self, name))
Beispiel #25
0
class TrackMap(MapAttribute):
    id = UnicodeAttribute()
    name = UnicodeAttribute()
    album_id = UnicodeAttribute()
    album_name = UnicodeAttribute()
    artist_id = UnicodeAttribute()
    artist_name = UnicodeAttribute()
    duration = NumberAttribute()
    listeners = NumberAttribute()
    playback_date = UnicodeAttribute()
    playcount = UnicodeAttribute()
    reproduction = NumberAttribute()
    total_tracks = NumberAttribute()
    tags = ListAttribute()
    genres = ListAttribute()
    release_date = UnicodeAttribute()
Beispiel #26
0
class StringNumberIndex(GlobalSecondaryIndex):

    string = UnicodeAttribute(hash_key=True)
    number = NumberAttribute(range_key=True)

    class Meta:
        projection = AllProjection()
Beispiel #27
0
 def test_number_deserialize(self):
     """
     NumberAttribute.deserialize
     """
     attr = NumberAttribute()
     self.assertEqual(attr.deserialize('1'), 1)
     self.assertEqual(attr.deserialize('3.141'), 3.141)
class Thread(Model):
    class Meta:
        table_name = 'Thread'

    forum_name = UnicodeAttribute(hash_key=True)
    subject = UnicodeAttribute(range_key=True)
    views = NumberAttribute(default=0)
Beispiel #29
0
class Subscription(Model):
    class Meta:
        table_name = 'unimapa_subscription'

    map_id = NumberAttribute(hash_key=True)
    username = UnicodeAttribute(range_key=True)
    subscription_time = UTCDateTimeAttribute()
Beispiel #30
0
class BaseTestModel(BaseModel):
    __update_action_hooks__ = {
        'set': {
            'non_key_value': 'test_hook_action_generation'
        }
    }

    class Meta(BaseMeta):
        table_name = 'base'

    def __init__(self, hash_key=None, range_key=None, **attributes):
        self.assign_or_update('update_action_hooks',
                              __class__.__update_action_hooks__)
        super().__init__(hash_key, range_key, **attributes)

    hash_key = UnicodeAttribute(hash_key=True)
    range_key = UnicodeAttribute(range_key=True)
    hook_attribute = UnicodeAttribute(null=True)
    list_attribute = ListAttribute(default=list())
    non_key_value = UnicodeAttribute()
    numeric_value = NumberAttribute(null=True)
    unicode_set = UnicodeSetAttribute(default=set())

    def test_hook_action_generation(self, value):
        return [BaseTestModel.hook_attribute.set(value)]