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

Embedathon team registration #48

Merged
merged 2 commits into from
Dec 15, 2023
Merged
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
4 changes: 4 additions & 0 deletions corpus/embedathon/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from django.contrib import admin
from embedathon.models import EmbedathonUser
from embedathon.models import Invite
from embedathon.models import Team

# Register your models here.

admin.site.register(EmbedathonUser)
admin.site.register(Team)
admin.site.register(Invite)
14 changes: 14 additions & 0 deletions corpus/embedathon/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from embedathon.models import EmbedathonUser
from embedathon.models import Invite
from embedathon.models import Team

from corpus.forms import CorpusModelForm

Expand All @@ -16,3 +18,15 @@ class Meta:
"ieee_membership_no",
"cass_member",
]


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
@@ -0,0 +1,71 @@
# Generated by Django 4.2.4 on 2023-12-11 13:50
import django.db.models.deletion
from django.db import migrations
from django.db import models


class Migration(migrations.Migration):

dependencies = [
("embedathon", "0001_initial"),
]

operations = [
migrations.CreateModel(
name="Team",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("team_name", models.CharField(max_length=200)),
(
"team_leader",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="leader",
to="embedathon.embedathonuser",
),
),
],
),
migrations.CreateModel(
name="Invite",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("invite_email", models.EmailField(max_length=254)),
(
"inviting_team",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="invite_to_team",
to="embedathon.team",
),
),
],
),
migrations.AddField(
model_name="embedathonuser",
name="team",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="team",
to="embedathon.team",
),
),
]
23 changes: 23 additions & 0 deletions corpus/embedathon/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,29 @@ class EmbedathonUser(models.Model):
ieee_member = models.BooleanField(default=False)
ieee_membership_no = models.BigIntegerField(blank=True, null=True)
cass_member = models.BooleanField(default=False)
team = models.ForeignKey(
"Team", on_delete=models.CASCADE, related_name="team", blank=True, null=True
)

def __str__(self):
return self.user.__str__()


class Team(models.Model):
team_name = models.CharField(max_length=200, blank=False, null=False)
team_leader = models.ForeignKey(
EmbedathonUser, on_delete=models.CASCADE, related_name="leader"
)

def __str__(self):
return self.team_name


class Invite(models.Model):
inviting_team = models.ForeignKey(
Team, on_delete=models.CASCADE, related_name="invite_to_team"
)
invite_email = models.EmailField(blank=False, null=False)

def __str__(self):
return self.invite_email
8 changes: 8 additions & 0 deletions corpus/embedathon/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@
urlpatterns = [
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"
),
]
110 changes: 109 additions & 1 deletion corpus/embedathon/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
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

from corpus.decorators import module_enabled

Expand All @@ -18,13 +21,37 @@
@login_required
@module_enabled(module_name="embedathon")
def index(request):
args = {}
try:
embedathon_user = EmbedathonUser.objects.get(user=request.user)
args["embedathon_user"] = embedathon_user
except EmbedathonUser.DoesNotExist:
messages.error(request, "Please register for Embedathon first!")
return redirect("embedathon_register")

args = {"embedathon_user": embedathon_user}
# Check if user is part of a team.
if embedathon_user.team is not None:
args["in_team"] = True

team = embedathon_user.team
members = EmbedathonUser.objects.filter(team=team)

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

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 @@ -71,3 +98,84 @@ def register(request):
args = {"form": form}

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


@login_required
@module_enabled(module_name="embedathon")
def create_team(request):
if request.method == "POST":
form = TeamCreationForm(request.POST)
if form.is_valid():
team = form.save(commit=False)
embedathon_user = EmbedathonUser.objects.get(user=request.user)
team.team_leader = embedathon_user
team.save()

embedathon_user.team = team
embedathon_user.save()

messages.success(request, "Team created successfully!")
return redirect("embedathon_index")
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")
Loading