class IdeaModel(BaseModel): class Meta: table_name = IDEAS_TABLE_NAME write_capacity_units = 100 read_capacity_units = 100 # host = "http://localhost:4569" ideaId = UnicodeAttribute(range_key=True) sortKey = UnicodeAttribute(default='idea') userId = UnicodeAttribute(hash_key=True) content = UnicodeAttribute(null=True) title = UnicodeAttribute(null=True) slug = UnicodeAttribute(null=True) shortId = UnicodeAttribute(null=True) visibility = UnicodeAttribute(null=True) authorName = UnicodeAttribute(null=True) authorSlug = UnicodeAttribute(null=True) authorAvatar = UnicodeAttribute(null=True) createdDate = UTCDateTimeAttribute(null=True) ideaDate = UTCDateTimeAttribute(null=True) likesCount = NumberAttribute(default=0) commentsCount = NumberAttribute(default=0) usersIdeasByDateIndex = UsersIdeasByDateIndex() ideasByIdIndex = IdeasById()
class PermitClass(Model): class Meta: table_name = dynamo_table read_capacity_units = 5 write_capacity_units = 5 region = db_region host = db_host record_id = NumberAttribute(range_key=True) application_number = UnicodeAttribute(hash_key=True) status = UnicodeAttribute(default='issued') status_date = UTCDateTimeAttribute(null=True) file_date = UTCDateTimeAttribute(null=True) expiration_date = UTCDateTimeAttribute(null=True) estimated_cost = NumberAttribute(default=0, null=True) revised_cost = NumberAttribute(default=0, null=True) existing_use = UnicodeAttribute(null=True) proposed_use = UnicodeAttribute(null=True) description = UnicodeAttribute(null=True) address = UnicodeAttribute(null=True) # load_date = UTCDateTimeAttribute(null=True) # Unresolved Attribute reference caused by dated pynamodb library: # try method _get_attributes def __iter__(self): for name, attr in self._get_attributes().items(): yield name, attr.serialize(getattr(self, name))
class FavouritesTaxiStandModel(Model): class Meta: table_name = "SingaporeTaxiApiFavouritesTaxiStand" region = "ap-southeast-1" host = "https://dynamodb.ap-southeast-1.amazonaws.com" id = UnicodeAttribute(hash_key=True, null=False) taxiCode = UnicodeAttribute(null=False) latitude = NumberAttribute(null=False, default=0) longitude = NumberAttribute(null=False, default=0) bfa = UnicodeAttribute(null=False) ownership = UnicodeAttribute(null=False) type = UnicodeAttribute(null=False) name = UnicodeAttribute(null=False) userId = UnicodeAttribute(null=False) createdAt = UTCDateTimeAttribute(null=False, default=datetime.now()) updatedAt = UTCDateTimeAttribute(null=False, default=datetime.now()) def save(self, conditional_operator=None, **expected_values): self.updatedAt = datetime.now() super(FavouritesTaxiStandModel, self).save() def __iter__(self): for name, attr in self._get_attributes().items(): yield name, attr.serialize(getattr(self, name))
def test_utc_date_time_serialize(self): """ UTCDateTimeAttribute.serialize """ tstamp = datetime.now() attr = UTCDateTimeAttribute() assert attr.serialize(tstamp) == tstamp.replace(tzinfo=timezone.utc).strftime(DATETIME_FORMAT)
def test_utc_date_time_deserialize(self): """ UTCDateTimeAttribute.deserialize """ tstamp = Delorean(timezone=UTC).datetime attr = UTCDateTimeAttribute() self.assertEqual(tstamp, attr.deserialize(Delorean(tstamp, timezone=UTC).datetime.strftime(DATETIME_FORMAT)))
class AgendaModel(Model): class Meta: table_name = os.environ['DYNAMODB_TABLE'] if 'ENV' in os.environ: host = 'http://localhost:8000' else: region = 'us-west-2' host = 'https://dynamodb.us-west-2.amazonaws.com' agenda_id = UnicodeAttribute(hash_key=True, null=False) paciente_id = UnicodeAttribute(range_key=True, null=False) tipo_evento = UnicodeAttribute(null=False) fecha_evento = UnicodeAttribute(null=False) status = BooleanAttribute(null=False, default=True) create_at = UTCDateTimeAttribute(null=False, default=datetime.now()) updated_at = UTCDateTimeAttribute(null=False) def save(self, conditional_operator=None, **expected_values): self.updated_at = datetime.now() super(AgendaModel, self).save() def __iter__(self): for name, attr in self._get_attributes().items(): yield name, attr.serialize(getattr(self, name))
def test_utc_date_time_deserialize(self): """ UTCDateTimeAttribute.deserialize """ tstamp = datetime.now(UTC) attr = UTCDateTimeAttribute() assert attr.deserialize(tstamp.strftime(DATETIME_FORMAT)) == tstamp
def test_dateutil_parser_fallback(self): """ UTCDateTimeAttribute.deserialize """ expected_value = datetime(2047, 1, 6, 8, 21, tzinfo=tzutc()) attr = UTCDateTimeAttribute() assert attr.deserialize('January 6, 2047 at 8:21:00AM UTC') == expected_value
class User(Model): class Meta: table_name = settings.DYNAMODB_TABLE_USER if settings.DYNAMODB_URL: host = settings.DYNAMODB_URL user_id = UnicodeAttribute(hash_key=True) venmo_handle = UnicodeAttribute() created_date = UTCDateTimeAttribute() modified_date = UTCDateTimeAttribute() @classmethod def get_all_paged(cls, next_page: Optional[str] = None, limit: Optional[int] = None) -> Iterator['User']: last_evaluated_key = cls.format_last_evaluated_key(next_page) return cls.scan(limit=limit, last_evaluated_key=last_evaluated_key) @classmethod def format_last_evaluated_key(cls, user_id: Optional[str]) -> Optional[dict]: if user_id is None: return None return {'user_id': {'S': user_id}} def save(self, *args, **kwargs) -> Dict[str, Any]: if not self.created_date: self.created_date = datetime.utcnow() self.modified_date = datetime.utcnow() return super(User, self).save(*args, **kwargs)
class UserModel(Model): """ A DynamoDB User """ class Meta: table_name = os.environ.get("DYNAMODB_USER_TABLE") region = os.environ.get("REGION") if os.environ.get('DYNAMODB_HOST') is not None: host = os.environ.get("DYNAMODB_HOST") id = UnicodeAttribute(hash_key=True) email = UnicodeAttribute() given_name = UnicodeAttribute() username = UnicodeAttribute() createdAt = UTCDateTimeAttribute(null=False, default=datetime.utcnow) updatedAt = UTCDateTimeAttribute(null=False, default=datetime.utcnow) @classmethod def create(cls, event): return UserModel( event.get('request').get('userAttributes').get('sub'), given_name=event.get('request').get('userAttributes').get( 'given_name'), email=event.get('request').get('userAttributes').get('email'), username=event.get('userName'))
class DEV_GPS_LOCATION(Model): class Meta: aws_access_key_id = 'AKIAJXFC3JRVYNIHX2UA' aws_secret_access_key = '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' table_name = "DEV_GPS_LOCATION" region = 'ap-south-1' write_capacity_units = 2 read_capacity_units = 2 created_by = UnicodeAttribute(null=True) changed_by = UnicodeAttribute(null=True) gps_device_provider = UnicodeAttribute(null=True) created_on = UTCDateTimeAttribute(default=datetime.now()) updated_on = UTCDateTimeAttribute(default=datetime.now()) deleted = BooleanAttribute(default=False) deleted_on = UTCDateTimeAttribute(null=True) device_id = UnicodeAttribute(hash_key=True) imei = UnicodeAttribute(null=True) driver_name = UnicodeAttribute(null=True) driver_number = UnicodeAttribute(null=True) driving_licence_number = UnicodeAttribute(null=True) vehicle_number = UnicodeAttribute(null=True) vehicle_type = UnicodeAttribute(null=True) vehicle_status = UnicodeAttribute(null=True) location_time = UTCDateTimeAttribute(null=True) latitude = NumberAttribute(attr_name='latitude') longitude = NumberAttribute(attr_name='longitude') address = UnicodeAttribute(null=True) is_active = BooleanAttribute(default=True)
class BaseModel(Model): class Meta: if os.environ.get("IS_OFFLINE") == "true": host = "http://localhost:8000" def to_json(self, indent=2): return json.dumps(self.to_dict(), indent=indent) def to_dict(self): ret_dict = {} for name, attr in self.attribute_values.items(): ret_dict[name] = self._attr2obj(attr) return ret_dict def _attr2obj(self, attr): if isinstance(attr, list): _list = [] for _attr in attr: _list.append(self._attr2obj(_attr)) return _list elif isinstance(attr, MapAttribute): _dict = {} for k, v in attr.attribute_values.items(): _dict[k] = self._attr2obj(v) return _dict elif isinstance(attr, Enum): return attr.name.lower() elif isinstance(attr, datetime): return attr.isoformat() else: return attr createdAt = UTCDateTimeAttribute(default=datetime.utcnow) updatedAt = UTCDateTimeAttribute(default=datetime.utcnow)
def test_utc_date_time_serialize(self): """ UTCDateTimeAttribute.serialize """ tstamp = datetime.now() attr = UTCDateTimeAttribute() self.assertEqual(attr.serialize(tstamp), tstamp.replace(tzinfo=UTC).strftime(DATETIME_FORMAT))
class Server(Model): class Meta: if os.getenv("STAGE", "PRODUCTION") == "TESTING": host = "http://dynamodb:8000" table_name = "server" billing_mode = "PAY_PER_REQUEST" region = "us-west-2" server_index = ServerIndex() address = UnicodeAttribute(hash_key=True) game = UnicodeAttribute() protocol = NumberAttribute(default=0) status = JSONAttribute() players = JSONAttribute() player_count = NumberAttribute(default=0) active = BooleanAttribute(default=True) scraped = BooleanAttribute(default=False) first_seen = UTCDateTimeAttribute(default=datetime.utcnow()) last_seen = UTCDateTimeAttribute(default=datetime.utcnow()) country_code = UnicodeAttribute(default="ZZ")
class PostModel(Model): """ A DynamoDB post table """ id = UnicodeAttribute(hash_key=True) user_sub = UnicodeAttribute() post = UnicodeAttribute() title = UnicodeAttribute() status = UnicodeAttribute() createdAt = UTCDateTimeAttribute(null=False, default=datetime.utcnow) updatedAt = UTCDateTimeAttribute(null=False, default=datetime.utcnow) class Meta: table_name = os.environ.get("DYNAMODB_POST_TABLE") region = os.environ.get("REGION") if os.environ.get('DYNAMODB_HOST') is not None: host = os.environ.get("DYNAMODB_HOST") @classmethod @validate_post_request def create(cls, user_sub, data): return PostModel(str(uuid.uuid4()), user_sub=user_sub, post=data.get('post'), title=data.get('title'), status=data.get('status'))
class TodoModel(Model): class Meta: read_capacity_units = 1 write_capacity_units = 1 region = 'localhost' host = 'http://localhost:8000' todo_id = UnicodeAttribute(hash_key=True, null=False) text = UnicodeAttribute(null=False) checked = BooleanAttribute(null=False, default=False) created_at = UTCDateTimeAttribute(null=False, default=datetime.utcnow) updated_at = UTCDateTimeAttribute(null=False) def save(self, conditional_operator=None, **expected_values): self.updated_at = datetime.utcnow() super().save() def __iter__(self): for name, attr in self._get_attributes().items(): yield name, attr.serialize(getattr(self, name)) @staticmethod def setup_model(model, region, table_name, is_remote=False): model.Meta.table_name = table_name model.Meta.region = region if is_remote: model.Meta.host = 'https://dynamodb.{0}.amazonaws.com'.format(region)
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 []
class PermitClass(Model): class Meta: table_name = dynamo_table read_capacity_units = 5 write_capacity_units = 5 region = db_region host = db_host # set the partion key indicated by 'hash_key=True' application_number = UnicodeAttribute(hash_key=True) # set the sort key indicated by 'range_key=True' record_id = NumberAttribute(range_key=True) # define other expected attributes # UTCDateTimeAttribute into a UnicodeAttribute status = UnicodeAttribute(default='issued') status_date = UTCDateTimeAttribute(null=True) file_date = UTCDateTimeAttribute(null=True) expiration_date = UTCDateTimeAttribute(null=True) estimated_cost = NumberAttribute(default=0, null=True) revised_cost = NumberAttribute(default=0, null=True) existing_use = UnicodeAttribute(null=True) proposed_use = UnicodeAttribute(null=True) description = UnicodeAttribute(null=True) address = UnicodeAttribute(null=True)
class CustomerModel(BaseModel): class Meta: table_name = os.environ['DYNAMODB_TABLE'] if 'ENV' in os.environ: host = 'http://localhost:8000' else: region = aws_region host = f'https://dynamodb.{aws_region}.amazonaws.com' id = UnicodeAttribute(hash_key=True, null=False) first_name = UnicodeAttribute(null=False) last_name = UnicodeAttribute(null=False) # address = Address() version = NumberAttribute(null=False) email_addr = UnicodeAttribute(null=False) phone_number = UnicodeAttribute(null=True) createdAt = UTCDateTimeAttribute(null=False, default=datetime.now()) updatedAt = UTCDateTimeAttribute(null=False) def save(self, conditional_operator=None, **expected_values): version = self.version if version is None: self.version = 1 else: self.version = int(self.version) + 1 self.updatedAt = datetime.now() super(CustomerModel, self).save()
def test_utc_date_time_serialize(self): """ UTCDateTimeAttribute.serialize """ tstamp = datetime.now() attr = UTCDateTimeAttribute() assert attr.serialize(tstamp) == tstamp.replace(tzinfo=UTC).strftime(DATETIME_FORMAT)
def test_utc_date_time_serialize(self): """ UTCDateTimeAttribute.serialize """ tstamp = datetime.now() attr = UTCDateTimeAttribute() self.assertEqual(attr.serialize(tstamp), Delorean(tstamp, timezone=UTC).datetime.strftime(DATETIME_FORMAT))
class UserModel(Model): class Meta: table_name = USERS_TABLE_NAME write_capacity_units = 100 read_capacity_units = 100 # host = "http://localhost:4569" userId = UnicodeAttribute(hash_key=True) email = UnicodeAttribute(null=True) name = UnicodeAttribute(null=True) slug = UnicodeAttribute(null=True) avatar = UnicodeAttribute(null=True) ideasMailSchedule = UnicodeAttribute(null=True, default="1,2,3,4,5,6,7") lastRequestedIdeaDate = UTCDateTimeAttribute(null=True) # sortKey = UnicodeAttribute(range_key=True) createdDate = UTCDateTimeAttribute(null=True) firstLogin = BooleanAttribute(null=True) ideaReminders = BooleanAttribute(null=True) hotStreaks = BooleanAttribute(null=True) dailyDigests = BooleanAttribute(null=True) weeklyDigests = BooleanAttribute(null=True) snoozeEmails = UTCDateTimeAttribute(null=True) unsubscribedAt = UTCDateTimeAttribute(null=True) emailToken = UnicodeAttribute(null=True) followersCount = NumberAttribute(default=0) ideasCreated = NumberAttribute(default=0) followeesCount = NumberAttribute(default=0) emailIndex = UserEmailIndex()
class DatasetsModelBase(Model): """Dataset model.""" id = UnicodeAttribute( hash_key=True, attr_name="pk", default_for_new=lambda: f"{DATASET_ID_PREFIX}{new()}", ) title = UnicodeAttribute() created_at = UTCDateTimeAttribute(default_for_new=now) updated_at = UTCDateTimeAttribute(default=now) datasets_title_idx: DatasetsTitleIdx def as_dict(self) -> Dict[str, Any]: serialized = self.serialize() result: Dict[str, Any] = { key: value["S"] for key, value in serialized.items() } result["id"] = self.dataset_id return result @property def dataset_id(self) -> str: """Dataset ID value.""" return str(self.id).split(DB_KEY_SEPARATOR)[1] @property def dataset_prefix(self) -> str: """Dataset prefix value.""" return f"{self.title}{DATASET_KEY_SEPARATOR}{self.dataset_id}"
class DatasetsModelBase(Model): """Dataset model.""" id = UnicodeAttribute( hash_key=True, attr_name="pk", default_for_new=lambda: f"DATASET#{human_readable_ulid(new())}", ) title = UnicodeAttribute() created_at = UTCDateTimeAttribute(default_for_new=now) updated_at = UTCDateTimeAttribute(default=now) datasets_title_idx: DatasetsTitleIdx def as_dict(self) -> Dict[str, Any]: serialized = self.serialize() result: Dict[str, Any] = { key: value["S"] for key, value in serialized.items() } result["id"] = self.dataset_id return result @property def dataset_id(self) -> str: """Dataset ID value.""" return str(self.id).split("#")[1]
class Photo(Model): """ User table for DynamoDB """ class Meta: table_name = 'Photo' region = conf['AWS_REGION'] user_id = UnicodeAttribute(hash_key=True) id = NumberAttribute(range_key=True) tags = UnicodeAttribute(null=False) desc = UnicodeAttribute(null=False) filename_orig = UnicodeAttribute(null=False) filename = UnicodeAttribute(null=False) filesize = NumberAttribute(null=False) geotag_lat = UnicodeAttribute(null=False) geotag_lng = UnicodeAttribute(null=False) upload_date = UTCDateTimeAttribute(default=util.the_time_now()) taken_date = UTCDateTimeAttribute(default=util.the_time_now()) make = UnicodeAttribute(null=True) model = UnicodeAttribute(null=True) width = UnicodeAttribute(null=False) height = UnicodeAttribute(null=False) city = UnicodeAttribute(null=True) nation = UnicodeAttribute(null=False) address = UnicodeAttribute(null=False)
class MensajeModel(Model): class Meta: table_name = os.environ['DYNAMODB_MENSAJE'] if 'ENV' in os.environ: host = 'http://localhost:8000' else: region = 'us-west-2' host = 'https://dynamodb.us-west-2.amazonaws.com' mensaje_id = UnicodeAttribute(hash_key=True, null=False) nombre = UnicodeAttribute(null=False) correo = UnicodeAttribute(null=False) asunto = UnicodeAttribute(null=False) cuerpo = UnicodeAttribute(null=False) leido = BooleanAttribute(null=False, default=False) create_at = UTCDateTimeAttribute(null=False, default=datetime.now()) updated_at = UTCDateTimeAttribute(null=False) def save(self, conditional_operator=None, **expected_values): self.updated_at = datetime.now() super(MensajeModel, self).save() def __iter__(self): for name, attr in self._get_attributes().items(): yield name, attr.serialize(getattr(self, name))
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)
class Update(Model): class Meta: table_name = "updates" region = "us-east-2" url = UnicodeAttribute(hash_key=True) last_checked = UTCDateTimeAttribute(null=True) last_updated = UTCDateTimeAttribute(null=True)
class ScheduleAttribute(MapAttribute): account_ID = NumberAttribute(of=AccountAttribute) schedule_date = UTCDateTimeAttribute(null=False) schedule_time = UTCDateTimeAttribute(null=True) calories_burned = NumberAttribute(null=True) calories_intake = NumberAttribute(null=True) calories_difference = NumberAttribute(null=True) ingestion = UnicodeAttribute(null=True) training_memory = UnicodeAttribute(null=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 PynamoTestModel(Model): class Meta: table_name = 'pynamo_tests' test_id = UnicodeAttribute(hash_key=True) list = ListAttribute() description = UnicodeAttribute() start_time = UTCDateTimeAttribute() end_time = UTCDateTimeAttribute(null=True)
def test_utc_datetime_attribute(self): """ UTCDateTimeAttribute.default """ attr = UTCDateTimeAttribute() assert attr is not None assert attr.attr_type == STRING tstamp = datetime.now() attr = UTCDateTimeAttribute(default=tstamp) assert attr.default == tstamp
def test_utc_date_time_deserialize_parse_args(self): """ UTCDateTimeAttribute.deserialize """ tstamp = Delorean(timezone=UTC).datetime attr = UTCDateTimeAttribute() with patch('pynamodb.attributes.parse') as parse: attr.deserialize(Delorean(tstamp, timezone=UTC).datetime.strftime(DATETIME_FORMAT)) parse.assert_called_with(tstamp.strftime(DATETIME_FORMAT), dayfirst=False)
def test_utc_date_time_deserialize_parse_args(self, parse_mock, datetime_mock): """ UTCDateTimeAttribute.deserialize """ tstamp = datetime.now(UTC) attr = UTCDateTimeAttribute() tstamp_str = tstamp.strftime(DATETIME_FORMAT) attr.deserialize(tstamp_str) parse_mock.assert_not_called() datetime_mock.strptime.assert_called_once_with(tstamp_str, DATETIME_FORMAT)