예제 #1
0
def editquest(request, pk=None):
    """Render the Quests 'new/edit' page."""

    # preamble
    post_pk = request.POST.get('pk')
    if not pk and post_pk:
        pk = post_pk

    # auth
    if not request.user.is_authenticated:
        login_redirect = redirect('/login/github/?next=' + request.get_full_path())
        return login_redirect

    quest = None
    if pk:
        try:
            quest = Quest.objects.get(pk=pk)
            if not request.user.is_authenticated:
                raise Exception('no')
            if quest.creator != request.user.profile:
                if not request.user.is_staff:
                    raise Exception('no')
        except:
            raise Http404

    # setup vars
    package = request.POST.copy()
    answers = []
    questions = [{
        'question': '',
        'responses': ['','']
    }]

    #handle submission
    if package:

        questions = [{
            'question': ele,
            'seconds_to_respond': 30,
            'responses': [],
        } for ele in package.getlist('question[]', [])]

        # multi dimensional array hack
        counter = 0
        answer_idx = 0

        answers = package.getlist('answer[]',[])
        answer_correct = package.getlist('answer_correct[]',[])
        seconds_to_respond = package.getlist('seconds_to_respond[]',[])
        points = abs(int(float(package.get('points'))))

        # continue building questions object
        for i in range(0, len(seconds_to_respond)):
            questions[i]['seconds_to_respond'] = abs(int(seconds_to_respond[i]))

        for answer in answers:
            if answer == '_DELIMITER_':
                answer_idx += 1
            else:
                questions[answer_idx]['responses'].append({
                    'answer': answer,
                    'correct': bool(answer_correct[counter] == "YES"),
                })
            counter += 1

        validation_pass = True
        reward = None
        try:
            enemy = Token.objects.get(pk=package.get('enemy'))
            if package.get('reward'):
                reward = Token.objects.get(pk=package.get('reward'))
        except Exception as e:
            messages.error(request, 'Unable to find Kudos')
            validation_pass = False

        if validation_pass:
            seconds_per_question = package.get('seconds_per_question', 30)
            game_schema = {
              "intro": package.get('description'),
              "rules": f"You will be battling a {enemy.humanized_name}. You will have as much time as you need to prep before the battle, but once the battle starts you will have only seconds per move (keep an eye on the timer in the bottom right; don't run out of time!).",
              "seconds_per_question": seconds_per_question,
              'est_read_time_mins': package.get('est_read_time_mins', 20),
              "prep_materials": [
                {
                  "url": package.get('reading_material_url'),
                  "title": package.get('reading_material_name')
                }
              ]
            }
            game_metadata = {
              "video": package.get('background').replace('back', 'bg') + ".mp4" if package.get('video_enabled', 0) else None,
              "enemy": {
                "id": enemy.pk,
                "art": enemy.img_url,
                "level": "1",
                "title": enemy.humanized_name
              }
            }

            try:
                funct = Quest.objects.create
                edit_comments = ""
                visible = False
                if pk:
                    funct = Quest.objects.filter(pk=pk).update
                    edit_comments = quest.edit_comments
                    visible = quest.visible
                if package.get('comment'):
                    edit_comments += f"\n {timezone.now().strftime('%Y-%m-%dT%H:%M')}: {package['comment']} "

                quest = funct(
                    title=package.get('title'),
                    description=package.get('description'),
                    questions=questions,
                    game_schema=game_schema,
                    game_metadata=game_metadata,
                    kudos_reward=reward,
                    cooldown_minutes=package.get('minutes'),
                    background=package.get('background'),
                    visible=visible,
                    difficulty=package.get('difficulty'),
                    style=package.get('style'),
                    value=points,
                    creator=quest.creator if pk else request.user.profile,
                    edit_comments=edit_comments,
                    )
                if type(quest) == int:
                    quest = Quest.objects.get(pk=pk)
                new_quest_request(quest, is_edit=bool(pk))
                msg = f'Quest submission received.  We will respond via email in a few business days.  In the meantime, feel free to test your new quest @ {quest.url}'
                if pk:
                    msg = 'Edit has been made & administrators have been notified to re-review your quest (this is a SPAM/XSS prevention thing that we hope to eventually decentralize..).'
                messages.info(request, msg)
                if quest.is_dead_end:
                    messages.warning(request, "Warning: this quest has at least one dead end question (no possible answers). It won't be approved until that is corrected.")
            except Exception as e:
                if settings.DEBUG:
                    raise e
                logger.exception(e)
                messages.error(request, 'An unexpected error has occurred')

    #load edit page
    if pk and not package.get('title'):
        questions = quest.questions
        for key, val in quest.to_standard_dict().items():
            package[key] = val
        package['est_read_time_mins'] = quest.game_schema.get('est_read_time_mins')
        package['reading_material_url'] = quest.game_schema.get('prep_materials', [{}])[0].get('url')
        package['reading_material_name'] = quest.game_schema.get('prep_materials', [{}])[0].get('title')
        package['video_enabled'] = quest.video
        package['reward'] = quest.kudos_reward.pk if quest.kudos_reward else None
        package['enemy'] = quest.game_metadata.get('enemy', {}).get('id')
        package['points'] = quest.value
        package['minutes'] = quest.cooldown_minutes

    params = {
        'title': 'New Quest Application' if not quest else "Edit Quest",
        'pk': pk if not quest else quest.pk,
        'package': package,
        'the_quest': quest,
        'video_enabled_backgrounds': [f"back{i}" for i in video_enabled_backgrounds],
        'questions': questions,
        'avatar_url': request.build_absolute_uri(static('v2/images/twitter_cards/tw_cards-05.png')),
        'backgrounds': [ele[0] for ele in Quest.BACKGROUNDS],
        'answer_correct': request.POST.getlist('answer_correct[]',[]),
        'seconds_to_respond': request.POST.getlist('seconds_to_respond[]',[]),
        'answer': request.POST.getlist('answer[]',[]),
    }
    return TemplateResponse(request, 'quests/new.html', params)
예제 #2
0
def newquest(request):
    """Render the Quests 'new' page."""

    if not request.user.is_authenticated:
        login_redirect = redirect('/login/github?next=' +
                                  request.get_full_path())
        return login_redirect

    answers = []
    questions = [{'question': '', 'responses': ['', '']}]
    if request.POST:

        questions = [{
            'question': ele,
            'seconds_to_respond': 30,
            'responses': [],
        } for ele in request.POST.getlist('question[]', [])]

        # multi dimensional array hack
        counter = 0
        answer_idx = 0
        answers = request.POST.getlist('answer[]', [])
        answer_correct = request.POST.getlist('answer_correct[]', [])
        seconds_to_respond = request.POST.getlist('seconds_to_respond[]', [])

        # continue building questions object
        for i in range(0, len(seconds_to_respond)):
            questions[i]['seconds_to_respond'] = int(seconds_to_respond[i])

        for answer in answers:
            if answer == '_DELIMITER_':
                answer_idx += 1
            else:
                questions[answer_idx]['responses'].append({
                    'answer':
                    answer,
                    'correct':
                    bool(answer_correct[counter] == "YES"),
                })
            counter += 1

        validation_pass = True
        try:
            enemy = Token.objects.get(pk=request.POST.get('enemy'))
            reward = Token.objects.get(pk=request.POST.get('reward'))
        except Exception as e:
            messages.error(request, 'Unable to find Kudos')
            validation_pass = False

        if validation_pass:
            seconds_per_question = request.POST.get('seconds_per_question', 30)
            game_schema = {
                "intro":
                request.POST.get('description'),
                "rules":
                f"You will be battling a {enemy.humanized_name}. You will have as much time as you need to prep before the battle, but once the battle starts you will have only seconds per move (keep an eye on the timer in the bottom right; don't run out of time!).",
                "seconds_per_question":
                seconds_per_question,
                'est_read_time_mins':
                request.GET.get('est_read_time_mins', 20),
                "prep_materials": [{
                    "url":
                    request.POST.get('reading_material_url'),
                    "title":
                    request.POST.get('reading_material_name')
                }]
            }
            game_metadata = {
                "enemy": {
                    "art": enemy.img_url,
                    "level": "1",
                    "title": enemy.humanized_name
                }
            }

            try:
                quest = Quest.objects.create(
                    title=request.POST.get('title'),
                    description=request.POST.get('description'),
                    questions=questions,
                    game_schema=game_schema,
                    game_metadata=game_metadata,
                    kudos_reward=reward,
                    cooldown_minutes=request.POST.get('minutes'),
                    visible=False,
                    difficulty=request.POST.get('difficulty'),
                    style=request.POST.get('style'),
                    value=request.POST.get('points'),
                    creator=request.user.profile,
                )
                new_quest_request(quest)
                messages.info(
                    request,
                    f'Quest submission received.  We will respond via email in a few business days.  In the meantime, feel free to test your new quest @ https://gitcoin.co{quest.url}'
                )
            except Exception as e:
                logger.exception(e)
                messages.error(request, 'An unexpected error has occured')

    params = {
        'title': 'New Quest Application',
        'package': request.POST,
        'questions': questions,
        'answer_correct': request.POST.getlist('answer_correct[]', []),
        'seconds_to_respond': request.POST.getlist('seconds_to_respond[]', []),
        'answer': request.POST.getlist('answer[]', []),
    }
    return TemplateResponse(request, 'quests/new.html', params)