Skip to content

Commit 1c5b6ca

Browse files
committed
Update init script.
1 parent 04487c3 commit 1c5b6ca

File tree

2 files changed

+80
-5
lines changed

2 files changed

+80
-5
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ To install:
1414
* Run `virtualenv -p python3 <location>`.
1515
* Activate the virtualenv (see virtualenv docs for how to activate on your shell).
1616
* From the source directory, run `pip install ./barsystem`
17-
* Run `barsystem-installer`
17+
* Run `barsystem-installer init`
1818
* For testing, run barsystem with `django-admin runserver --settings barsystem.local_settings`
1919
* For production, a standard nginx + uwsgi stack is recommended.
2020

barsystem/src/barsystem/install.py

+79-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import os
33
import sys
44

5+
from django.core.management import execute_from_command_line
6+
7+
58
class BarsystemInstaller:
69
def main(self, argv):
710
# curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE)
@@ -14,21 +17,93 @@ def main(self, argv):
1417
# if key == 'q':
1518
# break
1619

20+
import django
21+
django.setup()
22+
1723
self.command = argv[0] if len(argv) else ''
1824
self.argv = argv[1:] if len(argv) > 1 else []
1925

2026
cmd_name = 'cmd_' + self.command
2127
if hasattr(self, cmd_name):
2228
getattr(self, cmd_name)()
2329
else:
24-
print('usage()')
30+
self.cmd_help()
31+
32+
def _command_list(self):
33+
return [cmd[len('cmd_'):] for cmd in dir(self) if cmd.startswith('cmd_')]
34+
35+
def cmd_help(self):
36+
print('Possible commands:')
37+
for cmd in self._command_list():
38+
print(' *', cmd)
2539

2640
def cmd_init(self):
27-
print('init!')
41+
self.cmd_generate_secret_key()
42+
self.cmd_migrate()
43+
self.cmd_createsuperuser()
44+
self.cmd_create_initial_db_entries()
45+
46+
def cmd_update(self):
47+
print('[Updating barsystem]')
48+
self.cmd_migrate()
49+
50+
def cmd_migrate(self):
51+
print('[Running migrations]')
52+
execute_from_command_line([
53+
sys.argv[0],
54+
'migrate'
55+
])
56+
57+
def cmd_createsuperuser(self):
58+
print('[Creating superuser]')
59+
60+
from django.contrib.auth import get_user_model
61+
for user in get_user_model().objects.all():
62+
if user.is_superuser:
63+
print('- There is already a superuser!')
64+
return
65+
66+
execute_from_command_line([
67+
sys.argv[0],
68+
'createsuperuser'
69+
])
70+
print('- Superuser created.')
2871

2972
def cmd_generate_secret_key(self):
30-
settings_module = os.getenv('DJANGO_SETTINGS_MODULE')
31-
print('gen sec key!', settings_module)
73+
print('[Generating secret key]')
74+
from django.conf import settings
75+
from django.core.management.utils import get_random_secret_key
76+
77+
if os.path.exists(settings.SECRET_KEY_FILE):
78+
print('- Secret file already exists!')
79+
return
80+
81+
os.makedirs(settings.CONFIG_DIR, exist_ok=True)
82+
83+
with open(settings.SECRET_KEY_FILE, 'w') as f:
84+
f.write(get_random_secret_key())
85+
print('- Secret key generated.')
86+
87+
def cmd_create_initial_db_entries(self):
88+
print('[Creating necesarry database entries]')
89+
from decimal import Decimal
90+
from barsystem.models import Product
91+
try:
92+
Product.objects.get(special_id='cash_deposit')
93+
print('- Cash deposit already exists!')
94+
except Product.DoesNotExist:
95+
cash_deposit = Product(
96+
name='Cash deposit',
97+
member_price=Decimal(-1),
98+
standard_price=Decimal(-1),
99+
active=True,
100+
special=True,
101+
special_id='cash_deposit',
102+
quantity_type='enter_numeric',
103+
)
104+
cash_deposit.save()
105+
print('- Cash deposit created.')
106+
32107

33108

34109
# Main entrypoint for barsystem-install executable

0 commit comments

Comments
 (0)