Skip to content

Commit

Permalink
Merge pull request #261 from UnitapApp/feature/token-nft-constraints
Browse files Browse the repository at this point in the history
Feature/token nft constraints
  • Loading branch information
Bastin authored Jan 12, 2024
2 parents 2bbc211 + 576a551 commit e1f5042
Show file tree
Hide file tree
Showing 12 changed files with 680 additions and 340 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 4.0.4 on 2024-01-12 12:54

from django.db import migrations, models
import django.db.models.expressions
import django.db.models.functions.text


class Migration(migrations.Migration):

dependencies = [
('authentication', '0028_remove_wallet_unique_wallet_address_and_more'),
]

operations = [
migrations.RemoveConstraint(
model_name='wallet',
name='unique_wallet_address',
),
migrations.RemoveConstraint(
model_name='wallet',
name='unique_wallet_user_profile_address',
),
migrations.AddConstraint(
model_name='wallet',
constraint=models.UniqueConstraint(django.db.models.functions.text.Lower('address'), django.db.models.expressions.F('wallet_type'), name='unique_wallet_address'),
),
]
26 changes: 10 additions & 16 deletions authentication/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import uuid

from django.contrib.auth.models import User
from django.core.cache import cache
from django.core.validators import RegexValidator
from django.db import models
from django.db.models import Q, UniqueConstraint
from django.db.models import UniqueConstraint
from django.db.models.functions import Lower
from django.utils import timezone
from safedelete.models import SafeDeleteModel
Expand All @@ -27,15 +29,15 @@ def get_or_create(self, first_context_id):

def get_by_wallet_address(self, wallet_address):
try:
return super().get_queryset().get(wallets__address=wallet_address)
except UserProfile.DoesNotExist:
return Wallet.objects.get(address=wallet_address).user_profile
except Wallet.DoesNotExist:
return None

def create_with_wallet_address(self, wallet_address):
_user = User.objects.create_user(username="UNT" + wallet_address)
_user = User.objects.create_user(username="UNT" + str(uuid.uuid4())[:16])
_profile = UserProfile.objects.create(user=_user)
Wallet.objects.create(
wallet_type=NetworkTypes.EVM,
wallet_type=NetworkTypes.EVM, # TODO support register with non evms
user_profile=_profile,
address=wallet_address,
)
Expand All @@ -45,8 +47,8 @@ def create_with_wallet_address(self, wallet_address):

def get_or_create_with_wallet_address(self, wallet_address):
try:
return super().get_queryset().get(wallets__address=wallet_address)
except UserProfile.DoesNotExist:
return Wallet.objects.get(address=wallet_address).user_profile
except Wallet.DoesNotExist:
return self.create_with_wallet_address(wallet_address)


Expand Down Expand Up @@ -137,20 +139,12 @@ class Meta:
UniqueConstraint(
Lower("address"),
"wallet_type",
condition=Q(deleted__isnull=True),
name="unique_wallet_address",
),
UniqueConstraint(
Lower("address"),
"user_profile",
"wallet_type",
name="unique_wallet_user_profile_address",
),
]

def __str__(self):
return f"{self.wallet_type} Wallet for profile with contextId \
{self.user_profile.initial_context_id}"
return f"{self.wallet_type} Wallet for {self.user_profile.username}"


class BaseThirdPartyConnection(models.Model):
Expand Down
Loading

0 comments on commit e1f5042

Please sign in to comment.