1

Currently, I have the following:

class ConfirmStepAView(UpdateProcessView):
    form_class = ConfirmationForm

    def validate(self):
        if not self.process.A: 
            raise ValidationError

class ConfirmStepBView(UpdateProcessView):
    form_class = ConfirmationForm

    def validate(self):
        if not self.process.B: 
            raise ValidationError

# Usage: 
flow.View(ConfirmStepAView)
flow.View(ConfirmStepBView)

How can I write a factory method that generates these classes on the fly so I can do something like this?

flow.View(ConfirmStepViewFactory('A'))
flow.View(ConfirmStepViewFactory('B'))
flow.View(ConfirmStepViewFactory('Something'))
flow.View(ConfirmStepViewFactory('Else'))

Note: I have to use this format because I am working with a third part library.

1 Answer 1

1

This is a common pattern. You can utilise closures in python to dynamically create and return your class.

def make_ConfirmStepView(attribute):
    class ConfirmStepView(UpdateProcessView):
        form_class = ConfirmationForm

        def validate(self):
            if not getattr(self.process, attribute): 
                raise ValidationError

    return ConfirmStepView

flow.View(make_ConfirmStepView('A'))
flow.View(make_ConfirmStepView('B'))
flow.View(make_ConfirmStepView('Something'))
flow.View(make_ConfirmStepView('Else'))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.