durusmail: quixote-users: keeping track of the modules/objects traversed
keeping track of the modules/objects traversed
2004-09-20
2004-09-20
2004-09-20
2004-09-20
2004-09-20
2004-09-21
2004-09-21
2004-09-20
keeping track of the modules/objects traversed
David M. Cooke
2004-09-20
On Sun, Sep 19, 2004 at 11:01:28PM -0600, Dustin Lee wrote:
> Sorry if this is an obvious question, but I'm fairly new to quixote
> and I'm stumped on this issue.
>
> I have a URL like the following:
>
> http://some.url.com/myapp.cgi/animal/x123/assessment/234/
>
> The basic idea is that for this animal inventory system there is an
> assessment which has an id of 234 for animal with an id of x123.  I
> have the keyword assessment in there because there are other entities
> associated with an animal id so I don't know what the object id
> following x123 refers to unless I also give the name.  The question is
> when I instantiate the assessment (234) is there an easy way to
> determine what the animal id was?  the object referred to by 234 is an
> AssessmentUI object in the assessment_ui module.

I've got something similiar. What I do is, instead of using modules,
is use objects. So my version would look like

[Note: I haven't tested this code...]
def _q_lookup(request, component):
    return AnimalUI(component)

class AnimalUI:
    _q_exports = ['assessment']
    def __init__(self, animal_id):
        self.animal_id = animal_id
        self.assessment = AssessmentHandler(animal_id)

class AssessmentHandler:
    _q_exports = []
    def __init__(self, animal_id):
        self.animal_id = animal_id
    def _q_lookup(self, request, component):
        return AssessmentUI(self.animal_id, component)

class AssessmentUI:
    _q_exports = []
    def __init__(self, animal_id, assessment_id):
        self.animal_id = animal_id
        self.assessment_id = assessment_id
    def _q_index(self, request):
        ... whatever ...

You can remove the need for a separate Handler class for each by
defining something like

class Handler:
    _q_exports = []
    def __init__(self, q_lookup_func, *args, **kw):
        self.q_lookup_func = q_lookup_func
        self.args = args
        self.kw = kw
    def _q_lookup(self, request, component):
        return self.q_lookup_func(component, *self.args, **self.kw)

then AnimalUI becomes
class AnimalUI:
    _q_exports = []
    def __init__(self, animal_id):
        self.animal_id = animal_id
        self.assessment = Handler(lookup_assessment, animal_id)

def lookup_assessment(component, animal_id):
    return AssessmentUI(animal_id, component)

Something to watch out for is the Handler __init__ methods should be
lightweight, as you're constructing a Handler object at each level its
needed, per request.

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke                      http://arbutus.physics.mcmaster.ca/dmc/
|cookedm@physics.mcmaster.ca

reply