Hi,

After learning Python and web2py deeper, I've restructured my web2py 
application so that all functions and data are now in classes with lazy 
methods in my own module. The lazy property class turns any method into a 
"cached attribute", thus I can make a db query that returns rows and if it 
is called again it uses the previous result.

What this has allowed me to do, is to clean practically all code in my 
controller as the logic and variables are in a class. Now I just pass the 
class to the view and the view prints the "attributes" directly without 
separate call and saving to dict. I've read that some don't like any logic 
in the view, but that's unavoidable as in the view you need to print stuff 
depending who the user is and is he logged in or not.

I'd like your opinions and comments on my strategy. Here are few simplified 
examples from my code:

Module:

class MyClass(object):
    def __init__(self):
        self.db = current.db

    @cached_property
    def all_participants(self):
        return self.db(self.db.participant.id>0).select()

class MyClassParticipant(MyClass):
     "Inherits base class"
    def __init__(self, auth_user_id=None):
        self.auth_user_id = auth_user_id

        super(MyClassParticipant, self).__init__()

    @cached_property
    def participant_stats(self):
        return 
self.db(self.db.participant.auth_user_id==self.auth_user_id).select()

Controller:

def index():
    if auth.user:
        xclassinstance = 
module.MyClassParticipant(auth_user_id=auth.user_id)
    else:
        xclassinstance = module.MyClass()

    return dict(xclassinstance=xclassinstance)

View:

{{if auth.user:}}
    Your ranking is {{=xclassinstance.participant_stats.ranking}}

Rankings of all participants
<br />
{{for participant in xclassinstance.all_participants:}}
    {{=participant.name}} ranking is {{=participant.ranking}}
    <br />

Ykä
    

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to