class TestModel(Model): attr_1 = StringAttribute(hash_key=True) attr_2 = StringAttribute(range_key=True) attr_3 = StringAttribute() class TestIndex(AllIndex): hash_key = 'attr_1' range_key = 'attr_3'
class TestModel(Model): attr_1 = StringAttribute(hash_key=True) attr_2 = StringAttribute() class TestIndex(GlobalAllIndex): read_throughput = 5 write_throughput = 5 hash_key = 'attr_2' range_key = 'attr_1'
class Message(Model): channel = StringAttribute(hash_key=True) published_at = NumberAttribute(range_key=True) type = StringAttribute() writer = StringAttribute() message = StringAttribute() def to_dict(self): return dict(type=self.type, writer=self.writer, published_at=float(self.published_at), message=self.message, channel=self.channel)
class ChannelJoinInfo(Model): channel = StringAttribute(hash_key=True) user_id = StringAttribute(range_key=True) joined_at = NumberAttribute(default=time.time) last_sent_at = NumberAttribute(default=time.time) last_read_at = NumberAttribute(default=time.time) class UserIndex(GlobalAllIndex): hash_key = 'user_id' range_key = 'channel' read_throughput = 1 write_throughput = 1 @property def partners_last_read_at(self): partners = self.get_partners(self.channel, self.user_id) if Channel.get_item(self.channel).is_group_chat: return dict((join_info.user_id, join_info.last_read_at) for join_info in partners) else: return partners[0].last_read_at @classmethod def by_channel(cls, channel_name): return cls.query(channel__eq=str(channel_name)) @classmethod def by_user(cls, user_id): return cls.query('UserIndex', user_id__eq=str(user_id)) @classmethod def get_partners(cls, channel_name, user_id): return [ join_info for join_info in cls.by_channel(channel_name) if join_info.user_id != user_id ]
class Channel(Model): name = StringAttribute(hash_key=True) is_group_chat = BooleanAttribute(default=False) @classmethod def create_channel(cls, user_ids, is_group_chat=False): channel = cls.put_item(name=str(uuid1()), is_group_chat=is_group_chat) join_infos = [] for user_id in user_ids: join_infos.append( ChannelJoinInfo.put_item( channel=channel.name, user_id=str(user_id), )) return channel, join_infos
def test_string_attribute_validation(): assert StringAttribute.valid('1234') assert StringAttribute.valid(u'1234') assert not StringAttribute.valid(1234)
class TestModel(Model): hash_key_attr = StringAttribute(hash_key=True) range_key_attr = StringAttribute(range_key=True) attr_1 = StringAttribute()
class TestModel(Model): hash_key = StringAttribute(hash_key=True) list_attr = ListAttribute(default=[]) map_attr = MapAttribute(default={})
class TestModel(Model): hash_key = StringAttribute(hash_key=True) attr = NumberAttribute()
class TestModel(Model): hash_key = StringAttribute(hash_key=True) attr = StringSetAttribute(default=set())
class QueryTestModel(Model): published_at = StringAttribute(hash_key=True) title = StringAttribute(range_key=True)
class TestModelWithNullable(Model): hash_key = StringAttribute(hash_key=True) attr = StringAttribute(null=True)
class TestModelDefault(Model): hash_key = StringAttribute(hash_key=True) attr = StringAttribute(default='Default value')
class ChannelUsageLog(Model): date = StringAttribute(hash_key=True) channel = StringAttribute(range_key=True) last_published_at = NumberAttribute()
class ChannelWithdrawalLog(Model): channel = StringAttribute(hash_key=True) user_id = StringAttribute(range_key=True) joined_at = NumberAttribute() last_read_at = NumberAttribute() withdrawal_at = NumberAttribute(default=time.time)