def test_challengeUpdateCompletedOperation(self): # check that error raised when record does not exist print('--- check that error raised when record does not exist') self.assertRaises(ErrorWithCode, challengeUpdateCompletedOperation, 1, 2, 2, 1) # adding challenge challenge = Challenge(1, 2, 2) db.session.add(challenge) db.session.commit() # check that when successful, result returned by function is of the correct type print( '--- check that when successful, result returned by function is of the correct type' ) challenge = Challenge.query.filter_by(from_student_id=1).filter_by( to_student_id=2).filter_by(quiz_id=1).first() self.assertEqual(challenge.is_completed, False) self.assertIsNone(challenge.winner_id) challengeUpdateCompletedOperation(1, 2, 1, 1) challenge = Challenge.query.filter_by(from_student_id=1).filter_by( to_student_id=2).filter_by(quiz_id=1).first() self.assertEqual(challenge.is_completed, True) self.assertEqual(challenge.winner_id, 1)
def api_get_challenge_create(): """ 创建新挑战,返回挑战id """ max_level = db.session.query(func.max(Challenge.level)).one_or_none()[0] challenge = Challenge(level=(1 if not max_level else max_level + 1)) db.session.add(challenge) db.session.commit() return make_response(0, id=challenge.id)
def setUp(self): print('\r') # drop all tables in the database db.session.remove() db.drop_all() # crete all tables in the database db.create_all() # adding users user_1 = User('*****@*****.**', encrypt('password'), 'student_1') student_1 = Student(user_1, 'U00000000A') db.session.add(student_1) user_2 = User('*****@*****.**', encrypt('password'), 'student_2') student_2 = Student(user_2, 'U00000000B') db.session.add(student_2) user_3 = User('*****@*****.**', encrypt('password'), 'teacher_1') staff_1 = Staff(user_3) db.session.add(staff_1) # adding topics topic = Topic(name='seng') db.session.add(topic) # adding lessons lesson = Lesson(1, 1, 'lesson_1', 'content') db.session.add(lesson) # adding quizzes quiz_1 = Quiz(3, 'quiz_1', True, '2020-03-30', '2020-03-31') db.session.add(quiz_1) quiz_2 = Quiz(3, 'quiz_2', True, '2020-03-30', '2020-03-31') db.session.add(quiz_2) # adding questions question_1 = Question(1, 1, 'question_1') db.session.add(question_1) question_2 = Question(1, 1, 'question_2') db.session.add(question_2) question_3 = Question(1, 1, 'question_3') db.session.add(question_3) question_4 = Question(1, 1, 'question_4') db.session.add(question_4) # adding challenges challenge = Challenge(1, 2, 1) db.session.add(challenge) db.session.commit()
def make_challenge(request, username): user = get_user(username) done = False while not done: try: challenge = Challenge(destination=user) challenge.save() done = True except IntegrityError: pass # occasional cleanup if challenge.code[1] == "0": Challenge.objects.filter(valid_until__lt=datetime.now()).delete() return HttpResponse(challenge.code)
def new_challenge(): if request.method == 'POST' and len(request.form) >= 6: if logged_in(): errors = [] #try: eid = request.form['eid'] name = request.form['name'] category = request.form['category'] value = int(request.form['value']) desc = request.form['description'] #except: if len(errors) > 0: return "Failed to add challenge [2]" else: new_chal = Challenge(eid, name, category, desc, value) db.session.add(new_chal) db.session.commit() cid = new_chal.cid #try: files = request.files.getlist('file[]') if files and len(files) > 0: for f in files: if f and len(f.filename) > 0: #try: upload_file(f) new_file = File(cid, f.filename) db.session.add(new_file) db.session.commit() #except: #errors.append("Something went wrong") else: errors.append( "Error: something wrong with the file or filename" ) #except: # errors.append("No files recieved") db.session.close() return redirect('/event/{}'.format(eid)) else: flash('Must be logged in to create challenges') return redirect(url_for('auth.login')) else: return "Failed to add challenge"
def challenge(request): if request.is_ajax(): challenge_bot_id = json.loads(request.body)['msg'] challenge_bot = Bot.objects.get(pk=challenge_bot_id) # get the user current bot user_prof = UserProfile.objects.get(user=request.user) if not user_prof.current_bot: print "Can not challenge if does not have a bot!" return HttpResponse("Error") if challenge_bot.owner == user_prof: print "[CHEATING!] - wrong challenge bot!" return HttpResponse("Error") # challenged bot must be the owners current bot if not challenge_bot.is_current_bot: print "[CHEATING!] - wrong challenge bot!, must be the owners current bot!." return HttpResponse("Error") print "Got a challenge for bot: ", challenge_bot # Get pending challenges for this user challenges = Challenge.objects.filter(requested_by=user_prof, played=False, canceled=False) if challenges.count() > 0: # has pending challenges, must wait. return HttpResponse("Can not challenge more than one bot at a time") # Check if these bots haven't already played. #played_challs = Challenge.objects.filter(challenger_bot=user_prof.current_bot, # challenged_bot=challenge_bot, played=True) #if played_challs.count() > 0: # # has already played against this bot, must upload a new one # return HttpResponse("Already played against this bot!. Upload a new one.") if (user_prof.current_bot.valid != Bot.READY or challenge_bot.valid != Bot.READY): return JsonResponse({'success': False, 'msg': 'One of the bot is not READY' }) new_challengue = Challenge() new_challengue.requested_by = user_prof new_challengue.challenger_bot = user_prof.current_bot new_challengue.challenged_bot = challenge_bot new_challengue.save() return JsonResponse({'success': True})
def create_challenge(jwt): data = request.get_json() error = False try: challenge = Challenge(task_id=data["task_id"], series_id=data["series_id"]) isTest = False if "isTest" in data: isTest = data["isTest"] DBHelper.insert(challenge) except: DBHelper.rollback() error = True finally: DBHelper.close() if error: abort(404) return jsonify({"success": True})
def test_challengeCreate(self): # create a new Challenge object challenge = Challenge(1, 2, 1) # add Challenge object to the database challengeCreate(challenge) # check that the number of record added is correct print('--- check that the number of record added is correct') self.assertEqual(len(Challenge.query.all()), 1) # check that object cannot be added to database if already exist print( '--- check that object cannot be added to database if already exist' ) challengeCreate(challenge) self.assertEqual(len(Challenge.query.all()), 1)
def test_challengeUpdate(self): # create a new Challenge object and add it to the database challenge = Challenge(1, 2, 1) db.session.add(challenge) db.session.commit() # update value of Topic object Challenge.query.filter_by(from_student_id=1).filter_by( to_student_id=2).filter_by(quiz_id=1).first().is_completed = True challengeUpdate() # check if value of Topic object has been updated print('--- check if value of Topic object has been updated') self.assertEqual( Challenge.query.filter_by(from_student_id=1).filter_by( to_student_id=2).filter_by(quiz_id=1).first().is_completed, True)
def test_challengeRead(self): # check that function returns the right number of result (= 0) print( '--- check that function returns the right number of result (= 0)') self.assertEqual(len(challengeRead(None, None, None, None)), 0) self.assertEqual(len(challengeRead(1, 2, 1, None)), 0) # create a new Challenge object and add to database challenge = Challenge(1, 2, 1) db.session.add(challenge) db.session.commit() # check that function returns the right number of result (= 1) print( '--- check that function returns the right number of result (= 1)') self.assertEqual(len(challengeRead(None, None, None, None)), 1) self.assertEqual(len(challengeRead(1, 2, 1, None)), 1) self.assertEqual(len(challengeRead(1, 2, None, None)), 1)
def create_challenge(): dat = json.loads(request.data) text = dat.get("text") GIPHY_SEARCH_PARAMETERS = { 'api_key': GIPHY_API_KEY, 'limit': 1, # default 'q': text } giphy_dat = requests.get(GIPHY_SEARCH_URL, params=GIPHY_SEARCH_PARAMETERS).json() challenge = Challenge( text=text, imgURL=giphy_dat['data'][0]['images']['fixed_height']['url'], timeToFinish=dat.get("timeToFinish")) db.session.add(challenge) db.session.commit() res = {'success': True, 'data': challenge.serialize()} return json.dumps(res), 201
def initializeChallenge(from_student_id, to_student_id, quiz_id): return Challenge(from_student_id, to_student_id, quiz_id)
def create(request): challenge = Challenge(creator=request.user) return _edit(request, challenge)
def createChallenge(self): task = self.createTask() series = self.createSeries() challenge = Challenge(task_id=task["id"], series_id=series["id"]) return self.createEntity(challenge)
def setUp(self): print('\r') # drop all tables in the database db.session.remove() db.drop_all() # crete all tables in the database db.create_all() self.maxDiff = None self.app = app.test_client() # adding students user_1 = User('*****@*****.**', encrypt('password'), 'student_1') student_1 = Student(user_1, 'U00000000A') db.session.add(student_1) user_2 = User('student_2gmail.com', encrypt('password'), 'student_2') student_2 = Student(user_2, 'U00000000B') db.session.add(student_2) # adding staff user_3 = User('*****@*****.**', encrypt('password'), 'staff_1') staff_1 = Staff(user_3) db.session.add(staff_1) # adding topics topic_1 = Topic(name='topic_1') db.session.add(topic_1) topic_2 = Topic(name='topic_2') db.session.add(topic_2) # adding lessons lesson_1 = Lesson(topic_id=1, id=1, name='lesson_1', content='content') db.session.add(lesson_1) lesson_2 = Lesson(topic_id=1, id=2, name='lesson_2', content='content') db.session.add(lesson_2) lesson_3 = Lesson(topic_id=1, id=3, name='lesson_3', content='content') db.session.add(lesson_3) lesson_4 = Lesson(topic_id=2, id=1, name='lesson_4', content='content') db.session.add(lesson_4) # adding quizzes quiz_1 = Quiz(3, 'quiz_1', True, '2020-03-30', '2020-03-31') db.session.add(quiz_1) quiz_2 = Quiz(3, 'quiz_2', True, '2020-03-30', '2020-03-31') db.session.add(quiz_2) quiz_3 = Quiz(3, 'quiz_3', True, '2020-03-30', '2020-03-31') db.session.add(quiz_3) # adding questions question_1 = Question(1, 1, 'description_1') db.session.add(question_1) question_2 = Question(1, 1, 'description_2') db.session.add(question_2) question_3 = Question(1, 3, 'description_3') db.session.add(question_3) question_4 = Question(1, 3, 'description_4') db.session.add(question_4) question_5 = Question(1, 3, 'description_5') db.session.add(question_5) # assign questions to quiz rs_1 = Rs_quiz_question_contain(1, 1) db.session.add(rs_1) rs_2 = Rs_quiz_question_contain(3, 3) db.session.add(rs_2) rs_3 = Rs_quiz_question_contain(3, 4) db.session.add(rs_3) rs_4 = Rs_quiz_question_contain(3, 5) db.session.add(rs_4) # add attempts to questions question_attempt_1 = QuestionAttempt(1, 1, True, 10) db.session.add(question_attempt_1) question_attempt_2 = QuestionAttempt(1, 1, False, 20) db.session.add(question_attempt_2) question_attempt_3 = QuestionAttempt(1, 2, True, 30) db.session.add(question_attempt_3) question_attempt_4 = QuestionAttempt(1, 3, False, 40) db.session.add(question_attempt_4) # add question choices to questions qc_1 = QuestionChoice(1, 1, 'choice_1', True) db.session.add(qc_1) qc_2 = QuestionChoice(1, 2, 'choice_2', True) db.session.add(qc_2) qc_3 = QuestionChoice(1, 3, 'choice_3', True) db.session.add(qc_3) # adding quiz attempts quiz_attempt_1 = QuizAttempt(1, 1, 0) db.session.add(quiz_attempt_1) quiz_attempt_2 = QuizAttempt(2, 3, 2) db.session.add(quiz_attempt_2) quiz_attempt_3 = QuizAttempt(1, 3, 3) db.session.add(quiz_attempt_3) # adding quizzes to lessons rs_lesson_quiz_contain_1 = Rs_lesson_quiz_contain(1, 1, 1) db.session.add(rs_lesson_quiz_contain_1) rs_lesson_quiz_contain_2 = Rs_lesson_quiz_contain(1, 1, 2) db.session.add(rs_lesson_quiz_contain_2) rs_lesson_quiz_contain_3 = Rs_lesson_quiz_contain(1, 3, 3) db.session.add(rs_lesson_quiz_contain_3) # adding courses course = Course('cz1005') db.session.add(course) # enrol students into courses enrol_1 = Rs_student_course_enrol(1, 'cz1005') db.session.add(enrol_1) enrol_2 = Rs_student_course_enrol(2, 'cz1005') db.session.add(enrol_2) # make staff teach course teach_1 = Rs_staff_course_teach(3, 'cz1005') db.session.add(teach_1) # adding quizzes to courses Rs_quiz_course_assign_1 = Rs_quiz_course_assign(1, 'cz1005') db.session.add(Rs_quiz_course_assign_1) Rs_quiz_course_assign_2 = Rs_quiz_course_assign(2, 'cz1005') db.session.add(Rs_quiz_course_assign_2) Rs_quiz_course_assign_3 = Rs_quiz_course_assign(3, 'cz1005') db.session.add(Rs_quiz_course_assign_3) # adding challenges challenge_1 = Challenge(1, 2, 1) db.session.add(challenge_1) challenge_2 = Challenge(1, 2, 2) db.session.add(challenge_2) challenge_3 = Challenge(2, 1, 1) db.session.add(challenge_3) db.session.commit()