def insert_user(self, request): resp = self.check_secret_token(request) if resp != None: return resp request_dict = request.get_json() del request_dict['secret'] params = ','.join("{}".format(x) for x in request_dict) values = ','.join("{}".format(add_quote_to_str(request_dict[x])) for x in request_dict) try: with self.connect_db() as con: cur = con.cursor() cur.execute("INSERT INTO User ({}) VALUES ({});".format( params, values)) idx = cur.lastrowid cur.execute("SELECT * FROM User WHERE id=?;", (idx, )) data = convert_to_dict(cur) con.commit() resp = Response(json.dumps(data), status=201, mimetype='application/json') except Exception as e: data = '"message": "{}"'.format(e.args[0]) resp = Response(data, status=400, mimetype='application/json') return resp
def update_user(self, username, request): request_dict = request.get_json() if self.login(request) != None: if self.check_secret_token(request) != None: return Response('{"message": "Not autorized"}', status=401, mimetype='application/error') if 'last_login' in request_dict: del request_dict['last_login'] if 'last_password' in request_dict: del request_dict['last_password'] if 'secret' in request_dict: del request_dict['secret'] params = ','.join("{}={}".format(x, add_quote_to_str(request_dict[x])) for x in request_dict) query = ("UPDATE User SET {} WHERE login='******';").format( params, username) try: with self.connect_db() as con: cur = con.cursor() cur.execute("SELECT id FROM User WHERE login=?", (username, )) idx = cur.fetchone()[0] cur.execute(query) cur.execute("SELECT * FROM User WHERE id=?", (idx, )) data = convert_to_dict(cur) resp = Response(json.dumps(data), status=200, mimetype='application/json') except Exception as e: data = '"message": "{}"'.format(e.args[0]) resp = Response(data, status=400, mimetype='application/json') return resp
def update_operator(self, operator_name, request): resp = self.check_secret_token(request) if resp != None: return resp request_dict = request.get_json() del request_dict['secret'] params = ','.join("{}={}".format(x, add_quote_to_str(request_dict[x])) for x in request_dict) query = ("UPDATE Operator SET {} WHERE name='{}';").format( params, operator_name) try: with self.connect_db() as con: cur = con.cursor() cur.execute("SELECT id FROM Operator WHERE name=?", (operator_name, )) idx = cur.fetchone()[0] cur.execute(query) cur.execute("SELECT * FROM Operator WHERE id=?", (idx, )) data = convert_to_dict(cur) resp = Response(json.dumps(data), status=200, mimetype='application/json') except Exception as e: data = '"message": "{}"'.format(e.args[0]) resp = Response(data, status=400, mimetype='application/json') return resp