Ejemplo n.º 1
0
def fpggdbpublications2models():
    db = 'publications'

    con = MySQLdb.connect(SN_DBSERVER, SN_DBUSER, SN_DBPW, SN_DBNAME, use_unicode=True, charset='utf8')
    c = con.cursor()

    #extract project
    c.execute("""SELECT id,title,date FROM fpgg_publication""")
    projects = c.fetchall()
    t = a = ""

    for project in projects:
        i, t, a = project
        #p, created = Instance.objects.using(db).get_or_create(id=i, title=t.encode("utf-8"), abstract=a.encode("utf-8"))
        p, created = Instance.objects.using(db).get_or_create(id=i, title=t, year=a.year)


        # extract concepts
#        if p.abstract:
#            concepts = extract_concepts(p.abstract)
#            create_CI_from_I(p, concepts, db)
        c.execute("""select fpgg_tag_id from fpgg_publication_tag where fpgg_publication_id = %s""", i)
        concept_ids = c.fetchall()
        for concept_id in concept_ids:
            c.execute("""select name from fpgg_tag where id = %s""", concept_id)
            concept_name = c.fetch_row()
            concept, created = Concept.objects.using(db).\
                get_or_create(id=concept_id, name=concept_name)
            if created: logging.debug("Created C: " + concept.__unicode__())
            weight = 1
            pconcept, created = InstanceConcept.objects.using(db).\
                get_or_create(instance=p, concept=concept, weight=weight)
            if created: 
                    logging.debug("Created CI: %s" % pconcept)
    
        generate_CCp(p, db)

        #extract actors
        c.execute("""select id, fpgg_user_id, fpgg_publication_id from fpgg_user_publication where fpgg_publication_id=%s""", i)
        projectactors = c.fetchall()
        if projectactors:
            weight = 1.0 / len(projectactors)
            f = l = ""
            for projectactor in projectactors:
                c.execute("""select id,first_name,last_name from fpgg_user where id=%s""", projectactor[1])
                actor = c.fetchone()
                i, f, l = actor
#                a, created = Actor.objects.using(db).get_or_create(id=i, name=f.encode("utf-8") + ' ' + l.encode("utf-8"))
                a, created = Actor.objects.using(db).get_or_create(id=i, name=f + ' ' + l)
    #            print "actor %s created" % actor[1]

                pa, created = InstanceActor.objects.using(db).get_or_create(id=projectactor[0], actor=a, instance=p, weight=weight)

                create_AAp_from_I_A(a, p, db)
                create_AC_from_A_I(a, p, db)
                create_CCa_from_A_I(a, p, db)
                create_AAc_from_A_I(a, p, db)


    con.close()
Ejemplo n.º 2
0
    def create(self, request, db, instance_id, actor_id):
        """
        @param instance_id: project id
        @param actor_id: actor id
        weight: 1/total number of actors in the project
        Example JSON as POST body:
            "id": "2",
            "weight": "1.0", 
        @return: 
            404 Not Found
            409 Conflict/Duplicate
            201 Created
            400 Bad Request
        """
        if db == 'projects': db = 'default'
        elif db != 'publications':
            resp = rc.BAD_REQUEST
            resp.write(" No database specified")
            logging.debug(resp)
            return resp
        logging.debug(str(instance_id) + str(actor_id))
        attrs = self.flatten_dict(request.POST)
        if request.content_type:
            try:
                data = request.data
            except AttributeError:
                pass
            else:
                attrs = data
        logging.debug(attrs)
        if 'id' not in attrs:
            resp = rc.BAD_REQUEST
            resp.write(" Missing id")
            logging.debug(resp)
            return resp
#        elif self.model.objects.using(db).filter(id=attrs['id']):
#            logging.debug(rc.DUPLICATE_ENTRY)
#            return rc.DUPLICATE_ENTRY
        else:
            try:
                p = Instance.objects.using(db).get(id=instance_id)
                a = Actor.objects.using(db).get(id=actor_id)
            except ObjectDoesNotExist:
                logging.debug(rc.NOT_FOUND)
                return rc.NOT_FOUND
            else:
                try:
                    pa = self.model(id=attrs['id'], instance=p, actor=a,
                                    weight=attrs.get('weight', None))
                    pa.save(using=db)
                    logging.debug("Created PA: " + pa.__unicode__())

                except IntegrityError:
                    logging.debug(rc.DUPLICATE_ENTRY)
                    return rc.DUPLICATE_ENTRY
                else:
                    create_AC_from_A_I(a, p, db)
                    print "after AC"
                    create_AAp_from_I_A(a, p, db)
                    print "after AAi"

                    create_CCa_from_A_I(a, p, db)
                    print "after CCa"
                    create_AAc_from_A_I(a, p, db) # a1axcx, a1aycy...
                    print "after AAc"

                    return rc.CREATED