hi antony,

thank you so much for your adviced, in my case (please see attached file),
would you prefer to put it on models or modules?
right now i put it on models, please give an advice or suggestion?

thank you very much in advance.

steve van christie


On Thu, Apr 14, 2011 at 7:17 AM, Anthony <[email protected]> wrote:

> If it's just a few (small) functions, go ahead and put them in model files,
> as reading a few extra function definitions will probably have a negligible
> impact on performance. In particular, if the functions are used in many/most
> requests, you would save little by moving them to modules because you would
> end up importing them on most requests anyway.
>
> On Wednesday, April 13, 2011 8:11:00 PM UTC-4, 黄祥 wrote:
>
>> hi,
>>
>> i try to follow the advise about functions from :
>> http://web2py.com/book/default/chapter/11#Efficiency-and-Scalability
>>
>> - Minimize the code in models: do not define functions there, define
>> functions in the controllers that need them or - even better - define
>> functions in modules, import them and use those functions as needed.
>> - Do not put many functions in the same controller but use many
>> controllers with few functions.
>>
>> i have a functions (e.g, add data, select data, update data) that been
>> used in many controller, i got an adviced to put my functions on the
>> models instead of on modules to make it simple, but the book said do
>> not define functions in models.
>> my intention is not to repeat the same code in many controller
>> (inefficiency) and make my code simple
>>
>> any suggestion or advice for this?
>>
>> thank you so much.
>>
>> steve van christie
>
>
def __index(table):
	menu = ('[ ', 
			A(T('Add'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'add'), 
			  _title = T('Add')), 
			' | ', 
			A(T('Search'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'search'), 
			  _title = T('Search')), 
			' ]')
	title = T('List')
	if len(request.args): 
		page = int(request.args[0])
	else: 
		page = 0
		items_per_page = 20
		limitby = (page * items_per_page, 
				  (page + 1) * items_per_page + 1)
		rows = db().select(table.ALL, 
						   limitby = limitby)
	return dict(menu = menu, 
				title = title, 
				rows = rows, 
				page = page, 
				items_per_page = items_per_page)

def __add(table):
	menu = ('[ ', 
			A(T('Index'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'index'), 
			  _title = T('Index')), 
			' | ', 
			A(T('Search'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'search'), 
			  _title = T('Search')), 
			' ]')
	title = T('Add')
	form = crud.create(table, 
					   next = URL(request.application, 
								  request.controller, 
								  'index'))
	return dict(menu = menu, 
				title = title, 
				form = form)

def __edit(table):
	menu = ('[ ', 
			A(T('Index'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'index'), 
			  _title = T('Index')), 
			' | ', 
			A(T('Add'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'add'), 
			  _title = T('Add')), 
			' | ', 
			A(T('Search'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'search'), 
			  _title = T('Search')),  
			' | ', 
			A(T('Show'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'show', 
						  args = request.args), 
			  _title = T('Show')), 
			' ]')
	title = T('Edit')
	page = table(request.args(0)) or redirect(URL(request.application, 
												  request.controller, 
												  'index'))
	form = crud.update(table, 
					   page, 
					   next = URL(request.application, 
								  request.controller, 
								  'show', 
								  args = request.args))
	return dict(menu = menu, 
				title = title, 
				form = form)

def __show(table):
	menu = ('[ ', 
			A(T('Index'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'index'), 
			  _title = T('Index')), 
			' | ', 
			A(T('Add'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'add'), 
			  _title = T('Add')), 
			' | ', 
			A(T('Search'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'search'), 
			  _title = T('Search')),  
			' | ', 
			A(T('Edit'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'edit', 
						  args = request.args), 
			  _title = T('Edit')), 
			' ]')
	page = table(request.args(0)) or redirect(URL(request.application, 
												  request.controller, 
												  'index'))
	return dict(menu = menu, 
				page = page)

def __show_review(table, table_review, field):
	menu = ('[ ', 
			A(T('Index'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'index'), 
			  _title = T('Index')), 
			' | ', 
			A(T('Add'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'add'), 
			  _title = T('Add')), 
			' | ', 
			A(T('Search'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'search'), 
			  _title = T('Search')),  
			' | ', 
			A(T('Edit'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'edit', 
						  args = request.args), 
			  _title = T('Edit')), 
			' ]')
	page = table(request.args(0)) or redirect(URL(request.application, 
												  request.controller, 
												  'index'))
	field.default = page.id
	form = crud.create(table_review,
					   message = T('Your Review is Posted'), 
					   next = URL(request.application, 
								  request.controller, 
								  request.function, 
								  args = page.id))
	reviews = db(field == page.id).select()
	return dict(menu = menu, 
				page = page, 
				reviews = reviews, 
				form = form)

def __search(table):
	menu = ('[ ', 
			A(T('Index'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'index'), 
			  _title = T('Index')), 
			' | ', 
			A(T('Add'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'add'), 
			  _title = T('Add')),
			' ] ')
	title = T('Search')
	search, rows = crud.search(table)
	return dict(menu = menu, 
				title = title, 
				table = table,
				search = search, 
				rows = rows)

def __manage(table):
	menu = ('[ ', 
			A(T('Index'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'index'), 
			  _title = T('Index')), 
			' | ', 
			A(T('Add'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'add'), 
			  _title = T('Add')), 
			' | ', 
			A(T('Search'), 
			  _href = URL(request.application, 
						  request.controller, 
						  'search'), 
			  _title = T('Search')),  
			' ] ')
	title = T('Manage')
	form = crud.update(table, 
					   request.args(1), 
					   message = T("Succeed"))
	table.id.represent = lambda id: A('Edit:', 
									  id, 
									  _href = URL(request.application, 
												  request.controller, 
												  request.function, 
												  args = (request.args(0), 
														  id)))
	search, rows = crud.search(table)
	return dict(menu = menu, 
				title = title, 
				form = form, 
				table = table,
				search = search, 
				rows = rows)

Reply via email to