Above statement was working on web2py 2.9.11 and before because of behavioral bug in web2py, which got fixed in web2py 2.9.11. Now
response.json() sets
content-type=application/json . So because of above line, on web2py >2.9.11 html code is displayed in browser instead of rendering it as html.

Correct way:

First convert python variable to json in controller and then pass it to view. And in view, use XML() to assign it to javascript variable so that it is not escaped

For all the web2py applications, web2py admin url is same, i. e. www.example.com/admin or 127.0.0.1:8000/admin. One should prevent access to admin interface to public or external users. Admin url should be changed from /admin to /some-other-url which is difficult to guess. admin is just another app like welcome or example app, so changing appliacation name will change url to it.

For example to change
admin to
w2p-adm2in do following steps

1. Rename admin folder with w2p-adm2in
On linux

1

2

cd web2py/applications

mv admin w2p-adm2in

On Windows

1

2

cd web2py/applications

rename admin w2p-adm2in

If you are in local environment, you can also do it using your file explorer

2. Fix broken links
Due to changes done in Step 1, links to admin app are broken. Links to admin app needs to be replaced with links to w2p-adm2in.

In app_name/controllers/appadmin.py
Update response.menu, change it to following

3. Instead of admin app, check user is logged in to w2p-adm2in app
while accessing appadmin, by default controller app_name/controllers/appadmin.py checks whether user is logged in to admin app or not, if not then it redirects to admin app(admin app login page), now it should check whether user is logged in to w2p-adm2in app and if not it should redirect to w2p-adm2in. To acheive this, pass
other_application='w2p-adm2in' as parameter to method
gluon.fileutils.check_credentials()

After step 2 and step 3, diff file of app_name/controllers/appadmin.py will look like following

diff

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

if not (gluon.fileutils.check_credentials(request) or auth.has_membership(manager_role)):

raise HTTP(403, "Not authorized")

menu = False

-elif (request.application == 'admin' and not session.authorized) or \

4. Change links to error page (tickets)
By default links to error is /admin/default/ticket/[ticket_id], Now it should be /w2p-adm2in/default/ticket/[ticket_id]
So add/replace/append following lines to web2py/routes.py

In this article, I will explain built in validator
IS_IMAGE and
IS_UPLOAD_FILENAME, also a custom validator.
It is important to validate file uploaded on server to restrict malicious files like .py .php, .exe bat etc.
Web2py provides 2 validators to validate files,
IS_IMAGE and
IS_UPLOAD_FILENAME .

IS_IMAGE

It checks whether uploaded file is of type image and file is in any of supported image formats

Following statement Checks if uploaded file is either png or bmp

Python

1

requires=IS_IMAGE(extensions=('png','bmp'))

2. IS_UPLOAD_FILENAME

It checks whether uploaded file meets the given rules for filename and extension.

You can provide regular expressions in filename and extension parameters.
If filename or extension doesn’t match the expression/criteria then it shows error in form.
You can provide your own error message using argument error_message

Example:

If you want to allow all files starting with word ‘log’ and having extension “txt”:

Python

1

requires=IS_UPLOAD_FILENAME(filename='^log',extension='txt')

Like IS_IMAGE validator we can not pass list of valid extensions to IS_UPLOAD_FILENAME validator.
But we can achieve this using regular expression or by writing our own validator

Using regular expression:

Python

1

requires=IS_UPLOAD_FILENAME(extension='^(pdf|docx|doc|xls)$')

The above expression will allow all the files having extension pdf, docx, doc or xls.

Suppose we have employee table with resume as uplaod field. And we want to download file on the basis of employee id.
Download url is like
app_name/custom_download/employee_id
or
<ahref="{{=URL('default', 'custom_download', args = employee_id)}}"download>

example: www.example.com/default/custom_download/1

Consider following employee schema:

1

2

3

4

5

db.define_table('employee',

Field('name','string',required=True),

Field('email',string,required=True),

Field('address','text',required=True),

Field('resume','upload',required=True))

Then custom download controller will be:

1

2

3

4

5

6

7

8

9

10

11

12

def custom_download():

employee_id=request.args(0)

emp_record=db(db.employee.id==employee_id).select().first()

download_file=emp_record.resume

# Name of file is table_name.field.XXXXX.ext, so retrieve original file name

Now open application from browser and open page you want to profile. Then stop web2py server. To view result run command ‘mprof plot’. But to plot result you need package ‘matplotlib’.
To install matplotlib, first install required packages for matplotlib:

1

sudo apt-get build-dep python-matplotlib

Now install matplotlib:

1

sudo pip install matplotlib

Now run following command to get memory profile graph (Memory Usage vs Time):

1

mprof plot

The available commands for mprof :

mprof run: running an executable, recording memory usage

mprof plot: plotting one the recorded memory usage (by default, the last one)

Other method to read .profile file is using command “cprofilev -f test_func.profile”

1

cprofilev-ftest_func.profile

Run above command in terminal and view stats in browser on http://127.0.0.1:4000

Advantage of this method is you can easily sort records ,just by one click on column name.

4. Profile web2py application

Create a empty text file in a directory and then pass this text file as argument to web2py. All profiling data will be stored in this file.
For example :
Create lcm.txt in ‘/home/gaurav/temp/lcm/” directory. Now run web2py server using following command with command line argument -F (profiler filename)

1

python web2py.py-p8000-ag0987-F/home/gaurav/temp/lcm/lcm.txt

Note: For some web2py version , we have to provide directory using -f (not -F) option and data will be written in that directory in the form of .profile files. We can read these files using above mentioned methods.

Now open application from browser and open pages you want to profile.Then stop web2py server. This will write profiling data to lcm.txt. Data in text file will be in readable format , no need of pstats module to read data.

The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceivers form the areas called cells (this term gave the name to the cellular phone) and every phone connects to the BTS with the strongest signal (in a little simplified view). Of course, BTSes need some attention and technicians need to check their function periodically.

ACM technicians faced a very interesting problem recently. Given a set of BTSes to visit, they needed to find the shortest path to visit all of the given points and return back to the central company building. Programmers have spent several months studying this problem but with no results. They were unable to find the solution fast enough. After a long time, one of the programmers found this problem in a conference article. Unfortunately, he found that the problem is so called “Travelling Salesman Problem” and it is very hard to solve. If we have N BTSes to be visited, we can visit them in any order, giving us N! possibilities to examine. The function expressing that number is called factorial and can be computed as a product 1.2.3.4….N. The number is very high even for a relatively small N.

The programmers understood they had no chance to solve the problem. But because they have already received the research grant from the government, they needed to continue with their studies and produce at least some results. So they started to study behaviour of the factorial function.

For example, they defined the function Z. For any positive integer N, Z(N) is the number of zeros at the end of the decimal form of number N!. They noticed that this function never decreases. If we have two numbersN1<N2, then Z(N1) <= Z(N2). It is because we can never “lose” any trailing zero by multiplying by any positive number. We can only get new and new zeros. The function Z is very interesting, so we need a computer program that can determine its value efficiently.

Input

There is a single positive integer T on the first line of input (equal to about 100000). It stands for the number of numbers to follow. Then there are T lines, each containing exactly one positive integer number N, 1 <= N<= 1000000000.

Output

For every number N, output a single line containing the single non-negative integer Z(N).