from application.models import SignedTitles
from application import app
from application import db
from flask import request
import json
from kombu import Connection, Exchange, Queue, Consumer, eventloop



@app.route("/")
def check_status():
    return "Tester is OK"


@app.route("/count")
def count_rows():
    the_count = db.session.query(SignedTitles).count()
    return str(the_count)


@app.route("/deletelastrecord")
def delete_last_record():
    last_record = get_last_record()
    db.session.delete(last_record)
    db.session.commit()
    return 'deleted'


@app.route("/getlastrecord")
def get_last_signature():
    last_record = get_last_record()
    #convert the sor dictionary to a string
    sor_as_string = json.dumps(last_record.sor)
    return sor_as_string


def get_last_record():
    #Returns an instance of SignedTitles, the last one in the table
    return db.session.query(SignedTitles).order_by(SignedTitles.id.desc()).first()


def query_nested_stuff_like_this():
    signed_titles_instance = db.session.query(SignedTitles).first()
    a_dict = signed_titles_instance.sor
    my_element = a_dict['data']['titleno']
    return my_element


def query_by_an_id_like_this(the_id):
    athing = db.session.query(SignedTitles).get(the_id)
    return athing


@app.route("/getlastqueuemessage")
def get_last_queue_message():
    #: By default messages sent to exchanges are persistent (delivery_mode=2),
    #: and queues and exchanges are durable.
    exchange = Exchange()
    connection = Connection('amqp://guest:guest@localhost:5672//')

    # Create/access a queue bound to the connection.
    queue = Queue('system_of_record', exchange, routing_key='system_of_record')(connection)
    queue.declare()

    message = queue.get()

    if message:
        signature = message.body
        message.ack() #acknowledges message, ensuring its removal.
        return signature

    else:
        return "no message"



@@ -1293,16 +1293,24 @@
 /getlast
+incoming
 queuemes
@@ -1329,16 +1329,25 @@
 et_last_
+incoming_
 queue_me
@@ -1626,16 +1626,16 @@
 ection.%0A
-
     queu
@@ -1944,12 +1944,683 @@
  message%22%0A%0A%0A
+@app.route(%22/getlastoutgoingqueuemessage%22)%0Adef get_last_outgoing_queue_message():%0A    #: By default messages sent to exchanges are persistent (delivery_mode=2),%0A    #: and queues and exchanges are durable.%0A    exchange = Exchange()%0A    connection = Connection('amqp://guest:guest@localhost:5672//')%0A%0A    # Create/access a queue bound to the connection.%0A    queue = Queue('OUTGOING_QUEUE', exchange, routing_key='OUTGOING_QUEUE')(connection)%0A    queue.declare()%0A%0A    message = queue.get()%0A%0A    if message:%0A        signature = message.body%0A        message.ack() #acknowledges message, ensuring its removal.%0A        return signature%0A%0A    else:%0A        return %22no message%22%0A%0A%0A

test that outgoing q is populated