From eee57bea59ab23ae31b2b052ebdc78097c0958ba Mon Sep 17 00:00:00 2001 From: Michael Villeneuve Date: Tue, 23 Jul 2024 14:51:05 +0200 Subject: [PATCH] wip: Checker class --- proposals/utils/stepper.py | 97 ++++++++++++++++++++++++++++++++------ 1 file changed, 82 insertions(+), 15 deletions(-) diff --git a/proposals/utils/stepper.py b/proposals/utils/stepper.py index 3f15aee51..9899401ed 100644 --- a/proposals/utils/stepper.py +++ b/proposals/utils/stepper.py @@ -3,6 +3,8 @@ from django.template import loader, Template from django.core.exceptions import ImproperlyConfigured +from proposals.views.proposal_views import ProposalUpdate + def flatten(lst): if lst == []: return lst @@ -27,11 +29,13 @@ def render(self, context={}): class Stepper(renderable): template_name = "base/stepper.html" + starting_checkers = [] def __init__(self, proposal, current=None): self.proposal = proposal self.current = current + self.check_all(self.starting_checkers) def get_context_data(self): return {"stepper": self} @@ -53,32 +57,95 @@ def collect_items(self): some_pages.append( Page(self.proposal) ) + first_page = FirstPage() + some_pages.append(first_page) return some_pages + def check_all(self, next_checkers): + # No more checkers means we are done + if next_checkers == []: + return True + # Instantiate next checker + # and give it access to the stepper + current = next_checkers.pop(0)(self) + # Run the check method + # and gather new checkers from its output + new_checkers = current.check() + # Combine the lists, new checkers come first + next_checkers = new_checkers + next_checkers + # Recurse until next_checkers is empty + return self.check_all(next_checkers) + +class BaseChecker: + + def __init__(self, stepper): + self.stepper = stepper + + def __call__(self, *args, **kwargs): + """ + This class may be called to initialize it when it is + already initialized. For now, we don't do anything with this + and pretend we just got initialized. + """ + return self + + def check(self): + """ + This method gets called to process an item in the proposal creation + process. It finall returns a list of checkers with which to continue + the checking process. This list can be empty. + """ + return [] + -class StepperContextMixin: +class StepperItem: """ - Includes a stepper object in the view's context + Represents an item in the stepper """ - def get_context_data(self, *args, **kwargs): - context = super().get_context_data(*args, **kwargs) - # Try to determine proposal - if hasattr(self, "proposal"): - proposal = self.proposal - else: - proposal = Proposal.objects.get(pk=self.kwargs.get("pk")) - # Initialize and insert stepper object - context["stepper"] = Stepper(proposal) - return context + stepper_title = "Stepper item" + stepper_children = [] + available = True + + @property + def stepper_url(self): + return "#" + + def is_current(self, request): + """ + Returns True if this item represents the page the user + is currently on. + """ + return False + +class FormPage(StepperItem): + + def instantiate(self): + pass + +class ViewPage(StepperItem): + + view_class = None + + def is_current(self, request): + breakpoint() + return True + def css_classes(self): + classes = set() + if self.is_current(): + classess.add("current") -class Page: + +class FirstPage(ViewPage): + + view_class = ProposalUpdate + + +class Page(StepperItem): """Represents both a visitable page in the proposal form, and an entry in the stepper.""" - stepper_title = "Stepper item" - def __init__(self, proposal, current=None): """Initialize the page with the proposal object""" self.proposal = proposal