def recompute_top_choices(self): for suggestable in Session.query(model.Suggestable): debug_print( suggestable ) suggestable.top_choices = [] Session.add(suggestable) suggestions = Session.query(model.Suggestion).filter( or_( model.Suggestion.high_choice_id==suggestable.id, model.Suggestion.low_choice_id==suggestable.id ) ) recomendations = defaultdict(lambda:0) for suggestion in suggestions: high = suggestion.high_choice low = suggestion.low_choice rec = high if high.id != suggestable.id else low if rec.category.name != 'team': continue weight = rec.weight or 1 recomendations[rec.id] += suggestion.weight/weight rec_list = [ (x,y) for x,y in recomendations.items()] rec_list.sort(key=lambda x:-x[1]) for rec in rec_list[:5]: top_choice = model.TopChoice() top_choice.chooser = suggestable top_choice.choice_id = rec[0] top_choice.weight = rec[1] Session.add(top_choice) Session.commit()
def dict_ajax(self): selected = request.POST.getone("selected") deselected = request.POST.getone("deselected") sid_hash = request.POST.getone("sid_hash") if selected: like = model.Like(sid_hash, selected) Session.save(like) Session.commit() if deselected: unliked = Session.query(model.Like).filter_by(sid_hash=sid_hash, item=deselected).first() print unliked Session.delete(unliked) Session.commit() all_likes = [] for row in Session.query(model.Like.item).filter_by(sid_hash=sid_hash): all_likes.append(row[0]) print all_likes suggestions = [] c.recs = [] if len(all_likes) > 0: for suggestion in g.rec.suggest(all_likes): if suggestion[0] in g.teams: suggestions.append(suggestion[0]) c.recs = suggestions[:3] return render("/xml_recs.mak")
def modify_by_sid(self, sid_hash, item, weight): results = Session.query(model.Interaction).filter_by(sid_hash=sid_hash) if (results.count() != 0): interaction = results.first() else: interaction = model.Interaction(sid_hash) choices = map(lambda x: x.id, interaction.choices) if interaction.choices: self.add(item,choices,weight) item_suggestable = Session.query(model.Suggestable).filter_by(id=item).first() if item_suggestable and weight > 0: interaction.choices.append(item_suggestable) elif weight < 0: interaction.choices.remove(item_suggestable) Session.add(interaction) Session.commit()
def add(self,item,relations,weight=1): debug_print("Adding %s to %s" % (", ".join(map(str,relations)),item)) result = Session.query(model.Suggestable).filter_by(id=item) if (result.count() > 0): item_suggestable = result.first() else: return item_suggestable.weight += weight Session.add(item_suggestable) for relation in relations: if (relation == item): continue result = Session.query(model.Suggestable).filter_by(id=relation) relation_suggestable = result.first() if not relation_suggestable: continue results = Session.query(model.Suggestion).filter( or_( and_( model.Suggestion.high_choice_id == relation_suggestable.id, model.Suggestion.low_choice_id == item_suggestable.id ), and_( model.Suggestion.high_choice_id == item_suggestable.id, model.Suggestion.low_choice_id == relation_suggestable.id ) ) ) if (results.count() > 0): suggestion = results.first() suggestion.weight += weight if suggestion.weight <= 0: Session.delete(suggestion) else: Session.add(suggestion) else: suggestion = model.Suggestion(item_suggestable,relation_suggestable) Session.add(suggestion) Session.commit()