This file is indexed.

/usr/lib/python2.7/dist-packages/schooltool/gradebook/activity.py is in python-schooltool.gradebook 2.6.3-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#
# SchoolTool - common information systems platform for school administration
# Copyright (c) 2005 Shuttleworth Foundation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
"""Activity implementation
"""
__docformat__ = 'reStructuredText'

import persistent.dict
from decimal import Decimal

import zope.interface
from zope import annotation
from zope.container.interfaces import INameChooser
from zope.keyreference.interfaces import IKeyReference
from zope.security import proxy
from zope.component import queryAdapter, getAdapters, getUtility
from zope.schema.interfaces import IVocabularyFactory

from schooltool.app.interfaces import ISchoolToolApplication
from schooltool.gradebook import GradebookMessage as _
from schooltool.requirement import requirement, scoresystem
from schooltool.gradebook import interfaces
from schooltool.term.interfaces import IDateManager
from schooltool.course.interfaces import ISection

ACTIVITIES_KEY = 'schooltool.gradebook.activities'
CATEGORY_WEIGHTS_KEY = 'schooltool.gradebook.categoryweights'
COURSE_ACTIVITIES_KEY = 'schooltool.gradebook.course_activities'
COURSE_DEPLOYED_WORKSHEETS_KEY = 'schooltool.gradebook.course_deployed'


def ensureAtLeastOneWorksheet(worksheets, factory=None, title=None):
    # only create new worksheet if no personal
    # worksheet (hidden or not) is found
    for worksheet in worksheets.all_worksheets:
        if not worksheet.deployed:
            return
    if factory is None:
        factory = Worksheet
    if title is None:
        title = _('Sheet1')
    sheet1 = factory(title)
    chooser = INameChooser(worksheets)
    name = chooser.chooseName('', sheet1)
    worksheets[name] = sheet1


def createSourceString(sourceObj):
    if interfaces.IActivity.providedBy(sourceObj):
        act_hash = unicode(hash(IKeyReference(sourceObj)))
        worksheet = sourceObj.__parent__
    else:
        act_hash = 'ave'
        worksheet = sourceObj
    section = worksheet.__parent__.__parent__
    sectionContainer = section.__parent__
    return '%s_%s_%s_%s' % (sectionContainer.__name__, section.__name__,
        unicode(hash(IKeyReference(worksheet))), act_hash)


def getSourceObj(source):
    if source is None:
        return None

    items = source.split('_')
    scid = items[0]
    ws_hash = items[-2]
    act_hash = items[-1]
    sid = '_'.join(items[1:-2])

    app = ISchoolToolApplication(None)
    sectionContainer = app['schooltool.course.section'].get(scid, None)
    if sectionContainer is None:
        return None

    section = sectionContainer.get(sid, None)
    if section is None:
        return None

    for worksheet in interfaces.IActivities(section).values():
        if ws_hash == unicode(hash(IKeyReference(worksheet))):
            break
    else:
        return None

    if act_hash == 'ave':
        return worksheet
    for key, activity in worksheet.items():
        if act_hash == unicode(hash(IKeyReference(activity))):
            return activity
    return None


def isHiddenSource(source):
    obj = getSourceObj(source)
    if obj is None:
        return True
    if interfaces.IActivityWorksheet.providedBy(obj):
        worksheet = obj
    else:
        worksheet = obj.__parent__
    return worksheet.hidden


def today():
    today = getUtility(IDateManager).today
    return today


class Worksheets(requirement.Requirement):
    zope.interface.implements(interfaces.IWorksheets)

    annotations_current_worksheet_key = None

    def getDefaultWorksheet(self):
        # order of preference is:
        # 1) first non-hidden personal sheet
        # 2) first non-hidden deployed sheet
        # 3) first sheet outright if all are hidden
        # 4) None if there are no worksheets at all
        firstDeployed, firstAbsolute = None, None
        for worksheet in self.all_worksheets:
            if firstAbsolute is None:
                firstAbsolute = worksheet
            if worksheet.deployed:
                if firstDeployed is None and not worksheet.hidden:
                    firstDeployed = worksheet
            elif not worksheet.hidden:
                return worksheet
        if firstDeployed is not None:
            return firstDeployed
        return firstAbsolute

    def getCurrentSectionWorksheets(self, person):
        if self.annotations_current_worksheet_key is None:
            return None
        person = proxy.removeSecurityProxy(person)
        ann = annotation.interfaces.IAnnotations(person)
        if self.annotations_current_worksheet_key not in ann:
            ann[self.annotations_current_worksheet_key] = persistent.dict.PersistentDict()
        return ann[self.annotations_current_worksheet_key]

    @property
    def worksheets(self):
        return self.values()

    @property
    def all_worksheets(self):
        return [w for k, w in self.items()]

    def values(self):
        worksheets = super(Worksheets, self).values()
        return [w for w in worksheets if not w.hidden]

    def resetCurrentWorksheet(self, person):
        person = proxy.removeSecurityProxy(person)
        default = self.getDefaultWorksheet()
        self.setCurrentWorksheet(person, default)

    def getCurrentWorksheet(self, person):
        default = self.getDefaultWorksheet()
        current = self.getCurrentSectionWorksheets(person)
        if not current:
            return default
        section_id = hash(IKeyReference(self.__parent__))
        worksheet = current.get(section_id, default)
        if worksheet is not None and worksheet.hidden:
            return default
        return worksheet

    def setCurrentWorksheet(self, person, worksheet):
        current = self.getCurrentSectionWorksheets(person)
        worksheet = proxy.removeSecurityProxy(worksheet)
        section_id = hash(IKeyReference(self.__parent__))
        current[section_id] = worksheet

    def getCurrentActivities(self, person):
        worksheet = self.getCurrentWorksheet(person)
        if worksheet:
            return list(worksheet.values())
        else:
            return []


class Activities(Worksheets):
    zope.interface.implements(interfaces.IActivities)

    annotations_current_worksheet_key = 'schooltool.gradebook.currentworksheet'


class CourseActivities(requirement.Requirement):
    zope.interface.implements(interfaces.ICourseActivities)


class CourseDeployedWorksheets(requirement.Requirement):
    zope.interface.implements(interfaces.ICourseDeployedWorksheets)


class GenericWorksheet(requirement.Requirement):
    zope.interface.implements(interfaces.IWorksheet,
                              annotation.interfaces.IAttributeAnnotatable)

    deployed = False
    hidden = False


class WorksheetAnnotatableMixin(object):
    zope.interface.implements(annotation.interfaces.IAttributeAnnotatable)

    def getCategoryWeights(self):
        ann = annotation.interfaces.IAnnotations(self)
        if CATEGORY_WEIGHTS_KEY not in ann:
            ann[CATEGORY_WEIGHTS_KEY] = persistent.dict.PersistentDict()
        return ann[CATEGORY_WEIGHTS_KEY]

    def setCategoryWeight(self, category, weight):
        ann = annotation.interfaces.IAnnotations(self)
        if CATEGORY_WEIGHTS_KEY not in ann:
            ann[CATEGORY_WEIGHTS_KEY] = persistent.dict.PersistentDict()
        ann[CATEGORY_WEIGHTS_KEY][category] = weight


class Worksheet(GenericWorksheet, WorksheetAnnotatableMixin):
    zope.interface.implements(interfaces.IActivityWorksheet,
                              annotation.interfaces.IAttributeAnnotatable)

    def values(self):
        activities = []
        for activity in super(Worksheet, self).values():
            if interfaces.ILinkedColumnActivity.providedBy(activity):
                if isHiddenSource(activity.source):
                    continue
            activities.append(activity)
        return activities

    def canAverage(self):
        return True


class ReportWorksheet(requirement.Requirement, WorksheetAnnotatableMixin):
    zope.interface.implements(interfaces.IReportWorksheet)

    deployed = False


class CourseWorksheet(requirement.Requirement, WorksheetAnnotatableMixin):
    zope.interface.implements(interfaces.ICourseWorksheet)

    deployed = False


class Activity(requirement.Requirement):
    zope.interface.implements(interfaces.IActivity)

    def __init__(self, title, category, scoresystem,
                 description=None, label=None, due_date=None, date=None):
        super(Activity, self).__init__(title)
        self.label = label
        self.description = description
        self.category = category
        self.scoresystem = scoresystem
        if not due_date:
            due_date = today()
        self.due_date = due_date
        if not date:
            date = today()
        self.date = date

    def __repr__(self):
        return '<%s %r>' %(self.__class__.__name__, self.title)


class ReportActivity(Activity):
    zope.interface.implements(interfaces.IReportActivity)


def getSectionActivities(context):
    '''IAttributeAnnotatable object to IActivities adapter.'''
    annotations = annotation.interfaces.IAnnotations(context)
    try:
        return annotations[ACTIVITIES_KEY]
    except KeyError:
        activities = Activities(_('Activities'))
        annotations[ACTIVITIES_KEY] = activities
        zope.container.contained.contained(
            activities, context, 'activities')
        return activities

# Convention to make adapter introspectable
getSectionActivities.factory = Activities


def getCourseActivities(context):
    '''IAttributeAnnotatable object to ICourseActivities adapter.'''
    annotations = annotation.interfaces.IAnnotations(context)
    try:
        return annotations[COURSE_ACTIVITIES_KEY]
    except KeyError:
        activities = CourseActivities(_('Course Activities'))
        annotations[COURSE_ACTIVITIES_KEY] = activities
        zope.container.contained.contained(
            activities, context, 'activities')
        return activities

# Convention to make adapter introspectable
getCourseActivities.factory = CourseActivities


def getCourseDeployedWorksheets(context):
    '''IAttributeAnnotatable object to ICourseDeployedWorksheets adapter.'''
    annotations = annotation.interfaces.IAnnotations(context)
    try:
        return annotations[COURSE_DEPLOYED_WORKSHEETS_KEY]
    except KeyError:
        worksheets = CourseDeployedWorksheets(_('Deployed Worksheets'))
        annotations[COURSE_DEPLOYED_WORKSHEETS_KEY] = worksheets
        zope.container.contained.contained(
            worksheets, context, 'deployed_worksheets')
        return worksheets

# Convention to make adapter introspectable
getCourseDeployedWorksheets.factory = CourseDeployedWorksheets


class LinkedActivity(Activity):
    zope.interface.implements(interfaces.ILinkedActivity)

    def __init__(self, external_activity, category, points, label,
                 due_date=None):
        custom = scoresystem.RangedValuesScoreSystem(
            u'generated', min=Decimal(0), max=Decimal(points))
        super(LinkedActivity, self).__init__(external_activity.title,
                                             category,
                                             custom,
                                             external_activity.description,
                                             label,
                                             due_date)
        self.source = external_activity.source
        self.external_activity_id = external_activity.external_activity_id

    @property
    def points(self):
        return int(self.scoresystem.max)

    @points.setter
    def points(self, value):
        self.scoresystem.max = Decimal(value)

    def getExternalActivity(self):
        section = self.__parent__.__parent__.__parent__
        adapter = queryAdapter(section, interfaces.IExternalActivities,
                               name=self.source)
        if adapter is not None:
            return adapter.getExternalActivity(self.external_activity_id)


class ExternalActivitiesSource(object):
    zope.interface.implements(interfaces.IExternalActivitiesSource)

    def __init__(self, context):
        self.section = context

    def activities(self):
        result = []
        for name, adapter in getAdapters((self.section,),
                                         interfaces.IExternalActivities):
            for external_activity in adapter.getExternalActivities():
                result.append((adapter, external_activity))
        return sorted(result, key=self.sortByTitles())

    def sortByTitles(self):
        return lambda x:(x[0].title, x[1].title)

    def __iter__(self):
        return iter(self.activities())

    def __len__(self):
        return len(self.activities())

    def __contains__(self, other_tuple):
        try:
            external_activity = other_tuple[1]
            return bool([value for value in self.activities()
                         if value[1] == external_activity])
        except (IndexError,):
            return False


class ExternalActivitiesVocabulary(object):
    zope.interface.implements(IVocabularyFactory)

    def __call__(self, context):
        try:
            section = ISection(context)
        except (TypeError,):
            linked_activity = proxy.removeSecurityProxy(context)
            section = ISection(linked_activity.__parent__)
        return ExternalActivitiesSource(section)


class LinkedColumnActivity(Activity):
    zope.interface.implements(interfaces.ILinkedColumnActivity)

    def __init__(self, title, category, label, source):
        super(LinkedColumnActivity, self).__init__(title, category, None, '',
            label)
        self.source = source