How to simplify controller 'return' line?

3 views
Skip to first unread message

Lingfeng Xiong

unread,
Oct 31, 2011, 4:43:03 AM10/31/11
to TurboGears
hi there,
I am building a website with a complex template, in the 'master.html'
which every template files are included, there are several variables:
[code]
<li class="xos"> <a id="xlf" class="xns" href="help/">Welcome back $
{username}</a> </li>
...
<li class="xbp" py:for="phone in phonelist">
...
[/code]
For show my templates correctly, I have to make a very complex
'return' stances for every exposed functions:
[code]
return dict(page = 'rename',
username = self.user.user_name,
phoneinfo = self.phoneInfo,
phonelist = PhoneInfo.GetPhoneList(self.user),
codeleft = GetCodeLeft(self.user),
phonename = PhoneInfo.GetPhoneName(user = self.user, imei =
self.phoneInfo.IMEI)
)
[/ciode]
In the example above, most of the parametered are common, such as
'username', 'codeleft' and 'phonelist'. Is there anyway for me to
shorten my return line? Thanks.

julien tayon

unread,
Nov 2, 2011, 5:59:05 AM11/2/11
to turbo...@googlegroups.com
Hello,

If something comes often, base of computer science is Dont Repeat Yourself :
have in your User model a method like : common_templating_info that
build part of this dict, and update it in the contoller before return.
btw GetCodeLeft, PhoneInfo and GetPhoneList are functions that takes
the user as an argument, it seems to me it would make more sense if
they were @property of the User class.
And regarding PEP8 CamelCase is mostly suited for class while
under_score notation is pretty much for the rest (attributes, method
and function names).

I spot you have some strong coupling in your code between PhoneInfo
and User that might makes it difficult to apply the aforementioned
solution, maintenance will be nightmarish, but this has nothing to do
with turbogears.

Have Fun and good day

2011/10/31 Lingfeng Xiong <xiongl...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups "TurboGears" group.
> To post to this group, send email to turbo...@googlegroups.com.
> To unsubscribe from this group, send email to turbogears+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/turbogears?hl=en.
>
>

Christoph Zwerschke

unread,
Nov 2, 2011, 3:40:13 PM11/2/11
to turbo...@googlegroups.com
Am 31.10.2011 09:43, schrieb Lingfeng Xiong:
> In the example above, most of the parametered are common, such as
> 'username', 'codeleft' and 'phonelist'. Is there anyway for me to
> shorten my return line? Thanks.

Why don't you make a method in your controller, like:

def common_info(self, page, **kw):
user = sefl.user
info = dict(page=page,
username = user.user_name,
phoneinfo = self.phoneInfo,
phonelist = PhoneInfo.GetPhoneList(user),
codeleft = GetCodeLeft(user),
phonename = PhoneInfo.GetPhoneName(
user=useruser, imei=self.phoneInfo.IMEI))
info.update(kw)
return info

And then in your exposed controller method, you can write:

...
return self.common_info('rename')

or

...
return self.common_info('otherpage', moreinfo='etc')

Also, you can use the add_global_tmpl_vars function in lib.helpers to
provide info that will be available in *all* templates globally.

-- Christoph

Sean DiZazzo

unread,
Nov 4, 2011, 12:23:13 PM11/4/11
to TurboGears
Similar to Christoph's solution; you could use a decorator...

def common_info(func):
def newfunc(*args, **kwargs):
kwargs["phonelist"] = "..."
kwargs["codeleft"] = "..."
return func(*args, **kwargs)
return newfunc

@common_info
def index(self):
...

~Sean

Lingfeng Xiong

unread,
Nov 6, 2011, 8:15:55 AM11/6/11
to turbo...@googlegroups.com, Christoph Zwerschke, julien tayon
Thanks all for help :-)
I believe
[quote]

Why don't you make a method in your controller, like:

def common_info(self, page, **kw):
user = sefl.user
info = dict(page=page,
username = user.user_name,
phoneinfo = self.phoneInfo,
phonelist = PhoneInfo.GetPhoneList(user),
codeleft = GetCodeLeft(user),
phonename = PhoneInfo.GetPhoneName(
user=useruser, imei=self.phoneInfo.IMEI))
info.update(kw)
return info

And then in your exposed controller method, you can write:

...
return self.common_info('rename')

or

...
return self.common_info('otherpage', moreinfo='etc')

[/quote]
is the most suitable way for me.
Thanks again for all your kindly help!

----------
Regards,
Lingfeng Xiong

“六岁至十二岁之学龄儿童,一律受基本教育,免纳学费。其贫苦者,由政府供给
书籍。已逾学龄未受基本教育之国民,一律受补习教育,免纳学费,其书籍亦由政
府供给。”
-- 《中华民国宪法》

Reply all
Reply to author
Forward
0 new messages