class QuestionSearchProvider(SearchProvider): name = 'question' index = 'portal' _query = Question.query.options(db.eagerload(Question.author)) def _prepare(self, question): return { 'id': question.id, 'text': question.text, 'date': question.date_created.date(), 'author': question.author.username, 'title': question.title, 'link': href(question), 'tag': question.tags, } def prepare(self, ids): query = self._query.filter(Question.id.in_(ids)) documents = {p.id: self._prepare(p) for p in query} for id, document in documents.iteritems(): yield document def prepare_all(self): for question in db.select_blocks(self._query, Question.id): yield question.id, self._prepare(question)
class NewsSearchProvider(SearchProvider): name = 'news' index = 'portal' _query = Article.query.options(db.eagerload(Article.author)) def _prepare(self, article): return { 'id': article.id, 'title': article.title, 'text': article.intro + article.text, 'date': article.pub_date.date(), 'author': article.author.username, 'link': href(article), 'tag': article.tags, } def prepare(self, ids): query = self._query.filter(Article.id.in_(ids)) documents = {p.id: self._prepare(p) for p in query} for id, document in documents.iteritems(): yield document def prepare_all(self): for article in db.select_blocks(self._query, Article.id): yield article.id, self._prepare(article)
def article_feed(self, request, slug=None): """ Shows all news entries that match the given criteria in an atom feed. """ query = Article.query title = u"News" if slug: # filter the articles matching a defined tag tag = Tag.query.public().filter_by(slug=slug).one() query = tag.articles title = _(u"News for %s" % slug) query = query.options(db.eagerload("author")).order_by(Article.updated.desc()).limit(20) feed = AtomFeed( _(u"%s – %s" % (title, ctx.cfg["website_title"])), feed_url=request.url, url=request.url_root, icon=href("static", file="img/favicon.ico"), ) for article in query.all(): value = u"%s\n%s" % ( article.get_rendered_text(article.intro, request, "html"), article.get_rendered_text(article.text, request, "html"), ) feed.add( article.title, value, content_type="html", url=href(article, _external=True), author={"name": article.author.display_name, "uri": href(article.author.profile)}, id=article.guid, updated=article.updated, published=article.pub_date, ) return feed
def article_feed(self, request, slug=None): """ Shows all news entries that match the given criteria in an atom feed. """ query = Article.query title = u'News' if slug: # filter the articles matching a defined tag tag = Tag.query.public().filter_by(slug=slug).one() query = tag.articles title = _(u'News for %s' % slug) query = query.options(db.eagerload('author')) \ .order_by(Article.updated.desc()).limit(20) feed = AtomFeed(_(u'%s – %s' % (title, ctx.cfg['website_title'])), feed_url=request.url, url=request.url_root, icon=href('static', file='img/favicon.ico')) for article in query.all(): value = u'%s\n%s' % ( article.get_rendered_text(article.intro, request, 'html'), article.get_rendered_text(article.text, request, 'html')) feed.add(article.title, value, content_type='html', url=href(article, _external=True), author={ 'name': article.author.display_name, 'uri': href(article.author.profile) }, id=article.guid, updated=article.updated, published=article.pub_date) return feed
class AnswerSearchProvider(SearchProvider): name = 'answer' index = 'portal' _query = Answer.query.options(db.eagerload(Answer.question, Answer.author)) def _prepare(self, answer): return { 'id': answer.id, 'text': answer.text, 'date': answer.date_created.date(), 'title': answer.question.title, 'author': answer.author.username, 'link': href(answer), } def prepare(self, ids): query = self._query.filter(Answer.entry_id.in_(ids)) documents = {p.id: self._prepare(p) for p in query} for id, document in documents.iteritems(): yield document def prepare_all(self): for answer in db.select_blocks(self._query, Answer.id): yield answer.id, self._prepare(answer)
def create_forum_test_data(): global _link_file from inyoka.forum.models import Tag, Forum, Question, Answer, Vote, ForumEntry from inyoka.core.auth.models import User links = [] u1 = User.query.filter_by(username=u'dummuser').first() u2 = User.query.filter_by(username=u'quaki').first() # tags gnome = Tag(name=u'GNOME') gtk = Tag(name=u'GTK') kde = Tag(name=u'KDE') qt = Tag(name=u'QT') window_manager = Tag(name=u'Window-Manager') hardware = Tag(name=u'Hardware') inyoka = Tag(name=u'Inyoka') audio = Tag(name=u'Audio') db.session.commit() main_tags = [gnome, gtk, kde, qt, window_manager, hardware, inyoka, audio] # forums inyoka_forum = Forum( name=u'Inyoka Project', description=u'Please tell us your opinion about the new Inyoka!', tags=[inyoka]) gnome_forum = Forum( name=u'The GNOME Desktop (Ubuntu)', description=u'Here you can find all GNOME and GTK related questions.', tags=[gnome, gtk]) kde_forum = Forum( name=u'KDE Plasma (Kubuntu)', description=u'Everything about KDE, the desktop environment of Kubuntu.', tags=[kde, qt]) window_manager_forum = Forum( name=u'Desktop Environments and Window Managers', description=u'Aks everything about GNOME, KDE or any other exotic window manager here', subforums=[gnome_forum, kde_forum], tags=[window_manager]) hardware_forum = Forum( name=u'Hardware Problems', description=u'Describe your hardware problems here', tags=[hardware]) db.session.commit() tags = Tag.query.public().all() users = tuple(User.query.options(db.eagerload('groups')).all()) last_date = None questions = [] num, var = {'small': (50, 10), 'medium': (250, 50), 'large': (1000, 200)}[SIZE] for x in xrange(randrange(num - var, num + var)): if random() >= 0.8: # we use them a bit more than others, to get a more realistic # tag usage. these_tags = main_tags else: these_tags = list(tags) shuffle(these_tags) question = Question(title=generate_lorem_ipsum(1, False, 3, 9), text=chomsky(randint(0, 10) or 40), author=choice(users), date_created=get_date(last_date), tags=these_tags[:randrange(1, 6)]) last_date = question.date_created questions.append(question) db.session.commit() links.extend([href(q, _external=True) for q in questions]) # answers replies = {'small': 4, 'medium': 8, 'large': 12}[SIZE] answers = [] last_date = questions[-1].date_created shuffle(questions) for question in questions[:randrange(len(questions))]: for x in xrange(randrange(2, replies)): answer = Answer(question=question, author=choice(users), text=chomsky(randint(0, 10) or 40), date_created=get_date(last_date)) answers.append(answer) last_date = answer.date_created db.session.commit() voted_map = [] objects = answers + questions for obj in objects[:randrange(len(objects))]: for x in xrange(randrange(2, replies * 4)): entry = choice(objects) user = choice(users) if (user.id, entry.entry_id) not in voted_map: if random() >= 0.2: v = Vote(score=+1, user=user) elif random() >= 0.5: v = Vote(score=-1, user=user) else: break v.entry_id = entry.entry_id v.favorite = random() > 0.9 entry.votes.append(v) voted_map.append((user.id, entry.entry_id)) db.session.commit() _link_file.write(u'\n'.join(links))
def create_forum_test_data(): global _link_file from inyoka.forum.models import Tag, Forum, Question, Answer, Vote, ForumEntry from inyoka.core.auth.models import User links = [] u1 = User.query.filter_by(username=u'dummuser').first() u2 = User.query.filter_by(username=u'quaki').first() # tags gnome = Tag(name=u'GNOME') gtk = Tag(name=u'GTK') kde = Tag(name=u'KDE') qt = Tag(name=u'QT') window_manager = Tag(name=u'Window-Manager') hardware = Tag(name=u'Hardware') inyoka = Tag(name=u'Inyoka') audio = Tag(name=u'Audio') db.session.commit() main_tags = [gnome, gtk, kde, qt, window_manager, hardware, inyoka, audio] # forums inyoka_forum = Forum( name=u'Inyoka Project', description=u'Please tell us your opinion about the new Inyoka!', tags=[inyoka]) gnome_forum = Forum( name=u'The GNOME Desktop (Ubuntu)', description=u'Here you can find all GNOME and GTK related questions.', tags=[gnome, gtk]) kde_forum = Forum( name=u'KDE Plasma (Kubuntu)', description= u'Everything about KDE, the desktop environment of Kubuntu.', tags=[kde, qt]) window_manager_forum = Forum( name=u'Desktop Environments and Window Managers', description= u'Aks everything about GNOME, KDE or any other exotic window manager here', subforums=[gnome_forum, kde_forum], tags=[window_manager]) hardware_forum = Forum(name=u'Hardware Problems', description=u'Describe your hardware problems here', tags=[hardware]) db.session.commit() tags = Tag.query.public().all() users = tuple(User.query.options(db.eagerload('groups')).all()) last_date = None questions = [] num, var = { 'small': (50, 10), 'medium': (250, 50), 'large': (1000, 200) }[SIZE] for x in xrange(randrange(num - var, num + var)): if random() >= 0.8: # we use them a bit more than others, to get a more realistic # tag usage. these_tags = main_tags else: these_tags = list(tags) shuffle(these_tags) question = Question(title=generate_lorem_ipsum(1, False, 3, 9), text=chomsky(randint(0, 10) or 40), author=choice(users), date_created=get_date(last_date), tags=these_tags[:randrange(1, 6)]) last_date = question.date_created questions.append(question) db.session.commit() links.extend([href(q, _external=True) for q in questions]) # answers replies = {'small': 4, 'medium': 8, 'large': 12}[SIZE] answers = [] last_date = questions[-1].date_created shuffle(questions) for question in questions[:randrange(len(questions))]: for x in xrange(randrange(2, replies)): answer = Answer(question=question, author=choice(users), text=chomsky(randint(0, 10) or 40), date_created=get_date(last_date)) answers.append(answer) last_date = answer.date_created db.session.commit() voted_map = [] objects = answers + questions for obj in objects[:randrange(len(objects))]: for x in xrange(randrange(2, replies * 4)): entry = choice(objects) user = choice(users) if (user.id, entry.entry_id) not in voted_map: if random() >= 0.2: v = Vote(score=+1, user=user) elif random() >= 0.5: v = Vote(score=-1, user=user) else: break v.entry_id = entry.entry_id v.favorite = random() > 0.9 entry.votes.append(v) voted_map.append((user.id, entry.entry_id)) db.session.commit() _link_file.write(u'\n'.join(links))