Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement referral code system with leaderboard #3707

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions website/migrations/0209_add_referral_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.7 on 2023-09-15 12:00

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('website', '0208_auto_20230915_1159'),
]

operations = [
migrations.AddField(
model_name='userprofile',
name='referral_clicks',
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name='userprofile',
name='referral_signups',
field=models.PositiveIntegerField(default=0),
),
]
11 changes: 11 additions & 0 deletions website/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,14 @@ class InviteFriend(models.Model):
def __str__(self):
return f"Invite from {self.sender}"

def increment_referral_clicks(self):
self.sender.userprofile.referral_clicks += 1
self.sender.userprofile.save()

def increment_referral_signups(self):
self.sender.userprofile.referral_signups += 1
self.sender.userprofile.save()


def user_images_path(instance, filename):
from django.template.defaultfilters import slugify
Expand Down Expand Up @@ -706,6 +714,9 @@ def check_team_membership(self):
longest_streak = models.IntegerField(default=0)
last_check_in = models.DateField(null=True, blank=True)

referral_clicks = models.PositiveIntegerField(default=0)
referral_signups = models.PositiveIntegerField(default=0)

def avatar(self, size=36):
if self.user_avatar:
return self.user_avatar.url
Expand Down
2 changes: 2 additions & 0 deletions website/templates/includes/_leaderboard_widget.html
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@
{% endif %}
</a>
<span class="w-full overflow-clip font-semibold text-[#500000bd] text-[12px] mb-2">{{ leader.username }}</span>
<span class="w-full overflow-clip font-semibold text-[#500000bd] text-[12px] mb-2">Clicks: {{ leader.userprofile.referral_clicks }}</span>
<span class="w-full overflow-clip font-semibold text-[#500000bd] text-[12px] mb-2">Signups: {{ leader.userprofile.referral_signups }}</span>
<button type="button"
id="openModal-{{ leader.id }}"
class="btn btn-danger text-black"
Expand Down
2 changes: 2 additions & 0 deletions website/templates/leaderboard_eachmonth.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ <h1 class="page-header">Monthly Leaderboard</h1>
<span class="pull-right badge">${{ month.user.userprofile.winnings|default:""|floatformat }}</span>
{% endif %}
<span><kbd class="{{ month.user.userprofile.get_title_display }} titleuser">{{ month.user.userprofile.get_title_display }}</kbd></span>
<span class="pull-right badge">Signups: {{ month.user.userprofile.referral_signups }}</span>
<span class="pull-right badge">Clicks: {{ month.user.userprofile.referral_clicks }}</span>
{% endif %}
</div>
{% endfor %}
Expand Down
35 changes: 35 additions & 0 deletions website/templates/leaderboard_global.html
Original file line number Diff line number Diff line change
Expand Up @@ -263,5 +263,40 @@ <h1 class="page-header">Global Leaderboard</h1>
{% endif %}
</div>
</div>
<div class="leaderboard-section">
<div class="leaderboard-title">Referral Leaderboard</div>
<div class="list-group">
{% if referral_leaderboard %}
{% for leader in referral_leaderboard %}
<div class="list-group-item">
{% if leader.userprofile.avatar %}
<img src="{{ leader.userprofile.avatar }}"
class="img-responsive img-thumbnail profileimage"
alt="{{ leader.username }}"
width="50px"
height="50px">
{% elif leader.socialaccount_set.all.0.get_avatar_url %}
<img src="{{ leader.socialaccount_set.all.0.get_avatar_url }}"
class="img-responsive img-thumbnail profileimage"
alt="username"
width="50px"
height="50px">
{% else %}
<img src="{% gravatar_url leader.email 50 %}"
class="img-responsive img-thumbnail profileimage"
alt="username"
width="50px"
height="50px">
{% endif %}
<a href="{% url 'profile' slug=leader.username %}">{{ leader.username }}</a>
<span class="pull-right badge">{{ leader.userprofile.referral_signups }} Signups</span>
<span class="pull-right badge">{{ leader.userprofile.referral_clicks }} Clicks</span>
</div>
{% endfor %}
{% else %}
<p class="text-red-900">No referral data available</p>
{% endif %}
</div>
</div>
</div>
{% endblock content %}
2 changes: 2 additions & 0 deletions website/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def handle_user_signup(request, user, **kwargs):
invite = InviteFriend.objects.get(referral_code=referral_token)
invite.recipients.add(user)
invite.point_by_referral += 2
invite.increment_referral_signups()
invite.save()
reward_sender_with_points(invite.sender)
del request.session["ref"]
Expand Down Expand Up @@ -727,6 +728,7 @@ def referral_signup(request):
if referral_token:
try:
invite = InviteFriend.objects.get(referral_code=referral_token)
invite.increment_referral_clicks()
request.session["ref"] = referral_token
except InviteFriend.DoesNotExist:
messages.error(request, "Invalid referral token")
Expand Down
Loading