def add_q_stats(uid, qid): answered = (session.query(AnswerModel).filter_by(userID=uid, questionID=qid)).one() recorded = (session.query(UserHistoryModel).filter_by(userid=uid, source_id=qid)).first() if (recorded != None): boole = recorded.qanswered thing = UserHistoryModel.get_user_latest_data(uid) # in case the question was immediately answered if (answered != None) and (recorded == None): thing = UserHistoryModel.get_user_latest_data(uid) thing.asked = thing.asked + 1 thing.answered = thing.answered + 1 thing.qanswered = True session.add(UserHistoryModel(thing.userid, thing.trust, thing.answered, thing.asked)) session.commit # in case the question was not answered at all elif (answered == None) and (recorded == None): thing = UserHistoryModel.get_user_latest_data(uid) thing.asked = thing.asked + 1 #thing.answered = thing.answered + 0 thing.qanswered = False session.add(UserHistoryModel(thing.userid, thing.trust, thing.answered, thing.asked)) session.commit # in case the question was previously asked, but only answered after unlocking it elif (answered != None) and (recorded != None) and not (boole): thing = UserHistoryModel.get_user_latest_data(uid) #thing.asked = thing.asked + 0 thing.answered = thing.answered + 1 thing.qanswered = True session.add(UserHistoryModel(thing.userid, thing.trust, thing.answered, thing.asked)) session.commit
def get_active_questions(userid, courseid): anssub = session.query(AnswerModel).filter(AnswerModel.userID == userid).\ subquery() # HACK: I can't figure out how to do timedelta stuff inside a filter, # so that is done after pulling all data... Slow! # Need to use the old Alias.c.[columname] when using subquery! tmp = session.query(Question).\ outerjoin(anssub, anssub.c.questionID == Question.id).\ filter(Question._answerable == True).\ filter(Question.course_id == courseid) #print tmp #print [(x.modified + timedelta(seconds=x.time), datetime.now()) for x in tmp] questions = [] for x in tmp: if x.time == 0: questions.append(x) elif x.activate_time + timedelta(seconds=x.time) > datetime.now(): questions.append(x) return questions
def get_list_asked(): """Retrieves questions asked by the user currently logged in.""" if g.lti.is_instructor(): # TODO: pagination, etc..... same goes for get_questions session.commit() return render_template('question_list.html', questions=session.query(Question).filter_by(user_id=g.lti.get_user_id() ) ) else: session.commit() return render_template('question_list.html', questions=session.query(Question).filter_by(user_id=g.lti.get_user_id() ) )
def get_filtered_offset(cls,limit,offset=0,orderby='id',**kws): count = session.query(cls).count() curpage = (offset / limit)+1 if curpage == 0: curpage = 1 maxpages = (count/limit) if count % limit == 0 else (count/limit)+1 startpage = (curpage/5)*5 if curpage > 5 else 1 pagecount = startpage + 5 if maxpages >= startpage+5 else maxpages if len(kws) > 0: return session.query(cls).filter_by(**kws).order_by(getattr(cls,orderby).desc()).offset(offset).limit(limit) return (session.query(cls).order_by(getattr(cls,orderby).desc()).offset(offset).limit(limit), curpage, maxpages, startpage, pagecount)
def save(questionID, userID, answerText): try: ranking = ((session.query(UserModel).filter_by(userid=userID).one().trust - 1000.0) / 4) + 1000.0 except exc.InvalidRequestError: ranking = 1000.0 session.add(AnswerModel(questionID=questionID, userID=userID, text=answerText, edit=0, ranking=ranking)) session.commit()
def add_list(list): for a_id, u_id in list: if session.query(Schedule).filter(Schedule.answer_id == a_id, Schedule.user_id == u_id).first() is None: session.add(Schedule(a_id, u_id)) session.commit()
def get_tag_ids(answerID): tlist = session.query(AnswerTag.tag_id).filter( AnswerTag.answer_id==answerID).all() map(list, tlist) endlist = [] for x in tlist: endlist.append(x[0]) return endlist
def test_4updateAnswer(self): AnswerModel.updateAnswer(1,'NEW') obj = None try: obj = session.query(AnswerModel).filter_by(id=1,text="NEW").one() except: pass self.assertTrue(obj is not None)
def test_3savereview(self): AnswerModel.savereview(999,9999,'TEST',1) obj = None try: obj = session.query(AnswerModel).filter_by(questionID=999,userID=9999).one() except: pass self.assertTrue(obj is not None)
def test_2save(self): AnswerModel.save(101,1234,'TEST') obj = None try: obj = session.query(AnswerModel).filter_by(questionID=101,userID=1234).one() except: pass self.assertTrue(obj is not None)
def add_tag(name): if session.query(Tag.name).filter(Tag.name == name).first() is None: tag = Tag(name) session.add(tag) session.commit() return tag.id else: return False
def get_answer(user_id): answer_id = session.query(Schedule.answer_id).filter( Schedule.user_id == user_id).first() if answer_id is not None: return AnswerModel.by_id(answer_id[0]) return None
def test_4remove_by_id(self): AnswerModel.remove_by_id(1) obj = None try: obj = session.query(AnswerModel).filter_by(id=1).one() except: pass self.assertTrue(obj is None)
def get_answered_active_questions(userid, courseid): """ Exactly the same as get_unanswered_questions except we want the answered ones """ anssub = session.query(AnswerModel).filter(AnswerModel.userID == userid).\ subquery() # Need to use the old Alias.c.[columname] when using subquery! tmp = session.query(Question).\ outerjoin(annsub, anssub.c.questionID == Question.id).\ filter(Question.available == True).\ filter(Question.course_id == courseid).\ filter(anssub.c.id != None).all() print tmp print [(x.modified + timedelta(seconds=x.time), datetime.now()) for x in tmp]
def check_answer_exists(uID, qID): if engine.dialect.has_table(engine.connect(), "answer"): try: answer = session.query(AnswerModel).filter_by(questionID=qID, userID=uID, edit=0).one() return 1 except exc.InvalidRequestError: return 0 else: return 0
def time_since_last(cls, user_id): created = session.query(cls.created).filter( cls.user_id == user_id).order_by(cls.id.desc()).first() if created is None: return None dt = datetime.now() - created[0] return dt.seconds + dt.days*86400
def __init__(self): # create a few database items for model in session.query(Question): break else: session.add(Question("1","What am I?",True)) session.add(Question("1","Who am I?",True)) session.add(Question("1","Where am I?",True)) session.commit()
def setTrust(uID, trust): # set current trust (in usermodel) user = session.query(UserModel).filter_by(userid=uID).one() user.trust = trust # fetch last known history record old = UserHistoryModel.get_user_latest_data(uID) # create a new trust entry in userhistory history = UserHistoryModel(uID, trust, old.answered, old.asked) session.add(history) session.commit()
def getAnswerID(uID, qID): if engine.dialect.has_table(engine.connect(), "answer"): try: answer = session.query(AnswerModel).filter_by( questionID=qID, userID=uID).one() return answer.id except exc.InvalidRequestError: return 0 else: return 1
def get_list_to_answer(): """Retrieves questions to be answered by the instructor (all questions )""" if g.lti.is_instructor(): # TODO: pagination, etc..... same goes for get_questions session.commit() return render_template('answer_student_questions.html', questions=session.query(Question).\ filter(Question.course_id == g.lti.get_course_id() ).\ filter(Question.course_id != g.lti.get_user_id() )) #Filter questions by instructor #Only instructors can answer these questions else: return render_template('access_restricted.html')
def get_filtered(cls, **kws): if len(kws) > 0: return session.query(cls).filter_by(**kws).all() return cls.get_all()
def by_ids(cls, ids): if not ids: return [] return session.query(cls).filter(cls.id.in_(ids)).all()
def by_id(cls, id): try: return session.query(cls).filter(cls.id == id).one() except: return None
def get_all(cls): return session.query(cls).filter().all()
def add_answerrating(aid, rid): if session.query(AnswerRating).filter( AnswerRating.answer_id==aid, AnswerRating.rating_id==rid).first() is None: session.add(AnswerRating(aid, rid))
def by_course_id(cls, course_id): return session.query(cls).filter(cls.course_id == course_id).all()
def update_answer(answerID, answerText): session.query(AnswerModel).filter_by(id=answerID).update( {"text": answerText}, synchronize_session=False)
def get_answers_ordered_by_rank(question_id): return session.query(AnswerModel).filter(AnswerModel.questionID==question_id).order_by(AnswerModel.ranking.desc())
def render(self): return render_template('index.html',questions=session.query(Question).filter().all())
def get_all(): return session.query(AnswerRating).filter().all()