summaryrefslogtreecommitdiffstats
path: root/src/pfwbged/folder/folder.py
blob: 0cbad53c3c47a8f04de8cd33c70ed4b0ec3dcfee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from AccessControl import getSecurityManager
from zope.interface import implements
from five import grok
from zope import component
from zope import schema
from zope.interface import Interface
from zope.i18n import translate

from zc.relation.interfaces import ICatalog
from zope.app.intid.interfaces import IIntIds

from plone.dexterity.content import Container
from plone.uuid.interfaces import IUUID

from Acquisition import aq_parent

from plone.supermodel import model
from plone.app.layout.viewlets.interfaces import IBelowContentBody
import plone.app.contenttypes.interfaces
from plone.dexterity.interfaces import IDexterityContainer
from plone.app.contentlisting.interfaces import IContentListingObject

from plone import api

from plone.dexterity.browser.view import DefaultView

from collective.z3cform.rolefield.field import LocalRolesToPrincipals
from collective.dms.basecontent.widget import AjaxChosenMultiFieldWidget
from plone.autoform import directives as form
from plone.directives.form import default_value

import z3c.table.column



from pfwbged.collection.searchview import ResultsTable
from collective.dms.thesaurus.keywordsfield import ThesaurusKeywords

from collective.dms.basecontent.dmsdocument import IDmsDocument


from . import _

class IFolder(model.Schema):
    """ """
    treating_groups = LocalRolesToPrincipals(
        title=_(u"Can edit"),
        required=False,
        roles_to_assign=('Editor', 'Contributor',),
        value_type=schema.Choice(vocabulary=u'collective.dms.basecontent.treating_groups',)
    )
    form.widget(treating_groups=AjaxChosenMultiFieldWidget)

    recipient_groups = LocalRolesToPrincipals(
        title=_(u"Can view"),
        required=False,
        roles_to_assign=('Reader',),
        value_type=schema.Choice(vocabulary=u'collective.dms.basecontent.recipient_groups')
    )
    form.widget(recipient_groups=AjaxChosenMultiFieldWidget)

    keywords = ThesaurusKeywords(title=_(u'Keywords'), required=False)


@default_value(field=IDmsDocument['treating_groups'])
@default_value(field=IFolder['treating_groups'])
def canEditDefaultValue(data):
    try:
        return data.context.treating_groups
    except AttributeError:
        return []

@default_value(field=IDmsDocument['recipient_groups'])
@default_value(field=IFolder['recipient_groups'])
def canEditDefaultValue(data):
    try:
        return data.context.recipient_groups
    except AttributeError:
        return []


class Folder(Container):
    """ """
    implements(IFolder)

    def context_title(self):
        current_user = api.user.get_current()
        current_user_id = current_user.getId()
        members_folder = getattr(api.portal.get(), 'Members')
        if members_folder.get(current_user_id) == self:
            return translate(_(u'Home Folder'), context=self.REQUEST)
        else:
            return self.title

    def parent_folders(self):
        from .link import ILink
        parents = []
        sm = getSecurityManager()
        for id, item in self.contentItems():
            if not ILink.providedBy(item):
                continue
            if item.folder.to_object:
                if not sm.checkPermission('View', item.folder.to_object):
                    continue
                parents.append(item.folder.to_object)
        return parents

    def parent_folders_intid_tree(self, limit=5):
        intids = component.getUtility(IIntIds)
        l = [intids.getId(self)]
        if limit > 0:
            for parent in self.parent_folders():
                l.extend(parent.parent_folders_intid_tree(limit=limit-1))
        return l

    def child_folders_brains(self):
        from .link import ILink
        intids = component.getUtility(IIntIds)
        intid_catalog = component.getUtility(ICatalog)
        try:
            intid = intids.getId(self)
        except KeyError:
            return []

        children = []
        uuids = []
        sm = getSecurityManager()
        for item in intid_catalog.findRelations({
                'to_id': intid,
                'from_interfaces_flattened': ILink}):
            if item.isBroken():
                continue

            link = item.from_object
            if not sm.checkPermission('View', link):
                continue

            document = aq_parent(link)
            if IFolder.providedBy(document):
                uuids.append(IUUID(document))

        portal_catalog = api.portal.get_tool('portal_catalog')
        return portal_catalog.searchResults({'UID': uuids}, sort_on='sortable_title')

    def child_folders(self):
        return [IContentListingObject(x) for x in self.child_folders_brains()]

    def child_folders_objects(self):
        return [x.getObject() for x in self.child_folders_brains()]

    def intid(self):
        intids = component.getUtility(IIntIds)
        try:
            return intids.getId(self)
        except KeyError:
            return None


grok.templatedir('templates')
grok.context(IDexterityContainer)


from collective.dms.basecontent.browser import column

class ClassifiedItems:

    def documents(self):
        from .link import ILink
        if self.context.id == 'documents' and aq_parent(self.context).portal_type == 'Plone Site':
            # never return anything in the main documents folder
            return []

        intids = component.getUtility(IIntIds)
        intid_catalog = component.getUtility(ICatalog)
        try:
            intid = intids.getId(self.context)
        except KeyError:
            return []

        uuids = []
        sm = getSecurityManager()
        for item in intid_catalog.findRelations({
                'to_id': intid,
                'from_interfaces_flattened': ILink}):
            if item.isBroken():
                continue

            link = item.from_object
            if not sm.checkPermission('View', link):
                continue

            document = aq_parent(link)
            uuids.append(IUUID(document))

        portal_catalog = api.portal.get_tool('portal_catalog')
        children = portal_catalog.searchResults({'UID': uuids})

        return [IContentListingObject(x) for x in children]


#class FolderViewlet(grok.Viewlet, ClassifiedItems):
#    grok.context(plone.app.contenttypes.interfaces.IFolder)
#    grok.template('foldersviewlet')
#    grok.viewletmanager(IBelowContentBody)
#    grok.order(15)


class FolderView(DefaultView, ClassifiedItems):
    pass