Ejemplo n.º 1
0
    def POST(self):
        if not is_loggedin():
            print "***** INVALID SESSION"
            raise web.seeother('/errpage')
            return render.errpage("You are not logged in!",
            "You probably logged out, so.. login again.")
        else:
            print "**** YOU HAVE SESSION"

        message_data = web.data()       # get data sent by xhr
        print "Chat POST: raw xhr = ", message_data

        # validate data format
        regex = r"message=(.+)";
        if not request_parser.match_param(message_data, regex):
            print "[Chat POST] Invalid data format:"
            print message_data
            raise web.seeother(render.errpage("Something went horribly wrong \
            geek code: [Chat POST] Invalid data format", "Quick! Contact the geek who runs this thing!"))

        # values will contain tuple
        values = request_parser.getvalues(message_data, regex)
        message, = values
        print "SENT", message

        # store new message in DB
        print "pid =", session.pid
        print "rid =", session.rid

        mid = db.insert("Message", text=message, pid=session.pid, rid=session.rid)

        time = datetime.now().time()
        return messageFormat(time.hour, time.minute, session.user, message)
Ejemplo n.º 2
0
    def POST(self):
        login_data = web.data()     # get data from POST
        print "login_data = ", login_data
        regex = r"user=([\w]+)&room=([\w]+)"
        if not request_parser.match_param(login_data, regex):
            print "[Index POST] Invalid data format:"
            print login_data
            return render.errpage("Something went horribly wrong \
            geek code: [Index POST] Invalid data format", "Quick! Contact the geek who runs this thing!")

        values = request_parser.getvalues(login_data, regex)
        user, room = values
        print "room = ", room, "user = "******"SELECT * FROM Person WHERE alias=$alias", vars={'alias' : user})
        if len(rset) != 0:
            return render.errpage("The codename \"" + user + "\" is already taken, try again with a different one.",
            "Your codename is lame... Think of a cool name like \"not_" + user + "\"")

        # no duplicate name, insert new user and proceed to room
        # first param is table name
        pid = db.insert("Person", alias=user)
        rid = None

        # check if room exists
        rset = db.query("SELECT * FROM Room WHERE name=$name", vars={'name' : room})
        if len(rset) == 0:
            rid = db.insert("Room", name=room)
            message = "%s created the room." %(user)
            mid = db.insert("Message", text=message, pid=pid, rid=rid)
        else:
            rid = rset[0].rid
            message = "%s joined the conversation." %(user)
            mid = db.insert("Message", text=message, pid=pid, rid=rid)

        print "message =", message
        # store information to client session
        session.user = user
        session.room = room
        session.pid = pid
        session.rid = rid
        session.logintime = datetime.today().strftime("%Y-%m-%d %H:%M:%S")
        print "[INDEX]session = ", session
        print "[INDEX]session.logintime = ", session.logintime
        return render.talk(room, user, message)