Exemplo n.º 1
0
    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()
Exemplo n.º 2
0
 def shared_uploads_for_student_by_key(self):
     """
     Returns **and caches** all of the SharedFileUpload records
     for this student/course/item.
     """
     shared_uploads = SharedFileUpload.by_student_course_item(**self.student_item_dict)
     return {shared_upload.file_key: shared_upload for shared_upload in shared_uploads}
Exemplo n.º 3
0
def delete_shared_files_for_team(course_id, item_id, team_id):
    """
    Delete shared files for a team for this block
    """
    uploads = SharedFileUpload.by_team_course_item(team_id, course_id, item_id)

    for upload in uploads:
        remove_file(upload.file_key)
        upload.delete()
Exemplo n.º 4
0
 def shared_uploads_for_team_by_key(self):
     """
     Returns **and caches** all of the SharedFileUpload records
     for this student/course/item.
     """
     shared_uploads = SharedFileUpload.by_team_course_item(
         team_id=self.team_id,
         course_id=self.student_item_dict['course_id'],
         item_id=self.student_item_dict['item_id'],
     )
     return {
         shared_upload.file_key: shared_upload
         for shared_upload in shared_uploads
     }
Exemplo n.º 5
0
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
Exemplo n.º 6
0
    def shared_uploads_for_team_by_key(self, team_id):
        """
        Returns **and caches** all of the SharedFileUpload records
        for this student/course/item and team.

        Realistically, only one team_id will ever be requested, but this is a simple enough pattern
        """
        if team_id not in self.shared_uploads_for_team_by_key_cache:
            shared_uploads = SharedFileUpload.by_team_course_item(
                team_id=team_id,
                course_id=self.student_item_dict['course_id'],
                item_id=self.student_item_dict['item_id'],
            )
            shared_uploads_for_team_by_key = {
                shared_upload.file_key: shared_upload for shared_upload in shared_uploads
            }
            self.shared_uploads_for_team_by_key_cache[team_id] = shared_uploads_for_team_by_key
        return self.shared_uploads_for_team_by_key_cache[team_id]