Example #1
0
 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.')
 def publish_point_received(
     self,
     point_id: PyObjectId,
     organization_id: PyObjectId,
     device_id: PyObjectId,
 ):
     topic = self.topic_to_routing_key(self.topic_point_received)
     topic = topic.replace("#", str(organization_id))
     organization = db.organizations.find_one({"_id": organization_id})
     organization = Organization.validate(organization)
     organization_dict = organization.dict(by_alias=True)
     organization_dict["_id"] = str(organization.id)
     result = json.dumps(
         {
             "point_id": str(point_id),
             "device_id": str(device_id),
             "organization": organization_dict,
         },
         ensure_ascii=False,
     )
     self.mqtt.publish(topic, result, qos=0, retain=True)
     OnMessageLog(logging.INFO, "포인트 수신 신호 전달", topic, result).report()
Example #3
0
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']