-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgi_script.py
executable file
·51 lines (40 loc) · 1.36 KB
/
cgi_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
from src.computers.computers import ComputersList
import cgi
import json
def generateHeaders(headers):
head = '<thead class="thead-inverse"><tr>'
for header in headers:
head += '<th>' + header + '</th>'
head += '</tr></thead>'
return head
def generateRows(computers):
body = '<tbody>'
for i in range(len(computers)):
row = '<tr>'
row += '<th scope="row">' + str(i+1) + '</th>'
row += '<td>' + str(computers[i].name) + '</td>'
row += '<td>' + '$' + str(computers[i].price) + '</td>'
row += '<td>' + str(computers[i].calculated_rate) + '</td>'
row += '</tr>'
body += row
body += '</tbody>'
return body
def generateTable(headers, computers):
return '<table class="table table-hover table-lg">' + generateHeaders(headers) + generateRows(computers) + '</table>'
def generateOutput(headers, computers):
return '<html><body>'+generateTable(headers, computers)+'</body></html>'
computers = ComputersList()
form = cgi.FieldStorage()
price = float(form.getvalue('price'))
screen_size = form.getvalue('screen_size')
if screen_size.startswith('17'):
screen_size = 17
elif screen_size.startswith('15'):
screen_size = 15
else:
screen_size = 14
best = computers.get_best(price, 10, screen_size)
headers = ['#', 'model', 'price', 'rate']
print("Content-type: text/html\n\n");
print(generateOutput(headers, best))