def evospace(request):
    if request.method == 'POST':
        population = Population(popName)
        #print 'Raw Data___: "%s"' % request.body
        #print type(request.body)
        json_data = json.loads(request.body)
        method = json_data["method"]
        params = json_data["params"]
        id = json_data["id"]



        if method == "initialize":
            result = population.initialize()
            data = json.dumps({"result": result, "error": None, "id": id})
            print data
            return HttpResponse(data, mimetype='application/javascript')
        elif method == "getSample":
            #Auto ReInsert
            if population.read_sample_queue_len() >= REINSERT_THRESHOLD:
                population.respawn(5)
            result = population.get_sample(params[0])
            if result:
                data = json.dumps({"result": result, "error": None, "id": id})
            else:
                data = json.dumps({"result": None, "error":
                    {"code": -32601, "message": "EvoSpace empty"}, "id": id})
            return HttpResponse(data, mimetype='application/json')
        elif method == "read_pop_keys":
            result = population.read_pop_keys()
            if result:
                data = json.dumps({"result": result, "error": None, "id": id})
            else:
                data = json.dumps({"result": None, "error":
                    {"code": -32601, "message": "EvoSpace empty"}, "id": id})
            return HttpResponse(data, mimetype='application/json')
        elif method == "read_sample_queue":
            result = population.read_sample_queue()
            if result:
                data = json.dumps({"result": result, "error": None, "id": id})
            else:
                data = json.dumps({"result": None, "error":
                    {"code": -32601, "message": "EvoSpace empty"}, "id": id})
            return HttpResponse(data, mimetype='application/json')

        elif method == "putSample":
            #Cada EVOLUTION_INTERVAL evoluciona
            print "##################"
            if not population.get_returned_counter() % EVOLUTION_INTERVAL:
                try:
                    print "Evolucionando"
                    evolve_Tournament()
                except Exception as e:
                    exc_type, exc_value, exc_traceback = sys.exc_info()
                    print e.message
                    traceback.print_tb(exc_traceback, limit=20, file=sys.stdout)

                pass
            population.put_sample(params[0])

            #Aqui se construye el grafo con el individuo
            if request.user.is_authenticated():
                usr = request.user.username
                first_name = request.user.first_name
                #last_name = request.user.last_name
                name = first_name
                nodo = Nodo()
                person = Person()
                person_result = person.get_person(name)
                activity_stream = Activity_stream()

                #print u
                print "=========Parametros==========="
                print params[0]
                usrLastDate=str(usr_last_date(params[0]['sample'][0]['fitness']))
                print usrLastDate
                usr_date_key = usr + ":" + usrLastDate
                print usr_date_key
                print params[0]['sample'][0]['fitness'][usr_date_key]
                rate = params[0]['sample'][0]['fitness'][usr_date_key]

                if params[0]["individuals_with_like"]:
                    for item in params[0]["individuals_with_like"]:
                        id = item
                        print id
                        individual_node = Graph_Individual()

                        print "prueba ", params[0]["individuals_with_like"]

                        # Verificar si el nodo individual existe con status last
                        individual_node_exist = individual_node.get_node(id)

                        if person_result:
                            nodo1 = node(person_result[0][0])

                        if individual_node_exist: # si la lista esta vacia quiere decir que no existe
                            nodo2 = node(individual_node_exist[0][0])

                        relation = Relations()
                        relation.likes(nodo1,nodo2,rate)

                        #request.user.username in k

                        #Agreagando Activity stream para el verbo like
                        activity_stream.activity("person", "like", "evaluate", usr)

                print "=========Parametros==========="
            else:
                print "Usuario anonimo"


            return HttpResponse(json.dumps("Success"), mimetype='application/json')
        elif method == "init_pop":
            data = init_pop(populationSize=params[0])
            return HttpResponse(json.dumps("Success"), mimetype='application/javascript')
        elif method == "respawn":
            data = population.respawn(n=params[0])
            return HttpResponse(json.dumps("Success"), mimetype='application/javascript')
        elif method == "put_individual":
            print  "params", params[0]
            population.put_individual(**params[0])
            data = json.dumps({"result": None, "error": None, "id": id})
            return HttpResponse(data, mimetype='application/json')


    else:
        return HttpResponse("ajax & post please", mimetype='text')
def home(request):
    #print request
    if request.user.is_authenticated():
        #REFACTOR GET_FRIENDS
        print request.user.username
        face = FacebookSession.objects.get(uid=request.user.username)
        friends = None
        
        friends = face.query("me", connection_type="friends", fields='name,installed')
       
        #print friends
       
        
        #print "Amigos"
        #print friends



            #print "Hola, mundo!!"


        #Consultar nodo usuario
        u = request.user.username
        e = request.user.email
        fn = request.user.first_name
        #ln = request.user.last_name
        na = fn
        n = Nodo()
        p = Person()
        #print na
        person_result = p.get_person(na)
        print "if resultado igual a error"
        print person_result
        print "2323232323232323"
        #print u

        # if not r:
        #     print "creando nodo"
        #     n.create_nodo(element_type="person", id=u, email=e, name=na)
        #     print "creo nodo"
        #     print n

        #print "+++++++++++++++++"
        #print na, r[0][0]["name"]
        #n = Person()
        # participation = p.get_participation(u)
        # participation = participation[0][0]
        # print participation
        # ranking=get_level(participation)
        # print ranking



        if request.method == 'POST':
            if request.POST.has_key("webDesing"):
                web_design = request.POST["webDesing"]
                if web_design == "on":
                    nodo_web_design = Nodo()
                    nodo_web_design_exist = WebDesign() # creando objeto WebDesig
                    web_design_result = nodo_web_design_exist.get_node("web design") # Verificar si hay resultado

                    print "estoy en el if"
                    if not web_design_result: # si lista esta vacia quiere decir que el nodo web design no exist hay que crearlo
                        nodo_web_design.create_nodo(element_type="web_des", name="web design")
                        print "Nodo Web design creado"

                    #nodo_web_design_exist = WebDesign() # creando objeto WebDesig
                    #result = nodo_web_design_exist.get_node("web design") # Verificar si hay resultado

                    nodo1 = node(person_result[0][0])
                    nodo2 = node(web_design_result[0][0])
                    print "Making a relition LIKE between nodes"
                    relation = Relations()
                    relation.likes(nodo1,nodo2)
                    print "You like web design"

            if request.POST.has_key("webDevelopment"):
                web_dev = request.POST["webDevelopment"]
                if web_dev == "on":
                    nodo_web_dev = Nodo()
                    nodo_web_dev_exist = WebDev()
                    web_develoment_result = nodo_web_dev_exist.get_node("web develoment")

                    if not web_develoment_result:
                        nodo_web_dev.create_node(element_type="web_dev", name="web design")

                    web_develoment_result = nodo_web_dev_exist.get_node("web develoment")

                    nodo1 = node(person_result[0][0])
                    nodo2 = node(web_develoment_result[0][0])
                    relation = Relations()
                    relation.likes(nodo1,nodo2)
                    print "You like web develoment"

            if request.POST.has_key("internet"):
                internet = request.POST["internet"]

                if internet == "on":
                    nodo_internet = Nodo()
                    nodo_internet_exist = WebDev
                    web_develoment_result = nodo_web_dev_exist.get_node("web develoment")

                    if not web_develoment_result:
                        nodo_web_dev.create_node(element_type="web_dev", name="web design")

                    web_develoment_result = nodo_web_dev_exist.get_node("web develoment")

                    nodo1 = node(person_result[0][0])
                    nodo2 = node(web_develoment_result[0][0])
                    relation = Relations()
                    relation.likes(nodo1,nodo2)

                    print "You like internet"
                    
            #internet = request.POST["internet"]
            #elementary = request.POST["elementary"]
            #phd = request.POST['PhD']

        if friends:
        # Mejor con FQL
            app_friends = [f for f in friends['data'] if f.has_key('installed')]

            if app_friends:
                for i in range(len(app_friends)):
                    person_relation_knows = p.get_relation_knows(na, app_friends[i]["name"])
                    if person_relation_knows:
                        print "Existe relacion entre ambos"
                        pass
                    else:
                        print "crea relacion knows"
                        friend_result = p.get_person(app_friends[i]["name"])
                        if friend_result:
                            nodo_user = node(person_result[0][0])
                            nodo_friend = node(friend_result[0][0])
                            relation_knows = Relations()
                            relation_knows.knows(nodo_user, nodo_friend)

        else:
            app_friends = None
    else:
        app_friends = None



    return render_to_response('django_index.html', {'static_server': 'https://s3.amazonaws.com/evospace/prototype/',
                                                    'api_server': 'http://app.evospace.org', 'friends': app_friends,
                                                    },
                              context_instance=RequestContext(request))
Example #3
0
def inicio(request):
    #print request
    if request.user.is_authenticated():
        #REFACTOR GET_FRIENDS
        print request.user.username
        face = FacebookSession.objects.get(uid=request.user.username)
        friends = None

        friends = face.query("me",
                             connection_type="friends",
                             fields='name,installed')

        print friends

        #print "Amigos"
        #print friends

        #print "Hola, mundo!!"

        #Condultar nodo usuario
        u = request.user.username
        e = request.user.email
        fn = request.user.first_name
        ln = request.user.last_name
        na = fn + " " + ln
        n = Nodo()
        p = Person()
        print na
        person_result = p.get_person(na)
        print "if resultado igual a error"
        print person_result

        # if not r:
        #     print "creando nodo"
        #     n.create_nodo(element_type="person", id=u, email=e, name=na)
        #     print "creo nodo"
        #     print n

        print "+++++++++++++++++"
        #print na, r[0][0]["name"]

        if request.method == 'POST':
            if request.POST.has_key("webDesing"):
                web_design = request.POST["webDesing"]
                if web_design == "on":
                    nodo_web_design = Nodo()
                    nodo_web_design_exist = WebDesign(
                    )  # creando objeto WebDesig
                    web_design_result = nodo_web_design_exist.get_node(
                        "web design")  # Verificar si hay resultado

                    print "estoy en el if"
                    if not web_design_result:  # si lista esta vacia quiere decir que el nodo web design no exist hay que crearlo
                        nodo_web_design.create_nodo(element_type="web_des",
                                                    name="web design")
                        print "Nodo Web design creado"

                    #nodo_web_design_exist = WebDesign() # creando objeto WebDesig
                    #result = nodo_web_design_exist.get_node("web design") # Verificar si hay resultado

                    nodo1 = node(person_result[0][0])
                    nodo2 = node(web_design_result[0][0])
                    print "Making a relition LIKE between nodes"
                    relation = Relations()
                    relation.likes(nodo1, nodo2)
                    print "You like web design"

            if request.POST.has_key("webDevelopment"):
                web_dev = request.POST["webDevelopment"]
                if web_dev == "on":
                    nodo_web_dev = Nodo()
                    nodo_web_dev_exist = WebDev()
                    web_develoment_result = nodo_web_dev_exist.get_node(
                        "web develoment")

                    if not web_develoment_result:
                        nodo_web_dev.create_node(element_type="web_dev",
                                                 name="web design")

                    web_develoment_result = nodo_web_dev_exist.get_node(
                        "web develoment")

                    nodo1 = node(person_result[0][0])
                    nodo2 = node(web_develoment_result[0][0])
                    relation = Relations()
                    relation.likes(nodo1, nodo2)
                    print "You like web develoment"

            if request.POST.has_key("internet"):
                internet = request.POST["internet"]

                if internet == "on":
                    nodo_internet = Nodo()
                    nodo_internet_exist = WebDev
                    web_develoment_result = nodo_web_dev_exist.get_node(
                        "web develoment")

                    if not web_develoment_result:
                        nodo_web_dev.create_node(element_type="web_dev",
                                                 name="web design")

                    web_develoment_result = nodo_web_dev_exist.get_node(
                        "web develoment")

                    nodo1 = node(person_result[0][0])
                    nodo2 = node(web_develoment_result[0][0])
                    relation = Relations()
                    relation.likes(nodo1, nodo2)

                    print "You like internet"

            #internet = request.POST["internet"]
            #elementary = request.POST["elementary"]
            #phd = request.POST['PhD']

        if friends:
            # Mejor con FQL
            app_friends = [
                f for f in friends['data'] if f.has_key('installed')
            ]

            if app_friends:
                for i in range(len(app_friends)):
                    person_relation_knows = p.get_relation_knows(
                        na, app_friends[i]["name"])
                    if person_relation_knows:
                        print "Existe relacion entre ambos"
                        pass
                    else:
                        print "crea relacion knows"
                        friend_result = p.get_person(app_friends[i]["name"])
                        if friend_result:
                            nodo_user = node(person_result[0][0])
                            nodo_friend = node(friend_result[0][0])
                            relation_knows = Relations()
                            relation_knows.knows(nodo_user, nodo_friend)

        else:
            app_friends = None
    else:
        app_friends = None

    return render_to_response('epanol_index.html', {
        'static_server': 'https://s3.amazonaws.com/evospace/prototype/',
        'api_server': 'http://app.evospace.org',
        'friends': app_friends
    },
                              context_instance=RequestContext(request))
Example #4
0
def evospace(request):
    if request.method == 'POST':
        population = Population(popName)
        #print 'Raw Data___: "%s"' % request.body
        #print type(request.body)
        json_data = json.loads(request.body)
        method = json_data["method"]
        params = json_data["params"]
        id = json_data["id"]

        if method == "initialize":
            result = population.initialize()
            data = json.dumps({"result": result, "error": None, "id": id})
            print data
            return HttpResponse(data, mimetype='application/javascript')
        elif method == "getSample":
            #Auto ReInsert
            if population.read_sample_queue_len() >= REINSERT_THRESHOLD:
                population.respawn(5)
            result = population.get_sample(params[0])
            if result:
                data = json.dumps({"result": result, "error": None, "id": id})
            else:
                data = json.dumps({
                    "result": None,
                    "error": {
                        "code": -32601,
                        "message": "EvoSpace empty"
                    },
                    "id": id
                })
            return HttpResponse(data, mimetype='application/json')
        elif method == "read_pop_keys":
            result = population.read_pop_keys()
            if result:
                data = json.dumps({"result": result, "error": None, "id": id})
            else:
                data = json.dumps({
                    "result": None,
                    "error": {
                        "code": -32601,
                        "message": "EvoSpace empty"
                    },
                    "id": id
                })
            return HttpResponse(data, mimetype='application/json')
        elif method == "read_sample_queue":
            result = population.read_sample_queue()
            if result:
                data = json.dumps({"result": result, "error": None, "id": id})
            else:
                data = json.dumps({
                    "result": None,
                    "error": {
                        "code": -32601,
                        "message": "EvoSpace empty"
                    },
                    "id": id
                })
            return HttpResponse(data, mimetype='application/json')

        elif method == "putSample":
            #Cada EVOLUTION_INTERVAL evoluciona
            print "##################"
            if not population.get_returned_counter() % EVOLUTION_INTERVAL:
                try:
                    print "Evolucionando"
                    evolve_Tournament()
                except Exception as e:
                    exc_type, exc_value, exc_traceback = sys.exc_info()
                    print e.message
                    traceback.print_tb(exc_traceback,
                                       limit=20,
                                       file=sys.stdout)

                pass
            population.put_sample(params[0])

            #Aqui se va armar la machaca del individuo
            if request.user.is_authenticated():
                usr = request.user.username
                first_name = request.user.first_name
                last_name = request.user.last_name
                name = first_name + " " + last_name
                nodo = Nodo()
                person = Person()
                person_result = person.get_person(name)
                activity_stream = Activity_stream()

                #print u
                print "=========Parametros==========="
                print params[0]

                if params[0]["individuals_with_like"]:
                    for item in params[0]["individuals_with_like"]:
                        id = item
                        print id
                        individual_node = Graph_Individual()

                        print "prueba ", params[0]["individuals_with_like"]

                        # Verificar si el nodo individual existe con status last
                        individual_node_exist = individual_node.get_node(id)

                        if person_result:
                            nodo1 = node(person_result[0][0])

                        if individual_node_exist:  # si la lista esta vacia quiere decir que no existe
                            nodo2 = node(individual_node_exist[0][0])

                        relation = Relations()
                        relation.likes(nodo1, nodo2)

                        #request.user.username in k

                        #Agreagando Activity stream para el verbo like
                        activity_stream.activity("person", "like", "evaluate",
                                                 usr)

                        #Curret experience calculation
                        current_experience(request)

                print "=========Parametros==========="
            else:
                print "Usuario anonimo"

            return HttpResponse(json.dumps("Success"),
                                mimetype='application/json')
        elif method == "init_pop":
            data = init_pop(populationSize=params[0])
            return HttpResponse(json.dumps("Success"),
                                mimetype='application/javascript')
        elif method == "respawn":
            data = population.respawn(n=params[0])
            return HttpResponse(json.dumps("Success"),
                                mimetype='application/javascript')
        elif method == "put_individual":
            print "params", params[0]
            population.put_individual(**params[0])
            data = json.dumps({"result": None, "error": None, "id": id})
            return HttpResponse(data, mimetype='application/json')

    else:
        return HttpResponse("ajax & post please", mimetype='text')
def home(request):
    #print request
    if request.user.is_authenticated():
        #REFACTOR GET_FRIENDS
        face = FacebookSession.objects.get(uid=request.user.username)
        print "FACEBOOK"
        print face
        friends = face.query("me", connection_type="friends", fields='name,installed')
        print friends


        #print "Hola, mundo!!"
        #r = redis.StrictRedis(host='localhost', port=6379, db=0)
        from lib.store import redis
        r = redis
        experience = 0

        #Condultar nodo usuario
        u = request.user.username
        e = request.user.email
        fn = request.user.first_name
        ln = request.user.last_name
        na = fn + " " + ln
        n = Nodo()
        p = Person()
        print na
        person_result = p.get_person(na)
        print "if resultado igual a error"
        print person_result

        # if not r:
        #     print "creando nodo"
        #     n.create_nodo(element_type="person", id=u, email=e, name=na)
        #     print "creo nodo"
        #     print n

        print "+++++++++++++++++"
        #print na, r[0][0]["name"]

        if request.method == 'POST':
            if request.POST.has_key("webDesing"):
                web_design = request.POST["webDesing"]
                if web_design == "on":
                    nodo_web_design = Nodo()
                    nodo_web_design_exist = WebDesign() # creando objeto WebDesig
                    web_design_result = nodo_web_design_exist.get_node("web design") # Verificar si hay resultado

                    print "estoy en el if"
                    if not web_design_result: # si lista esta vacia quiere decir que el nodo web design no exist hay que crearlo
                        nodo_web_design.create_nodo(element_type="web_des", name="web design")
                        print "Nodo Web design creado"

                    #nodo_web_design_exist = WebDesign() # creando objeto WebDesig
                    #result = nodo_web_design_exist.get_node("web design") # Verificar si hay resultado

                    nodo1 = node(person_result[0][0])
                    nodo2 = node(web_design_result[0][0])
                    print "Making a relition LIKE between nodes"
                    relation = Relations()
                    relation.likes(nodo1,nodo2)
                    print "You like web design"

            if request.POST.has_key("webDevelopment"):
                web_dev = request.POST["webDevelopment"]
                if web_dev == "on":
                    nodo_web_dev = Nodo()
                    nodo_web_dev_exist = WebDev()
                    web_develoment_result = nodo_web_dev_exist.get_node("web develoment")

                    if not web_develoment_result:
                        nodo_web_dev.create_node(element_type="web_dev", name="web design")

                    web_develoment_result = nodo_web_dev_exist.get_node("web develoment")

                    nodo1 = node(person_result[0][0])
                    nodo2 = node(web_develoment_result[0][0])
                    relation = Relations()
                    relation.likes(nodo1,nodo2)
                    print "You like web develoment"

            if request.POST.has_key("internet"):
                internet = request.POST["internet"]

                if internet == "on":
                    nodo_internet = Nodo()
                    nodo_internet_exist = WebDev
                    web_develoment_result = nodo_web_dev_exist.get_node("web develoment")

                    if not web_develoment_result:
                        nodo_web_dev.create_node(element_type="web_dev", name="web design")

                    web_develoment_result = nodo_web_dev_exist.get_node("web develoment")

                    nodo1 = node(person_result[0][0])
                    nodo2 = node(web_develoment_result[0][0])
                    relation = Relations()
                    relation.likes(nodo1,nodo2)

                    print "You like internet"

            #internet = request.POST["internet"]
            #elementary = request.POST["elementary"]
            #phd = request.POST['PhD']

        if friends:
        #Mejor con FQL
            app_friends = [f for f in friends['data'] if f.has_key('installed')]

            if app_friends:
                for i in range(len(app_friends)):
                    person_relation_knows = p.get_relation_knows(na, app_friends[i]["name"])
                    if person_relation_knows:
                        print "Existe relacion entre ambos"
                        pass
                    else:
                        print "crea relacion knows"
                        friend_result = p.get_person(app_friends[i]["name"])
                        if friend_result:
                            nodo_user = node(person_result[0][0])
                            nodo_friend = node(friend_result[0][0])
                            relation_knows = Relations()
                            relation_knows.knows(nodo_user, nodo_friend)

        else:
            app_friends = None

        # Calculo de experiencia del usuario
       

    
        

        #json_string = json.dumps(user_activity[0])
        #j=json_string.replace("'", "\"")
        #s=l[0].replace("'", "\"")
        #d = ast.literal_eval(s)


        
        current_experience(request)
        print "user=======" + " " + request.user.username



    else:
        app_friends = None

          #'friends': app_friends 
    return render_to_response('django_index.html', {'static_server': 'https://s3.amazonaws.com/evospace/prototype/',
                                                    'api_server': 'http://app.evospace.org', 'friends': app_friends },
                              context_instance=RequestContext(request))