Skip to content

Commit

Permalink
Added seed command to the migrate compose target
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Rusakov committed Oct 1, 2024
1 parent 5323991 commit 1f16f1f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 22 deletions.
30 changes: 8 additions & 22 deletions api/management/commands/seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def getOrCreate(self, model: Type[T], defaults: MutableMapping[str, any] | None
return value

def addUser(self, username: str, email: str, password: str) -> User:
return self.getOrCreate(UserModel, username=username, defaults={email: email, password: password})
return self.getOrCreate(UserModel, username=username, defaults={"email": email, "password": password})

def setIdentity(self, user: User, params: dict[str, any]) -> Identity:
for k, v in params.items():
Expand All @@ -54,29 +54,16 @@ def setIdentity(self, user: User, params: dict[str, any]) -> Identity:
self.success(f"Updated identity for user '{user.username}'")
return user.identity

def addContact(self, user: User, params: dict[str, any]) -> Contact:
params["belongs_to"] = user
contact = self.addContact(**params)
self.success(f"Created contact \"{contact.first_name} {contact.last_name}\"")
return contact

def addOrderType(self, name: str) -> OrderType:
return self.getOrCreate(OrderType, name=name)

def addProduct(self, user: User, label: str) -> OrderType:
return self.getOrCreate(Product, label=label, defaults={
"metadata": self.getOrCreate(Metadata, name="metadata", defaults={"modified_user_id": user.id}),
"metadata": self.getOrCreate(Metadata, id_name="metadata", name="metadata", defaults={"modified_user_id": user.id}),
"pricing": self.getOrCreate(Pricing, name="Free", defaults={"pricing_type": Pricing.PricingType.FREE}),
"provider": user
})

def addDataFormat(self, name: str) -> DataFormat:
return self.getOrCreate(DataFormat, name=name)

def seed(self):
# Create users
rincevent = self.addUser(
'rincevent', '[email protected]', 'rincevent')
rincevent = self.addUser('rincevent', '[email protected]', 'rincevent')
self.setIdentity(rincevent, {
"email": os.environ.get('EMAIL_TEST_TO', '[email protected]'),
"first_name": 'Jean',
Expand Down Expand Up @@ -198,7 +185,7 @@ def seed(self):
)
))

order_type_prive = self.addOrderType('Privé')
order_type_prive = self.getOrCreate(OrderType, name='Privé', defaults={})

# Create orders
order1 = Order.objects.create(
Expand Down Expand Up @@ -277,10 +264,10 @@ def seed(self):
product_deprecated = self.addProduct(mma,
'MO07 - Objets divers et éléments linéaires - linéaires')

data_format = self.addDataFormat('Geobat NE complet (DXF)')
data_format_maquette = self.addDataFormat('3dm (Fichier Rhino)')
data_format = self.getOrCreate(DataFormat, name='Geobat NE complet (DXF)', defaults={})
data_format_maquette = self.getOrCreate(DataFormat, name='3dm (Fichier Rhino)', defaults={})

orderitems = [
for order_item in [
OrderItem.objects.create(order=order1, product=product1),
OrderItem.objects.create(order=order1, product=product2),
OrderItem.objects.create(order=order_download, product=product1),
Expand All @@ -290,8 +277,7 @@ def seed(self):
OrderItem.objects.create(order=order4, product=product2),
OrderItem.objects.create(
order=order_mka2, product=product1, data_format=data_format)
]
for order_item in orderitems:
]:
order_item.set_price()
order_item.save()
order_item_deprecated = OrderItem.objects.create(
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ services:
command: >
bash -c "
python3 manage.py migrate &&
python3 manage.py seed &&
python3 manage.py collectstatic --noinput &&
python3 manage.py fixturize"
volumes:
Expand Down

0 comments on commit 1f16f1f

Please sign in to comment.