Skip to content

Commit

Permalink
Support placeholder text in questionnaire FreeText fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ihalaij1 committed May 23, 2024
1 parent 3961823 commit 3c91dc2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
21 changes: 19 additions & 2 deletions access/types/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from django.utils.safestring import mark_safe
from django.utils.translation import gettext as _

from access.config import ConfigParser
from util.templates import template_to_str
from util import forms as custom_forms
from .auth import make_hash
Expand All @@ -34,6 +35,7 @@ def __init__(self, *args, **kwargs):
if "exercise" not in kwargs:
raise ConfigError("Missing exercise configuration from form arguments.")
self.exercise = kwargs.pop("exercise")
self.course = kwargs.pop("course")
self.model_answer = kwargs.pop('model_answer', False)
self.reveal_correct = kwargs.pop('reveal_correct', False)
if not self.exercise.get('reveal_model_at_max_submissions', False):
Expand Down Expand Up @@ -127,13 +129,28 @@ def __init__(self, *args, **kwargs):
forms.ChoiceField, forms.Select,
initial, correct, neutral, choices, False)
elif t == "text":
attrs = {'class': 'form-control'}
if 'extra_info' in field:
_, exercise_entry = ConfigParser().exercise_entry(
self.course['key'],
self.exercise['key'],
self.exercise.get('lang', None),
)
for key in ['placeholder']:
if key in field['extra_info']:
value = exercise_entry['fieldgroups'][g]['fields'][j]['extra_info'][key]
attrs[key] = value
i, f = self.add_field(i, field,
self._get_text_field_type(field), forms.TextInput)
self._get_text_field_type(field), forms.TextInput,
widget_attrs=attrs)
elif t == "textarea":
attrs = {'class': 'form-control'}
for key in ['rows','cols']:
for key in ['rows', 'cols']:
if key in field:
attrs[key] = field[key]
for key in ['placeholder']:
if 'extra_info' in field and key in field['extra_info']:
attrs[key] = str(field['extra_info'][key])
i, f = self.add_field(i, field,
self._get_text_field_type(field), forms.Textarea,
widget_attrs=attrs)
Expand Down
4 changes: 2 additions & 2 deletions access/types/stdsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def createForm(request, course, exercise, post_url):

try:
form = GradedForm(request.POST or None, request.FILES or None,
exercise=exercise, reveal_correct=last, request=request)
exercise=exercise, reveal_correct=last, request=request, course=course)
except PermissionDenied:
# Randomized forms raise PermissionDenied when the POST data contains
# forged checksums or samples. It could be cleaner to check those
Expand Down Expand Up @@ -155,7 +155,7 @@ def createForm(request, course, exercise, post_url):


def createFormModel(request, course, exercise, parameter):
form = GradedForm(None, exercise=exercise, model_answer=True)
form = GradedForm(None, exercise=exercise, model_answer=True, course=course)
form.bind_initial()
points,error_groups,error_fields = form.grade()
result = {
Expand Down
16 changes: 10 additions & 6 deletions util/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,19 @@ def form_fields(languages, exercises):
def i18n_map(values):
if all(v == "" for v in values):
return ""
key = str(values[0])
key = str(values.copy()[0])
if key in values[1:]:
key = "i18n_" + "_".join(key.split())
translation_dict = {
l: str(v)
for l, v in zip(languages, values)
}
if key in i18n and i18n[key] == translation_dict:
# Identical key with the same value already exists, no need to create a duplicate
return key
while key in i18n:
key += "_duplicate"
i18n[key] = {
l: v
for l,v in zip(languages, values)
}
i18n[key] = translation_dict
return key

def field_spec(fs, n):
Expand Down Expand Up @@ -173,7 +177,7 @@ def field_spec(fs, n):
if 'extra_info' in f:
es = list_get(fs, 'extra_info', {})
extra = es[0]
for key in ['validationMessage']:
for key in ['validationMessage', 'placeholder']:
if key in extra:
extra[key] = i18n_map(list_get(es, key, ''))
field.update(extra)
Expand Down

0 comments on commit 3c91dc2

Please sign in to comment.