Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

#!/usr/bin/env python 

# -*- coding: utf-8 -*- 

 

############################################################################### 

#  Copyright 2013 Kitware Inc. 

# 

#  Licensed under the Apache License, Version 2.0 ( the "License" ); 

#  you may not use this file except in compliance with the License. 

#  You may obtain a copy of the License at 

# 

#    http://www.apache.org/licenses/LICENSE-2.0 

# 

#  Unless required by applicable law or agreed to in writing, software 

#  distributed under the License is distributed on an "AS IS" BASIS, 

#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

#  See the License for the specific language governing permissions and 

#  limitations under the License. 

############################################################################### 

 

import datetime 

 

from .model_base import AccessControlledModel, ValidationException 

from girder.constants import AccessType 

 

 

class Collection(AccessControlledModel): 

    """ 

    Collections are the top level roots of the data hierarchy. They are used 

    to group and organize data that is meant to be shared amongst users. 

    """ 

 

    def initialize(self): 

        self.name = 'collection' 

        self.ensureIndices(['name']) 

        self.ensureTextIndex({ 

            'name': 10, 

            'description': 1 

        }) 

 

    def filter(self, collection, user=None): 

        """Helper to filter the collection model.""" 

        filtered = self.filterDocument( 

            collection, allow=('_id', 'name', 'description', 'public', 

                               'created', 'updated', 'size')) 

 

        if user: 

            filtered['_accessLevel'] = self.getAccessLevel(collection, user) 

 

        return filtered 

 

    def validate(self, doc): 

        doc['name'] = doc['name'].strip() 

        if doc['description']: 

            doc['description'] = doc['description'].strip() 

 

        if not doc['name']: 

            raise ValidationException( 

                'Collection name must not be empty.', 'name') 

 

        # Ensure unique name for the collection 

        q = { 

            'name': doc['name'] 

            } 

        if '_id' in doc: 

            q['_id'] = {'$ne': doc['_id']} 

        duplicates = self.find(q, limit=1, fields=['_id']) 

        if duplicates.count() != 0: 

            raise ValidationException('A collection with that name already' 

                                      'exists.', 'name') 

 

        return doc 

 

    def remove(self, collection): 

        """ 

        Delete a collection recursively. 

 

        :param collection: The collection document to delete. 

        :type collection: dict 

        """ 

        # Delete all folders in the community recursively 

        folders = self.model('folder').find({ 

            'parentId': collection['_id'], 

            'parentCollection': 'collection' 

            }, limit=0) 

        for folder in folders: 

            self.model('folder').remove(folder) 

 

        # Delete this collection 

        AccessControlledModel.remove(self, collection) 

 

    def list(self, user=None, limit=50, offset=0, sort=None): 

        """ 

        Search for collections with full text search. 

        """ 

        cursor = self.find({}, limit=0, sort=sort) 

 

        for r in self.filterResultsByPermission(cursor=cursor, user=user, 

                                                level=AccessType.READ, 

                                                limit=limit, offset=offset): 

            yield r 

 

    def createCollection(self, name, creator, description='', public=True): 

        """ 

        Create a new collection. 

 

        :param name: The name of the collection. Must be unique. 

        :type name: str 

        :param description: Description for the collection. 

        :type description: str 

        :param public: Public read access flag. 

        :type public: bool 

        :param creator: The user who is creating this collection. 

        :type creator: dict 

        :returns: The collection document that was created. 

        """ 

        assert '_id' in creator 

 

        now = datetime.datetime.now() 

 

        collection = { 

            'name': name, 

            'description': description, 

            'creatorId': creator['_id'], 

            'created': now, 

            'updated': now, 

            'size': 0 

            } 

 

        self.setPublic(collection, public=public) 

        self.setUserAccess( 

            collection, user=creator, level=AccessType.ADMIN) 

 

        # Validate and save the collection 

        self.save(collection) 

 

        # Create some default folders for the collection and give the creator 

        # admin access to them 

        privateFolder = self.model('folder').createFolder( 

            collection, 'Private', parentType='collection', public=False, 

            creator=creator) 

        self.model('folder').setUserAccess( 

            privateFolder, creator, AccessType.ADMIN, save=True) 

 

        if public: 

            publicFolder = self.model('folder').createFolder( 

                collection, 'Public', parentType='user', public=True, 

                creator=creator) 

            self.model('folder').setUserAccess( 

                publicFolder, creator, AccessType.ADMIN, save=True) 

 

        return collection 

 

    def updateCollection(self, collection): 

        """ 

        Updates a collection. 

 

        :param collection: The collection document to update 

        :type collection: dict 

        :returns: The collection document that was edited. 

        """ 

        collection['updated'] = datetime.datetime.now() 

 

        # Validate and save the collection 

        return self.save(collection)