def undo(self, subject): session = Session() fact = Fact.query.filter_by(subject=subject).first() if fact: if fact.previous_meaning: meaning = fact.meaning fact.meaning = fact.previous_meaning fact.previous_meaning = meaning session.commit()
def activate(self): if not self.to_be_indexed: return log.info("Indexing the quotes database for the first time") session = Session() writer = self.ix.writer() for quote in session.query(Quote).all(): self.index_quote(quote, writer, commit=False) writer.commit()
def apprendi(self, event, subject, meaning): session = Session() fact = Fact.query.filter_by(subject=subject).first() if fact: fact.previous_meaning = fact.meaning fact.meaning = meaning else: fact = Fact(subject, meaning) session.add(fact) session.commit()
def on_cmd_addquote(self, event): if not event.text: event.reply(u"NO!") return quote = Quote(quote=event.text, author=event.user.nickname) session = Session() session.add(quote) session.commit() self.index_quote(quote) event.reply(u"Ho aggiunto la %d" % quote.id)
def on_cmd_addquote(self, event): if not event.text: event.reply(u"Errore! Te pare zi'?") return quote = Quote(quote=event.text, author=event.user.nickname) session = Session() session.add(quote) session.commit() self.xapian_add_quote(quote) event.reply(u"%s: Ho stipato la %i!" % (event.user.nickname, quote.id))
def on_cmd_search(self, event): limit = 5 if not event.text: event.reply(u"Eh sì, stocazzo") return match = r_search_page.match(event.text) if match: page = int(match.group(1)) else: page = 1 session = Session() results_id = [] with self.ix.searcher() as searcher: qp = QueryParser("quote", self.ix.schema) query = qp.parse(event.text) # results = searcher.search(query, limit=limit) try: results = searcher.search_page(query, page, pagelen=limit) except ValueError: event.reply(u"La tua ricerca mi ha inibito il cervello") return found = results.scored_length() if not found: event.reply(u"Non ho trovato un cazzo!") return event.reply( u"Risultati pagina %d di %d, risultati %d-%d di %d" % ( results.pagenum, results.pagecount, results.offset + 1, results.offset + results.pagelen + 1, len(results), ) ) for result in results: results_id.append(result["id"]) for quote in session.query(Quote).filter(Quote.id.in_(results_id)).all(): event.reply(u"(%i) %s" % (quote.id, quote.quote))