From 8f52ef8511a2c452e3b37f3420af67673ffe63ea Mon Sep 17 00:00:00 2001 From: JisanAR03 Date: Sun, 25 Feb 2024 10:59:05 +0600 Subject: [PATCH] copy link feature added --- blt/urls.py | 7 +- website/forms.py | 8 +- ...084_alter_invitefriend_options_and_more.py | 53 +++++++++++++ website/models.py | 14 +--- website/templates/email/invitation_email.html | 60 --------------- website/templates/invite_friend.html | 49 ++++++------ website/views.py | 75 +++++-------------- 7 files changed, 101 insertions(+), 165 deletions(-) create mode 100644 website/migrations/0084_alter_invitefriend_options_and_more.py delete mode 100644 website/templates/email/invitation_email.html diff --git a/blt/urls.py b/blt/urls.py index 8af5ef5be..4eb84e269 100644 --- a/blt/urls.py +++ b/blt/urls.py @@ -30,7 +30,6 @@ DomainDetailView, StatsDetailView, InviteCreate, - CreateInviteFriend, ScoreboardView, CustomObtainAuthToken, CreateHunt, @@ -117,6 +116,7 @@ handler500 = "website.views.handler500" urlpatterns = [ + path('invite-friend/', website.views.invite_friend, name='invite_friend'), path('referral/', website.views.referral_signup, name='referral_signup'), path("captcha/", include("captcha.urls")), re_path(r"^auth/registration/", include("dj_rest_auth.registration.urls")), @@ -355,11 +355,6 @@ re_path(r"^hunt/$", login_required(HuntCreate.as_view()), name="hunt"), re_path(r"^hunts/$", ListHunts.as_view(), name="hunts"), re_path(r"^invite/$", InviteCreate.as_view(template_name="invite.html")), - re_path( - r"^invite-friend/$", - login_required(CreateInviteFriend.as_view()), - name="invite_friend", - ), re_path(r"^terms/$", TemplateView.as_view(template_name="terms.html"),name="terms"), re_path(r"^about/$", TemplateView.as_view(template_name="about.html")), re_path(r"^privacypolicy/$", TemplateView.as_view(template_name="privacy.html")), diff --git a/website/forms.py b/website/forms.py index a61ca24e3..24f74cd7d 100644 --- a/website/forms.py +++ b/website/forms.py @@ -1,15 +1,9 @@ from django import forms -from .models import InviteFriend, UserProfile, Hunt +from .models import UserProfile, Hunt from mdeditor.fields import MDTextFormField from captcha.fields import CaptchaField -class FormInviteFriend(forms.ModelForm): - class Meta: - model = InviteFriend - fields = ['recipient'] - widgets = {'recipient': forms.TextInput(attrs={'class': 'form-control'})} - class UserProfileForm(forms.ModelForm): class Meta: diff --git a/website/migrations/0084_alter_invitefriend_options_and_more.py b/website/migrations/0084_alter_invitefriend_options_and_more.py new file mode 100644 index 000000000..a0ced5295 --- /dev/null +++ b/website/migrations/0084_alter_invitefriend_options_and_more.py @@ -0,0 +1,53 @@ +# Generated by Django 5.0.2 on 2024-02-25 03:59 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("website", "0083_invitefriend_referral_code_and_more"), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.AlterModelOptions( + name="invitefriend", + options={}, + ), + migrations.RemoveField( + model_name="invitefriend", + name="recipient", + ), + migrations.RemoveField( + model_name="invitefriend", + name="sent", + ), + migrations.RemoveField( + model_name="invitefriend", + name="signup_completed", + ), + migrations.AddField( + model_name="invitefriend", + name="point_by_referral", + field=models.IntegerField(default=0), + ), + migrations.AddField( + model_name="invitefriend", + name="recipients", + field=models.ManyToManyField( + blank=True, related_name="received_invites", to=settings.AUTH_USER_MODEL + ), + ), + migrations.AlterField( + model_name="invitefriend", + name="sender", + field=models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="sent_invites", + to=settings.AUTH_USER_MODEL, + ), + ), + ] diff --git a/website/models.py b/website/models.py index aa34269b3..f2a080b93 100644 --- a/website/models.py +++ b/website/models.py @@ -378,19 +378,13 @@ class Points(models.Model): class InviteFriend(models.Model): - sender = models.ForeignKey(User, on_delete=models.CASCADE) - recipient = models.EmailField() - sent = models.DateTimeField(auto_now_add=True, db_index=True) + sender = models.ForeignKey(User, related_name='sent_invites', on_delete=models.CASCADE) + recipients = models.ManyToManyField(User, related_name='received_invites', blank=True) referral_code = models.CharField(max_length=100, default=uuid.uuid4, editable=False) - signup_completed = models.BooleanField(default=False) - - class Meta: - ordering = ("-sent",) - verbose_name = "invitation" - verbose_name_plural = "invitations" + point_by_referral = models.IntegerField(default=0) def __str__(self): - return f"Invite from {self.sender} to {self.recipient}" + return f"Invite from {self.sender}" def user_images_path(instance, filename): diff --git a/website/templates/email/invitation_email.html b/website/templates/email/invitation_email.html deleted file mode 100644 index 4a3c55c16..000000000 --- a/website/templates/email/invitation_email.html +++ /dev/null @@ -1,60 +0,0 @@ - - - - - - - - -
-
- BLT Logo -

Welcome to {{ site_name }}

-
-
-

Hi there!

-

You've been invited by {{ sender_username }} to join our community at {{ site_name }}.

-

We're excited to have you onboard and explore the vast opportunities to find and submit bugs, earn rewards, and be part of our thriving community.

- Join Now -
- -
- - diff --git a/website/templates/invite_friend.html b/website/templates/invite_friend.html index 88d4ce0f8..400ea868d 100644 --- a/website/templates/invite_friend.html +++ b/website/templates/invite_friend.html @@ -1,29 +1,26 @@ {% extends "base.html" %} {% block content %} -
- {% csrf_token %} - {% for error in form.non_field_errors %} - - {% endfor %} - -
- - {{ form.recipient }} - {% if form.recipient.errors %} -

{{ form.recipient.errors }}

- {% endif %} -
- -
- -

- Invite a friend to join the site and earn 2 points! -

-
-
+
+ +
+ + + or + Send Mail +
+
+

+ Invite a friend to join the site and earn 2 points! +

+
+
+ {% endblock %} diff --git a/website/views.py b/website/views.py index e20d04682..337c397cb 100644 --- a/website/views.py +++ b/website/views.py @@ -81,7 +81,7 @@ Company, IssueScreenshot ) -from .forms import FormInviteFriend, UserProfileForm, HuntForm, CaptchaForm, QuickIssueForm +from .forms import UserProfileForm, HuntForm, CaptchaForm, QuickIssueForm from decimal import Decimal from django.conf import settings @@ -2062,52 +2062,6 @@ def assign_issue_to_user(request, user, **kwargs): assigner.request = request assigner.process_issue(user, issue, created, domain) - -class CreateInviteFriend(CreateView): - template_name = "invite_friend.html" - model = InviteFriend - form_class = FormInviteFriend - success_url = reverse_lazy("invite_friend") - - def form_valid(self, form): - recipient_email = form.cleaned_data["recipient"] - - # Check if a user with the given email already exists - if User.objects.filter(email=recipient_email).exists(): - messages.error(self.request, "User with this email already exists.") - return super().form_invalid(form) - try: - invite = InviteFriend.objects.get(recipient=recipient_email) - time_difference = now() - invite.sent - if time_difference.days < 1: # 1 day - messages.error(self.request, "An invitation has already been sent to this email recently.") - return super().form_invalid(form) - except InviteFriend.DoesNotExist: - pass - instance = form.save(commit=False) - instance.sender = self.request.user - instance.save() - - site = get_current_site(self.request) - referral_link = f"https://{site.domain}/referral/?ref={instance.referral_code}" - - context = { - 'site_name': site.name, - 'sender_username': self.request.user.username, - 'referral_link': referral_link, - } - html_content = render_to_string('email/invitation_email.html', context) - text_content = strip_tags(html_content) - subject = f"Invitation to {site.name} from {self.request.user.username}" - from_email = settings.DEFAULT_FROM_EMAIL - to = [instance.recipient] - msg = EmailMultiAlternatives(subject, text_content, from_email, to) - msg.attach_alternative(html_content, "text/html") - msg.send() - - messages.success(self.request, "An email has been sent to your friend. Keep inviting your friends and earn rewards!") - return super().form_valid(form) - @login_required(login_url="/accounts/login") def follow_user(request, user): if request.method == "GET": @@ -3750,15 +3704,11 @@ def handle_user_signup(request, user, **kwargs): if referral_token: try: invite = InviteFriend.objects.get(referral_code=referral_token) - # also check the signup mail is same as the invite mail or not - if (not invite.signup_completed) and (invite.recipient == user.email): - # Mark the signup as completed - invite.signup_completed = True - invite.save() - # Reward the user who sent the invite with points - reward_sender_with_points(invite.sender) - # Clear the referral token from the session - del request.session['ref'] + invite.recipients.add(user) + invite.point_by_referral += 2 + invite.save() + reward_sender_with_points(invite.sender) + del request.session['ref'] except InviteFriend.DoesNotExist: pass @@ -3780,6 +3730,19 @@ def referral_signup(request): return redirect('account_signup') return redirect('account_signup') + +def invite_friend(request): + current_site = get_current_site(request) + referral_code, created = InviteFriend.objects.get_or_create(sender=request.user) + referral_link = f"https://{current_site.domain}/referral/?ref={referral_code.referral_code}" + context = { + 'referral_link': referral_link, + } + return render(request, 'invite_friend.html', context) + + + + # class CreateIssue(CronJobBase): # RUN_EVERY_MINS = 1