Commit 4ea9806a764507246286a8f18f2f8cb2a7866bcb

Authored by Tássia Camões Araújo
1 parent 24c02613
Exists in master and in 1 other branch add_vagrant

Integration with real recommender code. Content-based strategy is the only one

enabled. Edited templates and forms.
src/web/server.py 100644 → 100755
@@ -3,15 +3,13 @@ @@ -3,15 +3,13 @@
3 import web 3 import web
4 from web import form 4 from web import form
5 import tempfile 5 import tempfile
  6 +import sys
6 7
7 -render = web.template.render('templates/', base='layout') 8 +sys.path.insert(0,"../")
8 9
9 -urls = ('/', 'Index',  
10 - '/feedback', 'FeedbackForm',  
11 - '/thanks', 'FeedbackThanks'  
12 - )  
13 -  
14 -web.webapi.internalerror = web.debugerror 10 +from config import *
  11 +from recommender import *
  12 +from user import *
15 13
16 def add_global_hook(): 14 def add_global_hook():
17 g = web.storage({"counter": "1"}) 15 g = web.storage({"counter": "1"})
@@ -20,29 +18,29 @@ def add_global_hook(): @@ -20,29 +18,29 @@ def add_global_hook():
20 return handler() 18 return handler()
21 return _wrapper 19 return _wrapper
22 20
23 -  
24 -if __name__ == "__main__":  
25 - app = web.application(urls, globals())  
26 - app.add_processor(add_global_hook())  
27 - app.run()  
28 -  
29 class Index: 21 class Index:
30 def GET(self): 22 def GET(self):
31 - action = "/feedback"  
32 - method = "post"  
33 - return render.index(action, method, self._form()) 23 + form_action = "/feedback"
  24 + form_method = "post"
  25 + form = self._form()
  26 + return render.index(form_action, form_method, form)
34 27
35 def _form(self): 28 def _form(self):
36 send_form = form.Form( 29 send_form = form.Form(
37 - form.File("myfile", description="Packages list"),  
38 - form.Dropdown('Limit results', [('5', '05'), ('10', '10'), ('20', '20')]),  
39 - form.Checkbox("strategy_1", value="True", checked=True, description="Strategy 1"),  
40 - form.Checkbox("strategy_2", value="True", checked=True, description="Strategy 2"),  
41 - form.Checkbox("strategy_3", value="True", checked=True, description="Strategy 3"),  
42 - form.Checkbox("strategy_4", value="True", checked=True, description="Strategy 4"),  
43 - form.Checkbox("strategy_5", value="True", checked=True, description="Strategy 5"),  
44 - form.Checkbox("strategy_6", value="True", checked=True, description="Strategy 6"),  
45 - 30 + form.File("pkgs_file", description="Packages list"),
  31 + form.Dropdown('limit', [('5', '05'), ('10', '10'), ('20', '20')],
  32 + description = "Limit"),
  33 + form.Checkbox("strategy_cb", value="True", checked=False,
  34 + description="Content-based"),
  35 + form.Checkbox("strategy_col", value="True", checked=False,
  36 + description="Collaborative"),
  37 + form.Checkbox("strategy_hybrid", value="True", checked=False,
  38 + description="Hybrid"),
  39 + form.Checkbox("strategy_hybrid_plus", value="True", checked=False,
  40 + description="Hybrid plus"),
  41 + validators = [form.Validator("You must select at least one strategy",
  42 + lambda i: i.strategy_cb | i.startegy_col |
  43 + i.strategy_hybrid | i.strategy_hybrid_plus)]
46 ) 44 )
47 return send_form() 45 return send_form()
48 46
@@ -50,32 +48,63 @@ class FeedbackForm: @@ -50,32 +48,63 @@ class FeedbackForm:
50 def POST(self): 48 def POST(self):
51 action = "/thanks" 49 action = "/thanks"
52 method = "post" 50 method = "post"
53 - outputdir = tempfile.mkdtemp() + '/'  
54 - x = web.input(myfile={})  
55 - f = open(outputdir + x['myfile'].filename, "wb")  
56 - content = x['myfile'].value  
57 - while 1:  
58 - chunk = x['myfile'].file.read(10000)  
59 - if not chunk:  
60 - break  
61 - f.write(chunk) 51 + outputdir = tempfile.mkdtemp(prefix='',dir='./submissions/')
  52 + user_id = outputdir.lstrip('./submissions/')
  53 + x = web.input(pkgs_file={})
  54 + f = open(outputdir + "/packages_list", "wb")
  55 + #content = x['pkgs_file'].value
  56 + self.pkgs_list = []
  57 + for line in x['pkgs_file'].file:
  58 + self.pkgs_list.append(line.split()[0])
  59 + f.write(line)
  60 + # while 1:
  61 + # chunk = x['pkgs_file'].file.read(10000)
  62 + # if not chunk:
  63 + # break
  64 + # f.write(chunk)
62 f.close() 65 f.close()
63 - return render.feedbackForm(action, method, self._recommends(), self._form())  
64 66
65 - def _recommends(self):  
66 - results = [['Strategy 1', 'gnome-subtitles', 'brasero', 'inkscape', 'kde'], ['Strategy 2', 'airstrike', 'gimp', 'gthumb', 'iceweasel']] 67 + strategies = []
  68 + if 'strategy_cb' in x: strategies.append("cta")
  69 + if 'strategy_col' in x: strategies.append("col")
  70 + if 'strategy_hybrid' in x: strategies.append("hybrid")
  71 + if 'strategy_hybrid+' in x: strategies.append("hybrid+")
  72 +
  73 + return render.feedbackForm(action, method,
  74 + self._recommends(user_id,strategies),
  75 + self._form())
  76 +
  77 + def _recommends(self,user_id,strategies):
  78 + user = User(dict.fromkeys(self.pkgs_list,1),user_id)
  79 + user.maximal_pkg_profile()
  80 + cfg = Config()
  81 + rec = Recommender(cfg)
  82 + results = dict()
  83 + for strategy in strategies:
  84 + eval("rec."+strategy+"(cfg)")
  85 + prediction = rec.get_recommendation(user).get_prediction()
  86 + results[rec.strategy.description] = [result[0] for result in prediction]
  87 + # Colaborative strategies can not go online yet
  88 + results['Hybrid+'] = []
  89 + results['Hybrid'] = []
  90 + results['Collaborative'] = []
67 return results 91 return results
68 92
69 def _form(self): 93 def _form(self):
70 send_form = form.Form( 94 send_form = form.Form(
71 - form.Radio('Strategy 1',[('1','1 '),('2','2 '),('3','3 '),('4','4 '),('5','5')]),  
72 - form.Radio('Strategy 2',[('1','1 '),('2','2 '),('3','3 '),('4','4 '),('5','5')]),  
73 - form.Radio('Strategy 3',[('1','1 '),('2','2 '),('3','3 '),('4','4 '),('5','5')]),  
74 - form.Radio('Strategy 4',[('1','1 '),('2','2 '),('3','3 '),('4','4 '),('5','5')]),  
75 - form.Radio('Strategy 5',[('1','1 '),('2','2 '),('3','3 '),('4','4 '),('5','5')]),  
76 - form.Radio('Strategy 6',[('1','1 '),('2','2 '),('3','3 '),('4','4 '),('5','5')]),  
77 - form.Dropdown('level', ["Newbie", "Intermediate", "Advanced", "Guru", "Chuck Norris"], description ='Expertise'),  
78 - form.Textarea('anything', rows="7", cols="60", description='Anything to share?'), 95 + form.Radio('Content-based',
  96 + [('1','1 '),('2','2 '),('3','3 '),('4','4 '),('5','5')]),
  97 + form.Radio('Collaborative',
  98 + [('1','1 '),('2','2 '),('3','3 '),('4','4 '),('5','5')]),
  99 + form.Radio('Hybrid',
  100 + [('1','1 '),('2','2 '),('3','3 '),('4','4 '),('5','5')]),
  101 + form.Radio('Hybrid+',
  102 + [('1','1 '),('2','2 '),('3','3 '),('4','4 '),('5','5')]),
  103 + form.Dropdown('expertise',
  104 + ["Newbie", "Intermediate", "Advanced", "Guru", "Chuck Norris"],
  105 + description ='Debian expertise'),
  106 + form.Textarea('comments', rows="7", cols="60",
  107 + description="Anything else to share?"),
79 108
80 ) 109 )
81 return send_form 110 return send_form
@@ -83,3 +112,18 @@ class FeedbackForm: @@ -83,3 +112,18 @@ class FeedbackForm:
83 class FeedbackThanks: 112 class FeedbackThanks:
84 def POST(self): 113 def POST(self):
85 return render.feedbackThanks() 114 return render.feedbackThanks()
  115 +
  116 +render = web.template.render('templates/', base='layout')
  117 +
  118 +urls = ('/', 'Index',
  119 + '/feedback', 'FeedbackForm',
  120 + '/thanks', 'FeedbackThanks'
  121 + )
  122 +
  123 +web.webapi.internalerror = web.debugerror
  124 +
  125 +if __name__ == "__main__":
  126 + app = web.application(urls, globals())
  127 + app.add_processor(add_global_hook())
  128 + app.run()
  129 +
src/web/templates/feedbackForm.html
@@ -6,31 +6,31 @@ $var mod = 'feedback'; @@ -6,31 +6,31 @@ $var mod = 'feedback';
6 6
7 7
8 <center> 8 <center>
9 - <h1>Results</h1> 9 + <h1>Recommendations</h1>
10 </center> 10 </center>
11 11
12 -<p>Results index:</p> 12 +<p>Results per strategy:</p>
13 13
14 <ul class="toc"> 14 <ul class="toc">
15 -$for list in recommends:  
16 - <li><a href="#$list[0]">$list[0]</a></li> 15 +$for strategy, result in recommends.items():
  16 + <li><a href="#$strategy">$strategy</a></li>
17 <li><a href="#feedback">Your feedback is appreciated!</a></li> 17 <li><a href="#feedback">Your feedback is appreciated!</a></li>
18 </ul> 18 </ul>
19 19
20 <form action="$action" method="$method"> 20 <form action="$action" method="$method">
21 -$for list in recommends:  
22 - <h2><a name="$list[0]" id="$list[0]">$list[0]</a></h2>  
23 - $for p in list[1:]: 21 +$for strategy, result in recommends.items():
  22 + <h2><a name="$strategy" id="$strategy">$strategy</a></h2>
  23 + $for pkg in result:
24 <div class="line"> 24 <div class="line">
25 <div class="item col50"> 25 <div class="item col50">
26 - <image src="http://screenshots.debian.net/screenshot/$p" width="300"> 26 + <image src="http://screenshots.debian.net/screenshot/$pkg" width="300">
27 </div> 27 </div>
28 <div class="item col50 lastcol"> 28 <div class="item col50 lastcol">
29 - <script>loadPackage("$p");</script>  
30 - <b id="pack-$p"></b><div id="desc-$p"> </div> 29 + <script>loadPackage("$pkg");</script>
  30 + <b id="pack-$pkg"></b><div id="desc-$pkg"> </div>
31 <ul> 31 <ul>
32 - <li id="maint-$p"></li>  
33 - <li id="homepage-$p"></li> 32 + <li id="maint-$pkg"></li>
  33 + <li id="homepage-$pkg"></li>
34 </ul> 34 </ul>
35 </div> 35 </div>
36 <div class="clear"></div> 36 <div class="clear"></div>