Index: WAeUP_SRP/base/skins/waeup_student/add_course_result.py
===================================================================
--- WAeUP_SRP/base/skins/waeup_student/add_course_result.py	(revision 2459)
+++ WAeUP_SRP/base/skins/waeup_student/add_course_result.py	(revision 2460)
@@ -33,5 +33,4 @@
 member = mtool.getAuthenticatedMember()
 
-
 d = context.getCourseInfo(course_id)
 args = {}
@@ -44,13 +43,16 @@
     if d['title'] == 'unknown':
         err = "No such course!"
-        #logger.info('%s tried to add StudentCourseResult object %s for non-existing course' % (member,course_id))
         break
-    if context.hasObject(course_id):
+    d['course_id'] = course_id
+    d['student_id'] = student_id = context.getStudentId()
+    d['level_id'] = level_id = context.getId()
+    session_id = context.getLevelSession(context.getContent(),student_id,level_id)
+    d['session_id'] = session_id
+    d['key'] = "%s|%s|%s" % (student_id,level_id,course_id)
+    try:
+        context.course_results.addRecord(**d)
+        break
+    except ValueError:
         err = "Course already exists!"
-        #logger.info('%s tried to add StudentCourseResult object %s which is already in course list' % (member,course_id))
-        break
-    if context.hasObject("%s_co" % course_id):
-        err = "Course already exists as carryover course!"
-        #logger.info('%s tried to add StudentCourseResult object %s which already exists as carryover course' % (member,course_id))
         break
     break
@@ -58,24 +60,8 @@
 if err:
     args['course_id'] = course_id
-    #args['error'] = err
     args['portal_status_message'] = err
     url = context.absolute_url() + '/' + action_after_validate + '?' + urlencode(args)
     return request.RESPONSE.redirect(url)
 
-
-# cr_id = context.invokeFactory('StudentCourseResult',course_id)
-# cr = getattr(context,cr_id)
-# context.portal_workflow.doActionFor(cr,'open')
-# cr.getContent().edit(mapping=d)
-d['course_id'] = course_id
-d['student_id'] = student_id = context.getStudentId()
-d['level_id'] = level_id = context.getId()
-session_id = context.getLevelSession(context.getContent(),student_id,level_id)
-d['session_id'] = session_id
-d['key'] = "%s|%s|%s" % (student_id,level_id,course_id)
-
-context.course_results.addRecord(**d)
-#context.portal_workflow.doActionFor(cr,'close')
-#args['course_id'] = course_id
 
 logger.info('%s added StudentCourseResult object %s at level %s for %s' % (member,
Index: WAeUP_SRP/base/skins/waeup_student/refresh_level.py
===================================================================
--- WAeUP_SRP/base/skins/waeup_student/refresh_level.py	(revision 2459)
+++ WAeUP_SRP/base/skins/waeup_student/refresh_level.py	(revision 2460)
@@ -42,46 +42,13 @@
 student = context.students_catalog(id=context.getStudentId())[0]
 cert_id = student.course
-current_level = student.level
+student_id = context.getStudentId()
 level = context
-#results = context.results_import(matric_no = student.matric_no)
-# results = False #skip carry overs
-# if results:
-#     #carry_overs = [brain for brain in results if brain.GRADE in ('D','E','F')]
-#     l = []
-#     carry_overs = results
-#     for co in carry_overs:
-#         carry_over = False
-#         query = Eq('code',co.CosCode)
-#         c_res = aq_courses(query)
-#         if not c_res:
-#             logger.info('%s, course %s not found in courses_catalog' % (student.id,co.CosCode))
-#             continue
-#         course_cat_entry = c_res[0]
-#         if not course_cat_entry.passmark:
-#             carry_over = co.GRADE in ('D','E','F')
-#         else:
-#             try:
-#                 score = int(co.Score)
-#             except:
-#                 logger.info('%s, course %s has invalid score %s' % (student.id,
-#                                                                     co.CosCode,
-#                                                                     co.Score))
-#                 continue
-#             if int(c_res[0].passmark) <= score:
-#                 continue
-#             carry_over = True
-#         if not carry_over:
-#             continue
-#         d = context.getCourseInfo(co.CosCode)
-#         d['grade'] = co.GRADE
-#         d['carry_level'] = co.CarryLevel
-#         course_id = "%s_co" % co.CosCode
-#         if not context.hasObject(course_id):
-#             cr_id = level.invokeFactory('StudentCourseResult',course_id)
-#             course_result = getattr(level,cr_id)
-#             context.portal_workflow.doActionFor(course_result,'open')
-#             course_result.getContent().edit(mapping=d)
+level_id = context.getId()
 res = context.portal_catalog(portal_type="Certificate", id = cert_id)
+query = Eq('student_id',student_id) & Eq('level_id', level_id)
+existing_courses = [course.code for course in 
+                    context.course_results.evalAdvancedQuery(query)]
 l = []
+records = []
 if res:
     cert = res[0]
@@ -90,20 +57,21 @@
             Eq('portal_type','CertificateCourse')
     courses = aq_portal(query)
-    course_ids = [c.getId  for c in courses if c.getId not in level.objectIds()]
+    course_ids = [c.getId  for c in courses if c.getId not in existing_courses]
     for course_id in course_ids:
         #set_trace()
+        if course_id in l:
+            # don't add twice
+            continue
+        l += course_id,
         d = context.getCourseInfo(course_id)
-        # cr_id = level.invokeFactory('StudentCourseResult',c_id)
-        # course_result = getattr(level,cr_id)
-        # context.portal_workflow.doActionFor(course_result,'open')
-        # course_result.getContent().edit(mapping=d)
-        # context.waeup_tool.doCommit()
         d['core_or_elective'] = getattr(c.getObject().getContent(),'core_or_elective')
-        #d['core_or_elective'] = True
         d['course_id'] = course_id
-        d['student_id'] = student_id = context.getStudentId()
-        d['level_id'] = level_id = context.getId()
+        d['student_id'] = student_id 
+        d['level_id'] = level_id 
         d['key'] = "%s|%s|%s" % (student_id,level_id,course_id)
-        context.course_results.addRecord(**d)
+        records.append(d)
+if records:
+    context.course_results.addMultipleRecords(records)
+
 #session.set('in_progress','finished')
 logger.info('%s updated course list' % member)
