Repository: e-valuation/EvaP

Issue:
Don't sort by name_de by default
Several models have `name_de` as their default `ordering`. That's almost always the wrong choice, since either we want to sort by `name` (this is the localized name, either `name_de` or `name_en`, and must be sorted in the views), or we want to sort by `pk` or `created_at` or something similar.

We should probably remove that. Maybe do some spot tests (search for e.g. `Course.objects`, `.courses` and `.course_set`) whether there are any places that actually need sorting, but don't explicitly specify any.


Discussion:

Resolving patch:
diff --git a/evap/evaluation/migrations/0108_remove_default_name_de_ordering.py b/evap/evaluation/migrations/0108_remove_default_name_de_ordering.py
new file mode 100644
--- /dev/null
+++ b/evap/evaluation/migrations/0108_remove_default_name_de_ordering.py
@@ -0,0 +1,25 @@
+# Generated by Django 2.2.10 on 2020-03-15 20:55
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('evaluation', '0107_remove_default_evaluation_ordering'),
+    ]
+
+    operations = [
+        migrations.AlterModelOptions(
+            name='course',
+            options={'verbose_name': 'course', 'verbose_name_plural': 'courses'},
+        ),
+        migrations.AlterModelOptions(
+            name='questionnaire',
+            options={'ordering': ('type', 'order', 'pk'), 'verbose_name': 'questionnaire', 'verbose_name_plural': 'questionnaires'},
+        ),
+        migrations.AlterModelOptions(
+            name='semester',
+            options={'ordering': ('-created_at', 'pk'), 'verbose_name': 'semester', 'verbose_name_plural': 'semesters'},
+        ),
+    ]
diff --git a/evap/evaluation/models.py b/evap/evaluation/models.py
--- a/evap/evaluation/models.py
+++ b/evap/evaluation/models.py
@@ -50,7 +50,7 @@ class Semester(models.Model):
     created_at = models.DateField(verbose_name=_("created at"), auto_now_add=True)
 
     class Meta:
-        ordering = ('-created_at', 'name_de')
+        ordering = ('-created_at', 'pk')
         verbose_name = _("semester")
         verbose_name_plural = _("semesters")
 
@@ -173,7 +173,7 @@ class Questionnaire(models.Model):
     objects = QuestionnaireManager()
 
     class Meta:
-        ordering = ('type', 'order', 'name_de')
+        ordering = ('type', 'order', 'pk')
         verbose_name = _("questionnaire")
         verbose_name_plural = _("questionnaires")
 
@@ -181,10 +181,10 @@ def __str__(self):
         return self.name
 
     def __lt__(self, other):
-        return (self.type, self.order, self.name_de) < (other.type, other.order, self.name_de)
+        return (self.type, self.order, self.pk) < (other.type, other.order, other.pk)
 
     def __gt__(self, other):
-        return (self.type, self.order, self.name_de) > (other.type, other.order, self.name_de)
+        return (self.type, self.order, self.pk) > (other.type, other.order, other.pk)
 
     @property
     def is_above_contributors(self):
@@ -288,7 +288,6 @@ class Course(models.Model):
     last_modified_user = models.ForeignKey(settings.AUTH_USER_MODEL, models.SET_NULL, null=True, blank=True, related_name="courses_last_modified+")
 
     class Meta:
-        ordering = ('name_de',)
         unique_together = (
             ('semester', 'name_de'),
             ('semester', 'name_en'),
diff --git a/evap/staff/views.py b/evap/staff/views.py
--- a/evap/staff/views.py
+++ b/evap/staff/views.py
@@ -600,6 +600,7 @@ def semester_grade_reminder(request, semester_id):
 
     courses = semester.courses.filter(evaluations__state__in=['evaluated', 'reviewed', 'published'], is_graded=True, gets_no_grade_documents=False).distinct()
     courses = [course for course in courses if not course.final_grade_documents.exists()]
+    courses.sort(key=lambda course: course.name)
 
     responsibles = list(set(responsible for course in courses for responsible in course.responsibles.all()))
     responsibles.sort(key=lambda responsible: (responsible.last_name.lower(), responsible.first_name.lower()))


Test patch: