prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Grade preview
  There must be an option for FSR members to see the grades for a course before publishing it.
  This should be available in "in evaluation", "evaluated" and "reviewed".
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `evap/results/views.py`
  Content:
  ```
  1 from django.conf import settings
  2 from django.http import HttpResponse
  3 from django.shortcuts import get_object_or_404, render_to_response
  4 from django.template import RequestContext
  5 from django.utils.translation import get_language
  6 
  7 from evap.evaluation.auth import login_required, fsr_required
  8 from evap.evaluation.models import Semester
  9 from evap.evaluation.tools import calculate_results, calculate_average_and_medium_grades, TextResult, can_publish_grades
  10 
  11 from evap.results.exporters import ExcelExporter
  12 
  13 
  14 @login_required
  15 def index(request):
  16     semesters = Semester.get_all_with_published_courses()
  17 
  18     return render_to_response(
  19         "results_index.html",
  20         dict(semesters=semesters),
  21         context_instance=RequestContext(request))
  22 
  23 
  24 @login_required
  25 def semester_detail(request, semester_id):
  26     semester = get_object_or_404(Semester, id=semester_id)
  27     courses = list(semester.course_set.filter(state="published"))
  28 
  29     # annotate each course object with its grades
  30     for course in courses:
  31         # first, make sure that there are no preexisting grade attributes
  32         course.avg_grade, course.med_grade = calculate_average_and_medium_grades(course)
  33         course.can_publish_grades = can_publish_grades(course, request.user.is_staff)
  34 
  35     return render_to_response(
  36         "results_semester_detail.html",
  37         dict(
  38             semester=semester,
  39             courses=courses
  40         ),
  41         context_instance=RequestContext(request))
  42 
  43 
  44 @fsr_required
  45 def semester_export(request, semester_id):
  46     semester = get_object_or_404(Semester, id=semester_id)
  47 
  48     filename = "Evaluation-%s-%s.xls" % (semester.name, get_language())
  49 
  50     response = HttpResponse(mimetype="application/vnd.ms-excel")
  51     response["Content-Disposition"] = "attachment; filename=\"%s\"" % filename
  52 
  53     exporter = ExcelExporter(semester)
  54 
  55     if 'all' in request.GET:
  56         exporter.export(response, True)
  57     else:
  58         exporter.export(response)
  59 
  60     return response
  61 
  62 
  63 @login_required
  64 def course_detail(request, semester_id, course_id):
  65     semester = get_object_or_404(Semester, id=semester_id)
  66     course = get_object_or_404(semester.course_set.filter(state="published"), id=course_id)
  67 
  68     sections = calculate_results(course, request.user.is_staff)
  69 
  70     if (request.user.is_staff == False): # if user is not a student representative
  71     # remove TextResults if user is neither the evaluated person (or a delegate) nor responsible for the course (or a delegate)
  72         for section in sections:
  73             if not user_can_see_textresults(request.user, course, section):
  74                 for index, result in list(enumerate(section.results))[::-1]:
  75                     if isinstance(section.results[index], TextResult):
  76                         del section.results[index]
  77 
  78         # remove empty sections
  79         sections = [section for section in sections if section.results]
  80 
  81     # check whether results are published
  82     published = course.num_voters >= settings.MIN_ANSWER_COUNT and float(course.num_voters) / course.num_participants >= settings.MIN_ANSWER_PERCENTAGE
  83 
  84     # show a publishing warning to fsr members when the results are not publicly available
  85     warning = (not published) and request.user.is_staff
  86 
  87     return render_to_response(
  88         "results_course_detail.html",
  89         dict(
  90             course=course,
  91             sections=sections,
  92             warning=warning
  93         ),
  94         context_instance=RequestContext(request))
  95 
  96 
  97 def user_can_see_textresults(user, course, section):
  98     if section.contributor == user:
  99         return True
  100     if course.is_user_responsible_or_delegate(user):
  101         return True
  102 
  103     represented_userprofiles = user.represented_users.all()
  104     represented_users = [profile.user for profile in represented_userprofiles]
  105     if section.contributor in represented_users:
  106         return True
  107 
  108     return False
  109
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/evap/results/views.py b/evap/results/views.py
  --- a/evap/results/views.py
  +++ b/evap/results/views.py
  @@ -63,7 +63,13 @@
   @login_required
   def course_detail(request, semester_id, course_id):
       semester = get_object_or_404(Semester, id=semester_id)
  -    course = get_object_or_404(semester.course_set.filter(state="published"), id=course_id)
  +    
  +    # staff can see preview of results even when course is not published
  +    if request.user.is_staff:
  +        course = get_object_or_404(semester.course_set.filter(state__in=['inEvaluation', 'evaluated', 'reviewed', 'published']), id=course_id)
  +    # everyone else can only see published results
  +    else:
  +        course = get_object_or_404(semester.course_set.filter(state="published"), id=course_id)
   
       sections = calculate_results(course, request.user.is_staff)
   
  @@ -78,18 +84,24 @@
           # remove empty sections
           sections = [section for section in sections if section.results]
   
  -    # check whether results are published
  -    published = course.num_voters >= settings.MIN_ANSWER_COUNT and float(course.num_voters) / course.num_participants >= settings.MIN_ANSWER_PERCENTAGE
  +    # show a warning if course is still in evaluation (for staff preview)
  +    evaluation_warning = course.state != 'published'
  +
  +    # check whether course has a sufficient number of votes for publishing it
  +    sufficient_votes = course.num_voters >= settings.MIN_ANSWER_COUNT and float(course.num_voters) / course.num_participants >= settings.MIN_ANSWER_PERCENTAGE
   
  -    # show a publishing warning to fsr members when the results are not publicly available
  -    warning = (not published) and request.user.is_staff
  +    # results for a course might not be visible because there are not enough answers
  +    # but it can still be "published" e.g. to show the comment results to lecturers
  +    # the FSR can still see all results but gets a warning message
  +    sufficient_votes_warning = (not sufficient_votes) and request.user.is_staff
   
       return render_to_response(
           "results_course_detail.html",
           dict(
               course=course,
               sections=sections,
  -            warning=warning
  +            evaluation_warning=evaluation_warning,
  +            sufficient_votes_warning=sufficient_votes_warning
           ),
           context_instance=RequestContext(request))
  
verification_info: {"golden_diff": "diff --git a/evap/results/views.py b/evap/results/views.py\n--- a/evap/results/views.py\n+++ b/evap/results/views.py\n@@ -63,7 +63,13 @@\n @login_required\n def course_detail(request, semester_id, course_id):\n     semester = get_object_or_404(Semester, id=semester_id)\n-    course = get_object_or_404(semester.course_set.filter(state=\"published\"), id=course_id)\n+    \n+    # staff can see preview of results even when course is not published\n+    if request.user.is_staff:\n+        course = get_object_or_404(semester.course_set.filter(state__in=['inEvaluation', 'evaluated', 'reviewed', 'published']), id=course_id)\n+    # everyone else can only see published results\n+    else:\n+        course = get_object_or_404(semester.course_set.filter(state=\"published\"), id=course_id)\n \n     sections = calculate_results(course, request.user.is_staff)\n \n@@ -78,18 +84,24 @@\n         # remove empty sections\n         sections = [section for section in sections if section.results]\n \n-    # check whether results are published\n-    published = course.num_voters >= settings.MIN_ANSWER_COUNT and float(course.num_voters) / course.num_participants >= settings.MIN_ANSWER_PERCENTAGE\n+    # show a warning if course is still in evaluation (for staff preview)\n+    evaluation_warning = course.state != 'published'\n+\n+    # check whether course has a sufficient number of votes for publishing it\n+    sufficient_votes = course.num_voters >= settings.MIN_ANSWER_COUNT and float(course.num_voters) / course.num_participants >= settings.MIN_ANSWER_PERCENTAGE\n \n-    # show a publishing warning to fsr members when the results are not publicly available\n-    warning = (not published) and request.user.is_staff\n+    # results for a course might not be visible because there are not enough answers\n+    # but it can still be \"published\" e.g. to show the comment results to lecturers\n+    # the FSR can still see all results but gets a warning message\n+    sufficient_votes_warning = (not sufficient_votes) and request.user.is_staff\n \n     return render_to_response(\n         \"results_course_detail.html\",\n         dict(\n             course=course,\n             sections=sections,\n-            warning=warning\n+            evaluation_warning=evaluation_warning,\n+            sufficient_votes_warning=sufficient_votes_warning\n         ),\n         context_instance=RequestContext(request))\n", "issue": "Grade preview\nThere must be an option for FSR members to see the grades for a course before publishing it.\nThis should be available in \"in evaluation\", \"evaluated\" and \"reviewed\".\n\n", "before_files": [{"content": "from django.conf import settings\nfrom django.http import HttpResponse\nfrom django.shortcuts import get_object_or_404, render_to_response\nfrom django.template import RequestContext\nfrom django.utils.translation import get_language\n\nfrom evap.evaluation.auth import login_required, fsr_required\nfrom evap.evaluation.models import Semester\nfrom evap.evaluation.tools import calculate_results, calculate_average_and_medium_grades, TextResult, can_publish_grades\n\nfrom evap.results.exporters import ExcelExporter\n\n\n@login_required\ndef index(request):\n    semesters = Semester.get_all_with_published_courses()\n\n    return render_to_response(\n        \"results_index.html\",\n        dict(semesters=semesters),\n        context_instance=RequestContext(request))\n\n\n@login_required\ndef semester_detail(request, semester_id):\n    semester = get_object_or_404(Semester, id=semester_id)\n    courses = list(semester.course_set.filter(state=\"published\"))\n\n    # annotate each course object with its grades\n    for course in courses:\n        # first, make sure that there are no preexisting grade attributes\n        course.avg_grade, course.med_grade = calculate_average_and_medium_grades(course)\n        course.can_publish_grades = can_publish_grades(course, request.user.is_staff)\n\n    return render_to_response(\n        \"results_semester_detail.html\",\n        dict(\n            semester=semester,\n            courses=courses\n        ),\n        context_instance=RequestContext(request))\n\n\n@fsr_required\ndef semester_export(request, semester_id):\n    semester = get_object_or_404(Semester, id=semester_id)\n\n    filename = \"Evaluation-%s-%s.xls\" % (semester.name, get_language())\n\n    response = HttpResponse(mimetype=\"application/vnd.ms-excel\")\n    response[\"Content-Disposition\"] = \"attachment; filename=\\\"%s\\\"\" % filename\n\n    exporter = ExcelExporter(semester)\n\n    if 'all' in request.GET:\n        exporter.export(response, True)\n    else:\n        exporter.export(response)\n\n    return response\n\n\n@login_required\ndef course_detail(request, semester_id, course_id):\n    semester = get_object_or_404(Semester, id=semester_id)\n    course = get_object_or_404(semester.course_set.filter(state=\"published\"), id=course_id)\n\n    sections = calculate_results(course, request.user.is_staff)\n\n    if (request.user.is_staff == False): # if user is not a student representative\n    # remove TextResults if user is neither the evaluated person (or a delegate) nor responsible for the course (or a delegate)\n        for section in sections:\n            if not user_can_see_textresults(request.user, course, section):\n                for index, result in list(enumerate(section.results))[::-1]:\n                    if isinstance(section.results[index], TextResult):\n                        del section.results[index]\n\n        # remove empty sections\n        sections = [section for section in sections if section.results]\n\n    # check whether results are published\n    published = course.num_voters >= settings.MIN_ANSWER_COUNT and float(course.num_voters) / course.num_participants >= settings.MIN_ANSWER_PERCENTAGE\n\n    # show a publishing warning to fsr members when the results are not publicly available\n    warning = (not published) and request.user.is_staff\n\n    return render_to_response(\n        \"results_course_detail.html\",\n        dict(\n            course=course,\n            sections=sections,\n            warning=warning\n        ),\n        context_instance=RequestContext(request))\n\n\ndef user_can_see_textresults(user, course, section):\n    if section.contributor == user:\n        return True\n    if course.is_user_responsible_or_delegate(user):\n        return True\n\n    represented_userprofiles = user.represented_users.all()\n    represented_users = [profile.user for profile in represented_userprofiles]\n    if section.contributor in represented_users:\n        return True\n\n    return False\n", "path": "evap/results/views.py"}], "after_files": [{"content": "from django.conf import settings\nfrom django.http import HttpResponse\nfrom django.shortcuts import get_object_or_404, render_to_response\nfrom django.template import RequestContext\nfrom django.utils.translation import get_language\n\nfrom evap.evaluation.auth import login_required, fsr_required\nfrom evap.evaluation.models import Semester\nfrom evap.evaluation.tools import calculate_results, calculate_average_and_medium_grades, TextResult, can_publish_grades\n\nfrom evap.results.exporters import ExcelExporter\n\n\n@login_required\ndef index(request):\n    semesters = Semester.get_all_with_published_courses()\n\n    return render_to_response(\n        \"results_index.html\",\n        dict(semesters=semesters),\n        context_instance=RequestContext(request))\n\n\n@login_required\ndef semester_detail(request, semester_id):\n    semester = get_object_or_404(Semester, id=semester_id)\n    courses = list(semester.course_set.filter(state=\"published\"))\n\n    # annotate each course object with its grades\n    for course in courses:\n        # first, make sure that there are no preexisting grade attributes\n        course.avg_grade, course.med_grade = calculate_average_and_medium_grades(course)\n        course.can_publish_grades = can_publish_grades(course, request.user.is_staff)\n\n    return render_to_response(\n        \"results_semester_detail.html\",\n        dict(\n            semester=semester,\n            courses=courses\n        ),\n        context_instance=RequestContext(request))\n\n\n@fsr_required\ndef semester_export(request, semester_id):\n    semester = get_object_or_404(Semester, id=semester_id)\n\n    filename = \"Evaluation-%s-%s.xls\" % (semester.name, get_language())\n\n    response = HttpResponse(mimetype=\"application/vnd.ms-excel\")\n    response[\"Content-Disposition\"] = \"attachment; filename=\\\"%s\\\"\" % filename\n\n    exporter = ExcelExporter(semester)\n\n    if 'all' in request.GET:\n        exporter.export(response, True)\n    else:\n        exporter.export(response)\n\n    return response\n\n\n@login_required\ndef course_detail(request, semester_id, course_id):\n    semester = get_object_or_404(Semester, id=semester_id)\n    \n    # staff can see preview of results even when course is not published\n    if request.user.is_staff:\n        course = get_object_or_404(semester.course_set.filter(state__in=['inEvaluation', 'evaluated', 'reviewed', 'published']), id=course_id)\n    # everyone else can only see published results\n    else:\n        course = get_object_or_404(semester.course_set.filter(state=\"published\"), id=course_id)\n\n    sections = calculate_results(course, request.user.is_staff)\n\n    if (request.user.is_staff == False): # if user is not a student representative\n    # remove TextResults if user is neither the evaluated person (or a delegate) nor responsible for the course (or a delegate)\n        for section in sections:\n            if not user_can_see_textresults(request.user, course, section):\n                for index, result in list(enumerate(section.results))[::-1]:\n                    if isinstance(section.results[index], TextResult):\n                        del section.results[index]\n\n        # remove empty sections\n        sections = [section for section in sections if section.results]\n\n    # show a warning if course is still in evaluation (for staff preview)\n    evaluation_warning = course.state != 'published'\n\n    # check whether course has a sufficient number of votes for publishing it\n    sufficient_votes = course.num_voters >= settings.MIN_ANSWER_COUNT and float(course.num_voters) / course.num_participants >= settings.MIN_ANSWER_PERCENTAGE\n\n    # results for a course might not be visible because there are not enough answers\n    # but it can still be \"published\" e.g. to show the comment results to lecturers\n    # the FSR can still see all results but gets a warning message\n    sufficient_votes_warning = (not sufficient_votes) and request.user.is_staff\n\n    return render_to_response(\n        \"results_course_detail.html\",\n        dict(\n            course=course,\n            sections=sections,\n            evaluation_warning=evaluation_warning,\n            sufficient_votes_warning=sufficient_votes_warning\n        ),\n        context_instance=RequestContext(request))\n\n\ndef user_can_see_textresults(user, course, section):\n    if section.contributor == user:\n        return True\n    if course.is_user_responsible_or_delegate(user):\n        return True\n\n    represented_userprofiles = user.represented_users.all()\n    represented_users = [profile.user for profile in represented_userprofiles]\n    if section.contributor in represented_users:\n        return True\n\n    return False\n", "path": "evap/results/views.py"}]}