def render(self): if self.debug: return render_template('index_debug.html', lti_dump=g.lti.dump_all()) if g.lti.is_instructor(): return render_template('index_instructor.html') return render_template('index_student.html')
def answerQuestion(self, uID, qID, qText, timerD, questionStartTime): if answer.AnswerModel.check_answer_exists(uID, qID): aID = answer.AnswerModel.get_answer_id(uID, qID) if self.timeLeft(timerD, questionStartTime): return render_template('answer.html', questionID=qID, userID=uID, questionText=qText, timerDuration=timerD, date=time.mktime(questionStartTime.timetuple()), go="true") else: return render_template('answer.html', questionID=qID, userID=uID, questionText=qText, timerDuration=timerD, date=time.mktime(questionStartTime.timetuple()), go="false") else: #answer.AnswerModel.save(qID, uID, "") return render_template('answer.html', questionID=qID, userID=uID, questionText=qText, timerDuration=timerD, date=time.mktime(questionStartTime.timetuple()), go="true")
def render(self): if self.debug: return render_template('index_debug.html', lti_dump=g.lti.dump_all()) if g.lti.is_instructor(): return render_template('question_list.html') else: UserModel.save(g.lti.get_user_id(), g.lti.get_user_name()) return render_template('index_student.html')
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_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(self, page_slug=""): if page_slug: t_values = {} posts = Entry.all().filter("is_external_page =", True).filter("entrytype =", 'page').filter("slug =", page_slug) if posts.count() == 1: logging.warning("find one page with slug=%s" % (page_slug)) posts = posts.fetch(limit=1) post = posts[0] t_values['post'] = post # dump(post) # find all comments comments = Comment.all().filter("entry =", post).order("date") t_values['comments'] = comments else: logging.warning("%d entries share the same slug %s" % (posts.count(), page_slug)) links = Link.all().order("date") t_values['links'] = links categories = Category.all() t_values['categories'] = categories pages = Entry.all().filter("is_external_page =", True).filter("entrytype =", 'page').order("date") t_values['pages'] = pages return self.response.out.write(render_template("page.html", t_values, "basic", False)) else: self.redirect(uri_for("weblog.index"))
def get(self, page_id="", operation=""): t_values = {} logging.info("PageManager get: page_id = %s, operation = %s" % (page_id, operation)) # find current_post based on page_id if page_id: current_post = Entry.get_by_id(long(page_id)) if current_post: logging.info("find post %s from post id %s" % (page_id, current_post.title)) if operation == "edit": t_values['current_post'] = current_post elif operation == "publish": current_post.is_external_page = True current_post.put() t_values['alert_message'] = "Post %s has been changed to public" % (current_post.title) elif operation == "unpublish": current_post.is_external_page = False current_post.put() t_values['alert_message'] = "Post %s has been changed to private" % (current_post.title) elif operation == "delete": current_post.delete() t_values['alert_message'] = "Post %s has been changed to deleted" % (current_post.title) # show all posts posts = Entry.all().filter("entrytype =", 'page') t_values['posts'] = posts return self.response.out.write(render_template("pages.html", t_values, "", True))
def renderanswerform(): try: questionid = int(request.values['question_id']) question = Question.by_id(questionid) except: return abort(404) return render_template('student_answer.html', question = question)
def post(self): # add new post or edit existed post t_values = {} current_post_id = self.request.POST["current_post_id"] post_title = self.request.POST["blog_title"] post_slug = get_safe_slug(self.request.POST["blog_slug"]) post_content = self.request.POST["blog_content"] # find category blog_category_id = self.request.POST["blog_category_id"] post_category = Category.get_by_id(long(blog_category_id)) if post_category: logging.info("find category %s for id %s" % (post_category.name, blog_category_id)) else: logging.error("category id %s can't be located" % (blog_category_id)) if current_post_id: logging.info("PostManager: post : edit post current_post_id = %s" % (current_post_id)) # update existed post post = Entry.get_by_id(long(current_post_id)) if post: t_values['alert_message'] = "Post %s has been updated!" % (post.title) post.title = post_title post.slug = post_slug post.content = post_content post.entrytype = "post" # update category count if this post is public if post.is_external_page and post.category != post_category: if post.category and (post.category.entrycount > 0): post.category.entrycount -= 1 post.category.put() post_category.entrycount += 1 post.category.put() post.category = post_category post.put() else: logging.info("PostManager: post : new post title %s" % (self.request.POST['blog_title'])) # create new post post = Entry() post.title = post_title post.slug = post_slug post.content = post_content post.entrytype = 'post' post.category = post_category # save as public or private? operation = self.request.POST["submit_action"] if operation == "save_publish": post.is_external_page = True # update category count post.category.entrycount += 1 post.category.put() else: # "save" operation post.is_external_page = False # save the post post.put() t_values['alert_message'] = "Post %s has been created!" % (post.title) # show all posts posts = Entry.all().filter("entrytype =", 'post') t_values['posts'] = posts return self.response.out.write(render_template("posts.html", t_values, "", True))
def post(self): t_values = {} current_link_id = self.request.POST['current_link_id'] link_title = self.request.POST['link_title'] link_target = self.request.POST['link_target'] link_sequence = self.request.POST['link_sequence'] logging.info("LinkManager post: current_link_id = %s, link_title = %s, link_target = %s, link_sequence = %s" % (current_link_id, link_title, 'link_target', 'link_sequence')) if current_link_id: # edit existed link link = Link.get_by_id(long(current_link_id)) link.title = link_title link.target = link_target link.sequence = long(link_sequence) link.put() t_values['alert_message'] = "link %s has been updated" % (link.title) else: # create new link link = Link(title=link_title, target=link_target, sequence=long(link_sequence)) link.put() t_values['alert_message'] = "link %s has been added" % (link.title) # find all links links = Link.all().order("-date") t_values["links"] = links return self.response.out.write(render_template("links.html", t_values, "", True))
def saveReviewAnswer(self): questionID = int(self.request.form['questionID']) userID = self.request.form['userID'] reviewAnswer = self.request.form['reviewAnswer'] edit = int(self.request.form['edit']) answer.AnswerModel.savereview( questionID, userID, reviewAnswer, edit) return render_template('answersaved.html', flag='true')
def get(self): t_values = {} logging.info("CategoryManager: get") # find all categories cates = Category.all().order("name") t_values["categories"] = cates return self.response.out.write(render_template("categories.html", t_values, "", True))
def get_list(): questions = Question.get_filtered() for question in questions: if question is not None and question.activate_time is not None: if question.get_time_left() < 0: question.answerable = False session.commit() return render_template('question_list.html', questions=questions)
def get(self, page_id="", operation=""): # find all comments, and list all comments t_values = {} logging.info("CommentManager get") # show all comments comments = Comment.all().order("entry") t_values['comments'] = comments return self.response.out.write(render_template("comments.html", t_values, "", True))
def import_data(): if 'file' not in request.files or not request.files['file']: return render_template('error.html', error="No file given.") #answers = request.form.get('answers', False) == 'on' data = request.files['file'].read() data = yaml.load(data) return Question.import_course(g.lti.get_user_id(), g.lti.get_course_id(), data) #, answers)
def get(self): link = Link(title="linkx1", target="http://baidu.com", sequence=9) link.put() link = Link(title="linkx2", target="http://baidu.com", sequence=9) link.put() link = Link(title="linkx3", target="http://baidu.com", sequence=9) link.put() return self.response.out.write(render_template("index.html", {}, "basic", False))
def render_filtered_by_questionid(self,questionid): postdata = self.request.form args = {"questionID": questionid} if self.request.method == "POST": if "userID" in postdata and len(postdata["userID"]) > 0: args["userID"] = postdata["userID"] if "id" in postdata and len(postdata["id"]) > 0: args["id"] = postdata["id"] return render_template('answerfilter_by_questionid.html', answers=answer.AnswerModel.get_filtered(**args))
def get_pagination(): curpage = int(request.args['currentpage']) startpage = int(request.args['startpage']) pagecount = int(request.args['pagecount']) maxpages = int(request.args['maxpages']) return render_template('pagination.html', currentpage=curpage, startpage=startpage, pagecount=pagecount, maxpages=maxpages)
def get_list_table(limit,offset): (questions, curpage, maxpages, startpage, pagecount) = Question.get_filtered_offset(limit,offset,orderby='created') for question in questions: if question is not None and question.activate_time is not None: if question.get_time_left() < 0: question.answerable = False session.commit() return render_template('question_list_tbl.html', questions=questions, currentpage=curpage,startpage=startpage,pagecount=pagecount,maxpages=maxpages)
def get(self): # find stats for this blog stats = {} stats['posts'] = Entry.all().filter("entrytype =", "post").filter("is_external_page =", True).count() stats['pages'] = Entry.all().filter("entrytype =", "page").filter("is_external_page =", True).count() stats['comments'] = Comment.all().count() stats['categories'] = Category.all().count() stats['links'] = Link.all().count() t_values = {} t_values['stats'] = stats return self.response.out.write(render_template("index.html", t_values, "", True))
def review(): answer = Schedule.get_answer(g.lti.get_user_id()) if answer == None: return "No answers to review." fsession['reviewanswer'] = answer.id enabledtags = AnswerTag.get_tag_ids(answer.id) reviews = Review.get_list(answer.id) return render_template('reviewanswer.html', answer=answer, tags=Tag.get_all(), enabledtags=enabledtags, reviews=reviews)
def _export_feeds(self, options, args): '''Exports feeds to OPML file''' if not args: raise CommandError('no output OPML file given') filename = args[0] #@@TODO Use a 'http_datetime' filter in template instead timestamp = format_http_datetime(datetime.utcnow()) groups = [ (group.title, self.get_group_feeds(group)) for group in self.get_groups() ] with open(filename, 'w') as f: f.write(render_template(os.path.join(template_dir, 'export.xml'), locals()))
def _export_feeds(self, options, args): '''Exports feeds to OPML file''' filename = args[0] #@@TODO Use a 'http_datetime' filter in template instead timestamp = format_http_datetime(datetime.utcnow()) groups = [(group.title, self.get_group_feeds(group)) for group in self.get_groups()] with open(filename, 'w') as f: f.write( render_template(os.path.join(template_dir, 'export.xml'), locals()))
def import_course(user_id, course_id, data): #, import_answers): for question in data: qid = QuestionController.create_question(question['question'], user_id, course_id, False, 0, True, True, True) # In order to import answers, you need a unique user id for every # answer. This introduces a lot of bugs and is therefore not active. #if import_answers and 'answers' in question: # start_time = datetime.now(); # for answer in question['answers']: # AnswerController().saveAnswer(user_id, qid, 0, start_time, # answer) questions = map(lambda x: x['question'], data) return render_template('import.html', questions=questions)
def _export_saved_entries(self, options, args): '''Exports saved entries to Atom file''' if not args: raise CommandError('no output Atom file given') filename = args[0] timestamp = datetime.utcnow() q = self.get_saved_entries() guid = FEED_TAG_URI % (timestamp.year, make_sha1_hash(self.user.email or self.user.username)) version = VERSION_STRING with open(filename, 'w') as f: f.write(render_template(os.path.join(template_dir, 'export-saved.xml'), locals(), filters))
def lobby(self): def randpop(array): return array.pop(randrange(0,len(array))) def getotheranswers(userID,questionID): allanswers = (AnswerModel.get_all()) allanswerchoices = (AnswerChoiceModel.get_all()) validAnswers = [] for currentanswer in allanswers: # if relevant answer and not submitted by the current user if currentanswer.userID != userID and currentanswer.questionID == questionID: shouldadd = True for currentanswerchoice in allanswerchoices: # if answer was not rated before by the current user if (currentanswerchoice.best_answer_id == currentanswer.id or currentanswerchoice.other_answer_id == currentanswer.id) and currentanswerchoice.user_id == userID: break else: validAnswers.append(currentanswer.id) return validAnswers def getuncommons(answers): answerchoices = (AnswerChoiceModel.get_all()) cnt = Counter() for answer in answers: cnt[answer] = 0 for answerchoice in answerchoices: if answerchoice.best_answer_id in answers: cnt[answerchoice.best_answer_id] += 1 if answerchoice.other_answer_id in answers: cnt[answerchoice.other_answer_id] += 1 if len(cnt) > 1: return [cnt.most_common()[-1][0],cnt.most_common()[-2][0]] else: return False userID = g.lti.get_user_id() questionID = int(request.values['question_id']) validAnswers = getotheranswers(userID,questionID) print validAnswers uncommons = getuncommons(validAnswers) print uncommons if uncommons == False: return render_template('choicelobby.html',question=questionID) else: #return redirect('/answerchoice?questionid='+str(questionID)+'&answerid1='+str(randpop(validAnswers))+'&answerid2='+str(randpop(validAnswers))) return redirect('/answerchoice?questionid='+str(questionID)+'&answerid1='+str(randpop(uncommons))+'&answerid2='+str(randpop(uncommons)))
def review(): answer = Schedule.get_answer(g.lti.get_user_id()) if answer == None: return "No answers to review." fsession['reviewanswer'] = answer.id enabledtags = AnswerTag.get_tag_ids(answer.id) reviews = Review.get_list(answer.id) question = Question.by_id(answer.questionID) if question is None: return "Question was not found." return render_template('reviewanswer.html', answer=answer, tags=Tag.get_all(), enabledtags=enabledtags, reviews=reviews, question=question)
def command_export(self, options, args): '''Exports feeds to OPML file''' if not args: raise CommandError('no OPML file given') self.user = self._get_user(options.username) filename = args[0] timestamp = format_http_datetime(datetime.utcnow()) groups = [ (group.title, self.get_group_feeds(group)) for group in self.get_groups() ] with open(filename, 'w') as f: f.write(render_template(os.path.join(template_dir, 'export.xml'), locals())) print "Export completed for user %s." % self.user.username
def get(self, post_id="", operation=""): t_values = {} logging.info("PostManager get: post_id = %s, operation = %s" % (post_id, operation)) # find current_post based on post_id if post_id: current_post = Entry.get_by_id(long(post_id)) if current_post: logging.info("find post %s from post id %s" % (post_id, current_post.title)) if operation == "edit": t_values['current_post'] = current_post elif operation == "publish": if not current_post.is_external_page: current_post.category.entrycount += 1 current_post.category.put() current_post.is_external_page = True current_post.put() t_values['alert_message'] = "Post %s has been changed to public" % (current_post.title) else: t_values['alert_message'] = "Post %s was public already" % (current_post.title) elif operation == "unpublish": if current_post.is_external_page: current_post.category.entrycount -= 1 current_post.category.put() current_post.is_external_page = False current_post.put() t_values['alert_message'] = "Post %s has been changed to private" % (current_post.title) else: t_values['alert_message'] = "Post %s was private already" % (current_post.title) elif operation == "delete": if current_post.is_external_page: current_post.category.entrycount -= 1 current_post.category.put() current_post.delete() t_values['alert_message'] = "Post %s has been changed to deleted" % (current_post.title) # show all posts posts = Entry.all().filter("entrytype =", 'post') t_values['posts'] = posts # load all categories categories = Category.all().order("name") t_values['categories'] = categories return self.response.out.write(render_template("posts.html", t_values, "", True))
def get(self, page="1", cate_slug=""): t_values = {} page = int(page) logging.info("IndexHandler - get: page = %d, cate_slug = %s" % (page, cate_slug)) # find all entries by order query = Entry.all().filter("is_external_page =", True).filter("entrytype =", 'post').order("-date") # add category filter? if cate_slug: cates = Category.all().filter("slug =", cate_slug) if cates: query = query.filter("category =", cates[0]) # pagination total_posts = query.count() q_limit = Configuration["posts_per_page"] q_offset = (page - 1) * Configuration["posts_per_page"] logging.info("limit = %d, offset = %d" % (q_limit, q_offset)) # get entries entries = query.fetch(limit=q_limit, offset=q_offset) t_values['entries'] = entries # show entries for debug purpose # for entry in entries: # logging.info("entry title: %s, public = %s, cate = %s" % (entry.title, entry.is_external_page, entry.category.name)) logging.info("total posts = %d, current_page = %d, posts_per_page = %d" % (total_posts, page, Configuration['posts_per_page'])) t_values['navlist'] = generateNavList(total_posts, page, Configuration["posts_per_page"]) # logging.info(t_values['navlist']) # find all links links = Link.all().order("date") t_values['links'] = links # find all categories categories = Category.all() t_values['categories'] = categories # find all pages pages = Entry.all().filter("is_external_page =", True).filter("entrytype =", 'page').order("date") t_values['pages'] = pages # show index page return self.response.out.write(render_template("index.html", t_values, "basic", False))
def post(self): result = {'message': '', 'status': 'success', 'content': ''} logging.info(self.request.POST) current_cate_id = self.request.POST.get("current_cate_id") cate_name = self.request.POST.get("cate_name") cate_slug = self.request.POST.get("cate_slug") operation = self.request.POST.get("operation") logging.info("CategoryManager post: current_cate_id = %s, cate_name = %s, cate_slug = %s, operation = %s" % (current_cate_id, cate_name, cate_slug, operation)) if current_cate_id: # edit existed link cate = Category.get_by_id(long(current_cate_id)) if cate: # current_cate_id exists if operation == "delete": if cate.entrycount == 0: cate.delete() result['message'] = "category %s has been deleted" % (current_cate_id) else: result['status'] = 'failed' result['message'] = "category %s can't be delete since it's still being used!" % (current_cate_id) else: cate.name = cate_name cate.slug = cate_slug cate.put() result['message'] = "category %s has been updated" % (cate.name) else: logging.info("category id=%s does not exist!" % (current_cate_id)) else: # create new cate cate = Category(name=cate_name, slug=cate_slug) cate.put() result['message'] = "category %s has been created" % (cate.name) result['content'] = render_template("category_block.html", {'cate': cate}, "", True) logging.info("content = %s" % (result['content'])) # return json result self.response.content_type = 'application/json' return self.response.out.write(json.encode(result))