def delete_upload(self, index): """ Given a file index to remove, null out its metadata in our stored file metadata fields. This will also delete any ``SharedFileUpload`` records associated with the file's key (if the file has been shared with a team). Args: index (integer): file index to remove """ file_key = self.get_file_key(index) remove_file(file_key) stored_file_descriptions, stored_file_names, stored_file_sizes = self._get_metadata_from_block( ) stored_file_descriptions[index] = None self._set_file_descriptions(stored_file_descriptions) stored_file_names[index] = None self._set_file_names(stored_file_names) stored_file_sizes[index] = 0 self._set_file_sizes(stored_file_sizes) if self.block.is_team_assignment(): try: SharedFileUpload.by_key(file_key).delete() except SharedFileUpload.DoesNotExist: logger.warning( 'Could not find SharedFileUpload to delete: {}'.format( file_key)) self.invalidate_cached_shared_file_dicts()
def can_delete_file(current_user_id, teams_enabled, key, team_id=None, shared_file=None): """ A user is allowed to delete any file they own if this is not a team-enabled response. If the response is team-enabled, a user, who is a member of a team, is allowed to delete a file they own as long as they are still a member of the team with which the file has been shared. params: current_user_id (string): The anonymous id of the current user in an ORA block. teams_enabled (boolean): Indicates if teams are enabled for an ORA block. key (string): The key of the file to check if we can delete. team_id (string): The id of the team of the user who may be able to delete the file. shared_file (SharedFileUpload): Optional. A SharedFileUpload object corresponding to the given key. It's useful to pass this in if you've already fetched all of the SharedFileUpload records for a given item/team. raises: SharedFileUpload.DoesNotExist If teams are enabled, a team_id is provided, and no SharedFileUpload corresponding to the file key exists. returns: Boolean indicating if the file with the given key can be deleted by the current user. """ if not teams_enabled: return True if not shared_file: try: shared_file = SharedFileUpload.by_key(key) except SharedFileUpload.DoesNotExist: logger.info( 'While checking ORA file-deletion ability, could not find file with key: {}' .format(key)) return True if shared_file.owner_id != current_user_id: return False if shared_file.team_id != team_id: return False # If we've made it this far, the current user has a team, and it's the same # team that the file is shared with, so let them (as the file's owner) delete it. return True