def get_or_create_prize_url(quest, profile): tweet_url = f"{settings.BASE_URL}{quest.url}".replace( 'gitcoin.co//', 'gitcoin.co/' ) #total hack but prevents https://github.com/gitcoinco/web/issues/5298#issuecomment-545657239 reward = f"{quest.kudos_reward.humanized_name} Kudos" if quest.kudos_reward else f"{quest.reward_tip.value_true} {quest.reward_tip.tokenName}" add_params = f"?cb=ref:{profile.ref_code}&tweet_url={tweet_url}&tweet=I just won a {reward} by beating the '{quest.title} Quest' on @gitcoin quests." if quest.reward_tip: return f"{quest.reward_tip.receive_url}{add_params}" else: btc = None btcs = BulkTransferCoupon.objects.filter( token=quest.kudos_reward, tag='quest', metadata__recipient=profile.pk) if btcs.exists(): btc = btcs.first() else: btc = BulkTransferCoupon.objects.create( token=quest.kudos_reward, tag='quest', num_uses_remaining=1, num_uses_total=1, current_uses=0, secret=random.randint(10**19, 10**20), comments_to_put_in_kudos_transfer= f"Congrats on beating the '{quest.title}' Gitcoin Quest", sender_profile=get_profile('gitcoinbot'), metadata={ 'recipient': profile.pk, }, ) prize_url = f"{btc.url}{add_params}" return prize_url
def record_award_helper(qa, profile, layer=1, action='Beat', value_multiplier=1): """ Awards point awards (and referral rewards) to a winner """ #max depth if layer > max_ref_depth: return # record points value = abs(value_multiplier * qa.quest.value / (10**(layer - 1))) from quests.views import current_round_number QuestPointAward.objects.create( questattempt=qa, profile=profile, value=value, action=action, round_number=current_round_number, ) # record kudos if layer > 1 or settings.DEBUG: gitcoinbot = get_profile('gitcoinbot') quest = qa.quest btc = BulkTransferCoupon.objects.create( token=quest.kudos_reward, tag='quest', num_uses_remaining=1, num_uses_total=1, current_uses=0, secret=random.randint(10**19, 10**20), comments_to_put_in_kudos_transfer= f"Congrats on beating the '{quest.title}' Gitcoin Quest", sender_profile=gitcoinbot, metadata={ 'recipient': profile.pk, }, make_paid_for_first_minutes=1, ) cta_url = btc.url cta_text = 'Redeem Kudos' msg_html = f"@{qa.profile.handle} just beat '{qa.quest.title}'. You earned {round(value,2)} quest points & a kudos for referring them." send_notification_to_user(gitcoinbot.user, profile.user, cta_url, cta_text, msg_html) # recursively record points for your referals quest if profile.referrer: return record_award_helper(qa, profile.referrer, layer + 1, action, value_multiplier)
def process_win(request, qa): """ Processes the win on behalf of the user """ if qa.profile.pk != request.user.profile.pk: messages.info(request, "Invalid Quest attempt") return "https://gitcoin.co/quests" quest = qa.quest was_already_beaten = quest.is_beaten(request.user) first_time_beaten = not was_already_beaten record_quest_activity(quest, request.user.profile, 'beat_quest') btcs = BulkTransferCoupon.objects.filter( token=quest.kudos_reward, tag='quest', comments_to_put_in_kudos_transfer= f"Congrats on beating the '{quest.title}' Gitcoin Quest", metadata__recipient=request.user.profile.pk) btc = None if btcs.exists(): btc = btcs.first() else: btc = BulkTransferCoupon.objects.create( token=quest.kudos_reward, tag='quest', num_uses_remaining=1, num_uses_total=1, current_uses=0, secret=random.randint(10**19, 10**20), comments_to_put_in_kudos_transfer= f"Congrats on beating the '{quest.title}' Gitcoin Quest", sender_profile=get_profile('gitcoinbot'), metadata={ 'recipient': request.user.profile.pk, }, make_paid_for_first_minutes=1, ) prize_url = tweetify_prize_url(btc.url, quest, request.user) qa.success = True qa.save() if not qa.quest.visible: # return a mock prize URL because quiz not approved yet messages.info( request, "cannot redeem prize for a quest thats not live yet! returning you to quests homepage" ) return "https://gitcoin.co/quests" if first_time_beaten: record_award_helper(qa, qa.profile) return prize_url
def response_change(self, request, obj): from django.shortcuts import redirect if "btc_coupon" in request.POST: # TODO: mint this token import random from kudos.views import get_profile gitcoinbot = get_profile('gitcoinbot') btc = BulkTransferCoupon.objects.create( token=obj, tag='admin', num_uses_remaining=1, num_uses_total=1, current_uses=0, secret=random.randint(10**19, 10**20), comments_to_put_in_kudos_transfer=f"Hi from the admin", sender_profile=gitcoinbot, metadata={}, make_paid_for_first_minutes=0, ) self.message_user( request, f"Created Bulk Transfer Coupon with default settings") return redirect(btc.admin_url) return redirect(obj.admin_url)
def details(request, obj_id, name): if not request.user.is_authenticated and request.GET.get('login'): return redirect('/login/github?next=' + request.get_full_path()) # stopgap while this is admin only if not request.user.is_authenticated or ( not request.user.is_staff and not request.user.groups.filter(name='quests').exists()): messages.info( request, 'You dont have permissions to access this alpha feature. Pls contact an admin' ) return redirect('/') """Render the Kudos 'detail' page.""" if not re.match(r'\d+', obj_id): raise ValueError( f'Invalid obj_id found. ID is not a number: {obj_id}') try: quest = Quest.objects.get(pk=obj_id) if not quest.is_unlocked_for(request.user): messages.info( request, 'This quest is locked. Try again after you have unlocked it') return redirect('/quests') except: raise Http404 try: payload = json.loads(request.body) qn = payload.get('question_number') can_continue = True did_win = False prize_url = False if qn is not None and request.user.is_authenticated: save_attempt = qn == 0 if save_attempt: QuestAttempt.objects.create( quest=quest, success=False, profile=request.user.profile, state=0, ) else: qas = QuestAttempt.objects.filter( quest=quest, profile=request.user.profile, state=(qn - 1), created_on__gt=(timezone.now() - timezone.timedelta(minutes=5))) qa = qas.order_by('-pk').first() correct_answers = [ ele['answer'] for ele in quest.questions[qn - 1]['responses'] if ele['correct'] ] their_answers = payload.get('answers') did_they_do_correct = set(correct_answers) == set( their_answers) can_continue = did_they_do_correct if can_continue: qa.state += 1 qa.save() did_win = can_continue and len(quest.questions) <= qn if did_win: btc = BulkTransferCoupon.objects.create( token=quest.kudos_reward, num_uses_remaining=1, num_uses_total=1, current_uses=0, secret=random.randint(10**19, 10**20), comments_to_put_in_kudos_transfer= f"Congrats on beating the '{quest.title}' Gitcoin Quest", sender_profile=get_profile('gitcoinbot'), ) prize_url = btc.url qa.success = True qa.save() response = { "question": quest.questions_safe(qn), "can_continue": can_continue, "did_win": did_win, "prize_url": prize_url, } return JsonResponse(response) except Exception as e: print(e) pass if quest.is_within_cooldown_period(request.user): if request.user.is_staff: messages.info( request, 'You are within this quest\'s 30 min cooldown period. Normally wed send you to another quest.. but.. since ur staff u can try it!' ) else: messages.info( request, 'ou are within this quest\'s 30 min cooldown period. Try again later.' ) return redirect('/quests') if quest.is_beaten(request.user): if request.user.is_staff: messages.info( request, 'You have beaten this quest. Normally wed send you to another quest.. but.. since ur staff u can try it again!' ) else: messages.info(request, 'Youve already conquered this quest! Congrats.') return redirect('/quests') params = { 'quest': quest, 'body_class': 'quest_battle', 'title': quest.title, 'card_desc': quest.description, 'quest_json': quest.to_json_dict(exclude="questions"), } return TemplateResponse(request, 'quests/quest.html', params)