-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d622b76
commit 4f9067f
Showing
12 changed files
with
229 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
referral/migrations/0003_referral_created_at_referral_inviter_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Generated by Django 4.1.1 on 2023-04-19 07:17 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import django.utils.timezone | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('referral', '0002_remove_referral_referral_code_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='referral', | ||
name='created_at', | ||
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now), | ||
preserve_default=False, | ||
), | ||
migrations.AddField( | ||
model_name='referral', | ||
name='inviter', | ||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='referrals_made', to=settings.AUTH_USER_MODEL), | ||
), | ||
migrations.AddField( | ||
model_name='referral', | ||
name='referred_user', | ||
field=models.OneToOneField(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='referred_by', to=settings.AUTH_USER_MODEL), | ||
), | ||
migrations.AddField( | ||
model_name='referral', | ||
name='successful', | ||
field=models.BooleanField(default=False), | ||
), | ||
migrations.AlterUniqueTogether( | ||
name='referral', | ||
unique_together={('inviter', 'referred_user')}, | ||
), | ||
migrations.RemoveField( | ||
model_name='referral', | ||
name='referred_users', | ||
), | ||
migrations.RemoveField( | ||
model_name='referral', | ||
name='user', | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from django.db.models.signals import post_save | ||
from django.dispatch import receiver | ||
from datetime import timedelta | ||
from accounts.models import CustomUser | ||
from .models import Referral | ||
|
||
|
||
@receiver(post_save, sender=CustomUser) | ||
def process_referral(sender, instance, **kwargs): | ||
user = instance | ||
print("Signal Referral received") | ||
|
||
# Check if the user's subscription_status has become 'subscribed' | ||
if user.subscription_status == 'subscribed': | ||
try: | ||
# Fetch the referral object where the user is the referred_user | ||
referral = Referral.objects.get(referred_user=user) | ||
|
||
# If the referral is not successful yet, process it | ||
if not referral.successful: | ||
inviter = referral.inviter | ||
|
||
# Extend the inviter's payment due date by 14 days | ||
inviter.expires_at += timedelta(days=14) | ||
inviter.save() | ||
|
||
# Mark the referral as successful | ||
referral.successful = True | ||
referral.save() | ||
except Referral.DoesNotExist: | ||
# No referral exists for this user, do nothing | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
{% extends 'dashboard/base.html' %} | ||
{% load referral_tags %} | ||
|
||
{% block content %} | ||
<div class="container"> | ||
<div class="row"> | ||
<div class="col"> | ||
|
||
<h5>Invite a friend and get $2 when they subscribe</h5> | ||
|
||
<!-- Invite link field --> | ||
<div class="row align-items-center mb-3"> | ||
<div class="col-auto"> | ||
<label for="inviteLink" class="form-label">Your Invite Link:</label> | ||
</div> | ||
<div class="col-4"> | ||
<div class="input-group"> | ||
<input type="text" class="form-control" id="inviteLink" value="{{ request.scheme }}://{{ request.get_host }}?ref={{ user.referral_code }}" readonly> | ||
<button class="btn btn-primary" onclick="copyInviteLink()" title="Copy Link" type="button"> | ||
<svg fill="none" stroke="currentColor" stroke-width="1.5" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="18" height="18"> | ||
<path stroke-linecap="round" stroke-linejoin="round" d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 01-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 011.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 00-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 01-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 00-3.375-3.375h-1.5a1.125 1.125 0 01-1.125-1.125v-1.5a3.375 3.375 0 00-3.375-3.375H9.75"></path> | ||
</svg> | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<h2>My Referrals</h2> | ||
|
||
<!-- Referrals table --> | ||
<table class="table table-striped"> | ||
<thead> | ||
<tr> | ||
<th scope="col">Email</th> | ||
<th scope="col">Status</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{% for referral in referrals %} | ||
<tr> | ||
<td>{{ referral.referred_user.email|obfuscate_email }}</td> | ||
<td> | ||
{%if referral.successful %} | ||
<svg class="icon icon-xs text-success" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"></path></svg></td> | ||
{%else%} | ||
<svg class="icon icon-xs text-danger" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd"></path></svg> | ||
{%endif%} | ||
</tr> | ||
{% empty %} | ||
<tr> | ||
<td colspan="2" class="text-center">No referrals found.</td> | ||
</tr> | ||
{% endfor %} | ||
</tbody> | ||
</table> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
{% endblock %} | ||
|
||
{% block extra_js %} | ||
<script> | ||
function copyInviteLink() { | ||
const inviteLinkField = document.getElementById('inviteLink'); | ||
inviteLinkField.select(); | ||
document.execCommand('copy'); | ||
alert('Invite link copied to clipboard.'); | ||
} | ||
</script> | ||
{% endblock %} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from django import template | ||
|
||
register = template.Library() | ||
|
||
|
||
@register.filter | ||
def obfuscate_email(email): | ||
email_parts = email.split('@') | ||
if len(email_parts) != 2: | ||
return email | ||
|
||
username, domain = email_parts | ||
|
||
if len(username) <= 3: | ||
return email | ||
|
||
obfuscated_username = username[:2] + '*' * \ | ||
(len(username) - 3) + username[-1] | ||
obfuscated_email = obfuscated_username + '@' + domain | ||
|
||
return obfuscated_email |