diff --git a/elections2021/models.py b/elections2021/models.py index d8f6170d8dd0beee8e5f038fc039cb383cbef4ee..3d59a76faadaf3d2833d514b3f4fc18537362652 100644 --- a/elections2021/models.py +++ b/elections2021/models.py @@ -33,6 +33,7 @@ from shared.utils import get_subpage_url from tuning import help from .constants import ( + AGE_30_49, ARTICLE_RICH_TEXT_FEATURES, ARTICLES_PER_PAGE, BENEFITS_CHOICES, @@ -64,6 +65,7 @@ from .constants import ( MINISTRY_TRANSPORT, NATURE, OCCUPATION_BUSINESS, + OCCUPATION_WORKING, PARENTS, PARTY_CHOICES, PARTY_NAME, @@ -93,6 +95,7 @@ from .constants import ( STYLE_CSS, TECHNOLOGY, TOP_CANDIDATES_NUM, + WEALTH_AVERAGE, WEALTH_BAD, WHITE, WORKING_SENIORS, @@ -888,15 +891,17 @@ class Elections2021ProgramPage( def get_meta_image(self): return self.search_image or self.photo - def get_context(self, request, parent_context=None): - context = super().get_context(request, parent_context=parent_context) + def get_context(self, request): + context = super().get_context(request) context["plan_choices"] = PLAN_CHOICES context["ministry_choices"] = MINISTRY_CHOICES context["show_archetype_icon"] = True - context["show_app_banner"] = True context["show_pagination"] = True context["active_my_program"] = False - context["has_my_program"] = self.get_my_selection(request) is not None + context["has_my_program"] = ( + Elections2021ProgramAppPage.get_my_selection(request) is not None + ) + context["show_app_banner"] = not context["has_my_program"] return context @staticmethod @@ -906,15 +911,6 @@ class Elections2021ProgramPage( def get_my_program_url(self): return self.url + self.reverse_subpage("my_program") - def get_my_selection(self, request): - data = request.get_signed_cookie(settings.ELECTIONS2021_COOKIE_NAME, None) - if data: - try: - return json.loads(data) - except json.JSONDecodeError: - pass - return None - @route(r"^$") def plan_all(self, request, param=None): points = Elections2021ProgramPointPage.objects.live().specific() @@ -1037,7 +1033,7 @@ class Elections2021ProgramPage( @route(r"^muj-program/$") def my_program(self, request): - selection = self.get_my_selection(request) + selection = Elections2021ProgramAppPage.get_my_selection(request) if selection is None: response = HttpResponseRedirect(self.url) # očekávali jsme data z cookie, ale nepodařilo se je získat, tak pro @@ -1607,3 +1603,40 @@ class Elections2021ProgramAppPage(SubpageMixin, MetadataPageMixin, Page): ) return response return super().serve(request) + + @staticmethod + def get_my_selection(request): + data = request.get_signed_cookie(settings.ELECTIONS2021_COOKIE_NAME, None) + if data: + try: + return json.loads(data) + except json.JSONDecodeError: + pass + return None + + def get_context(self, request): + context = super().get_context(request) + selection = self.get_my_selection(request) + if selection is None: + selection = { + "age": AGE_30_49, + "kids": True, + "wealth": WEALTH_AVERAGE, + "occupation": OCCUPATION_WORKING, + "topics": [], + } + context["active"] = { + "age": selection["age"], + "kids": selection["kids"], + "wealth": selection["wealth"], + "occupation": selection["occupation"], + "nature": NATURE in selection["topics"], + "sport": SPORT in selection["topics"], + "health": HEALTH in selection["topics"], + "culture": CULTURE in selection["topics"], + "technology": TECHNOLOGY in selection["topics"], + "countryside": COUNTRYSIDE in selection["topics"], + "housing": HOUSING in selection["topics"], + "education": EDUCATION in selection["topics"], + } + return context diff --git a/elections2021/templates/elections2021/elections2021_program_app_page.html b/elections2021/templates/elections2021/elections2021_program_app_page.html index 5688eadcb2c46e17d80e88076d33deccedc0a43b..631055cdb6fa93797ea1fd37c1d250a2377c9591 100644 --- a/elections2021/templates/elections2021/elections2021_program_app_page.html +++ b/elections2021/templates/elections2021/elections2021_program_app_page.html @@ -30,22 +30,22 @@ </div> <div class="col-span-4 md:col-span-3 md:pl-16 mt-5 md:mt-0 inline-flex flex-wrap"> <div class="radio program-radio form-field__control mb-2 mr-2"> - <input type="radio" name="age" id="18-29" value="18-29"> + <input type="radio" name="age" id="18-29" value="18-29" {% if active.age == "18-29" %}checked=""{% endif %}> <label for="18-29">18 - 29 LET</label> </div> <div class="radio program-radio form-field__control mb-2 mr-2"> - <input type="radio" name="age" id="30-49" value="30-49" checked=""> + <input type="radio" name="age" id="30-49" value="30-49" {% if active.age == "30-49" %}checked=""{% endif %}> <label for="30-49">30 - 49 LET</label> </div> <div class="radio program-radio form-field__control mb-2 mr-2"> - <input type="radio" name="age" id="50-64" value="50-64"> + <input type="radio" name="age" id="50-64" value="50-64" {% if active.age == "50-64" %}checked=""{% endif %}> <label for="50-64">50 - 64 LET</label> </div> <div class="radio program-radio form-field__control mb-2 mr-2"> - <input type="radio" name="age" id="65-plus" value="65-plus"> + <input type="radio" name="age" id="65-plus" value="65-plus" {% if active.age == "65-plus" %}checked=""{% endif %}> <label for="65-plus">65+ LET</label> </div> @@ -59,12 +59,12 @@ </div> <div class="col-span-4 md:col-span-3 md:pl-16 mt-5 md:mt-0 inline-flex flex-wrap"> <div class="radio program-radio form-field__control mb-2 mr-2"> - <input type="radio" name="kids" id="ano" value="true" checked=""> + <input type="radio" name="kids" id="ano" value="true" {% if active.kids %}checked=""{% endif %}> <label for="ano">ANO</label> </div> <div class="radio program-radio form-field__control mb-2 mr-2"> - <input type="radio" name="kids" id="ne" value="false"> + <input type="radio" name="kids" id="ne" value="false" {% if not active.kids %}checked=""{% endif %}> <label for="ne">NE</label> </div> @@ -78,17 +78,17 @@ </div> <div class="col-span-4 md:col-span-3 md:pl-16 mt-5 md:mt-0 inline-flex flex-wrap"> <div class="radio program-radio form-field__control mb-2 mr-2"> - <input type="radio" name="wealth" id="bad" value="bad"> + <input type="radio" name="wealth" id="bad" value="bad" {% if active.wealth == "bad" %}checked=""{% endif %}> <label for="bad">NIC MOC</label> </div> <div class="radio program-radio form-field__control mb-2 mr-2"> - <input type="radio" name="wealth" id="average" value="average" checked=""> + <input type="radio" name="wealth" id="average" value="average" {% if active.wealth == "average" %}checked=""{% endif %}> <label for="average">PRŮMĚRNĚ</label> </div> <div class="radio program-radio form-field__control mb-2 mr-2"> - <input type="radio" name="wealth" id="good" value="good"> + <input type="radio" name="wealth" id="good" value="good" {% if active.wealth == "good" %}checked=""{% endif %}> <label for="good">SKVĚLE</label> </div> @@ -102,27 +102,27 @@ </div> <div class="col-span-4 md:col-span-3 md:pl-16 mt-5 md:mt-0 inline-flex flex-wrap"> <div class="radio program-radio form-field__control mb-2 mr-2"> - <input type="radio" name="occupation" id="student" value="student"> + <input type="radio" name="occupation" id="student" value="student" {% if active.occupation == "student" %}checked=""{% endif %}> <label for="student">STUDUJI</label> </div> <div class="radio program-radio form-field__control mb-2 mr-2"> - <input type="radio" name="occupation" id="working" value="working" checked=""> + <input type="radio" name="occupation" id="working" value="working" {% if active.occupation == "working" %}checked=""{% endif %}> <label for="working">PRACUJI</label> </div> <div class="radio program-radio form-field__control mb-2 mr-2"> - <input type="radio" name="occupation" id="business" value="business"> + <input type="radio" name="occupation" id="business" value="business" {% if active.occupation == "business" %}checked=""{% endif %}> <label for="business">PODNIKÁM</label> </div> <div class="radio program-radio form-field__control mb-2 mr-2"> - <input type="radio" name="occupation" id="parenting" value="parenting"> + <input type="radio" name="occupation" id="parenting" value="parenting" {% if active.occupation == "parenting" %}checked=""{% endif %}> <label for="parenting">NA RODIČOVSKÉ</label> </div> <div class="radio program-radio form-field__control mb-2 mr-2"> - <input type="radio" name="occupation" id="retired" value="retired"> + <input type="radio" name="occupation" id="retired" value="retired" {% if active.occupation == "retired" %}checked=""{% endif %}> <label for="retired">V DŮCHODU</label> </div> @@ -137,49 +137,49 @@ <div class="col-span-4 md:col-span-3 md:pl-16 mt-5 md:mt-0 inline-flex flex-wrap"> <div class="checkbox program-checkbox form-field__control mr-2 mb-2 "> <label> - <input type="checkbox" id="nature" value="nature" name="topics"> <span>PŘÍRODA</span> + <input type="checkbox" id="nature" value="nature" name="topics" {% if active.nature %}checked=""{% endif %}> <span>PŘÍRODA</span> </label> </div> <div class="checkbox program-checkbox form-field__control mr-2 mb-2 "> <label> - <input type="checkbox" id="sport" value="sport" name="topics"> <span>SPORT</span> + <input type="checkbox" id="sport" value="sport" name="topics" {% if active.sport %}checked=""{% endif %}> <span>SPORT</span> </label> </div> <div class="checkbox program-checkbox form-field__control mr-2 mb-2 "> <label> - <input type="checkbox" id="health" value="health" name="topics"> <span>ZDRAVÍ</span> + <input type="checkbox" id="health" value="health" name="topics" {% if active.health %}checked=""{% endif %}> <span>ZDRAVÍ</span> </label> </div> <div class="checkbox program-checkbox form-field__control mr-2 mb-2 "> <label> - <input type="checkbox" id="culture" value="culture" name="topics"> <span>KULTURA</span> + <input type="checkbox" id="culture" value="culture" name="topics" {% if active.culture %}checked=""{% endif %}> <span>KULTURA</span> </label> </div> <div class="checkbox program-checkbox form-field__control mr-2 mb-2 "> <label> - <input type="checkbox" id="technology" value="technology" name="topics"> <span>TECHNOLOGIE</span> + <input type="checkbox" id="technology" value="technology" name="topics" {% if active.technology %}checked=""{% endif %}> <span>TECHNOLOGIE</span> </label> </div> <div class="checkbox program-checkbox form-field__control mr-2 mb-2 "> <label> - <input type="checkbox" id="countryside" value="countryside" name="topics"> <span>REGIONY</span> + <input type="checkbox" id="countryside" value="countryside" name="topics" {% if active.countryside %}checked=""{% endif %}> <span>REGIONY</span> </label> </div> <div class="checkbox program-checkbox form-field__control mr-2 mb-2 "> <label> - <input type="checkbox" id="housing" value="housing" name="topics"> <span>BYDLENÍ</span> + <input type="checkbox" id="housing" value="housing" name="topics" {% if active.housing %}checked=""{% endif %}> <span>BYDLENÍ</span> </label> </div> <div class="checkbox program-checkbox form-field__control mr-2 mb-2 "> <label> - <input type="checkbox" id="education" value="education" name="topics"> <span>VZDĚLÁVÁNÍ</span> + <input type="checkbox" id="education" value="education" name="topics" {% if active.education %}checked=""{% endif %}> <span>VZDĚLÁVÁNÍ</span> </label> </div>