def new_sheet_request(): form = request.form tid = form.get("tid").lstrip("0") mid = form.get("mid").strip().upper() alliance = form.get("alliance") if not validate_match(mid): # Match id does not match the regex raise WebException("Invalid match id.") _match = match.get_match(mid=mid).first() if _match is None: # Match does not exist yet, so create one match.add_match(mid) _team = team.get_team(tid=tid).first() if _team is None: # Team does not exist yet, so create one team.add_team(tid) sheet = get_sheet(tid=tid, mid=mid, alliance=alliance).first() if sheet is not None: # Duplicate sheet exists, so alert the user raise WebException("Sheet already exists.") # Create a new sheet new_sheet(tid, mid, alliance) return {"success": 1, "message": "Sheet created."}
def callback(channel, method, properties, body): # required signature for the callback; no return print("Received a match from recommendation") with match.app.test_request_context('/match/'): # print("The body is") # print(type(body)) # print(body) # print(body.decode("UTF-8")) print(f"Message received: {json.loads(body)}") data = json.loads(body) id1 = data["userid"] # print(id1) id2 = data["visitedid"] # print(id2) result = match.add_match(id1, id2) # print("Type is ") # print(type(result)) # print(result) print(f"Message from match {result[0].get_json()}") # result = json.dumps(result,default=str) # result = get_all() # result = processOrder(json.loads(body)) # json.dump(result, sys.stdout, default=str) # print processing result; not really needed # json.dump(result, sys.stdout, default=str) # convert the JSON object to a string and print out on screen print() # print a new line feed to the previous json dump # print() # print another new line as a separator # prepare the reply message and send it out # replymessage = json.dumps(result, default=str) # convert the JSON object to a string # replyqueuename="profile.reply" # A general note about AMQP queues: If a queue or an exchange doesn't exist before a message is sent, # - the broker by default silently drops the message; # - So, if really need a 'durable' message that can survive broker restarts, need to # + declare the exchange before sending a message, and # + declare the 'durable' queue and bind it to the exchange before sending a message, and # + send the message with a persistent mode (delivery_mode=2). # set up the exchange if the exchange doesn't exist # exchangename="recommendation_direct" # channel.exchange_declare(exchange=exchangename, exchange_type='direct') # channel.queue_declare(queue=replyqueuename, durable=True) # make sure the queue used for "reply_to" is durable for reply messages # channel.queue_bind(exchange=exchangename, queue=replyqueuename, routing_key=replyqueuename) # make sure the reply_to queue is bound to the exchange # channel.basic_publish(exchange=exchangename, # routing_key=properties.reply_to, # use the reply queue set in the request message as the routing key for reply messages # body=result, # properties=pika.BasicProperties(delivery_mode = 2, # make message persistent (stored to disk, not just memory) within the matching queues; default is 1 (only store in memory) # correlation_id = properties.correlation_id, # use the correlation id set in the request message # ) # ) channel.basic_ack( delivery_tag=method.delivery_tag ) # acknowledge to the broker that the processing of the request message is completed
def update_sheet_request(): form = request.form sid = form.get("sid") mid = form.get("mid").strip().upper() tid = form.get("tid").lstrip("0") alliance = form.get("alliance") if not validate_match(mid): # Match id does not match the regex raise WebException("Invalid match id.") _match = match.get_match(mid=mid).first() if _match is None: # Match does not exist yet, so create one match.add_match(mid) _team = team.get_team(tid=tid).first() if _team is None: # Team does not exist yet, so create one team.add_team(tid) update_sheet(sid, mid, tid, alliance) return {"success": 1, "message": "Sheet updated."}