Skip to content

Commit

Permalink
wip: Checker class
Browse files Browse the repository at this point in the history
  • Loading branch information
miggol committed Jul 23, 2024
1 parent 66c96d4 commit eee57be
Showing 1 changed file with 82 additions and 15 deletions.
97 changes: 82 additions & 15 deletions proposals/utils/stepper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
Expand All @@ -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
Expand Down

0 comments on commit eee57be

Please sign in to comment.