def isLoginRequired(): db = DB() loginRequired = False for line in db.loadSystemSettings(): if line['setting_name'] == 'username': loginRequired = True break return loginRequired
def setting(): db = DB() action = request.args.get('action') if action == 'read': settings = {} # print 'READ SYSTEM CONF:' for line in db.loadSystemSettings(): if line['setting_name'] == 'password': continue settings.update({ line['setting_name']: line['setting_value'] }) response = json.dumps({ 'setting': [settings] }) # print response elif action == 'update': if request.method == 'POST': jsonCredentials = request.form['setting'] params = json.loads(jsonCredentials) response = json.dumps({ 'success': 'false' }) if 'username' in params: credentialUsername = params['username'] db.updateSystemSettings('username', credentialUsername) response = json.dumps({ 'success': 'true' }) if 'password' in params: credentialPassword = params['password'] db.updateSystemSettings('password', credentialPassword) response = json.dumps({ 'success': 'true' }) if 'valve_amount' in params: valveAmount = int(params['valve_amount']) actualValves = db.getValveCount() if actualValves <= valveAmount: db.updateSystemSettings('valve_amount', valveAmount) response = json.dumps({ 'success': 'true' }) else: response = json.dumps({ 'success': 'false', 'message': 'There are more valves set up than you want to allow. Please remove some of them first.' }) elif action == 'destroy': if request.method == 'POST': jsonCredentials = request.form['setting'] params = json.loads(jsonCredentials) # print params #for setting in params: for key, value in params.items(): # print 'checking: %s / %s' % (key, value) if value == '-DELETE-': db.deleteSystemSetting(key) response = json.dumps({ 'success': 'true' }) return response
def actionLogin(): if request.method == 'POST': params = request.get_json(); requestUsername = params['username'] requestPassword = params['password'] systemCredentials = {} db = DB() for line in db.loadSystemSettings(): if line['setting_name'] == 'username': systemCredentials['username'] = line['setting_value'] if line['setting_name'] == 'password': systemCredentials['password'] = line['setting_value'] if len(systemCredentials) == 2 and requestUsername == systemCredentials['username'] and requestPassword == systemCredentials['password']: print 'Login successful' token = generateAuthToken(request, systemCredentials, 600) response = json.dumps({ 'success': 'true', 'token': token }) else: print 'Login failed' response = json.dumps({ 'success': 'false', 'message': 'Invalid login.' }) return response