Skip to content

Commit f51aa7a

Browse files
authored
Merge pull request #2258 from IFRCGo/feature/per-form-component-description
PER Form Component: Add guidance fields.
2 parents f38a349 + e49b3d2 commit f51aa7a

File tree

8 files changed

+385
-4
lines changed

8 files changed

+385
-4
lines changed

per/admin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class FormAreaAdmin(CompareVersionAdmin, TranslationAdmin):
5656
class FormComponentAdmin(CompareVersionAdmin, TranslationAdmin):
5757
search_fields = ("title",)
5858
list_display = ("title", "area_number", "component_number")
59+
autocomplete_fields = ["question_responses"]
5960

6061
def component_number(self, obj):
6162
return f'{obj.component_num or ""}{obj.component_letter or ""}'
@@ -161,7 +162,9 @@ class FormComponentResponseAdmin(TranslationAdmin):
161162

162163

163164
class FormComponentQuestionAndAnswerAdmin(TranslationAdmin):
164-
pass
165+
search_fields = [
166+
"question",
167+
]
165168

166169

167170
class OrganizationTypesAdmin(admin.ModelAdmin):

per/drf_views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ def get(self, request, version=None):
543543
return response.Response(
544544
PerOptionsSerializer(
545545
dict(
546-
componentratings=PerComponentRating.objects.all(),
546+
componentratings=PerComponentRating.objects.all().order_by("value"),
547547
answers=FormAnswer.objects.all(),
548548
overviewassessmenttypes=AssessmentType.objects.all(),
549549
)

per/fixtures/form_components.csv

Lines changed: 203 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import csv
2+
import logging
3+
import os
4+
5+
import markdown
6+
from django.core.management.base import BaseCommand
7+
8+
from main.managers import BulkUpdateManager
9+
from per.models import FormComponent
10+
11+
logger = logging.getLogger(__name__)
12+
13+
14+
def format_description(description):
15+
markdown_text = markdown.markdown(description)
16+
return markdown_text
17+
18+
19+
class Command(BaseCommand):
20+
help = "Load form components from a CSV file"
21+
22+
def handle(self, *args, **kwargs):
23+
command_dir = os.path.dirname(os.path.abspath(__file__))
24+
csv_file_path = os.path.join(command_dir, "../../fixtures/form_components.csv")
25+
26+
with open(csv_file_path, newline="", encoding="utf-8") as csvfile:
27+
reader = csv.DictReader(csvfile)
28+
bulk_mgr = BulkUpdateManager(
29+
[
30+
"urban_considerations_guidance_en",
31+
"epi_considerations_guidance_en",
32+
"climate_environmental_considerations_guidance_en",
33+
],
34+
chunk_size=20,
35+
)
36+
37+
for row in reader:
38+
form_component = FormComponent.objects.filter(title__icontains=row["Component name"].strip()).first()
39+
if form_component is None:
40+
logger.warning(f"Form component with name {row['Component name']} is missing ... Skipping.....")
41+
continue
42+
bulk_mgr.add(
43+
FormComponent(
44+
id=form_component.id,
45+
urban_considerations_guidance_en=format_description(row["Urban Description"]),
46+
epi_considerations_guidance=format_description(row["Epidemics Description"]),
47+
climate_environmental_considerations_guidance=format_description(
48+
row["Climate & Environmental Description"]
49+
),
50+
)
51+
)
52+
bulk_mgr.done()
53+
self.stdout.write(self.style.SUCCESS(f"Updated: {bulk_mgr.summary()}"))
54+
55+
logger.info("PER From Component loaded Successfully")
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Generated by Django 4.2.15 on 2024-09-12 05:40
2+
3+
import tinymce.models
4+
from django.db import migrations
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
("per", "0120_alter_formcomponent_status"),
11+
]
12+
13+
operations = [
14+
migrations.AddField(
15+
model_name="formcomponent",
16+
name="climate_environmental_considerations_guidance",
17+
field=tinymce.models.HTMLField(blank=True, default="", verbose_name="Climate and Environmental Considerations "),
18+
),
19+
migrations.AddField(
20+
model_name="formcomponent",
21+
name="climate_environmental_considerations_guidance_ar",
22+
field=tinymce.models.HTMLField(
23+
blank=True, default="", null=True, verbose_name="Climate and Environmental Considerations "
24+
),
25+
),
26+
migrations.AddField(
27+
model_name="formcomponent",
28+
name="climate_environmental_considerations_guidance_en",
29+
field=tinymce.models.HTMLField(
30+
blank=True, default="", null=True, verbose_name="Climate and Environmental Considerations "
31+
),
32+
),
33+
migrations.AddField(
34+
model_name="formcomponent",
35+
name="climate_environmental_considerations_guidance_es",
36+
field=tinymce.models.HTMLField(
37+
blank=True, default="", null=True, verbose_name="Climate and Environmental Considerations "
38+
),
39+
),
40+
migrations.AddField(
41+
model_name="formcomponent",
42+
name="climate_environmental_considerations_guidance_fr",
43+
field=tinymce.models.HTMLField(
44+
blank=True, default="", null=True, verbose_name="Climate and Environmental Considerations "
45+
),
46+
),
47+
migrations.AddField(
48+
model_name="formcomponent",
49+
name="epi_considerations_guidance",
50+
field=tinymce.models.HTMLField(blank=True, default="", verbose_name="EPI Considerations"),
51+
),
52+
migrations.AddField(
53+
model_name="formcomponent",
54+
name="epi_considerations_guidance_ar",
55+
field=tinymce.models.HTMLField(blank=True, default="", null=True, verbose_name="EPI Considerations"),
56+
),
57+
migrations.AddField(
58+
model_name="formcomponent",
59+
name="epi_considerations_guidance_en",
60+
field=tinymce.models.HTMLField(blank=True, default="", null=True, verbose_name="EPI Considerations"),
61+
),
62+
migrations.AddField(
63+
model_name="formcomponent",
64+
name="epi_considerations_guidance_es",
65+
field=tinymce.models.HTMLField(blank=True, default="", null=True, verbose_name="EPI Considerations"),
66+
),
67+
migrations.AddField(
68+
model_name="formcomponent",
69+
name="epi_considerations_guidance_fr",
70+
field=tinymce.models.HTMLField(blank=True, default="", null=True, verbose_name="EPI Considerations"),
71+
),
72+
migrations.AddField(
73+
model_name="formcomponent",
74+
name="urban_considerations_guidance",
75+
field=tinymce.models.HTMLField(blank=True, default="", verbose_name="Urban Considerations"),
76+
),
77+
migrations.AddField(
78+
model_name="formcomponent",
79+
name="urban_considerations_guidance_ar",
80+
field=tinymce.models.HTMLField(blank=True, default="", null=True, verbose_name="Urban Considerations"),
81+
),
82+
migrations.AddField(
83+
model_name="formcomponent",
84+
name="urban_considerations_guidance_en",
85+
field=tinymce.models.HTMLField(blank=True, default="", null=True, verbose_name="Urban Considerations"),
86+
),
87+
migrations.AddField(
88+
model_name="formcomponent",
89+
name="urban_considerations_guidance_es",
90+
field=tinymce.models.HTMLField(blank=True, default="", null=True, verbose_name="Urban Considerations"),
91+
),
92+
migrations.AddField(
93+
model_name="formcomponent",
94+
name="urban_considerations_guidance_fr",
95+
field=tinymce.models.HTMLField(blank=True, default="", null=True, verbose_name="Urban Considerations"),
96+
),
97+
]

per/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ class FormComponentStatus(models.TextChoices):
101101
question_responses = models.ManyToManyField(FormComponentQuestionAndAnswer, verbose_name=_("Question responses"), blank=True)
102102
is_parent = models.BooleanField(verbose_name=_("Is parent"), null=True, blank=True)
103103
has_question_group = models.BooleanField(verbose_name=_("Has Question Group"), null=True, blank=True)
104+
urban_considerations_guidance = HTMLField(verbose_name=_("Urban Considerations"), blank=True, default="")
105+
epi_considerations_guidance = HTMLField(verbose_name=_("EPI Considerations"), blank=True, default="")
106+
climate_environmental_considerations_guidance = HTMLField(
107+
verbose_name=_("Climate and Environmental Considerations "), blank=True, default=""
108+
)
104109

105110
def __str__(self):
106111
return f"Component {self.component_num} - {self.title}"

per/serializers.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,19 @@ class FormComponentSerializer(NestedCreateMixin, NestedUpdateMixin, ModelSeriali
8686

8787
class Meta:
8888
model = FormComponent
89-
fields = ("id", "title", "component_num", "description", "area", "component_letter", "is_parent", "has_question_group")
89+
fields = (
90+
"id",
91+
"title",
92+
"component_num",
93+
"description",
94+
"area",
95+
"component_letter",
96+
"is_parent",
97+
"has_question_group",
98+
"epi_considerations_guidance",
99+
"climate_environmental_considerations_guidance",
100+
"urban_considerations_guidance",
101+
)
90102

91103

92104
class FormAnswerSerializer(ModelSerializer):

per/translation.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ class FormAreaTO(TranslationOptions):
2727

2828
@register(FormComponent)
2929
class FormComponentTO(TranslationOptions):
30-
fields = ("title", "description")
30+
fields = (
31+
"title",
32+
"description",
33+
"urban_considerations_guidance",
34+
"epi_considerations_guidance",
35+
"climate_environmental_considerations_guidance",
36+
)
3137

3238

3339
@register(FormAnswer)

0 commit comments

Comments
 (0)