def add_question(self, category, question, options, answer): ''' extract data from json ''' catid = category.catid question = Question(category=catid, question=question, real_answer=answer ) ''' extract options for question ''' quiz_options = ['a', 'b', 'c', 'd'] for k, v in enumerate(options): var = "var_%s" % quiz_options[k] setattr(question, var, options[k]) question.save()
def setUp(self): self.question = "Did the Main Character Dies?" self.title = "Spoil Hello World" self.difficulty = "Hardcore" self.topic_object = Topic(topic_name="Hello World") self.question_object = Question(topic=self.topic_object, question_text=self.question, question_title=self.title, difficulty=self.difficulty)
def get(self, request, category, limit=10): cache_key = "q%s" % str(category) questions = cache.get(cache_key) if questions is None: questions = Question.objects(category=category, approved=True) if questions.count() == 0: return Response({}) questions = [QuestionResponse(q).serialize() for q in questions] cache.set(cache_key, questions) random_questions = random.sample(questions, int(limit)) if len(random_questions) == 0: random_questions = {} return Response(random_questions)
def post(self, request): try: data = JSONParser().parse(request) data = data.get(PARAMETER_DATA) except JSONDecodeError: raise ParseError(detail="No data found on the request") resolved_questions = {} ids = [int(key) for key in data.keys()] if len(ids) > 0: questions = Question.objects(qid__in=ids) if len(questions) > 0: ''' compare result ''' resolved_questions = self.resolve_question(data, questions) return Response(resolved_questions)
def get_question_to_create(game: Game, existing_questions: List[Question]) -> Question: while True: question_pair = get_random_language_pair() existing_question_words = [ question.question for question in existing_questions ] new_question_word = question_pair["english_word"] if new_question_word not in existing_question_words: answer_words = get_question_answers(question_pair) return Question( game=game, question=new_question_word, correct_answer=question_pair["russian_word"], answer_words=answer_words, )
def get_question_to_create( game: Game, pictures_topic: PicturesTopic, existing_questions: List[Question], ) -> Question: while True: new_question_item = get_random_topic_item(pictures_topic) new_question_link = static( get_file_path(pictures_topic, new_question_item)) existing_question_links = [ question.question for question in existing_questions ] if new_question_link not in existing_question_links: answers = get_answers(pictures_topic, new_question_item) return Question( game=game, question=new_question_link, answer_words=answers, correct_answer=new_question_item[0], )
import json import os os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'jeopardy.settings') import django django.setup() from game.models import Question question_file = open('jeopardy_questions.json', 'r') data = question_file.read() obj = json.loads(data) l = len(Question.objects.all()) for o in range(len(obj)): q = Question(question_no=(l + o + 1), question=obj[o]['clue'], answer=obj[o]['answer'], points=obj[o]['points'], category=obj[o]['tag']) q.save()
from game.models import Question for op in map(str,range(4)): for i in range(1,11): for j in range (1,11): q = Question() q.first_number = i q.second_number = j q.operation = op q.save()
def update_score(self, user, score, game_type): ''' get game type if exists ''' if game_type is None: game_type = self.DEFAULT_GAME_TYPE game_obj = GameType.objects(typeid = game_type).first() if game_obj is None: ''' get default game type ''' game_obj = GameType.objects(typeid = self.DEFAULT_GAME_TYPE) ''' get default leaderboard type ''' leaderboard_type = LeaderboardType.objects(typeid = self.DEFAULT_LEADER_TYPE).first() ''' extract items from array ''' try: questions_ids = [int(d.get(PARAMETER_ID)) for d in score] except KeyError: raise ParseError("ID argument not found in the data field") user_score = {} total_score = 0 leaderboard = None if len(questions_ids) > 0: ''' get questions from DB ''' questions = Question.objects(qid__in=questions_ids) if len(questions) > 0: score_objects = [] leaderboard = Leaderboard.objects(uid=user.uid).first() if leaderboard is None: ''' create new entry in leaderboard if not exists ''' leaderboard = Leaderboard(uid=user.uid, full_name = user.name, corect_answers = 0, skipped = 0, kind = leaderboard_type, total_answers = 0 ) ''' extract settings from cache ''' answer_settings = ObjectMemoryCache.get_key(AnswerSettings) ''' for each item create score ''' real_score = 0 for item in score: id = item.get("id") user_answer = item.get("answer") time = item.get("time") ''' extract time coef. this will be used to calculate user score ''' time_coef = 0 if answer_settings and answer_settings.has_key(time): settings = answer_settings.get(time) if hasattr(settings, "coeficient"): time_coef = settings.coeficient ''' calculate score based on submited question ''' for q in questions: ''' check if questions is present in the user request data ''' if int(q.qid) == int(id): valid_answer = False ''' check if user give us the right answer for selected question ''' if q.real_answer == user_answer: valid_answer = True leaderboard.corect_answers += 1 else: leaderboard.skipped += 1 ''' create score object ''' score = ScoreDetails(uid=user.uid, question=q, correct_answer=valid_answer, answer_time=time, game_type = game_obj ) ''' calculate score based on answer time coef ''' if valid_answer: score.calculate_score(time_coef) ''' get user score ''' real_score += score.get_user_score() else: score.score = 0 leaderboard.total_answers += 1 score_objects.append(score) break if len(score_objects) > 0: try: ''' save score in DB ''' for score in score_objects: total_score += score.score score.save() ''' save leader board ''' leaderboard.score += int(round(real_score)) leaderboard.save() except: traceback.print_exc() ''' check user level ''' score_detail = {} if leaderboard: user, score_detail = level_up(user, leaderboard) score_detail["game_score"] = total_score return user, score_detail
#python3 dbpopulate.py filename domain category import csv, random, sys from game.models import Question, Choice, Category, Domain filename = sys.argv[1] d,res = Domain.objects.get_or_create(domain=sys.argv[2]) d.save() cat, res = Category.objects.get_or_create(category=sys.argv[3],domain=d) cat.save() with open('Questions-Repo/'+filename) as f: questions = csv.reader(f) for question in questions: text = question[0] c = Choice(choice=question[1],correct=True) c.save() choice_list = [c] for choice in question[2:]: if not choice: break c = Choice(choice=choice) c.save() choice_list.append(c) random.shuffle(choice_list) q = Question(question=text,category=cat) q.save() q.choices.set(choice_list) q.save()