def put(self): """ Called when HTTP PUT requests are received by the web application. The PUT body is JSON which is deserialized and used as values to update a file in Drive. The authorization access token for this action is retreived from the data store. """ # Create a Drive service service = self.CreateDrive() if service is None: return # Load the data that has been posted as JSON logging.debug('Get JSON data') data = self.RequestJSON() logging.debug('JSON data retrieved %s', json.dumps(data)) logging.info('Updating file %s', data['id']) # Create a new file data structure. content = FileUtils.get_content_from_data(data) #data['indexableText'] = {'text': content['content']} max_try = 5 for n in range(0, max_try): try: if content is not None: # Make an update request to update the file. A MediaInMemoryUpload # instance is used to upload the file body. Because of a limitation, this # request must be made in two parts, the first to update the metadata, and # the second to update the body. resource = service.files().update( fileId=data['id'], newRevision=self.request.get('newRevision', False), body=data, media_body=MediaInMemoryUpload( content, data['mimeType'], resumable=True) ).execute() else: # Only update the metadata, a patch request is prefered but not yet # supported on Google App Engine; see # http://code.google.com/p/googleappengine/issues/detail?id=6316. resource = service.files().update( fileId=data['id'], newRevision=self.request.get('newRevision', False), body=data).execute() # Respond with the new file id as JSON. return self.RespondJSON({'id': resource['id']}) except HttpError, http_error: logging.getLogger("error").exception("Try #%d: Exception occurred when updating file", n) # HTTP status code 403 indicates that the app is not authorized to save the file (third-party app disabled, user without access, etc.) # Don't need to try several times if http_error.resp.status == 403: return self.abort(403) else: time.sleep((2 ** n) + (random.randint(0, 1000) / 1000)) except HTTPException: logging.getLogger("error").exception("Try #%d: Exception occurred when updating file", n) time.sleep((2 ** n) + (random.randint(0, 1000) / 1000))
def put(self): """ Called when HTTP PUT requests are received by the web application. The PUT body is JSON which is deserialized and used as values to update a file in Drive. The authorization access token for this action is retreived from the data store. """ # Create a Drive service service = self.CreateDrive() if service is None: return # Load the data that has been posted as JSON logging.debug('Get JSON data') data = self.RequestJSON() logging.debug('JSON data retrieved %s', json.dumps(data)) logging.info('Updating file %s', data['id']) # Create a new file data structure. content = FileUtils.get_content_from_data(data) #data['indexableText'] = {'text': content['content']} max_try = 5 for n in range(0, max_try): try: if content is not None: # Make an update request to update the file. A MediaInMemoryUpload # instance is used to upload the file body. Because of a limitation, this # request must be made in two parts, the first to update the metadata, and # the second to update the body. resource = service.files().update( fileId=data['id'], newRevision=self.request.get('newRevision', False), body=data, media_body=MediaInMemoryUpload( content, data['mimeType'], resumable=True)).execute() else: # Only update the metadata, a patch request is prefered but not yet # supported on Google App Engine; see # http://code.google.com/p/googleappengine/issues/detail?id=6316. resource = service.files().update( fileId=data['id'], newRevision=self.request.get('newRevision', False), body=data).execute() # Respond with the new file id as JSON. return self.RespondJSON({'id': resource['id']}) except HttpError, http_error: logging.getLogger("error").exception( "Try #%d: Exception occurred when updating file", n) # HTTP status code 403 indicates that the app is not authorized to save the file (third-party app disabled, user without access, etc.) # Don't need to try several times if http_error.resp.status == 403: return self.abort(403) else: time.sleep((2**n) + (random.randint(0, 1000) / 1000)) except HTTPException: logging.getLogger("error").exception( "Try #%d: Exception occurred when updating file", n) time.sleep((2**n) + (random.randint(0, 1000) / 1000))
def post(self): """ Called when HTTP POST requests are received by the web application. The POST body is JSON which is deserialized and used as values to create a new file in Drive. The authorization access token for this action is retrieved from the data store. """ # Create a Drive service service = self.CreateDrive() if service is None: return # Load the data that has been posted as JSON logging.debug('Get JSON data') data = self.RequestJSON() logging.debug('JSON data retrieved %s', json.dumps(data)) content = FileUtils.get_content_from_data(data) max_try = 5 for n in range(0, max_try): try: if 'templateId' in data: body = {'title': 'Your notes'} resource = service.files().copy(fileId=data['templateId'], body=body).execute() else: # Create a new file data structure. resource = { 'title': data['title'], 'description': data['description'], 'mimeType': data['mimeType'], } if 'parent' in data and data['parent']: logging.debug('Creating from a parent folder %s', data['parent']) default_folder_id = data['parent'] else: if 'defaultFolderId' in self.session and self.session['defaultFolderId']: default_folder_id = self.session['defaultFolderId'] else: default_folder_list = service.files().list(q='title="VideoNot.es"').execute() if default_folder_list and 'items' in default_folder_list and len(default_folder_list['items']): default_folder_id = default_folder_list['items'][0]['id'] self.session['defaultFolderId'] = default_folder_id else: folder_ressource = { 'title': 'VideoNot.es', 'mimeType': 'application/vnd.google-apps.folder' } default_folder = service.files().insert(body=folder_ressource).execute() default_folder_id = default_folder['id'] self.session['defaultFolderId'] = default_folder_id resource['parents'] = [{'id':default_folder_id}] # Make an insert request to create a new file. A MediaInMemoryUpload # instance is used to upload the file body. logging.debug('Calling Drive API with content %s', str(content)) resource = service.files().insert( body=resource, media_body=MediaInMemoryUpload( content, data['mimeType'], resumable=True) ).execute() if BaseHandler.is_production(): # clement_permission = { # 'value': '*****@*****.**', # 'type': 'user', # 'role': 'reader' # } anyone_permission = { 'type': 'anyone', 'role': 'reader', 'withLink': True } # try: # logging.info('Add Clement as a reader') # service.permissions().insert(fileId=resource['id'], body=clement_permission).execute() # except HttpError: # logging.info('Error when adding Clement as a reader') try: logging.info('Add anyone as a reader') service.permissions().insert(fileId=resource['id'], body=anyone_permission).execute() except HttpError: logging.info('Error when adding anyone as a reader') # Respond with the new file id as JSON. logging.debug('New ID created %s', resource['id']) return self.RespondJSON({'id': resource['id']}) except AccessTokenRefreshError: # In cases where the access token has expired and cannot be refreshed # (e.g. manual token revoking) redirect the user to the authorization page # to authorize. logging.info('AccessTokenRefreshError') return self.abort(401) except HttpError, http_error: logging.getLogger("error").exception("Try #%d: Exception occurred when creating file", n) # HTTP status code 403 indicates that the app is not authorized to save the file (third-party app disabled, user without access, etc.) # Don't need to try several times if http_error.resp.status == 403: return self.abort(403) else: time.sleep((2 ** n) + (random.randint(0, 1000) / 1000)) except HTTPException: logging.getLogger("error").exception("Try #%d: Exception occurred when creating file", n) time.sleep((2 ** n) + (random.randint(0, 1000) / 1000))
def post(self): """ Called when HTTP POST requests are received by the web application. The POST body is JSON which is deserialized and used as values to create a new file in Drive. The authorization access token for this action is retrieved from the data store. """ # Create a Drive service service = self.CreateDrive() if service is None: return # Load the data that has been posted as JSON logging.debug('Get JSON data') data = self.RequestJSON() logging.debug('JSON data retrieved %s', json.dumps(data)) content = FileUtils.get_content_from_data(data) max_try = 5 for n in range(0, max_try): try: if 'templateId' in data: body = {'title': 'Your notes'} resource = service.files().copy(fileId=data['templateId'], body=body).execute() else: # Create a new file data structure. resource = { 'title': data['title'], 'description': data['description'], 'mimeType': data['mimeType'], } if 'parent' in data and data['parent']: logging.debug('Creating from a parent folder %s', data['parent']) default_folder_id = data['parent'] else: if 'defaultFolderId' in self.session and self.session[ 'defaultFolderId']: default_folder_id = self.session['defaultFolderId'] else: default_folder_list = service.files().list( q='title="VideoNot.es"').execute() if default_folder_list and 'items' in default_folder_list and len( default_folder_list['items']): default_folder_id = default_folder_list[ 'items'][0]['id'] self.session[ 'defaultFolderId'] = default_folder_id else: folder_ressource = { 'title': 'VideoNot.es', 'mimeType': 'application/vnd.google-apps.folder' } default_folder = service.files().insert( body=folder_ressource).execute() default_folder_id = default_folder['id'] self.session[ 'defaultFolderId'] = default_folder_id resource['parents'] = [{'id': default_folder_id}] # Make an insert request to create a new file. A MediaInMemoryUpload # instance is used to upload the file body. logging.debug('Calling Drive API with content %s', str(content)) resource = service.files().insert( body=resource, media_body=MediaInMemoryUpload( content, data['mimeType'], resumable=True)).execute() if BaseHandler.is_production(): # clement_permission = { # 'value': '*****@*****.**', # 'type': 'user', # 'role': 'reader' # } anyone_permission = { 'type': 'anyone', 'role': 'reader', 'withLink': True } # try: # logging.info('Add Clement as a reader') # service.permissions().insert(fileId=resource['id'], body=clement_permission).execute() # except HttpError: # logging.info('Error when adding Clement as a reader') try: logging.info('Add anyone as a reader') service.permissions().insert( fileId=resource['id'], body=anyone_permission).execute() except HttpError: logging.info( 'Error when adding anyone as a reader') # Respond with the new file id as JSON. logging.debug('New ID created %s', resource['id']) return self.RespondJSON({'id': resource['id']}) except AccessTokenRefreshError: # In cases where the access token has expired and cannot be refreshed # (e.g. manual token revoking) redirect the user to the authorization page # to authorize. logging.info('AccessTokenRefreshError') return self.abort(401) except HttpError, http_error: logging.getLogger("error").exception( "Try #%d: Exception occurred when creating file", n) # HTTP status code 403 indicates that the app is not authorized to save the file (third-party app disabled, user without access, etc.) # Don't need to try several times if http_error.resp.status == 403: return self.abort(403) else: time.sleep((2**n) + (random.randint(0, 1000) / 1000)) except HTTPException: logging.getLogger("error").exception( "Try #%d: Exception occurred when creating file", n) time.sleep((2**n) + (random.randint(0, 1000) / 1000))