Exemplo n.º 1
0
Arquivo: app.py Projeto: Alge/smspoll
    def on_message(self, message):
        print("Message recieved: {}".format(message))

        data = {}
        try:
            data = json.loads(message)

        except:
            self.write_message(json.dumps({"error": "not valid json"}))

        if "subscribe_poll" in data.keys():
            poll = Poll.get_or_none(Poll.id == data["subscribe_poll"])
            if not poll:
                self.write_message(
                    json.dumps({
                        "result":
                        "error",
                        "message":
                        "unable to find poll with id: {}".format(
                            data["subscribe_poll"])
                    }))
            else:
                self.subscriptions.append(poll)
                if poll.id not in clients.keys():
                    clients[poll.id] = []
                clients[poll.id].append(self)
                r = {
                    'type': 'response',
                    'response': 'success',
                    'message': "subscribed to poll {}".format(poll.id)
                }
                self.write_message(json.dumps(r))
                send_update(poll, self)
        else:
            self.write_message(json.dumps({"error": "unknown command"}))
Exemplo n.º 2
0
Arquivo: app.py Projeto: Alge/smspoll
    def post(self):
        try:
            sms = {}
            sms["to"] = self.get_argument("to")
            sms["from"] = self.get_argument("from")
            sms["message"] = self.get_argument("message")
            poll = Poll.get_or_none(Poll.number == sms["to"])
            if not poll:
                self.write("No poll with that number")
            else:
                if poll.add_answer(sms["message"], sms["from"]):
                    self.write("Success")
                    update_all_pollclients(poll)
                else:
                    self.write("Failed to add answer")
        except Exception as e:
            print(e)
            self.write("Failed tot parse request")

        self.finish()
Exemplo n.º 3
0
Arquivo: app.py Projeto: Alge/smspoll
    def get(self, poll_id):
        poll = Poll.get_or_none(Poll.id == poll_id)
        if poll == None:
            raise tornado.web.HTTPError(404)

        self.render("templates/poll.html", poll=poll)