def create_folder(self):
     """
     https://developers.box.com/docs/#folders-create-a-new-folder
     """
     params = json.load(request.body)
     name = params.get('name')
     if name is None:
         abort(400, 'Missing parameter: name')
     parent = params.get('parent')
     if parent is None or 'id' not in parent:
         abort(400, 'Missing parameter: parent(id)')
     parent_id = parent['id']
     try:
         parent_folder = self._db_session.query(FolderModel).filter_by(folder_id=parent_id).one()
     except NoResultFound:
         abort(404)
     owner = get_user_from_header(self._db_session)
     name_in_use = self._db_session.query(FolderModel).filter_by(name=name).count()
     if name_in_use:
         abort(409, 'An item with that name already exists.')
     folder = FolderModel(name=name, parent_id=parent_folder.id, owned_by=owner, created_by=owner)
     self._db_session.add(folder)
     self._db_session.commit()
     self._db_session.add(EventModel(event_type='ITEM_CREATE', source_id=folder.folder_id, source_type='folder'))
     self._db_session.commit()
     return json.dumps(folder)
 def update_folder_info(self, folder_id):
     folder = get_folder_by_id(self._db_session, folder_id)
     params = json.load(request.body)
     for key, value in params.items():
         if not hasattr(FolderModel, key):
             abort(400, 'Folder has no attribute {0}.'.format(key))
         if key == 'parent':
             # Move
             parent_id = value['id']
             parent_folder = get_folder_by_id(self._db_session, parent_id)
             folder.parent_id = parent_folder.id
             self._db_session.add(
                 EventModel(event_type='ITEM_MOVE', source_id=folder.folder_id, source_type='folder'),
             )
         else:
             setattr(folder, key, value)
             if key == 'name':
                 self._db_session.add(
                     EventModel(event_type='ITEM_RENAME', source_id=folder.folder_id, source_type='folder'),
                 )
             elif key == 'sync_state':
                 event_type = 'ITEM_SYNC' if value == FolderSyncState.IS_SYNCED else 'ITEM_UNSYNC'
                 self._db_session.add(
                     EventModel(event_type=event_type, source_id=folder.folder_id, source_type='folder'),
                 )
     self._db_session.commit()
     return json.dumps(folder)
 def update_folder_info(self, folder_id):
     folder = get_folder_by_id(self._db_session, folder_id)
     params = json.load(request.body)
     for key, value in params.items():
         if not hasattr(FolderModel, key):
             abort(400, "Folder has no attribute {0}.".format(key))
         if key == "parent":
             # Move
             parent_id = value["id"]
             parent_folder = get_folder_by_id(self._db_session, parent_id)
             folder.parent_id = parent_folder.id
             self._db_session.add(
                 EventModel(event_type="ITEM_MOVE", source_id=folder.folder_id, source_type="folder")
             )
         else:
             setattr(folder, key, value)
             if key == "name":
                 self._db_session.add(
                     EventModel(event_type="ITEM_RENAME", source_id=folder.folder_id, source_type="folder")
                 )
             elif key == "sync_state":
                 event_type = "ITEM_SYNC" if value == FolderSyncState.IS_SYNCED else "ITEM_UNSYNC"
                 self._db_session.add(
                     EventModel(event_type=event_type, source_id=folder.folder_id, source_type="folder")
                 )
     self._db_session.commit()
     return json.dumps(folder)
 def create_folder(self):
     """
     https://developers.box.com/docs/#folders-create-a-new-folder
     """
     params = json.load(request.body)
     name = params.get("name")
     if name is None:
         abort(400, "Missing parameter: name")
     parent = params.get("parent")
     if parent is None or "id" not in parent:
         abort(400, "Missing parameter: parent(id)")
     parent_id = parent["id"]
     try:
         parent_folder = self._db_session.query(FolderModel).filter_by(folder_id=parent_id).one()
     except NoResultFound:
         abort(404)
     owner = get_user_from_header(self._db_session)
     name_in_use = self._db_session.query(FolderModel).filter_by(name=name).count()
     if name_in_use:
         abort(409, "An item with that name already exists.")
     folder = FolderModel(name=name, parent_id=parent_folder.id, owned_by=owner, created_by=owner)
     self._db_session.add(folder)
     self._db_session.commit()
     self._db_session.add(EventModel(event_type="ITEM_CREATE", source_id=folder.folder_id, source_type="folder"))
     self._db_session.commit()
     return json.dumps(folder)
 def _get_parent(self):
     params = json.load(request.body)
     parent = params.get('parent')
     if parent is None or 'id' not in parent:
         abort(400, 'Missing parameter: parent(id)')
     parent_id = parent['id']
     return get_folder_by_id(self._db_session, parent_id)
Exemple #6
0
 def update_file_info(self, file_id):
     file_object = get_file_by_id(self._db_session, file_id)
     self._check_file_lock(file_object)
     params = json.load(request.body)
     for key, value in params.items():
         if not hasattr(FileModel, key):
             abort(400, 'File has no attribute {0}.'.format(key))
         if key == 'parent':
             # Move
             parent_id = value['id']
             parent_folder = get_folder_by_id(self._db_session, parent_id)
             file_object.parent_id = parent_folder.id
             self._db_session.add(
                 EventModel(event_type='ITEM_MOVE',
                            source_id=file_object.file_id,
                            source_type='file'), )
         else:
             setattr(file_object, key, value)
             if key == 'name':
                 self._db_session.add(
                     EventModel(event_type='ITEM_RENAME',
                                source_id=file_object.file_id,
                                source_type='file'), )
             elif key == 'sync_state':
                 event_type = 'ITEM_SYNC' if value == 'synced' else 'ITEM_UNSYNC'
                 self._db_session.add(
                     EventModel(event_type=event_type,
                                source_id=file_object.file_id,
                                source_type='file'), )
     self._db_session.commit()
     return json.dumps(file_object)
 def update_file_info(self, file_id):
     file_object = get_file_by_id(self._db_session, file_id)
     self._check_file_lock(file_object)
     params = json.load(request.body)
     for key, value in params.items():
         if not hasattr(FileModel, key):
             abort(400, 'File has no attribute {0}.'.format(key))
         if key == 'parent':
             # Move
             parent_id = value['id']
             parent_folder = get_folder_by_id(self._db_session, parent_id)
             file_object.parent_id = parent_folder.id
             self._db_session.add(
                 EventModel(event_type='ITEM_MOVE', source_id=file_object.file_id, source_type='file'),
             )
         else:
             setattr(file_object, key, value)
             if key == 'name':
                 self._db_session.add(
                     EventModel(event_type='ITEM_RENAME', source_id=file_object.file_id, source_type='file'),
                 )
             elif key == 'sync_state':
                 event_type = 'ITEM_SYNC' if value == 'synced' else 'ITEM_UNSYNC'
                 self._db_session.add(
                     EventModel(event_type=event_type, source_id=file_object.file_id, source_type='file'),
                 )
     self._db_session.commit()
     return json.dumps(file_object)