def create(): """ Create user function """ data = request.json["properties"] # validation of the received data if not validate_json(create_schema, data): return jsonify({"error": "Data is invalid"}), 400 # search users with the same email address temp = db.select_rows( f"select * from account where email='{data['email']}'") if temp is not None: return jsonify({"error": "User with this email addres already exists"}), 400 db.insert_data(f""" insert into account (first_name, last_name, email, password) values ( '{data["first_name"]}', '{data["last_name"]}', '{data["email"]}', '{hashlib.md5(data["password"].encode()).hexdigest()}' )""") db.commit() response = {"result": "ok"} return jsonify(response), 400
def user_in_room(): data = request.json user_id = data['id_user'] is_in_room = data['is_in_room'] code = data['code'] print(data) if is_in_room is 1: db.insert_data( # user_status_id: 1 - admin, 2 - user f""" insert into room_has_user (user_id, room_id, user_status_id) values ({int(user_id)}, {int(code)}, 2) """) db.commit() else: db.delete_rows(f""" delete from room_has_user where (user_id = {int(user_id)}) and (room_id = {int(code)}) and (user_status_id = 2) """) db.commit() response = {"message": "ok"} return jsonify(response)
def create_room(): """ Create room function :return: """ data = request.json # validation of the received data if not validate_json(data, room_create_schema): return jsonify({"error": "Data is invalid"}), 400 code = rand_code() try: # add room to db db.insert_data(f""" insert into room (id_room, name_room, note) values ( '{code}', '{data['name']}', '{data['description']}' )""") db.commit() # add room owner # status 1-admin, 2-user db.insert_data(f""" insert into room_has_user (user_id, room_id, user_status_id) values ( '{data['id_user']}', '{code}', 1 )""") db.commit() except: from sys import exc_info print(exc_info()[0]) return jsonify({"message": "Catch DB exception"}), 400 response = {"result": "ok"} return jsonify(response), 200
async def run_multiple(url: str, queue: Queue, cims_list=None): # for cims in cims_list: tasks = set() task_ref = 1 while True: cims = queue.get() if cims is None: break await asyncio.sleep(0.5) # asyncio.get_event_loop().run_in_executor(None, wikiloc_browser, url, cims) task = asyncio.create_task(wikiloc_browser(url, cims), name=task_ref) tasks.add(task) task_ref += 1 # wikiloc_browser(url, cims) while len(tasks): done, _pending = await asyncio.wait(tasks, timeout=1) tasks.difference_update(done) urls = (t.get_name() for t in tasks) print(f"Missing urls to be processed {' '.join(urls)}") db.commit() print("Everything commited into db")
def send_room_message(message): if message["data"]: send_on = datetime.now().strftime("%Y-%m-%d %H:%M:%S") db.insert_data(f""" insert into message(content, date_send, room_id, user_id) values ( '{message["data"]}', '{send_on}', {int(message["room"])}, {int(message["user"])} ) """) db.commit() response = [{ "user": get_user_by_id(message["user"]).title(), "data": message['data'], "send_on": send_on.split(' ')[1][:5] # негарно виглядає але працює }] emit('response', response, room=message["room"])