def find_message_in_db(self, cmd, channel, text): if not self.protocol.db: return with self.protocol.get_session() as session: row = (session.query(Message.message).filter_by( key=cmd, channel=channel).order_by(func.random()).first()) if row is None: return msg = row.message max_args = max( (int(m) + 1 if m.isnumeric() else 0 for m in (m.group(2) for m in re_db_args.finditer(msg))), default=0) args, kwargs = get_args_from_text(text, max_args) def replace_dollars(match): val = match.group(2) if val == '@': return match.group(1) + text return match.group(1) + args[int(val)] try: msg = re_db_args.sub(replace_dollars, msg) except IndexError: return return self.protocol.say(channel, msg)
def find_message_in_db(self, cmd, channel, text): if not self.protocol.db: return with self.protocol.get_session() as session: row = ( session .query(Message.message) .filter_by(key=cmd, channel=channel) .order_by(func.random()) .first() ) if row is None: return msg = row.message max_args = max( ( int(m) + 1 if m.isnumeric() else 0 for m in (m.group(2) for m in re_db_args.finditer(msg)) ), default=0 ) args, kwargs = get_args_from_text(text, max_args) def replace_dollars(match): val = match.group(2) if val == '@': return match.group(1) + text return match.group(1) + args[int(val)] try: msg = re_db_args.sub(replace_dollars, msg) except IndexError: return return self.protocol.say(channel, msg)
def find_message_in_db(self, cmd, channel): if not self.protocol.db: return print(cmd, channel) with self.protocol.get_session() as session: row = (session.query(Message.message).filter_by( key=cmd, channel=channel).order_by(func.random()).first()) if row is None: return return self.protocol.say(channel, row.message)
def testingAction(action): if action in ['begin']: session['testing'] = { 'tests': [ t.id for t in Test.query.order_by(func.random()).distinct( Test.id).limit(10).all() ], 'index': 0, 'answers': {}, 'correct': 0 } if action in ['submit']: if 'id' and 'option' in request.values: session['testing']['answers'].update( {request.values['id']: request.values['option']}) action = 'next' if action in ['prev', 'next']: index = session['testing']['index'] index = index + 1 if action == 'next' else index - 1 if index < 0: index = 0 session['testing'].update({'index': index}) if session['testing']['index'] > 9: return redirect('/testing/result') if action in ['result']: correct = 0 for id, value in session['testing']['answers'].iteritems(): test = Test.query.get(id) if test.correctanswer == int(value): correct += 1 session['testing'].update({'correct': correct}) if 'finished' not in session['testing']: stat = Stat(10, session['testing']['correct'], datetime.now()) db.session.add(stat) stat.student = current_user.student db.session.commit() session['testing'].update({'finished': True}) return redirect('/testing')
def get_quiz_questions(): previous_questions = request.json.get('previous_questions') quiz_category = request.json.get('quiz_category') if not quiz_category: return abort(400, 'Quiz Category missing in request payload') category_id = int(quiz_category.get('id')) questions = Question.query.filter( Question.category == category_id, ~Question.id.in_(previous_questions)) if category_id else \ Question.query.filter(~Question.id.in_(previous_questions)) question = questions.order_by(func.random()).first() if not question: return jsonify({}) return jsonify({'question': question.format()})
def find_message_in_db(self, cmd, channel): if not self.protocol.db: return print(cmd, channel) with self.protocol.get_session() as session: row = ( session .query(Message.message) .filter_by(key=cmd, channel=channel) .order_by(func.random()) .first() ) if row is None: return return self.protocol.say(channel, row.message)
def all(cls, _db, **kwargs): """Yield a sequence of CollectionCoverageProvider instances, one for every Collection that gets its licenses from cls.PROTOCOL. CollectionCoverageProviders will be yielded in a random order. :param kwargs: Keyword arguments passed into the constructor for CollectionCoverageProvider (or, more likely, one of its subclasses). """ if cls.PROTOCOL: collections = Collection.by_protocol(_db, cls.PROTOCOL) else: collections = _db.query(Collection) collections = collections.order_by(func.random()) for collection in collections: yield cls(collection, **kwargs)
def run(self): for edition in self._db.query(Edition).order_by(func.random()).limit( self.test_size): self.process_edition(edition) self.out.close()