Repository: apluslms/a-plus

Issue:
Store the language meta data for all submissions
Now that https://github.com/apluslms/a-plus/issues/581 has been done and submissions store a meta data field for at least the language and the group ID, the code should be changed so that the language is always stored in the meta data even if the HTTP POST request does not include the `__aplus__={"lang": "en"}` parameter. If the request does not define the language, then the language is taken from the user's session as usual (basically, Django's `get_language()` unless there is a better alternative). In the API, the user's default language from the user profile could maybe be used, though, other language issues may change the userprofile.

Storing the language is important so that it can be used for regrading. Otherwise, I think regrading might read the language from the teacher's session when he/she starts the regrading in the inspect submission page and that is not sensible at all.

The exercise grade method 
https://github.com/apluslms/a-plus/blob/0cf8c3126d6011f08201764c3878b930ec4f3e31/exercise/exercise_models.py#L693
should always take the language from the submission meta data. 


Discussion:

Resolving patch:
diff --git a/course/models.py b/course/models.py
--- a/course/models.py
+++ b/course/models.py
@@ -474,6 +474,14 @@ def clean(self):
     def is_valid_language(self, lang):
         return lang == "" or lang in [key for key,name in settings.LANGUAGES]
 
+    @property
+    def default_language(self):
+        language = self.language
+        language_code = language.lstrip('|').split('|', 1)[0]
+        if language_code:
+            return language_code
+        return settings.LANGUAGE_CODE.split('-', 1)[0]
+
     def save(self, *args, **kwargs):
         """
         Saves the model.
diff --git a/exercise/exercise_models.py b/exercise/exercise_models.py
--- a/exercise/exercise_models.py
+++ b/exercise/exercise_models.py
@@ -705,7 +705,7 @@ def grade(self, request, submission, no_penalties=False, url_name="exercise"):
         Loads the exercise feedback page.
         """
         # Get the language from the submission
-        language = submission.lang or get_language()
+        language = submission.lang or self.course_module.course_instance.default_language
 
         submission_url = update_url_params(
             api_reverse("submission-grader", kwargs={
diff --git a/exercise/submission_models.py b/exercise/submission_models.py
--- a/exercise/submission_models.py
+++ b/exercise/submission_models.py
@@ -8,7 +8,7 @@
 from django.db import models, DatabaseError
 from django.db.models.signals import post_delete
 from django.utils import timezone
-from django.utils.translation import ugettext_lazy as _
+from django.utils.translation import get_language, ugettext_lazy as _
 from mimetypes import guess_type
 
 from lib.fields import JSONField, PercentField
@@ -38,6 +38,9 @@ def create_from_post(self, exercise, submitters, request):
             meta_data_dict = json.loads(request.POST.get('__aplus__', '{}'))
         except json.JSONDecodeError:
             raise ValueError("The content of the field __aplus__ is not valid json")
+        if 'lang' not in meta_data_dict:
+            meta_data_dict['lang'] = get_language()
+
         try:
             new_submission = Submission.objects.create(
                 exercise=exercise,


Test patch: