Repository: e-valuation/EvaP

Issue:
TypeError at /staff/semester/19/export: unorderable types: CourseType() < CourseType()
When trying to export a semester using the default test database set this error occurs. This might be a test data set issue.



Discussion:

Resolving patch:
diff --git a/evap/evaluation/models.py b/evap/evaluation/models.py
--- a/evap/evaluation/models.py
+++ b/evap/evaluation/models.py
@@ -178,8 +178,11 @@ class Meta:
     def __str__(self):
         return self.name
 
+    def __lt__(self, other):
+        return self.name_de < other.name_de
+
     def can_staff_delete(self):
-        if self.pk == None:
+        if not self.pk:
             return True
         return not self.courses.all().exists()
 
diff --git a/evap/results/exporters.py b/evap/results/exporters.py
--- a/evap/results/exporters.py
+++ b/evap/results/exporters.py
@@ -1,11 +1,9 @@
-from evap.evaluation.models import Questionnaire
+from evap.evaluation.models import CourseType
 from evap.evaluation.tools import calculate_results, calculate_average_grades_and_deviation, get_grade_color, get_deviation_color, has_no_rating_answers
 
 from django.utils.translation import ugettext as _
 
 from collections import OrderedDict
-from collections import defaultdict
-import datetime
 import xlwt
 
 
@@ -16,9 +14,9 @@ def __init__(self, semester):
         self.styles = dict()
 
         self.CUSTOM_COLOR_START = 8
-        self.NUM_GRADE_COLORS = 21 # 1.0 to 5.0 in 0.2 steps
-        self.NUM_DEVIATION_COLORS = 13 # 0.0 to 2.4 in 0.2 steps
-        self.STEP = 0.2 # we only have a limited number of custom colors
+        self.NUM_GRADE_COLORS = 21  # 1.0 to 5.0 in 0.2 steps
+        self.NUM_DEVIATION_COLORS = 13  # 0.0 to 2.4 in 0.2 steps
+        self.STEP = 0.2  # we only have a limited number of custom colors
 
     def normalize_number(self, number):
         """ floors 'number' to a multiply of self.STEP """
@@ -109,7 +107,8 @@ def export(self, response, course_types_list, ignore_not_enough_answers=False, i
             courses_with_results.sort(key=lambda cr: cr[0].type)
             used_questionnaires = sorted(used_questionnaires)
 
-            writec(self, _("Evaluation {0}\n\n{1}").format(self.semester.name, ", ".join(course_types)), "headline")
+            course_type_names = [ct.name for ct in CourseType.objects.filter(pk__in=course_types)]
+            writec(self, _("Evaluation {0}\n\n{1}").format(self.semester.name, ", ".join(course_type_names)), "headline")
 
             for course, results in courses_with_results:
                 writec(self, course.name, "course", cols=2)
diff --git a/evap/staff/forms.py b/evap/staff/forms.py
--- a/evap/staff/forms.py
+++ b/evap/staff/forms.py
@@ -10,7 +10,6 @@
 from evap.evaluation.forms import BootstrapMixin, QuestionnaireMultipleChoiceField
 from evap.evaluation.models import Contribution, Course, Question, Questionnaire, Semester, UserProfile, FaqSection, \
                                    FaqQuestion, EmailTemplate, TextAnswer, Degree, RatingAnswerCounter, CourseType
-from evap.evaluation.tools import course_types_in_semester
 from evap.staff.fields import ToolTipModelMultipleChoiceField
 
 import logging
@@ -485,10 +484,10 @@ def clean_reviewed_answer(self):
 class ExportSheetForm(forms.Form, BootstrapMixin):
     def __init__(self, semester, *args, **kwargs):
         super(ExportSheetForm, self).__init__(*args, **kwargs)
-        course_types = course_types_in_semester(semester)
-        course_types = [(course_type, course_type) for course_type in course_types]
+        course_types = CourseType.objects.filter(courses__semester=semester).distinct()
+        course_type_tuples = [(ct.pk, ct.name) for ct in course_types]
         self.fields['selected_course_types'] = forms.MultipleChoiceField(
-            choices=course_types,
+            choices=course_type_tuples,
             required=True,
             widget=forms.CheckboxSelectMultiple(),
             label=_("Course types")


Test patch: