def set_answer(puzzle_name, player_id, question, content_type, content): player = Player.get(Player.id == player_id) puzzle, _ = Puzzle.get_or_create(name=puzzle_name) try: answer = Answer.get( (Answer.puzzle == puzzle) & (Answer.player == player)) except Answer.DoesNotExist: answer = Answer(puzzle=puzzle, player=player) answer.question = question if content_type == 'text': answer.value = content elif content_type == 'image': filename = '%s_%s.jpg' % (puzzle.id, player.id) path = '%s/images/%s' % (STATIC_DIR, filename) with open(path, 'wb') as fp: fp.write(content) answer.value = 'image:' + filename elif content_type == 'video': filename = '%s_%s.mp4' % (puzzle.id, player.id) path = '%s/videos/%s' % (STATIC_DIR, filename) with open(path, 'wb') as fp: fp.write(content) answer.value = 'video:' + filename answer.save()
def set_answer(puzzle_name, player_id, question, content_type, content): player = Player.get(Player.id == player_id) puzzle, _ = Puzzle.get_or_create(name=puzzle_name) try: answer = Answer.get((Answer.puzzle == puzzle) & (Answer.player == player)) except Answer.DoesNotExist: answer = Answer(puzzle=puzzle, player=player) answer.question = question if content_type == 'text': answer.value = content elif content_type == 'image': filename = '%s_%s.jpg' % (puzzle.id, player.id) path = '%s/images/%s' % (STATIC_DIR, filename) with open(path, 'wb') as fp: fp.write(content) answer.value = 'image:' + filename elif content_type == 'video': filename = '%s_%s.mp4' % (puzzle.id, player.id) path = '%s/videos/%s' % (STATIC_DIR, filename) with open(path, 'wb') as fp: fp.write(content) answer.value = 'video:' + filename answer.save()