Changeset 2434 for WAeUP_SRP/base


Ignore:
Timestamp:
25 Oct 2007, 14:35:59 (17 years ago)
Author:
joachim
Message:

adopt to new course_result table

File:
1 edited

Legend:

Unmodified
Added
Removed
  • WAeUP_SRP/base/WAeUPTables.py

    r2396 r2434  
    12971297        self._queue = []
    12981298
    1299     def addRecord(self, **data): ###(
    1300         """add one record"""
    1301 
    1302         uid = key = "%(student_id)s|%(level_id)s|%(course_id)s" % data
    1303         if key in self._queue:
    1304             return uid
    1305         data['queue_status'] = ADDING_SHEDULED
    1306         data['%s' % self.key] = uid
    1307         res = self.searchResults({"%s" % self.key : uid})
    1308         if len(res) > 0:
    1309             raise ValueError("More than one record with uid %s" % uid)
    1310         self.catalog_object(dict2ob(data), uid=uid)
    1311         if not hasattr(self,'_queue'):
    1312             self._queue = []
    1313         self._queue.append(key)
    1314         self._p_changed = 1
    1315         return uid
    1316     ###)
    1317 
    1318     # def _p_resolveConflict(self, oldstate, committed, newstate):
    1319     #     # Apply the changes made in going from old to newstate to
    1320     #     # committed
    1321 
    1322     #     # Note that in the case of undo, the olddata is the data for
    1323     #     # the transaction being undone and newdata is the data for the
    1324     #     # transaction previous to the undone transaction.
    1325 
    1326     #     # Find the conflict policy on the new state to make sure changes
    1327     #     # to it will be applied
    1328 
    1329     #     # Committed is always the currently committed data.
    1330     #     import pdb;pdb.set_trace()
    1331     #     oldstate_data  =  oldstate['_queue']
    1332     #     committed_data = committed['_queue']
    1333     #     newstate_data  =  newstate['_queue']
    1334 
    1335     #     # Merge newstate changes into committed
    1336     #     for uid, new in newstate_data.items():
    1337 
    1338     #         # Decide if this is a change
    1339     #         old = oldstate_data.get(uid)
    1340     #         current = committed_data.get(uid)
    1341 
    1342 
    13431299    def addMultipleRecords(self, records): ###(
    13441300        """add many records"""
     
    13461302        for data in records:
    13471303            uid = key = "%(student_id)s|%(level_id)s|%(course_id)s" % data
    1348             added_keys.append(key)
    1349             if key in self._queue:
    1350                 return uid
    1351             data['queue_status'] = ADDING_SHEDULED
    13521304            data['%s' % self.key] = uid
    13531305            res = self.searchResults({"%s" % self.key : uid})
     
    13551307                raise ValueError("More than one record with uid %s" % uid)
    13561308            self.catalog_object(dict2ob(data), uid=uid)
    1357         if not hasattr(self,'_queue'):
    1358             self._queue = added_keys
    1359         self._queue.extend(added_keys)
    1360         self._p_changed = 1
    13611309        return uid
    13621310    ###)
    13631311
    1364     def deleteRecord(self, uid): ###(
    1365         self.uncatalog_object(uid)
    1366         if uid in self._queue:
    1367             self._queue.remove(uid)
    1368     ###)
    1369 
    1370     def updateCourseResults(self,student_id,level_id,portal_catalog_results,course_results): ###(
    1371         # query = Eq('path',"%s/campus/students/%s/study_course/%s" %
    1372         #            (self.portal_url.getPortalPath(),
    1373         #             student_id,
    1374         #             level_id)) &\
    1375         #             Eq('portal_type', "StudentCourseResult")
    1376         # level_results = self.portal_catalog_real.evalAdvancedQuery(query)
    1377         # level_results = [r for r in course_results
    1378         #                  if level_id in r.relative_path.split('/')]
    1379         course_results_ids = [cr.getId for cr in course_results]
    1380         for r in portal_catalog_results:
    1381             if r.getId in course_results_ids:
     1312    def deleteResultsHere(self,level_id,student_id): ###(
     1313        #import pdb;pdb.set_trace()
     1314        query = Eq('student_id',student_id) & Eq('level_id', level_id)
     1315        course_results = self.course_results.evalAdvancedQuery(query)
     1316        for result in course_results:
     1317            self.deleteRecord(result.key)
     1318    ###)
     1319
     1320    def moveResultsHere(self,level,student_id): ###(
     1321        #import pdb;pdb.set_trace()
     1322        level_id = level.getId()
     1323        query = Eq('student_id',student_id) & Eq('level_id', level_id)
     1324        course_results = self.course_results.evalAdvancedQuery(query)
     1325        existing_courses = [cr.code for cr in course_results]
     1326        to_delete = []
     1327        for code,obj in level.objectItems():
     1328            to_delete += code
     1329            carry_over = False
     1330            if code.endswith('_co'):
     1331                carry_over = True
     1332                code  = code[:-3]
     1333            if code in existing_courses:
    13821334                continue
    1383             course_result_doc = r.getObject().getContent()
     1335            course_result_doc = obj.getContent()
    13841336            data = {}
    1385             course_id = r.getId
     1337            course_id = code
    13861338            for field in self.schema():
    13871339                data[field] = getattr(course_result_doc,field,'')
     
    13891341            data['student_id'] = student_id
    13901342            data['level_id'] = level_id
    1391             data['queue_status'] = OBJECT_CREATED
     1343            #data['queue_status'] = OBJECT_CREATED
    13921344            data['code'] = course_id
     1345            data['carry_over'] = carry_over
    13931346            self.catalog_object(dict2ob(data), uid=key)
     1347        level.manage_delObjects(to_delete)
     1348    ###)
     1349
     1350    def getCourses(self,student_id,level_id): ###(
    13941351        query = Eq('student_id',student_id) & Eq('level_id', level_id)
    1395         return self.course_results.evalAdvancedQuery(query)
    1396     ###)
    1397 
    1398     def getCourses(self,student_id,level_id): ###(
     1352        course_results = self.course_results.evalAdvancedQuery(query)
     1353        carry_overs = []
     1354        normal = []
     1355        credits = 0
     1356        for brain in course_results:
     1357            d = {}
     1358            credits += int(brain.credits)
     1359            for field in self.schema():
     1360                d[field] = getattr(brain,field,'')
     1361            #d['sheduled'] = brain.queue_status == ADDING_SHEDULED
     1362            d['coe'] = 'Elective'
     1363            if brain.core_or_elective:
     1364                d['coe'] = 'Core'
     1365            id = code = d['id'] = brain.code
     1366            is_carry_over = False
     1367            if code.endswith('_co'):
     1368                is_carry_over = True
     1369                code = code[:-3]
     1370            d['code'] = code
     1371            d['title'] = self.courses_catalog.evalAdvancedQuery(Eq('code',code))[0].title
     1372            if is_carry_over:
     1373                d['coe'] = 'Core'
     1374                carry_overs.append(d)
     1375            else:
     1376                normal.append(d)
     1377        normal.sort(cmp=lambda x,y: cmp(x['semester'], y['semester']))
     1378        carry_overs.sort(cmp=lambda x,y: cmp(x['semester'], y['semester']))
     1379        return credits,carry_overs,normal
     1380    ###)
     1381
     1382    def queue_getCourses(self,student_id,level_id): ###(
    13991383        level_path = "%s/campus/students/%s/study_course/%s" % (self.portal_url.getPortalPath(),
    14001384                                                                student_id,
     
    14411425    ###)
    14421426
    1443     def addObject(self,record): ###(
    1444         key = record.key
    1445         student_id,level_id,course_id = key.split('|')
    1446         level = getattr(getattr(self.portal_url.getPortalObject().campus.students,student_id).study_course,level_id)
    1447         cr_id = level.invokeFactory('StudentCourseResult', course_id)
    1448         course_result = getattr(level,cr_id)
    1449         self.portal_workflow.doActionFor(course_result,'open')
    1450         d = {}
    1451         for field in self.schema():
    1452             d[field] = getattr(record,field,'')
    1453         course_result.getContent().edit(mapping=d)
    1454     ###)
    1455 
    1456     security.declareProtected(ModifyPortalContent,"process_queue") ###(
    1457     def process_queue(self,limit=None):
    1458         """adds objects and removes them from the queue.
    1459         If limit is specified, at most (limit) events are removed.
    1460         """
    1461         if not hasattr(self,'_queue'):
    1462             return 0
    1463         queue= self._queue
    1464         if not limit or len(queue) <= limit:
    1465             keys = self._queue[:]
    1466         else:
    1467             keys = queue[:limit]
    1468         if not keys:
    1469             records = self.evalAdvancedQuery(Eq('queue_status',ADDING_SHEDULED))
    1470         else:
    1471             records = self.evalAdvancedQuery(In("%s" % self.key,keys))
    1472         for record in records:
    1473             if record.queue_status == OBJECT_CREATED:
    1474                 continue
    1475             self.addObject(record)
    1476             data = {}
    1477             data['key'] = record.key
    1478             data['queue_status'] = OBJECT_CREATED
    1479             self.modifyRecord(**data)
    1480         count = 0
    1481         for key in keys:
    1482             count +=1
    1483             if key in self._queue:
    1484                 self._queue.remove(key)
    1485         self._p_changed = 1
    1486         return count,len(self._queue)
    1487     ###)
    1488 
    1489     def manage_catalogClear(self, REQUEST=None, RESPONSE=None, URL1=None): ###(
    1490         """ clears the whole enchilada """
    1491         self._catalog.clear()
    1492 
    1493         if REQUEST and RESPONSE:
    1494             RESPONSE.redirect(
    1495               URL1 +
    1496               '/manage_catalogAdvanced?manage_tabs_message=Catalog%20Cleared')
    1497     ###)
    1498 
    1499     def manage_catalogReindex(self, REQUEST, RESPONSE, URL1): ###(
    1500         """ clear the catalog, then re-index everything """
    1501 
    1502         elapse = time.time()
    1503         c_elapse = time.clock()
    1504 
    1505         pgthreshold = self._getProgressThreshold()
    1506         handler = (pgthreshold > 0) and ZLogHandler(pgthreshold) or None
    1507         self.refreshCatalog(clear=1, pghandler=handler)
    1508 
    1509         elapse = time.time() - elapse
    1510         c_elapse = time.clock() - c_elapse
    1511 
    1512         RESPONSE.redirect(
    1513             URL1 +
    1514             '/manage_catalogAdvanced?manage_tabs_message=' +
    1515             urllib.quote('Catalog Updated \n'
    1516                          'Total time: %s\n'
    1517                          'Total CPU time: %s' % (`elapse`, `c_elapse`)))
    1518     ###)
    1519 
    1520     def refreshCatalog(self, clear=1, pghandler=None): ###(
    1521         """ re-index everything we can find """
    1522         students_folder = self.portal_url.getPortalObject().campus.students
    1523         if clear:
    1524             self._catalog.clear()
    1525         course_results = self.portal_catalog(portal_type="StudentCourseResult")
    1526         num_objects = len(course_results)
    1527         if pghandler:
    1528             pghandler.init('Refreshing catalog: %s' % self.absolute_url(1), num_objects)
    1529         #import pdb;pdb.set_trace()
    1530         for i in xrange(num_objects):
    1531             if pghandler:
    1532                 pghandler.report(i)
    1533             course_result_brain = course_results[i]
    1534             path_list = course_result_brain.getPath().split('/')
    1535             course_result_doc = course_result_brain.getObject().getContent()
    1536             data = {}
    1537             level_id = path_list[-2]
    1538             course_id = path_list[-1]
    1539             student_id = path_list[-4]
    1540             for field in self.schema():
    1541                 data[field] = getattr(course_result_doc,field,'')
    1542             data['key'] = key = "%(student_id)s|%(level_id)s|%(course_id)s" % vars()
    1543             data['student_id'] = student_id
    1544             data['level_id'] = level_id
    1545             data['queue_status'] = OBJECT_CREATED
    1546             self.catalog_object(dict2ob(data), uid=key)
    1547         if pghandler: pghandler.finish()
    1548     ###)
    1549 
    1550     security.declarePrivate('notify_event_listener') ###(
    1551     def notify_event_listener(self,event_type,object,infos):
    1552         "listen for events"
    1553         if not infos.has_key('rpath'):
    1554             return
    1555         pt = getattr(object,'portal_type',None)
    1556         mt = getattr(object,'meta_type',None)
    1557         data = {}
    1558         rpl = infos['rpath'].split('/')
    1559         if mt == 'CPS Proxy Folder':
    1560             return
    1561         if pt == 'StudentCourseResult' and event_type == "sys_modify_object":
    1562             data["%s" % self.key] = uid = "%s|%s|%s" % (rpl[-5],rpl[-3],rpl[-2])
    1563             records = self.searchResults({"%s" % self.key : uid})
    1564             if len(records) > 1:
    1565                 # Can not happen, but anyway...
    1566                 raise ValueError("More than one record with uid %s" % uid)
    1567             if len(records) == 0:
    1568                 raise KeyError("No record for uid %s" % uid)
    1569             record = records[0]
    1570             for field in ('core_or_elective','score'):
    1571                 value = getattr(object,field,None)
    1572                 data[field] = value
    1573             try:
    1574                 self.modifyRecord(record,**data)
    1575             except KeyError:
    1576                 pass
    1577         if pt == 'StudentStudyLevel' and event_type == "sys_del_object":
    1578             #import pdb;pdb.set_trace()
    1579             student_id = rpl[-3]
    1580             level_id = rpl[-1]
    1581             res = self.searchResults(student_id = student_id,
    1582                                      level_id = level_id)
    1583             for cr in res:
    1584                 self.deleteRecord(cr.key)
    1585     ###)
    1586 
    15871427InitializeClass(CourseResults)
    15881428###)
Note: See TracChangeset for help on using the changeset viewer.