Changed: Moved from global DBSession to dbsession injected into request
Version: Bumped to 4.0 Added: Dependency of pyramid_tm Changed: Changed from ACL Authorization to custom Permission Authorization Policy Using more of inbuilt functions. This should reduce the number of DB hits and improve performance
This commit is contained in:
171
brewman/scripts/initializedb.py
Normal file
171
brewman/scripts/initializedb.py
Normal file
@ -0,0 +1,171 @@
|
||||
import os
|
||||
import sys
|
||||
import transaction
|
||||
import uuid
|
||||
|
||||
from pyramid.paster import (
|
||||
get_appsettings,
|
||||
setup_logging,
|
||||
)
|
||||
|
||||
from pyramid.scripts.common import parse_vars
|
||||
|
||||
from ..models.meta import Base
|
||||
from ..models import (
|
||||
get_engine,
|
||||
get_session_factory,
|
||||
get_tm_session,
|
||||
)
|
||||
from brewman.models.auth import (
|
||||
Client,
|
||||
Group,
|
||||
Role,
|
||||
User,
|
||||
role_group,
|
||||
user_group,
|
||||
LoginHistory
|
||||
)
|
||||
from brewman.models.master import (
|
||||
Product,
|
||||
CostCentre,
|
||||
Employee,
|
||||
Ledger,
|
||||
LedgerBase,
|
||||
ProductGroup,
|
||||
Recipe,
|
||||
RecipeItem,
|
||||
DbSetting
|
||||
)
|
||||
from brewman.models.voucher import (
|
||||
Attendance,
|
||||
Batch,
|
||||
Fingerprint,
|
||||
Inventory,
|
||||
Journal,
|
||||
Product,
|
||||
SalaryDeduction,
|
||||
Voucher,
|
||||
ServiceCharge,
|
||||
DbImage
|
||||
)
|
||||
|
||||
|
||||
def usage(argv):
|
||||
cmd = os.path.basename(argv[0])
|
||||
print('usage: %s <config_uri> [var=value]\n'
|
||||
'(example: "%s development.ini")' % (cmd, cmd))
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def main(argv=sys.argv):
|
||||
if len(argv) < 2:
|
||||
usage(argv)
|
||||
config_uri = argv[1]
|
||||
options = parse_vars(argv[2:])
|
||||
setup_logging(config_uri)
|
||||
settings = get_appsettings(config_uri, options=options)
|
||||
|
||||
engine = get_engine(settings)
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
session_factory = get_session_factory(engine)
|
||||
|
||||
with transaction.manager:
|
||||
dbsession = get_tm_session(session_factory, transaction.manager)
|
||||
|
||||
user = User('Admin', '123456', False, uuid.UUID('8de98592-76d9-c74d-bb3f-d6184d388b5a'))
|
||||
dbsession.add(user)
|
||||
|
||||
groups = [Group('Owner', uuid.UUID('52e08c0c-048a-784f-be10-6e129ad4b5d4')),
|
||||
Group('Accountant', uuid.UUID('bc4c2d23-437a-984d-abd4-7d5fce677547')),
|
||||
Group('Accounts Manager', uuid.UUID('cfc44fa7-3392-5b45-b311-5959333f568f'))]
|
||||
|
||||
for group in groups:
|
||||
dbsession.add(group)
|
||||
user.groups.append(group)
|
||||
|
||||
roles = [Role('Attendance', uuid.UUID('09d05434-a09a-fa45-963b-769a2e3fc667')),
|
||||
Role('Trial Balance', uuid.UUID('3b099fec-ddc5-4243-b30e-afb78d9ca14a')),
|
||||
Role('Cash Flow', uuid.UUID('c4d3ae29-420b-ea4c-ae90-00a356263fd9')),
|
||||
Role('Cost Centres', uuid.UUID('6fcc1a20-6aec-e840-b334-1632b34aeab8')),
|
||||
Role('Users', uuid.UUID('c5b7d9d7-f178-0e45-8ea4-bf4e08ec901b')),
|
||||
Role('Daybook', uuid.UUID('c3edb554-a057-8942-8030-37b8e926d583')),
|
||||
Role('Edit Posted Vouchers', uuid.UUID('d6675817-ddf5-bf40-9de6-fa223eb4aaa6')),
|
||||
Role('Employees', uuid.UUID('e4edd0ac-7f5d-e64d-8611-73fdc4cd8ba2')),
|
||||
Role('Fingerprints', uuid.UUID('d9c45323-f997-ba46-9407-8a7145f0828b')),
|
||||
Role('Issue', uuid.UUID('03b602eb-f58a-b94f-af58-8cb47d7849d0')),
|
||||
Role('Journal', uuid.UUID('7661388f-62ce-1c41-8e0d-0326ee5d4018')),
|
||||
Role('Accounts', uuid.UUID('f438262f-72dd-2f4e-9186-5abc3af44fba')),
|
||||
Role('Product Ledger', uuid.UUID('018a2408-e804-1446-90c5-b015829da6ba')),
|
||||
Role('Backdated Vouchers', uuid.UUID('b67b2062-5ca7-134f-8258-5d284dd92426')),
|
||||
Role('Payment', uuid.UUID('f85c0b52-c3fd-7141-8957-7a56cdc014a4')),
|
||||
Role('Post Vouchers', uuid.UUID('36e741da-1a57-b047-a59e-dcd58fcf4338')),
|
||||
Role('Products', uuid.UUID('74fa6d21-eebb-e14c-8153-bebc57190ab4')),
|
||||
Role('Product Groups', uuid.UUID('08413a22-cf88-fd43-b2b7-365d2951d99f')),
|
||||
Role('Profit & Loss', uuid.UUID('0492ebb3-76f3-204e-ab94-bbfe880f0691')),
|
||||
Role('Purchase', uuid.UUID('12335acb-8630-2d41-a191-1517c8d172de')),
|
||||
Role('Purchase Entries', uuid.UUID('78a6422b-aa11-174c-9dfa-412a99e87e02')),
|
||||
Role('Purchase Return', uuid.UUID('ab33196e-d9e4-114c-ac8c-997954363756')),
|
||||
Role('Receipt', uuid.UUID('1f1ce53e-76ff-a346-974a-65db6f606e5f')),
|
||||
Role('Recipes', uuid.UUID('ffb7fb65-d42c-424d-9ff1-45069e3b4a29')),
|
||||
Role('Closing Stock', uuid.UUID('97515732-24e4-c94d-9585-d4bd7f6c7891')),
|
||||
Role('Ledger', uuid.UUID('a2120944-243f-3f49-be57-0ad633ce4801')),
|
||||
Role('Raw Material Cost', uuid.UUID('d462842b-baf1-2343-95e5-ffdba9bbc163')),
|
||||
Role('Edit Other User\'s Vouchers', uuid.UUID('a8328891-7ce2-a943-8c29-2eabc1ffeea3')),
|
||||
Role('Clients', uuid.UUID('cfad44f0-f2a9-7045-89d7-9019cf0f371a')),
|
||||
Role('Salary Deduction', uuid.UUID('92d70e80-1c32-384d-959e-abf84b804696')),
|
||||
Role('Messages', uuid.UUID('f586d128-b6d9-4090-a913-78fcbdb68e59')),
|
||||
Role('Lock Date', uuid.UUID('d52de0be-9388-4b0b-a359-7e122ab6e53a')),
|
||||
Role('Net Transactions', uuid.UUID('2c40f7cf-67fc-4efa-a670-8d16a2e7884d')),
|
||||
Role('Balance Sheet', uuid.UUID('40deb018-b8f2-460a-88be-8972c9fcdf04')),
|
||||
Role('Advanced Delete', uuid.UUID('197ebcd2-bc4a-4b65-a138-ce942ece32ea')),
|
||||
Role('Rebase', uuid.UUID('de204a88-5f9d-4579-a2d6-aa2f25efde42')),
|
||||
Role('Reset Stock', uuid.UUID('aecaf82f-aa41-4634-b754-0c1308b621b1')),
|
||||
Role('Reconcile', uuid.UUID('a5cb51cb-e38e-4705-84a7-cc1e9a8b866b')),
|
||||
Role('Stock Movement', uuid.UUID('20b707ee-2b59-41ad-be87-76d5fe1efca8')),
|
||||
Role('Purchases', uuid.UUID('cf7019c8-3fd3-45b0-9a42-601029ce5b71')),
|
||||
Role('Dashboard', uuid.UUID('53eecc09-bd06-4890-b6f5-6885dda762d4')),
|
||||
Role('Service Charge', uuid.UUID('99b56390-96c2-4f3d-8b0f-5ae3c868594f')),
|
||||
Role('Maintenance', uuid.UUID('770532e4-21de-4712-8a6b-4ff9fd63a503'))]
|
||||
|
||||
for role in roles:
|
||||
dbsession.add(role)
|
||||
groups[0].roles.append(role)
|
||||
|
||||
cost_centres = [CostCentre('Overall', uuid.UUID('36f59436-522a-0746-ae94-e0f746bf6c0d'), True),
|
||||
CostCentre('Purchase', uuid.UUID('7b845f95-dfef-fa4a-897c-f0baf15284a3'), True),
|
||||
CostCentre('Kitchen', uuid.UUID('b2d398ce-e3cc-c542-9feb-5d7783e899df'), True)]
|
||||
|
||||
for cost_centre in cost_centres:
|
||||
dbsession.add(cost_centre)
|
||||
|
||||
ledgers = [Ledger(1, 'All Purchases', 2, True, False, uuid.UUID('7b845f95-dfef-fa4a-897c-f0baf15284a3'),
|
||||
uuid.UUID('240dd899-c413-854c-a7eb-67a29d154490'), True),
|
||||
Ledger(1, 'Local Purchase', 9, True, False, uuid.UUID('36f59436-522a-0746-ae94-e0f746bf6c0d'),
|
||||
uuid.UUID('d2b75912-505f-2548-9093-466dfff6a0f9'), True),
|
||||
Ledger(1, 'ESI/PF - Payable', 11, True, False, uuid.UUID('36f59436-522a-0746-ae94-e0f746bf6c0d'),
|
||||
uuid.UUID('42277912-cc18-854b-b134-9f4b00dba419'), True),
|
||||
Ledger(2, 'ESI/PF - Expense', 7, True, False, uuid.UUID('36f59436-522a-0746-ae94-e0f746bf6c0d'),
|
||||
uuid.UUID('d2a1a286-e900-764b-a1a5-9f4b00dbb940'), True),
|
||||
Ledger(1, 'Cash in Hand', 1, True, False, uuid.UUID('36f59436-522a-0746-ae94-e0f746bf6c0d'),
|
||||
uuid.UUID('ed2341bb-80b8-9649-90db-f9aaca183bb3'), True),
|
||||
Ledger(1, 'Staff Salary', 7, True, False, uuid.UUID('36f59436-522a-0746-ae94-e0f746bf6c0d'),
|
||||
uuid.UUID('5c2b54d0-c174-004d-a0d5-92cdaadcefa7'), True),
|
||||
Ledger(2, 'Service Charges', 11, True, False, uuid.UUID('36f59436-522a-0746-ae94-e0f746bf6c0d'),
|
||||
uuid.UUID('b7eff754-e8ba-e047-ab06-9132c15c7640'), True),
|
||||
Ledger(1, 'Suspense', 4, True, False, uuid.UUID('36f59436-522a-0746-ae94-e0f746bf6c0d'),
|
||||
uuid.UUID('3854e317-6f3b-5142-ab26-9c44d4cddd08'), True)]
|
||||
|
||||
for ledger in ledgers:
|
||||
dbsession.add(ledger)
|
||||
|
||||
product_group = ProductGroup('Suspense', uuid.UUID('ae59a20c-87bb-444a-8abb-915ad5e58b83'), True)
|
||||
dbsession.add(product_group)
|
||||
|
||||
dbsession.add(ProductGroup('Semi', uuid.UUID('e6bf81b9-1e9b-499f-81d5-ab5662e9d9b1'), True))
|
||||
dbsession.add(ProductGroup('Menu Items', uuid.UUID('dad46805-f577-4e5b-8073-9b788e0173fc'), True))
|
||||
|
||||
product = Product(1, 'Suspense', '', 1, '', 1, uuid.UUID('ae59a20c-87bb-444a-8abb-915ad5e58b83'),
|
||||
uuid.UUID('240dd899-c413-854c-a7eb-67a29d154490'), 0, 0, False, False, False,
|
||||
uuid.UUID('aa79a643-9ddc-4790-ac7f-a41f9efb4c15'), True)
|
||||
dbsession.add(product)
|
||||
@ -1,31 +0,0 @@
|
||||
import os
|
||||
import sys
|
||||
import transaction
|
||||
|
||||
from sqlalchemy import engine_from_config
|
||||
|
||||
from pyramid.paster import (
|
||||
get_appsettings,
|
||||
setup_logging,
|
||||
)
|
||||
|
||||
from ..models import (
|
||||
DBSession,
|
||||
Base,
|
||||
)
|
||||
|
||||
def usage(argv):
|
||||
cmd = os.path.basename(argv[0])
|
||||
print('usage: %s <config_uri>\n'
|
||||
'(example: "%s development.ini")' % (cmd, cmd))
|
||||
sys.exit(1)
|
||||
|
||||
def main(argv=sys.argv):
|
||||
if len(argv) != 2:
|
||||
usage(argv)
|
||||
config_uri = argv[1]
|
||||
setup_logging(config_uri)
|
||||
settings = get_appsettings(config_uri)
|
||||
engine = engine_from_config(settings, 'sqlalchemy.')
|
||||
DBSession.configure(bind=engine)
|
||||
Base.metadata.create_all(engine)
|
||||
Reference in New Issue
Block a user