def test_queries_create_row_for_user_if_none_exits(self): """ Test to check that add_coins, remove_coins, and return_user_coins create a row in UserCoins for the user if none exists """ new_user_1 = User(username="******", email="*****@*****.**", password="******") new_user_1.save() self.assertEqual(len(UserCoins.objects.filter(user=new_user_1)), 0) add_coins(new_user_1, 100, 4) self.assertEqual(len(UserCoins.objects.filter(user=new_user_1)), 1) new_user_2 = User(username="******", email="*****@*****.**", password="******") new_user_2.save() self.assertEqual(len(UserCoins.objects.filter(user=new_user_2)), 0) remove_coins(new_user_2, 200, 6) self.assertEqual(len(UserCoins.objects.filter(user=new_user_2)), 1) new_user_3 = User(username="******", email="*****@*****.**", password="******") new_user_3.save() self.assertEqual(len(UserCoins.objects.filter(user=new_user_3)), 0) return_user_coins(new_user_3) self.assertEqual(len(UserCoins.objects.filter(user=new_user_3)), 1)
def test_can_add_and_remove_coins(self): """ Test to check that add_coins adds coins to the argument user's UserCoins row, and remove_coins removes them """ user_1 = choice(User.objects.all()) previous_coins = UserCoins.objects.get(user=user_1).coins add_coins(user_1, 100, 2) coins_after_addition = UserCoins.objects.get(user=user_1).coins self.assertEqual((coins_after_addition - 100), previous_coins) remove_coins(user_1, 300, 4) coins_after_subtraction = UserCoins.objects.get(user=user_1).coins self.assertEqual((coins_after_subtraction + 300), coins_after_addition)
def upvote_suggestion(request, id): """ View for upvoting a suggestion. An upvote is only added if the user has not already upvoted that suggestion. However, if coins are enabled, users can add several upvotes to the same FEATURE suggestion """ suggestion = get_object_or_404(Suggestion, id=id) if settings.COINS_ENABLED and suggestion.is_feature: remove_coins(request.user, get_coins_price("Upvote"), 2) add_coins(suggestion.user, coin_rewards.suggestion_upvoted, 7) already_upvoted = add_suggestion_upvote_to_database( request.user, suggestion) if already_upvoted: messages.info(request, "You have already upvoted this suggestion") else: messages.info(request, "Suggestion upvoted") return redirect("view_suggestion", id)
def create_user(request): """ Creates a new (non-admin) user and stores them in the database """ if request.method == "POST": form = UserSignupForm(request.POST) if form.is_valid(): new_user = form.save() if settings.COINS_ENABLED: add_coins(new_user, coin_rewards.signup, 5) messages.info(request, "Your account has been created. Please login") return redirect("login") else: form = UserSignupForm() return render(request, 'signup.html', {"form": form})
def declare_winner(): """ Note that this also adds coins """ try: winner = Suggestion.objects.filter( is_feature=True, suggestionadminpage__in_current_voting_cycle=True).annotate( upvotes=Count("upvote")).latest('upvote') winner_admin_object = SuggestionAdminPage.objects.get( suggestion=winner) winner_admin_object.current_winner = True winner_admin_object.save() if settings.COINS_ENABLED: add_coins(winner_admin_object.suggestion.user, coin_rewards.suggestion_successful, 8) except: return False
def declare_winner(): """ Whichever feature suggestions in the current voting cycle has the most upvotes has its current_winner value set to True. Adds coins to suggestion's user's account. Amount of coins specified in coin_rewards file """ try: winner = Suggestion.objects.filter( is_feature=True, suggestionadminpage__in_current_voting_cycle=True).annotate( upvotes=Count("upvote")).order_by("-upvotes").first() winner_admin_object = SuggestionAdminPage.objects.get( suggestion=winner) winner_admin_object.current_winner = True winner_admin_object.save() if settings.COINS_ENABLED: add_coins(winner_admin_object.suggestion.user, coin_rewards.suggestion_successful, 8) except: return False
def render_home(request): """ Renders UnicornAttractor home page. Post request occurs when user submits signup form """ form = UserSignupForm voting_end_date = get_voting_end_date() if request.method == "POST": form = UserSignupForm(request.POST) if form.is_valid(): new_user = form.save() if settings.COINS_ENABLED: add_coins(new_user, coin_rewards.signup, 5) login(request, new_user) messages.info( request, "Your account has been created. Welcome {}!".format( new_user.username)) return render(request, "home.html", { "form": form, "voting_end_date": voting_end_date })
def create_referred_user(request, ref_user_id): """ Creates a new non-admin user. Adds extra coins to both the new user and referee user's UserCoin. Amount of extra coins specified in coin_rewards """ if request.method == "POST": form = UserSignupForm(request.POST) if form.is_valid(): new_user = form.save() ref_user = get_object_or_404(User, id=ref_user_id) if settings.COINS_ENABLED: add_coins(new_user, coin_rewards.signup, 5) add_coins(ref_user, coin_rewards.referral, 3) add_coins(new_user, coin_rewards.received_referral, 6) messages.info(request, "Your account has been created. Please login") return redirect("login") else: form = UserSignupForm() return render(request, 'signup.html', {"form": form})