Ejemplo n.º 1
0
def add_delete_from_whitelist(request):
    user = authenticate_user(request)
    if user is None:
        return json_response({ "code":404, "error": "Username or password incorrect" }) 

    privacysettings = user.privacysettings_set.all()[0]
    json = request.raw_post_data

    if len(json.strip()) == 0:
        return json_response({ "code":200 })

    add_dels = JSONDecoder().decode(json)
    assert type(add_dels) == dict, "Received thing not a dict, erroring"
    adds = add_dels['add']
    dels = add_dels['delete']
    # delete urls
    if privacysettings.whitelist is None:   privacysettings.whitelist = ''
    
    wl = privacysettings.whitelist.split(' ')
    wl = filter( lambda x : x not in dels , wl)
    wl = wl +  [ x for x in adds if not x in wl ] 
    privacysettings.whitelist = ' '.join(wl)
    
    # Save 
    privacysettings.save()
    return json_response({ "code":200 });
Ejemplo n.º 2
0
def add_delete_from_whitelist(request):
    user = authenticate_user(request)
    if user is None:
        return json_response({
            "code": 404,
            "error": "Username or password incorrect"
        })

    privacysettings = user.privacysettings_set.all()[0]
    json = request.raw_post_data

    if len(json.strip()) == 0:
        return json_response({"code": 200})

    add_dels = JSONDecoder().decode(json)
    assert type(add_dels) == dict, "Received thing not a dict, erroring"
    adds = add_dels['add']
    dels = add_dels['delete']
    # delete urls
    if privacysettings.whitelist is None: privacysettings.whitelist = ''

    wl = privacysettings.whitelist.split(' ')
    wl = filter(lambda x: x not in dels, wl)
    wl = wl + [x for x in adds if not x in wl]
    privacysettings.whitelist = ' '.join(wl)

    # Save
    privacysettings.save()
    return json_response({"code": 200})
Ejemplo n.º 3
0
def friend_add(request): # this sends a friend request to the user
    if request.GET.has_key('username'):
        friend = get_object_or_404(User, username=request.GET['username'])
        friendship = FriendRequest(
            from_friend=request.user,
            to_friend=friend
            )
        try:
            friendship.save()             
            request.user.message_set.create(
                message='you are now following %s.' % friend.username
                )
        except:           
            friendship = FriendRequest(
                from_friend=friend,
                to_friend=request.user
                )
        
            friendship.save()
            request.user.message_set.create(
                message='you are now following %s.' % friend.username
                )
        return json_response({"code":200 })
    else:
        return json_response({"code":404 })
Ejemplo n.º 4
0
def login(request):
    request_user = basicauth_get_user_by_emailaddr(request);
    if not request_user:
        resp = json_response({"code":401,'autherror':"Incorrect user/password combination"})
        resp.status_code = 401;
        return resp
    resp = json_response({"code":200,"study1":is_consenting_study1(request_user),"study2":is_consenting_study2(request_user)})
    resp.status_code = 200;
    print "returning resp %s // %s " % (repr(is_consenting_study1(request_user)),repr(is_consenting_study2(request_user)))
    return resp
Ejemplo n.º 5
0
def post_events(request):
    ## lets the user post new activity in a giant single array of activity log elements
    request_user = authenticate_user(request);
    if not request_user:
        return json_response({"error":"Incorrect user/password combination"}, 401)
    
    logs = JSONDecoder().decode(request.raw_post_data)
    committed = [save_entry(entry, request_user) for entry in logs ]
    hosts = uniq([log['entity']['host'] for log in logs], lambda x:x,None)
    notifications = get_notifications_for_user(request_user,hosts) 
    return json_response({"committed":len(committed), "notifications":notifications}, 200)
Ejemplo n.º 6
0
def get_privacy_urls(request):
    user = authenticate_user(request)
    if user is None:
        return json_response({ "code":404, "error": "Username or password incorrect" }) 
        
    privacysettings = user.privacysettings_set.all()[0]
    
    lst = []
    if privacysettings.whitelist is not None:
        lst = privacysettings.whitelist.split() 

    return json_response({ "code":200, "results": lst }) 
Ejemplo n.º 7
0
def set_consenting_view(request):
    request_user = basicauth_get_user_by_emailaddr(request)
    if not request_user:
        resp = json_response({"code":401,'autherror':"Incorrect user/password combination"})
        resp.status_code = 401;
        return resp

    value = JSONDecoder().decode(request.raw_post_data)['consenting']
    print "set_consenting %s " % repr(value)
    set_consenting(request_user,value)
    resp = json_response({"code":200})
    resp.status_code = 200;
    return resp
Ejemplo n.º 8
0
def plugin_login_django(request):
    if request.user and request.user.username:
        return json_response({},200)

    # this user object does not have a 'backend' object attached to it and cannot be logged in                                                                                                                                                                                                                                # user = authenticate_user(request)                                                                                                                                                                                                                                                                                   

    decoded =  jv3.utils.basicauth_decode_email_password(request)
    if decoded is not None and len(decoded) == 2 and len(decoded[0]) > 0 and len(decoded[1]) > 0:
        user = authenticate(username=decoded[0], password=decoded[1])
        if user is not None:
            login(request, user)
            return json_response({},200)
        return json_response({"error":"was unable to log you in"},401);

    return json_response({"error":"Incorrect user/password combination"},401);
Ejemplo n.º 9
0
def delete_privacy_url(request):
    ## added by emax:
    user = authenticate_user(request)
    if user is None:
        return json_response({ "code":404, "error": "Username or password incorrect" }) 
    
    privacysettings = user.privacysettings_set.all()[0] 

    inpt = request.GET['input'].strip()

    privacysettings.whitelist = ' '.join([ x for x in privacysettings.whitelist.split() if not x == inpt])

    privacysettings.save()
    #return HttpResponseRedirect('/settings/')
    return json_response({ "code":200 }) 
Ejemplo n.º 10
0
def get_most_recent_event_time(request):
    err = ''
    try:
        user = authenticate_user(request);    
        if not user:
            return json_response({"error":"Incorrect user/password combination"},401);
        #print "!! get_most_recent %s " % _get_client(request)
        most_recent_activity = Event.objects.filter(owner=user,client=_get_client(request)).order_by("-start");
        if most_recent_activity.count() > 0:
            #print " most recent %d " % int(most_recent_activity[0].start)
            return json_response({'value':int(most_recent_activity[0].start)},200)
        return json_response({'value':0},200)
    except:
        err = sys.exc_info()                
    return json_response({'message':err},500)
Ejemplo n.º 11
0
def get_privacy_urls(request):
    user = authenticate_user(request)
    if user is None:
        return json_response({
            "code": 404,
            "error": "Username or password incorrect"
        })

    privacysettings = user.privacysettings_set.all()[0]

    lst = []
    if privacysettings.whitelist is not None:
        lst = privacysettings.whitelist.split()

    return json_response({"code": 200, "results": lst})
Ejemplo n.º 12
0
def plugin_login_django(request):
    if request.user and request.user.username:
        return json_response({}, 200)

    # this user object does not have a 'backend' object attached to it and cannot be logged in                                                                                                                                                                                                                                # user = authenticate_user(request)

    decoded = jv3.utils.basicauth_decode_email_password(request)
    if decoded is not None and len(decoded) == 2 and len(
            decoded[0]) > 0 and len(decoded[1]) > 0:
        user = authenticate(username=decoded[0], password=decoded[1])
        if user is not None:
            login(request, user)
            return json_response({}, 200)
        return json_response({"error": "was unable to log you in"}, 401)

    return json_response({"error": "Incorrect user/password combination"}, 401)
Ejemplo n.º 13
0
def delete_url_entry(request):
    urlID = request.POST['ID'].strip()

    url_entry = PageView.objects.filter(id=urlID)
    url_entry.delete()

    return json_response({ "code":200 });
Ejemplo n.º 14
0
def get_user_profile(request):
    user = authenticate_user(request)
    enduser = get_enduser_for_user(user)

    first_name = enduser.user.first_name
    last_name = enduser.user.last_name
    email = enduser.user.email
    gender = enduser.gender
    location = str(Country.objects.filter(printable_name=enduser.location)[0].name)
    homepage = ""
    if enduser.homepage:
        homepage = enduser.homepage
    
    birthdate = ""
    if enduser.birthdate:
        birthdate = enduser.birthdate
    #photo = enduser.photo

    tags = ""
    if enduser.tags:
        tags = ' '.join(tag.name for tag in enduser.tags.all())

    response = {
        'id':enduser.user.id,
        'first_name': first_name,
        'last_name': last_name,
        'email': email,
        'location': location,
        'tags': tags,
        'homepage': homepage,
        'birthdate': birthdate.ctime(),
        'gender': gender,
        }

    return json_response({ "code":200, "results": response }) 
Ejemplo n.º 15
0
def post_events(request):
    ## lets the user post new activity in a giant single array of activity log elements
    request_user = authenticate_user(request)
    if not request_user:
        return json_response({"error": "Incorrect user/password combination"},
                             401)

    logs = JSONDecoder().decode(request.raw_post_data)
    committed = [save_entry(entry, request_user) for entry in logs]
    hosts = uniq([log['entity']['host'] for log in logs], lambda x: x, None)
    notifications = get_notifications_for_user(request_user, hosts)
    return json_response(
        {
            "committed": len(committed),
            "notifications": notifications
        }, 200)
Ejemplo n.º 16
0
def delete_url_entry(request):
    urlID = request.POST['ID'].strip()

    url_entry = PageView.objects.filter(id=urlID)
    url_entry.delete()

    return json_response({"code": 200})
Ejemplo n.º 17
0
def set_consenting_view(request):
    request_user = basicauth_get_user_by_emailaddr(request)
    if not request_user:
        resp = json_response({
            "code": 401,
            'autherror': "Incorrect user/password combination"
        })
        resp.status_code = 401
        return resp

    value = JSONDecoder().decode(request.raw_post_data)['consenting']
    print "set_consenting %s " % repr(value)
    set_consenting(request_user, value)
    resp = json_response({"code": 200})
    resp.status_code = 200
    return resp
Ejemplo n.º 18
0
def add_groups(request):
    username = request.user.username
    user = get_object_or_404(User, username=username)
    enduser = get_enduser_for_user(user)
    if request.POST.has_key('groups'):
        groups = request.POST['groups'].split()
        #print groups

        for group in groups:
            if re.search(r'^(/w|/W|[^<>+?$%{}&])+$', group):
                tag, dummy = UserTag.objects.get_or_create(name=group)
                enduser.tags.add(tag)

        enduser.save()
        result = ' '.join(tag.name for tag in enduser.tags.all())

        return json_response({"code": 200, "results": result})
    return json_response({"code": 501})
Ejemplo n.º 19
0
def get_most_recent_event_time(request):
    err = ''
    try:
        user = authenticate_user(request)
        if not user:
            return json_response(
                {"error": "Incorrect user/password combination"}, 401)
        #print "!! get_most_recent %s " % _get_client(request)
        most_recent_activity = Event.objects.filter(
            owner=user, client=_get_client(request)).order_by("-start")
        if most_recent_activity.count() > 0:
            #print " most recent %d " % int(most_recent_activity[0].start)
            return json_response({'value': int(most_recent_activity[0].start)},
                                 200)
        return json_response({'value': 0}, 200)
    except:
        err = sys.exc_info()
    return json_response({'message': err}, 500)
Ejemplo n.º 20
0
def login(request):
    request_user = basicauth_get_user_by_emailaddr(request)
    if not request_user:
        resp = json_response({
            "code": 401,
            'autherror': "Incorrect user/password combination"
        })
        resp.status_code = 401
        return resp
    resp = json_response({
        "code": 200,
        "study1": is_consenting_study1(request_user),
        "study2": is_consenting_study2(request_user)
    })
    resp.status_code = 200
    print "returning resp %s // %s " % (repr(is_consenting_study1(
        request_user)), repr(is_consenting_study2(request_user)))
    return resp
Ejemplo n.º 21
0
def friend_add(request):  # this sends a friend request to the user
    if request.GET.has_key('username'):
        friend = get_object_or_404(User, username=request.GET['username'])
        friendship = FriendRequest(from_friend=request.user, to_friend=friend)
        try:
            friendship.save()
            request.user.message_set.create(
                message='you are now following %s.' % friend.username)
        except:
            friendship = FriendRequest(from_friend=friend,
                                       to_friend=request.user)

            friendship.save()
            request.user.message_set.create(
                message='you are now following %s.' % friend.username)
        return json_response({"code": 200})
    else:
        return json_response({"code": 404})
Ejemplo n.º 22
0
def delete_privacy_url(request):
    ## added by emax:
    user = authenticate_user(request)
    if user is None:
        return json_response({
            "code": 404,
            "error": "Username or password incorrect"
        })

    privacysettings = user.privacysettings_set.all()[0]

    inpt = request.GET['input'].strip()

    privacysettings.whitelist = ' '.join(
        [x for x in privacysettings.whitelist.split() if not x == inpt])

    privacysettings.save()
    #return HttpResponseRedirect('/settings/')
    return json_response({"code": 200})
Ejemplo n.º 23
0
def get_user_following(request):
    user = authenticate_user(request)
    following = [
        friendship.to_friend.username
        for friendship in user.friend_set.all().order_by('to_friend__username')
    ]
    followers = [
        friendship.from_friend.username for friendship in
        user.to_friend_set.all().order_by('from_friend__username')
    ]
    return json_response({"code": 200, "results": [following, followers]})
Ejemplo n.º 24
0
def add_groups(request):
    username = request.user.username
    user = get_object_or_404(User, username=username)
    enduser = get_enduser_for_user(user)
    if request.POST.has_key('groups'):    
        groups = request.POST['groups'].split()
        #print groups

        for group in groups:
            if re.search(r'^(/w|/W|[^<>+?$%{}&])+$', group):
                tag, dummy = UserTag.objects.get_or_create(name=group)
                enduser.tags.add(tag) 

        enduser.save()
        result = ' '.join(
            tag.name for tag in enduser.tags.all()
            )

        return json_response({ "code":200, "results": result }) 
    return json_response({ "code":501 }) 
Ejemplo n.º 25
0
def add_privacy_url(request):
    ## added by emax:
    user = authenticate_user(request)
    if user is None:
        return json_response({
            "code": 404,
            "error": "Username or password incorrect"
        })

    privacysettings = user.privacysettings_set.all()[0]

    request_inpt = request.GET['input'].strip()
    if request_inpt.split(','):
        request_inpt = request_inpt.split(',')

    for inpt in request_inpt:
        if inpt.startswith('http'):
            host = urlparse.urlparse(inpt)[1].strip()
        else:
            host = inpt
            if "/" in host:
                host = host[0:host.find("/")]

        val = {}

        if len(host) > 0:
            if privacysettings.whitelist is not None:
                wlist = privacysettings.whitelist.split(' ')
                if not host in wlist:
                    privacysettings.whitelist = ' '.join(wlist + [host])
                    val["host"] = host
            else:
                privacysettings.whitelist = host
                val["host"] = host

    # Save
    privacysettings.save()
    ## val will be non-null iff it's new
    return json_response(val, 200)
Ejemplo n.º 26
0
def add_privacy_url(request):
    ## added by emax:
    user = authenticate_user(request)
    if user is None:
        return json_response({ "code":404, "error": "Username or password incorrect" }) 

    privacysettings = user.privacysettings_set.all()[0]

    request_inpt = request.GET['input'].strip()
    if request_inpt.split(','):
        request_inpt = request_inpt.split(',')        

    for inpt in request_inpt:
        if inpt.startswith('http'):
            host = urlparse.urlparse(inpt)[1].strip()
        else:
            host = inpt
            if "/" in host:
                host = host[0:host.find("/")]

        val = {}

        if len(host) > 0:
            if privacysettings.whitelist is not None:
                wlist = privacysettings.whitelist.split(' ')
                if not host in wlist:
                    privacysettings.whitelist = ' '.join(wlist + [host])
                    val["host"] = host
            else:
                privacysettings.whitelist = host
                val["host"] = host
            
    # Save 
    privacysettings.save()
    ## val will be non-null iff it's new
    return json_response(val,200)
Ejemplo n.º 27
0
def post_usage_statistics(request):
    print "usage stats"
    print request.raw_post_data
    try:
        slog = ServerLog()
        request_user = basicauth_get_user_by_emailaddr(request)
        if request_user:
            slog.user = request_user
        slog.when = current_time_decimal()
        slog.action = "usage_statistics"
        slog.info = request.raw_post_data
        slog.host = ""
        slog.url = "post_diagnostics"
        slog.request = ""
        slog.save()
        return json_response("[]", 200)
    except:
        excinfo = sys.exc_info()
        response = HttpResponse(repr(excinfo), 'text/html')
        response.status_code = 500
        return response
Ejemplo n.º 28
0
def post_usage_statistics(request):
    print "usage stats"
    print request.raw_post_data
    try:
        slog = ServerLog()    
        request_user = basicauth_get_user_by_emailaddr(request);    
        if request_user:
            slog.user = request_user
        slog.when = current_time_decimal()
        slog.action = "usage_statistics"
        slog.info = request.raw_post_data
        slog.host = ""
        slog.url="post_diagnostics"
        slog.request=""
        slog.save()
        return json_response("[]", 200);
    except:
        excinfo = sys.exc_info()
        response = HttpResponse(repr(excinfo), 'text/html');
        response.status_code = 500
        return response
Ejemplo n.º 29
0
def get_user_profile(request):
    user = authenticate_user(request)
    enduser = get_enduser_for_user(user)

    first_name = enduser.user.first_name
    last_name = enduser.user.last_name
    email = enduser.user.email
    gender = enduser.gender
    location = str(
        Country.objects.filter(printable_name=enduser.location)[0].name)
    homepage = ""
    if enduser.homepage:
        homepage = enduser.homepage

    birthdate = ""
    if enduser.birthdate:
        birthdate = enduser.birthdate
    #photo = enduser.photo

    tags = ""
    if enduser.tags:
        tags = ' '.join(tag.name for tag in enduser.tags.all())

    response = {
        'id': enduser.user.id,
        'first_name': first_name,
        'last_name': last_name,
        'email': email,
        'location': location,
        'tags': tags,
        'homepage': homepage,
        'birthdate': birthdate.ctime(),
        'gender': gender,
    }

    return json_response({"code": 200, "results": response})
Ejemplo n.º 30
0
def plugin_login(request):
    user = authenticate_user(request)
    if not user:
        return json_response({"error": "Incorrect user/password combination"},
                             401)
    return json_response({}, 200)
Ejemplo n.º 31
0
def get_user_following(request):
    user = authenticate_user(request)
    following = [friendship.to_friend.username for friendship in user.friend_set.all().order_by('to_friend__username')]
    followers = [friendship.from_friend.username for friendship in user.to_friend_set.all().order_by('from_friend__username')]
    return json_response({ "code":200, "results": [following, followers] }) 
Ejemplo n.º 32
0
def plugin_login(request):
    user = authenticate_user(request);
    if not user:
        return json_response({"error":"Incorrect user/password combination"},401);
    return json_response({},200)