def POST(self): web_input = web.input(pkgs_file={}) if 'user_id' in web_input: user_id = web_input['user_id'].encode('utf8') user_dir = os.path.join(self.submissions_dir, user_id) logging.info("New recommendation for user %s" % user_id) uploaded_file = os.path.join(user_dir, "uploaded_file") with open(uploaded_file) as uploaded: if uploaded.readline().startswith('POPULARITY-CONTEST'): user = PopconSystem(uploaded_file, user_id) else: user = PkgsListSystem(uploaded_file, user_id) user.maximal_pkg_profile() if len(user.pkg_profile) < 10: error_msg = "Could not extract profile from uploaded file. It must have at least 10 applications." # noqa logging.critical(error_msg) return render.error([error_msg], "/survey/", "START") else: # Check the remaining strategies and select a new one old_strategies = [ dirs for root, dirs, files in os.walk( os.path.join(self.submissions_dir, user_id)) ] if old_strategies: strategies = [ s for s in self.strategies if s not in old_strategies[0] ] logging.info("Already used strategies %s" % old_strategies[0]) else: strategies = self.strategies if not strategies: return render.thanks(user_id) selected_strategy = random.choice(strategies) logging.info("Selected \'%s\' from %s" % (selected_strategy, strategies)) self.set_rec_strategy(selected_strategy) prediction = self.rec.get_recommendation(user, 10).get_prediction() logging.info("Prediction for user %s" % user_id) logging.info(str(prediction)) self.save_prediction(user_id, selected_strategy, prediction) # Load packages details recommendation = [result[0] for result in prediction] pkgs_details = [] for pkg_name in recommendation: logging.info("Getting details of package %s" % pkg_name) pkg = DebianPackage(pkg_name) pkg.load_details() pkgs_details.append(pkg) if pkgs_details: logging.info("Rendering survey slide...") return render.survey(pkgs_details, user_id, selected_strategy, len(strategies)) else: return render.error( ["No recommendation produced for the uploaded file."], "/survey/", "START")
def POST(self): web_input = web.input(pkgs_file={}) user_dir = tempfile.mkdtemp(prefix='', dir=self.requests_dir) user_id = user_dir.split("/")[-1] uploaded_file = os.path.join(user_dir, "uploaded_file") if web_input['pkgs_file'].value: lines = web_input['pkgs_file'].file.readlines() with open(uploaded_file, "w") as uploaded: uploaded.writelines(lines) with open(uploaded_file) as uploaded: if uploaded.readline().startswith('POPULARITY-CONTEST'): user = PopconSystem(uploaded_file, user_id) else: user = PkgsListSystem(uploaded_file, user_id) if len(user.pkg_profile) < 10: return render.error( [ "Could not extract profile from uploaded file. It must have at least 10 applications." ], # noqa "/", "RECOMMENDATION") else: self.rec.set_strategy("knn_eset") user.maximal_pkg_profile() prediction = self.rec.get_recommendation(user, 12).get_prediction() logging.info("Prediction for user %s" % user.user_id) logging.info(str(prediction)) recommendation = [result[0] for result in prediction] pkgs_details = [] for pkg_name in recommendation: logging.info("Getting details of package %s" % pkg_name) pkg = DebianPackage(pkg_name) pkg.load_summary() pkgs_details.append(pkg) if pkgs_details: logging.info("Rendering recommendation...") return render.apprec(pkgs_details) else: return render.error( ["No recommendation produced for the uploaded file."], "/", "RECOMMENDATION") # noqa
def GET(self, pkg_name): pkg = DebianPackage(pkg_name) pkg.load_details() return render_plain.package(pkg)