Skip to content

Commit

Permalink
fix: remove existing check from django commands
Browse files Browse the repository at this point in the history
  • Loading branch information
PauloUbirajara authored Apr 28, 2024
1 parent e107342 commit 7a4db14
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 31 deletions.
6 changes: 1 addition & 5 deletions apps/category/management/commands/add_default_categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def create_default_categories(user: User):
)
for category in default_categories:
try:
Category.objects.create(name=category, user=user)
Category.objects.get_or_create(name=category, user=user)
except Exception as err:
logging.warning("Could not create default category: {}".format(category))
logging.warning(str(err))
Expand All @@ -36,8 +36,4 @@ def handle(self, *args, **kwargs):
if user is None:
raise Exception("Could not find user")

if Category.objects.filter(user=user).count() > 0:
logging.warning("User already has one or more categories")
return

create_default_categories(user)
35 changes: 13 additions & 22 deletions apps/currency/management/commands/add_default_currencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
from django.core.management.base import BaseCommand


def create_default_currencies(user: User):
def create_default_currencies():
# Values from py-money python package
default_currencies_dict = {
"ARS": "Argentine Peso",
"AUD": "Australian Dollar",
"BRL": "Brazilian Real",
"CAD": "Canadian Dollar",
"ARS": "Peso Argentino",
"AUD": "Dólar Australiano",
"BRL": "Real Brasileiro",
"CAD": "Dólar Canadense",
"EUR": "Euro",
"GBP": "Pound Sterling",
"JPY": "Yen",
"MXN": "Mexican Peso",
"RUB": "Russian Ruble",
"USD": "US Dollar",
"GBP": "Libra Esterlina",
"JPY": "Iene",
"MXN": "Peso Mexicano",
"RUB": "Rublo Russo",
"USD": "Dólar Americano",
}

for representation, name in default_currencies_dict.items():
try:
Currency.objects.get_or_create(
name=name, representation=representation, user=user
name=name, representation=representation
)
except Exception as err:
logging.warning(
Expand All @@ -35,16 +35,7 @@ def create_default_currencies(user: User):


class Command(BaseCommand):
help = "Adds default currencies for a specific user"

def add_arguments(self, parser):
parser.add_argument(
"user_id", type=int, help="Specify which user to create default currencies"
)
help = "Adds default currencies"

def handle(self, *args, **kwargs):
user = User.objects.filter(id=kwargs["user_id"]).first()
if user is None:
raise Exception("Could not find user")

create_default_currencies(user)
create_default_currencies()
4 changes: 0 additions & 4 deletions apps/wallet/management/commands/add_default_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,4 @@ def handle(self, *args, **kwargs):
if user is None:
raise Exception("Could not find user")

if Wallet.objects.filter(user=user).count() > 0:
logging.warning("User already has one or more wallets")
return

create_default_wallet(user)

0 comments on commit 7a4db14

Please sign in to comment.