Skip to content

Commit

Permalink
Add first_run.py script to initialize bank accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
MEDVEDx64 committed Jan 10, 2022
1 parent dc6d74b commit 943f815
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ Requirements
Getting it working
------------------

* Copy/move `config_default.py` to `config.py` and edit it
* Run `python funky.py`.
* Copy `config_default.py` to `config.py` and edit it;
* Run `./first_run.py`;
* Run `./funky.py`.


Becoming an administrator
-------------------------
Expand Down
24 changes: 24 additions & 0 deletions first_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python2.7

import pymongo

from config import config

if __name__ == '__main__':
dbclient = pymongo.MongoClient(config['dbUri'])
db = dbclient.funky

bank_user = '__BANK__'
reserve_user = '__RESERVE__'

if not db['accounts'].find_one({'login': bank_user}):
print('Creating bank account')
db['accounts'].insert({
'login': bank_user, 'nickname': 'Steve', 'money': 0.0, 'password': '0',
'locked': True, 'flags': ['money_recv']})

if not db['accounts'].find_one({'login': reserve_user}):
print('Creating reserve account')
db['accounts'].insert({
'login': reserve_user, 'nickname': 'Steve', 'money': 1000000000.0, 'password': '0',
'locked': True, 'flags': ['money_send']})
15 changes: 4 additions & 11 deletions funky.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@

PORT = config['port']
HOST = config['host']
# if not PORT == 80:
# HOST += ':' + str(PORT)
db = None

COPY = '2014-2022 MEDVEDx64. Thanks to dmitro and AlexX.'
db = None

class FunkyHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def html_start(self):
Expand Down Expand Up @@ -724,7 +721,7 @@ def do_GET(self):
self.send_error(404, 'Not Found')

def do_POST(self):
missing_data_url = '/message?m=Missing some required fields, make sure you filled the form correctly.'
missing_data_url = '/message?m=Some required fields are missing or incorrectly filled.'
url = urlparse.urlparse(self.path)
q = urlparse.parse_qs(url.query)
if url.path == '/account':
Expand All @@ -744,7 +741,7 @@ def do_POST(self):
else:
self.html_bad_login()
elif q['do'][0] == 'register':
if not 'nickname' in data or data['login'][0][0:2] == '__':
if not 'nickname' in data or len(data['login'][0]) < 2 or data['login'][0][0:2] == '__':
self.html_redirect(missing_data_url)
return
acc = db.accounts.find_one({'login': data['login'][0]})
Expand Down Expand Up @@ -808,13 +805,9 @@ def do_POST(self):
return
if amount > d['left']:
amount = d['left']
bank_user = '__BANK__'
if not db['accounts'].find_one({'login': bank_user}):
db['accounts'].insert({'login': bank_user, 'nickname': 'Steve', 'money': 0.0, 'password': '0',
'locked': True, 'flags': ['money_recv']})
ok, message = True, 'Success'
if d['price']:
ok, message = money.transfer(db, u, bank_user, d['price'] * amount)
ok, message = money.transfer(db, u, '__BANK__', d['price'] * amount)
if ok:
if 'left' in d:
db['items'].update({'item_id': itemid}, {'$set': {'left': d['left'] - amount}})
Expand Down

0 comments on commit 943f815

Please sign in to comment.