예제 #1
0
 def _handle_creation():
     # Inline because f**k a scope
     created, obj = ping_summary(g.db, slug, expiration)
     if created:
         user['pings'][slug] = ping_obj
         set_user(g.db, user)
         to_return['ping_obj'] = obj
         return Response(json.dumps(to_return), mimetype="application/json")
     else:
         to_return['error'] = obj
         return Response(json.dumps(to_return), mimetype="application/json")
예제 #2
0
 def _handle_creation():
     # Inline because f**k a scope
     created, obj = ping_summary(g.db, slug, expiration)
     if created:
         user['pings'][slug] = ping_obj
         set_user(g.db, user)
         to_return['ping_obj'] = obj
         return Response(json.dumps(to_return), mimetype="application/json")
     else:
         to_return['error'] = obj
         return Response(json.dumps(to_return), mimetype="application/json")
예제 #3
0
def ping(slug):
    to_return = { 'success': True, 'error': "" }
    user = get_user()['user']

    if not user:
        return abort(503)

    if not slug:
        return abort(404)

    gmtime = time.gmtime()
    seconds = int(calendar.timegm(gmtime))
    expiration = seconds + (60 * 60 * 24) #24 hours later

    ping_obj = {
        'pinged': slug,
        'when': seconds
    }
    pings = user.get('pings', None)
    def _handle_creation():
        # Inline because f**k a scope
        created, obj = ping_summary(g.db, slug, expiration)
        if created:
            user['pings'][slug] = ping_obj
            set_user(g.db, user)
            to_return['ping_obj'] = obj
            return Response(json.dumps(to_return), mimetype="application/json")
        else:
            to_return['error'] = obj
            return Response(json.dumps(to_return), mimetype="application/json")

    if not pings:
        user['pings'] = { slug: ping_obj }
        set_user(g.db, user)
        return _handle_creation()
    else:
        last_ping = pings.get(slug, None)
        if not last_ping:
            return _handle_creation()

        if seconds > (last_ping['when'] + (60 * 60 * 24)):
            return _handle_creation()

        to_return['success'] = False
        to_return['error'] = "You need to wait 24 hours to ping again."

    return Response(json.dumps(to_return), mimetype="application/json")
예제 #4
0
def ping(slug):
    to_return = {'success': True, 'error': ""}
    user = get_user()['user']

    if not user:
        return abort(503)

    if not slug:
        return abort(404)

    gmtime = time.gmtime()
    seconds = int(calendar.timegm(gmtime))
    expiration = seconds + (60 * 60 * 24)  #24 hours later

    ping_obj = {'pinged': slug, 'when': seconds}
    pings = user.get('pings', None)

    def _handle_creation():
        # Inline because f**k a scope
        created, obj = ping_summary(g.db, slug, expiration)
        if created:
            user['pings'][slug] = ping_obj
            set_user(g.db, user)
            to_return['ping_obj'] = obj
            return Response(json.dumps(to_return), mimetype="application/json")
        else:
            to_return['error'] = obj
            return Response(json.dumps(to_return), mimetype="application/json")

    if not pings:
        user['pings'] = {slug: ping_obj}
        set_user(g.db, user)
        return _handle_creation()
    else:
        last_ping = pings.get(slug, None)
        if not last_ping:
            return _handle_creation()

        if seconds > (last_ping['when'] + (60 * 60 * 24)):
            return _handle_creation()

        to_return['success'] = False
        to_return['error'] = "You need to wait 24 hours to ping again."

    return Response(json.dumps(to_return), mimetype="application/json")