def post(self): # create a new organization # parse data recept by POST and get only fields of the object newobj = self.parseInput(Organization) # getting new integer id newobj['iid'] = yield Task(self.new_iid, Organization.collection()) try: neworg = Organization(newobj) neworg.validate() # the new object is valid, so try to save try: newsaved = yield self.db.organizations.insert( neworg.to_native()) output = neworg.to_native() info(output) output['obj_id'] = str(newsaved) # Change iid to id in the output self.switch_iid(output) self.finish( self.json_encode({ 'status': 'success', 'message': 'new organization saved', 'data': output })) except Exception as e: # duplicated index error self.response(409, 'Duplicated name for an organization.') except Exception as e: # received data is invalid in some way self.response(400, 'Invalid input data.')
response = {'messages':list()} for fn in files: print(jsonpath+fn) data = readData(jsonpath+fn) if fn == 'organizations.json': print('Importing Organizations') for d in data: obj = dict(d) obj['iid'] = obj['id'] del obj['id'] obj['created_at'] = datetime.strptime(obj['created_at'], '%Y-%m-%d %H:%M:%S.%f') obj['updated_at'] = datetime.strptime(obj['updated_at'], '%Y-%m-%d %H:%M:%S.%f') try: newobj = Organization(obj) newobj.validate() nob = db.organizations.insert(newobj.to_native()) print 'added: '+str(nob)+' - '+obj['name'] except ValidationError, e: print('Fail to import organizations. Errors: '+str(e)) break response['messages'].append('organizations imported') elif fn == 'lions.json': print('Importing Organizations') for d in data: obj = dict(d) obj['iid'] = obj['id'] del obj['id'] obj['organization_iid'] = obj['organization_id'] del obj['organization_id'] obj['primary_image_set_iid'] = obj['primary_image_set_id'] del obj['primary_image_set_id']