def create(self, request, db): """ Example JSON as POST body: 'id':'20001', 'title':'Enrichment of Affiliation Networks...', 'abstract':'As a result of the Linking Open Data project...' 'year': 2010 @return: 409 Conflict/Duplicate 201 Created 400 Bad Request @TODO: create concepts, create broaders """ if db == 'projects': db = 'default' elif db != 'publications': resp = rc.BAD_REQUEST resp.write(" No database specified") logging.debug(resp) return resp 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 or 'title' not in attrs: resp = rc.BAD_REQUEST resp.write(" Missing id or title") 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 = self.model(id=int(attrs['id']), title=attrs['title'], year=attrs.get('year', None), abstract=attrs.get('abstract', None)) # p = self.model(**attrs) p.save(using=db) logging.debug("created I: " + p.__unicode__()) except IntegrityError: logging.debug(rc.DUPLICATE_ENTRY) return rc.DUPLICATE_ENTRY else: if db == 'default' and p.abstract: concepts = extract_concepts(p.abstract) create_CI_from_I(p, concepts, db) generate_CCp(p, db) # search broaders # update CCb return rc.CREATED
def fpggdb2models(): db = 'default' 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,name,abstract FROM fpgg_project""") 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, abstract=a) # extract concepts if p.abstract: concepts = extract_concepts(p.abstract) create_CI_from_I(p, concepts, db) generate_CCp(p, db) #extract actors c.execute("""select id,fpgg_user_id, fpgg_project_id from fpgg_project_user where fpgg_project_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)