class Inbox(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='inbox_user') second = models.ForeignKey(User, on_delete=models.CASCADE, related_name='inbox_second_user') updated = models.DateTimeField(auto_now=True) timestamp = models.DateTimeField(auto_now_add=True) last_message = fields.EncryptedTextField(blank=True, ) last_message_from = models.ForeignKey(User, on_delete=models.SET_NULL, related_name='last_message_user', blank=True, null=True) read = models.BooleanField(default=False) tracker = FieldTracker() objects = InboxManager() class Meta: unique_together = ['user', 'second'] verbose_name = 'User Inbox' verbose_name_plural = 'User Inbox' def __str__(self): return f"{self.user} - {self.second} Inbox - ID: {self.pk}"
class BlockchainAccount(models.Model): network_type = models.CharField(max_length=50, choices=NetworkType.choices) address = models.CharField(max_length=100, primary_key=True) mnemonic = fields.EncryptedTextField(default='') _private_key = fields.EncryptedTextField(default='') @property def private_key(self): if self.mnemonic and self.network_type == NetworkType.ETHEREUM_LIKE: hd_wallet = BIP44HDWallet(symbol=ETH, account=0, change=False, address=0) hd_wallet.from_mnemonic(mnemonic=self.mnemonic) return hd_wallet.private_key() else: return self._private_key @private_key.setter def private_key(self, value): self._private_key = value
class Token(models.Model): dex = models.ForeignKey('tokens.Dex', on_delete=models.CASCADE, related_name='tokens') token_address = models.CharField(max_length=100) token_abi = models.JSONField(null=True, default=None) swap_address = models.CharField(max_length=100) swap_owner = models.CharField(max_length=100, default='') swap_abi = models.JSONField(null=True, default=None) swap_secret = fields.EncryptedTextField(default='') # private key for Ethereum-like, mnemonic for Binance-Chain fee_address = models.CharField(max_length=100) fee = models.IntegerField() decimals = models.IntegerField() symbol = models.CharField(max_length=50) network = models.CharField(max_length=100) is_original = models.BooleanField(default=False)
class Comments(models.Model): author = models.ForeignKey(to="User", related_name="comments", on_delete=models.CASCADE) comment = fields.EncryptedTextField() likes = models.ManyToManyField(to="User", related_name="comment_liker", blank=True) content = models.ForeignKey(to="Content", related_name="commented_stuff", on_delete=models.CASCADE) created_at = fields.EncryptedDateTimeField(default=timezone.now) updated_at = fields.EncryptedDateTimeField(default=timezone.now) def __str__(self): return self.comment
class ChatMessage(models.Model): thread = models.ForeignKey(Thread, null=True, blank=True, on_delete=models.SET_NULL) user = models.ForeignKey(User, verbose_name='sender', related_name='message_sender', on_delete=models.CASCADE) receiver = models.ForeignKey(User, related_name='message_receiver', verbose_name='receiver', on_delete=models.CASCADE) inbox = models.ManyToManyField(to=Inbox, blank=True) message = fields.EncryptedTextField() timestamp = models.DateTimeField(auto_now_add=True) attachment = models.ForeignKey(to=Attachment, on_delete=models.SET_NULL, null=True, blank=True)
class Token(models.Model): dex = models.ForeignKey('tokens.Dex', on_delete=models.CASCADE, related_name='tokens') token_address = models.CharField(max_length=100, default='', blank=True) token_abi = models.JSONField(blank=True, null=True, default=None) swap_address = models.CharField(max_length=100) swap_owner = models.CharField(max_length=100, default='', blank=True) swap_abi = models.JSONField(blank=True, null=True, default=None) swap_secret = fields.EncryptedTextField( default='', blank=True ) # private key for Ethereum-like, mnemonic for Binance-Chain fee_address = models.CharField(max_length=100) fee = models.IntegerField() decimals = models.IntegerField() symbol = models.CharField(max_length=50) network = models.CharField(max_length=100) is_original = models.BooleanField(default=False) def execute_swap_contract_function(self, func_name, *args): network = NETWORKS[self.network] w3 = Web3(HTTPProvider(network['node'])) tx_params = { 'nonce': w3.eth.getTransactionCount(self.swap_owner, 'pending'), 'gasPrice': w3.eth.gasPrice, 'gas': GAS_LIMIT, } contract = w3.eth.contract(address=self.swap_address, abi=self.swap_abi) func = getattr(contract.functions, func_name)(*args) initial_tx = func.buildTransaction(tx_params) signed_tx = w3.eth.account.signTransaction(initial_tx, self.swap_secret) tx_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction) tx_hex = tx_hash.hex() return tx_hex
class Content(models.Model): member = models.ForeignKey(to="User", related_name="poster", on_delete=models.CASCADE) author = models.CharField(max_length=255, blank=True, null=True) circle = models.ForeignKey(to="Circle", related_name="posts", on_delete=models.CASCADE) text_post = fields.EncryptedTextField(blank=True, null=True) img_post = models.ImageField(upload_to='images', blank=True, null=True) caption = fields.EncryptedCharField(max_length=200, blank=True, null=True) likes = models.ManyToManyField(to="User", related_name="content_liker", blank=True) created_at = fields.EncryptedDateTimeField(default=timezone.now) updated_at = fields.EncryptedDateTimeField(default=timezone.now) tags = models.ForeignKey(to="User", related_name="tagged", on_delete=models.CASCADE, blank=True, null=True) def __str__(self): return self.text_post
def test_key_from_settings_error(self, settings): """settings.FIELD_ENCRYPTION_KEYS should be list or tuple.""" settings.FIELD_ENCRYPTION_KEYS = "secret" with pytest.raises(ImproperlyConfigured): f = fields.EncryptedTextField() f.keys
def test_key_from_settings(self, settings): """Use settings.FIELD_ENCRYPTION_KEYS.""" settings.FIELD_ENCRYPTION_KEYS = ["secret"] f = fields.EncryptedTextField() assert f.keys == settings.FIELD_ENCRYPTION_KEYS
class EncryptedText(models.Model): value = fields.EncryptedTextField()
class SearchText(models.Model): value = fields.EncryptedTextField() search = fields.SearchField(hash_key="abc123", encrypted_field_name="value")
class DemoModel(models.Model): """Note that all regulare kwargs are added to EncryptedFields and not SearchFields. Eg 'default=', 'null=', 'blank='. Also note that we declare the SearchField *after* its EncryptedField. This is only important when using DRF ModelSerializers, but never the less should be the standard way of doing it.""" _email_data = fields.EncryptedEmailField() email = fields.SearchField(hash_key="123abc", encrypted_field_name="_email_data") _name_data = fields.EncryptedCharField( max_length=10, blank=True, null=True, help_text="This field is not required.") name = fields.SearchField(hash_key="123abc", encrypted_field_name="_name_data") _date_data = fields.EncryptedDateField() date = fields.SearchField(hash_key="123abc", encrypted_field_name="_date_data") date_2 = fields.EncryptedDateField( blank=True, null=True, help_text="This field is just encrypted and is not required.", ) _number_data = fields.EncryptedPositiveSmallIntegerField() number = fields.SearchField(hash_key="123abc", encrypted_field_name="_number_data") _text_data = fields.EncryptedTextField( help_text="A text area. Not typically used with a SearchField.") text = fields.SearchField(hash_key="123abc", encrypted_field_name="_text_data") info = fields.EncryptedCharField( blank=True, null=False, max_length=20, help_text= "Char field, required at db level, without a default and blank=True", ) # Examples with defaults _default_date_data = fields.EncryptedDateField(default=datetime.date.today) default_date = fields.SearchField( hash_key="123abc", encrypted_field_name="_default_date_data") _default_number_data = fields.EncryptedPositiveSmallIntegerField(default=1) default_number = fields.SearchField( hash_key="123abc", encrypted_field_name="_default_number_data") _default_char_data = fields.EncryptedCharField(default="foo default", max_length=20) default_char = fields.SearchField( hash_key="123abc", encrypted_field_name="_default_char_data") # typical use case created_at = fields.EncryptedDateTimeField(auto_now_add=True) updated_at = fields.EncryptedDateTimeField(auto_now=True) def __str__(self): return f"{self.pk}: {self.name}" def get_absolute_url(self): return reverse("demomodel-list")
class Task(models.Model): inputs = fields.EncryptedTextField() script = models.TextField()
class ClientSecret(models.Model): key_id = models.CharField(max_length=100, primary_key=True) key = fields.EncryptedTextField()