示例#1
0
def getSession(request, **kwargs):
    session_id = str(kwargs["session_id"])
    session = Session()
    try:
        session.read(session_id)
    except SessionInvalid:
        return HttpResponseBadRequest(json.dumps({"success" : False, "error" : "Session does not exist"}))

    return HttpResponse(json.dumps({"success" : True, "data" : session.data}))
示例#2
0
def setSession(request, **kwargs):
    session_id = str(kwargs["session_id"])
    try:
        data = request.POST["data"]
        if data == "":
            raise KeyError
    except KeyError:
        logger.error("%s - Couldn't find data to parse." % (session_id,))
        return HttpResponseBadRequest(json.dumps({"success" : False, "error" : "Invalid Data"}))

    try:
        ttl = int(request.POST["ttl"])
        if not ttl:
            raise KeyError
    except KeyError:
        logger.error("%s - No TTL was specified" % (session_id,))
        return HttpResponseBadRequest(json.dumps({"success" : False, "error" : "No TTL was specified"}))

    session = Session()
    try:
        session.read(session_id)
    except (SessionInvalid, SessionExpired):
        logger.debug("%s - Couldn't find an existing session so creating a blank one instead." % (session_id,))

    session.data = data
    session.ttl = ttl

    session.write()

    return HttpResponse(json.dumps({"success" : True}))