Exemplo n.º 1
0
 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)
Exemplo n.º 2
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)
Exemplo n.º 3
0
 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)
Exemplo n.º 4
0
 def upload_file(self):
     """
     https://developers.box.com/docs/#files-upload-a-file
     """
     attributes = request.forms.get('attributes') or request.forms.get(
         'metadata')
     if not attributes:
         abort(400, 'Missing parameter: attributes')
     attributes = json.loads(attributes)
     parent = attributes.get('parent')
     if parent is None or 'id' not in parent:
         abort(400, 'Missing parameter: parent(id)')
     parent_id = parent['id']
     folder = get_folder_by_id(self._db_session, parent_id)
     content = request.files.file.file.read()
     file_hash = sha1()
     file_hash.update(content)
     owner = get_user_from_header(self._db_session)
     file_object = FileModel(
         content=content,
         name=attributes.get('name', request.files.file.name),
         parent_id=folder.id,
         sha1=file_hash.hexdigest(),
         size=len(content),
         owned_by=owner,
         created_by=owner,
     )
     self._db_session.add(file_object)
     self._db_session.commit()
     self._db_session.add(
         EventModel(event_type='ITEM_UPLOAD',
                    source_id=file_object.file_id,
                    source_type='file'))
     self._db_session.commit()
     return json.dumps({'entries': [file_object]})
Exemplo n.º 5
0
 def delete_file(self, file_id):
     file_object = get_file_by_id(self._db_session, file_id)
     self._check_file_lock(file_object)
     self._db_session.delete(file_object)
     self._db_session.add(EventModel(event_type='ITEM_TRASH', source_id=file_object.file_id, source_type='file'))
     self._db_session.commit()
     response.status = 204
Exemplo n.º 6
0
 def delete_folder(self, folder_id):
     folder = get_folder_by_id(self._db_session, folder_id)
     self._db_session.delete(folder)
     self._db_session.commit()
     self._db_session.add(
         EventModel(event_type='ITEM_TRASH',
                    source_id=folder.folder_id,
                    source_type='folder'))
     response.status = 204
Exemplo n.º 7
0
 def copy_folder(self, folder_id):
     folder = get_folder_by_id(self._db_session, folder_id)
     parent_folder = self._get_parent()
     self._db_session.expunge(folder)
     make_transient(folder)
     folder.id = None
     folder.folder_id = None
     folder.parent_id = parent_folder.id
     self._db_session.add(folder)
     self._db_session.commit()
     self._db_session.add(EventModel(event_type='ITEM_COPY', source_id=folder.folder_id, source_type='folder'))
     self._db_session.commit()
     return json.dumps(folder)
Exemplo n.º 8
0
 def copy_file(self, file_id):
     file_object = get_file_by_id(self._db_session, file_id)
     self._check_file_lock(file_object)
     parent_folder = self._get_parent()
     self._db_session.expunge(file_object)
     make_transient(file_object)
     file_object.id = None
     file_object.file_id = None
     file_object.parent_id = parent_folder.id
     self._db_session.add(file_object)
     self._db_session.commit()
     self._db_session.add(EventModel(event_type='ITEM_COPY', source_id=file_object.file_id, source_type='file'))
     self._db_session.commit()
     return json.dumps(file_object)
Exemplo n.º 9
0
 def update_file(self, file_id):
     """
     https://developers.box.com/docs/#files-upload-a-new-version-of-a-file
     """
     file_object = get_file_by_id(self._db_session, file_id)
     self._check_file_lock(file_object)
     content = request.files.file.file.read()
     file_hash = sha1()
     file_hash.update(content)
     file_object.content = content
     file_object.sha1 = file_hash.hexdigest()
     file_object.size = len(content)
     self._db_session.add(EventModel(event_type='ITEM_UPLOAD', source_id=file_object.file_id, source_type='file'))
     self._db_session.commit()
     return json.dumps(file_object)