Skip to content

Commit

Permalink
Added invite system
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudhprabhakaran3 committed Dec 15, 2023
1 parent 0a6380c commit 36ae3d8
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 20 deletions.
7 changes: 7 additions & 0 deletions corpus/embedathon/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from embedathon.models import EmbedathonUser
from embedathon.models import Invite
from embedathon.models import Team

from corpus.forms import CorpusModelForm
Expand All @@ -23,3 +24,9 @@ class TeamCreationForm(CorpusModelForm):
class Meta:
model = Team
fields = ["team_name"]


class InviteForm(CorpusModelForm):
class Meta:
model = Invite
fields = ["invite_email"]
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# Generated by Django 4.2.4 on 2023-12-11 13:19
# Generated by Django 4.2.4 on 2023-12-11 13:50
import django.db.models.deletion
from django.conf import settings
from django.db import migrations
from django.db import models


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("embedathon", "0001_initial"),
]

Expand All @@ -31,7 +29,7 @@ class Migration(migrations.Migration):
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="leader",
to=settings.AUTH_USER_MODEL,
to="embedathon.embedathonuser",
),
),
],
Expand Down
2 changes: 1 addition & 1 deletion corpus/embedathon/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __str__(self):
class Team(models.Model):
team_name = models.CharField(max_length=200, blank=False, null=False)
team_leader = models.ForeignKey(
User, on_delete=models.CASCADE, related_name="leader"
EmbedathonUser, on_delete=models.CASCADE, related_name="leader"
)

def __str__(self):
Expand Down
7 changes: 7 additions & 0 deletions corpus/embedathon/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@
path("", views.index, name="embedathon_index"),
path("register", views.register, name="embedathon_register"),
path("create_team", views.create_team, name="embedathon_create_team"),
path("create_invite", views.create_invite, name="embedathon_create_invite"),
path(
"accept_invite/<int:pk>", views.accept_invite, name="embedathon_accept_invite"
),
path(
"delete_invite/<int:pk>", views.delete_invite, name="embedathon_delete_invite"
),
]
73 changes: 69 additions & 4 deletions corpus/embedathon/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.shortcuts import redirect
from django.shortcuts import render
from embedathon.forms import EmbedathonForm
from embedathon.forms import InviteForm
from embedathon.forms import TeamCreationForm
from embedathon.models import EmbedathonUser
from embedathon.models import Invite
Expand Down Expand Up @@ -34,19 +35,23 @@ def index(request):

team = embedathon_user.team
members = EmbedathonUser.objects.filter(team=team)
invites = Invite.objects.filter(invite_email=embedathon_user.user.email)

args["team"] = team
args["members"] = members
args["invites"] = invites

if team.team_leader == embedathon_user:
args["is_leader"] = True
invites = Invite.objects.filter(inviting_team=team)
args["invites_from_team"] = invites
args["invite_form"] = InviteForm()
else:
args["is_leader"] = False
else:
args["in_team"] = False
args["is_leader"] = False
args["team_creation_form"] = TeamCreationForm()
invites = Invite.objects.filter(invite_email=embedathon_user.user.email)
args["invites_for_user"] = invites

return render(request, "embedathon/index.html", args)

Expand Down Expand Up @@ -102,10 +107,10 @@ def create_team(request):
form = TeamCreationForm(request.POST)
if form.is_valid():
team = form.save(commit=False)
team.team_leader = request.user
embedathon_user = EmbedathonUser.objects.get(user=request.user)
team.team_leader = embedathon_user
team.save()

embedathon_user = EmbedathonUser.objects.get(user=request.user)
embedathon_user.team = team
embedathon_user.save()

Expand All @@ -114,3 +119,63 @@ def create_team(request):
else:
messages.error(request, "Illegal Request")
return redirect("embedathon_index")


@login_required
@module_enabled(module_name="embedathon")
def create_invite(request):
embedathon_user = EmbedathonUser.objects.get(user=request.user)
if request.method == "POST":
form = InviteForm(request.POST)
if form.is_valid():
if request.user.email == form.cleaned_data["invite_email"]:
messages.error(request, "You cannot invite yourself!")
return redirect("embedathon_index")

try:
invite = Invite.objects.get(
inviting_team=embedathon_user.team,
invite_email=form.cleaned_data["invite_email"],
)
messages.error(request, "Invite has already been sent!")
return redirect("embedathon_index")
except Invite.DoesNotExist:
pass

invite = form.save(commit=False)
inviting_team = embedathon_user.team
invite.inviting_team = inviting_team
invite.save()

messages.success(request, "Invite sent!")
return redirect("embedathon_index")
messages.error(request, "Illegal Request")
return redirect("embedathon_index")


@login_required
@module_enabled(module_name="embedathon")
def accept_invite(request, pk):
invite = Invite.objects.get(pk=pk)
if request.user.email != invite.invite_email:
messages.error(request, "Illegal request")
return redirect("embedathon_index")

embedathon_user = EmbedathonUser.objects.get(user=request.user)
embedathon_user.team = invite.inviting_team
embedathon_user.save()

Invite.objects.filter(invite_email=request.user.email).delete()

messages.success(request, "Invite accepted!")
return redirect("embedathon_index")


@login_required
@module_enabled(module_name="embedathon")
def delete_invite(request, pk):
invite = Invite.objects.get(pk=pk)
invite.delete()

messages.success(request, "Invite deleted!")
return redirect("embedathon_index")
100 changes: 89 additions & 11 deletions corpus/templates/embedathon/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,98 @@ <h1>Welcome to Embedathon!</h1>
<div class="card">
<div class="card-body">
{% if in_team %}
<h2>You are already in a team.</h2>
<h3 class="card-title">Team: {{ team.team_name }}</h3>
<div>
<h4>Team Members</h4>
<ul class="list-disc">
{% for member in members %}
<li>
{{ member.user }}
</li>
{% endfor %}
</ul>
</div>
{% if is_leader %}
<div class="flex flex-col w-full">
<div class="divider divider-primary">OR</div>
</div>
<div>
<h4>Active Invites</h4>
{% if invites_from_team %}
<table class="table">
<thead>
<tr>
<th></th>
<th>Email Address</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for invite in invites_from_team %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ invite.invite_email }}</td>
<td>
<a href="{% url 'embedathon_delete_invite' pk=invite.pk %}"
class="btn btn-sm btn-outline btn-error m-1">Revoke</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>You have no active invites.</p>
{% endif %}
<h4>Send Invite</h4>
<form method="post" action="{% url 'embedathon_create_invite' %}">
{% csrf_token %}
<div class="w-full my-2">
<label for="{{ invite_form.invite_email.id_for_label }}">Email
Address</label>
{{ invite_form.invite_email }}
<p class="text-sm font-bold">Please enter the email that your teammate will
be registering with on Corpus.</p>
{% if invite_form.invite_email.errors %}
<div class="my-1">
<div role="alert" class="alert alert-error">
{{ invite_form.invite_email.errors }}
</div>
</div>
{% endif %}
</div>
<div class="card-actions mt-5">
<button class="btn btn-primary btn-block">Send Invite</button>
</div>
</form>
</div>
{% endif %}
{% else %}
<div>
<h3>Current Invites</h3>
{% if invites %}
<ul>
{% for invite in invites %}
<li>
<a href="#">
{{ invite.team.name }}
</a>
</li>
<h3 class="card-title">Current Invites</h3>
{% if invites_for_user %}
<table class="table">
<thead>
<tr>
<th></th>
<th>Team Name</th>
<th>Invited By</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for invite in invites_for_user %}
<td>{{ forloop.counter }}</td>
<td>{{ invite.inviting_team.team_name }}</td>
<td>{{ invite.inviting_team.team_leader }}</td>
<td>
<a href="{% url 'embedathon_accept_invite' pk=invite.pk %}"
class="btn btn-sm btn-outline btn-success m-1">Accept</a>
<a href="{% url 'embedathon_delete_invite' pk=invite.pk %}"
class="btn btn-sm btn-outline btn-error m-1">Reject</a>
</td>
{% endfor %}
</ul>
</tbody>
</table>
{% else %}
<p>You have no invites.</p>
{% endif %}
Expand Down

0 comments on commit 36ae3d8

Please sign in to comment.