Exemplo n.º 1
0
    def get_context_data(self, *args, **kwargs):
        context = super(FolderContextMixin,
                        self).get_context_data(*args, **kwargs)
        folder = self.get_folder()
        if not folder:
            return context  # Exit in case of usage outside of Folders section, like committees, etc.

        account = get_current_account(self.request)
        membership = self.request.user.get_membership(account)
        # Folder forms
        context['folder_add_form'] = FolderAddForm()
        context['folder_edit_form'] = FolderEditForm()
        context['folder_move_form'] = FolderMoveForm()
        # Ordered items (files & folders)
        search = self.request.GET.get('search', None)
        if search:
            descendants = folder.get_descendants()
            sub_folders = descendants.filter(name__icontains=search)
            documents = Document.objects.filter(account=folder.account,
                                                folder__in=descendants,
                                                name__icontains=search)
        else:
            sub_folders = folder.children.all()
            documents = folder.documents.all()

        documents = documents.select_related('user')

        items = filter_by_permission(sub_folders, membership, PERMISSIONS.view)
        filtered_documents = filter_by_permission(documents, membership,
                                                  PERMISSIONS.view)
        Document.prefetch_revisions(filtered_documents)
        items += filtered_documents

        # Order items
        default_ordering = 'date' if folder.name == 'Meeting Documents' else 'default'
        ordering = self.request.GET.get('o', default_ordering).lower()
        items = FolderContextMixin.sort_items(items, ordering)

        # append members private folder
        if folder.is_account_root and not search:
            try:
                private_folder = membership.private_folder
            except Folder.DoesNotExist:
                private_folder = Folder.objects.create_membership_folder(
                    membership)
            items.append(private_folder)
            context['show_shared_folder'] = True
        # Set helper attributes
        FolderContextMixin.set_help_attrs(items)
        context['items'] = items
        context['ordering'] = ordering
        context['search'] = search
        qs = folder.get_ancestors(include_self=True).filter(
            membership_id=membership.id)
        membership_ancestor = qs[0] if len(qs) > 0 else None
        context['membership_ancestor'] = membership_ancestor
        return context
Exemplo n.º 2
0
    def list(self, request, *args, **kwargs):
        self.require_folder_permission('view')

        queryset = self.get_queryset().filter(folder=self.folder)
        documents = filter_by_permission(queryset,
                                         self.get_current_membership(),
                                         PERMISSIONS.view)
        Document.prefetch_revisions(documents)

        serializer = self.get_serializer(documents, many=True)

        return Response(serializer.data)