From 6b0bcdf7230c82f11f7de6631ddaa92d4bb5f2d3 Mon Sep 17 00:00:00 2001 From: "Lucas F." Date: Wed, 7 Feb 2024 10:08:33 -0300 Subject: [PATCH] update: create unidade and tag default elements --- .../contas/management/commands/elements.py | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/form_user/apps/contas/management/commands/elements.py b/form_user/apps/contas/management/commands/elements.py index 730b89e..cb51120 100644 --- a/form_user/apps/contas/management/commands/elements.py +++ b/form_user/apps/contas/management/commands/elements.py @@ -2,7 +2,7 @@ from decouple import config from django.core.management.base import BaseCommand from django.utils.translation import gettext_lazy as _ -from form_user.apps.contas.models import CustomUser +from form_user.apps.contas.models import CustomUser, TagUnidade, Unidade class AppException(Exception, BaseCommand): @@ -31,3 +31,23 @@ class Command(BaseCommand): self.stdout.write(self.style.SUCCESS(_("Superuser created!"))) else: self.stdout.write(self.style.NOTICE(_("Superuser already exists!"))) + + if not TagUnidade.objects.count(): + self.create_tag_unidade(model=TagUnidade, el_list=["primeira", "segunda"]) + else: + self.stdout.write(self.style.NOTICE(_("TagUnidade already exists!"))) + + if not Unidade.objects.count(): + self.create_tag_unidade(model=Unidade, el_list=["qualquer", "coisa"]) + else: + self.stdout.write(self.style.NOTICE(_("Unidade already exists!"))) + + def create_tag_unidade(self, model, el_list): + tags_uni = [] + + for el in el_list: + tags_uni.append(model(nome=el)) + + created_element = model.objects.bulk_create(tags_uni) + if created_element: + self.stdout.write(self.style.SUCCESS(f"{model.__name__} created!"))