def process_item(self, item, spider): session = self.Session() question = Question( question_number=item.get('question_number', ''), question=item.get('question', ''), possible_answers=item.get('possible_answers', ''), correct_answer=item.get('correct_answer', ''), explanation=item.get('explanation', ''), section=item.get('section', ''), type_of_quiz=item.get('type_of_quiz', ''), ) existing_query = session.query(Question).filter( Question.question_number == question.question_number).first() if existing_query: question = existing_query question.question_number = item.get('question_number', '') question.question = item.get('question', '') question.possible_answers = item.get('possible_answers', '') question.correct_answer = item.get('correct_answer', '') question.explanation = item.get('explanation', '') question.section = item.get('section', '') question.type_of_quiz = item.get('type_of_quiz', '') else: session.add(question) session.commit() session.close() return item
def post(self, set_name="", username="", class_name=""): user = self.getCookieCacheUser() if user.username == username: question_type = self.request.get("questionType") question = self.request.get("question") class_obj = self.cacheClass(user.key().id(), class_name) set_obj = self.cacheSet(set_name, class_obj) if question_type == 'check': correct_answer = [] for i in range(1, 6): correct_ans = self.request.get('correctAnswer' + str(i)) if correct_ans: correct_answer.append(correct_ans) else: break else: correct_answer = self.request.get("correctAnswer1") other_answers = [] for i in range(1, 6): other_ans = self.request.get('otherAnswers' + str(i)) if other_ans: other_answers.append(other_ans) else: break if question_type == "true/false": if correct_answer == 'true': other_answers.append('false') elif correct_answer == 'false': other_answers.append('true') other_answers_is_true = False if len(other_answers) > 0: other_answers_is_true = True if question and correct_answer and question_type and other_answers_is_true: if type(correct_answer) == list: for a in correct_answer: other_answers.append(a) else: other_answers.append(correct_answer) shuffle(other_answers) q = Question(set_name=set_obj, question=question, type=question_type, other_answers=other_answers, multiple_correct_answers=[], total_attempts=0, correct_attempts=0) if question_type == 'check': q.multiple_correct_answers = correct_answer else: q.correct_answer = correct_answer q.put() time.sleep(1) set_obj.put() memcache.delete( str(class_obj.key().id()) + str(set_obj.name) + 'q') self.redirect("/%s/%s/%s" % (user.username, class_obj.name, set_obj.name)) else: self.render("create-question.html", type=question_type, current_user=user, set_obj=set_obj, class_obj=class_obj, question=question, current_id=str(user.key().id()), correct_answer=correct_answer, class_active="active", page_title="New Question") else: self.redirect("/")