Skip to content

Commit

Permalink
Merge pull request #105 from NFM-Studios/team-invite-new
Browse files Browse the repository at this point in the history
Rewrite team invites - Closes #103
  • Loading branch information
techlover1 committed Aug 27, 2020
2 parents dd3efd6 + b55159d commit d5e854a
Show file tree
Hide file tree
Showing 10 changed files with 304 additions and 223 deletions.
220 changes: 112 additions & 108 deletions matches/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.shortcuts import render, redirect
from django.template.loader import render_to_string
from django.views.generic import DetailView, CreateView, View

from django.db.models import Q
from matches.models import Match, MatchReport, MatchDispute, MapPoolChoice
from teams.models import Team, TeamInvite
from .forms import MatchReportCreateFormGet, MatchReportCreateFormPost, DisputeCreateForm
Expand All @@ -23,10 +23,10 @@ def get(self, request, **kwargs):
class MatchList(View):

def get(self, request):
invites = TeamInvite.objects.filter(hasPerms=True, user_id=request.user.id)
team = list(Team.objects.filter(id__in=invites.values_list('team', flat=True)))
matches_away = Match.objects.filter(awayteam__in=team)
matches_home = Match.objects.filter(hometeam__in=team)
teams = Team.objects.filter(
Q(captains__exact=request.user) | Q(founder=request.user) | Q(players__exact=request.user))
matches_away = Match.objects.filter(awayteam__in=teams)
matches_home = Match.objects.filter(hometeam__in=teams)
matches = matches_away | matches_home
return render(request, 'matches/matches_list.html', {'matches': matches})

Expand Down Expand Up @@ -77,128 +77,132 @@ def post(self, request, pk):
if not match.bye_2 and not match.bye_1:
team1 = Team.objects.get(id=match.hometeam_id)
team2 = Team.objects.get(id=match.awayteam_id)
team1_reporters = TeamInvite.objects.filter(team=team1, hasPerms=True)
team2_reporters = TeamInvite.objects.filter(team=team2, hasPerms=True)

if TeamInvite.objects.filter(user=self.request.user, team=team1).exists():
reporter_team = TeamInvite.objects.get(user=self.request.user, team=team1)
elif TeamInvite.objects.filter(user=self.request.user, team=team2).exists():
reporter_team = TeamInvite.objects.get(user=self.request.user, team=team2)
team1_reporters = team1.captains
team2_reporters = team2.captains

one_perms = False
two_perms = False
if self.request.user in team1.captains or self.request.user == team1.founder:
one_perms = True
elif self.request.user in team2.captains or self.request.user == team2.founder:
two_perms = True
else:
messages.error(request, message="You aren't a part of the teams in this match")
if match.type == 'w':
return redirect('wagers:list')
return redirect('matches:detail', pk=pk)

if MatchReport.objects.filter(match=match.id,
reporting_team=team1).exists() and reporter_team.id == team1.id:
reporting_team=team1).exists() and one_perms:
messages.error(request, "Your team has already reported this match")
if match.type == 'w':
return redirect('wagers:list')
return redirect('matches:detail', pk=pk)
elif MatchReport.objects.filter(match=match.id,
reporting_team=team2).exists() and reporter_team.id == team2.id:
reporting_team=team2).exists() and two_perms:
messages.error(request, "Your team has already reported this match")
if match.type == 'w':
return redirect('wagers:list')
return redirect('matches:detail', pk=pk)
elif match.bye_1:
messages.error(request, 'There is only one team in this match, reporting is unnecessary')
return redirect('matches:list')

elif match.bye_2:
messages.error(request, 'There are no teams in this match')
return redirect('matches:list')
else:
if reporter_team in team1_reporters or reporter_team in team2_reporters:
report.match = match
report.reporting_team = reporter_team.team
reported_team = Team.objects.get(id=form.data['reported_winner'])
report.reported_winner = reported_team
if reporter_team.team == team1:
match.team1reported = True
match.team1reportedwinner = report.reported_winner
match.team1reportedwinner_id = report.reported_winner.id
elif reporter_team.team == team2:
match.team2reported = True
match.team2reportedwinner = report.reported_winner
match.team2reportedwinner_id = report.reported_winner.id
else:
messages.error(self.request, "Something went wrong (this shouldn't be seen)")
return redirect('singletournaments:list')
match.save()
report.save()
# if reporter_team in team1_reporters or reporter_team in team2_reporters:
report.match = match
reported_team = Team.objects.get(id=form.data['reported_winner'])
report.reported_winner = reported_team
if one_perms:
report.reporting_team = team1
match.team1reported = True
match.team1reportedwinner = report.reported_winner
match.team1reportedwinner_id = report.reported_winner.id

elif two_perms:
report.reporting_team = team2
match.team2reported = True
match.team2reportedwinner = report.reported_winner
match.team2reportedwinner_id = report.reported_winner.id
else:
messages.error(request, "ERROR: Could not verify the team that you're on")
return redirect('matches:detail', pk=match.id)

match.save()
report.save()
if match.team1reported and match.team2reported:
reports = MatchReport.objects.filter(match_id=match.id)
report1 = MatchReport.objects.get(reporting_team=team1, match_id=match.id)
report2 = MatchReport.objects.get(reporting_team=team2, match_id=match.id)
if reports[0].reported_winner != reports[1].reported_winner:
dispute = MatchDispute(id=match.id, match=match, team1=team1, team2=team2,
team1origreporter=report1.reporting_user,
team2origreporter=report2.reporting_user)
dispute.save()
match.disputed = True
match.save()

for i in [report1.reporting_user, report2.reporting_user]:
if i.user.email_enabled:
current_site = get_current_site(request)
mail_subject = settings.SITE_NAME + ' match disputed!'
message = render_to_string('matches/dispute_email.html', {
'user': i.username,
'site': settings.SITE_NAME,
'domain': current_site.domain,
'pk': dispute.pk
})
to_email = i.email
email = EmailMessage(
mail_subject, message, from_email=settings.FROM_EMAIL, to=[to_email]
)
email.send()

messages.warning(self.request,
"Both teams have reported different winners; a dispute has been created")
return redirect('matches:dispute', pk=dispute.pk)
if match.team1reported:
# team 1 reported
if match.team1reportedwinner == team2:
# team1 is reporting that team2 won
# declare team2 as winner
match.winner = team2
match.loser = team1
match.save()
elif match.team1reportedwinner == team1:
# have to wait for the other team to confirm
pass
elif match.team2reported:
if match.team2reportedwinner == team1:
# team 1 wins
match.winner = team1
match.loser = team2
match.save()
elif match.team2reportedwinner == team2:
pass
if match.team1reported and match.team2reported:
reports = MatchReport.objects.filter(match_id=match.id)
report1 = MatchReport.objects.get(reporting_team=team1, match_id=match.id)
report2 = MatchReport.objects.get(reporting_team=team2, match_id=match.id)
if reports[0].reported_winner != reports[1].reported_winner:
dispute = MatchDispute(id=match.id, match=match, team1=team1, team2=team2,
team1origreporter=report1.reporting_user,
team2origreporter=report2.reporting_user)
dispute.save()
match.disputed = True
if match.team2reportedwinner == team2 and match.team1reportedwinner == team2:
match.winner = team2
match.loser = team1
match.save()

for i in [report1.reporting_user, report2.reporting_user]:
if i.user.email_enabled:
current_site = get_current_site(request)
mail_subject = settings.SITE_NAME + ' match disputed!'
message = render_to_string('matches/dispute_email.html', {
'user': i.username,
'site': settings.SITE_NAME,
'domain': current_site.domain,
'pk': dispute.pk
})
to_email = i.email
email = EmailMessage(
mail_subject, message, from_email=settings.FROM_EMAIL, to=[to_email]
)
email.send()

messages.warning(self.request,
"Both teams have reported different winners; a dispute has been created")
return redirect('matches:dispute', pk=dispute.pk)
if match.team1reported:
# team 1 reported
if match.team1reportedwinner == team2:
# team1 is reporting that team2 won
# declare team2 as winner
match.winner = team2
match.loser = team1
match.save()
elif match.team1reportedwinner == team1:
# have to wait for the other team to confirm
pass
elif match.team2reported:
if match.team2reportedwinner == team1:
# team 1 wins
match.winner = team1
match.loser = team2
match.save()
elif match.team2reportedwinner == team2:
pass
if match.team1reported and match.team2reported:
if match.team2reportedwinner == team2 and match.team1reportedwinner == team2:
match.winner = team2
match.loser = team1
match.save()
elif match.team2reportedwinner == team1 and match.team1reportedwinner == team1:
match.winner = team1
match.loser = team2
match.save()
# self.success_url = reverse('matches:detail', args=[match.id])
messages.success(self.request, 'Your Report has been successfully submitted')
if match.type == 'w':
return redirect('wagers:list')
return redirect('matches:detail', pk=pk)
else:
messages.error(self.request, "You don't have permissions to report on this match")
if match.type == 'w':
return redirect('wagers:list')
return redirect('singletournaments:list')
# else:
# messages.error(request, "A report has already been created for this match")
# return redirect('matches:detail', pk=pk)
elif match.bye_1:
messages.error(request, 'There is only one team in this match, reporting is unnecessary')
return redirect('matches:list')
elif match.bye_2:
messages.error(request, 'There are no teams in this match')
return redirect('matches:list')
elif match.team2reportedwinner == team1 and match.team1reportedwinner == team1:
match.winner = team1
match.loser = team2
match.save()
# self.success_url = reverse('matches:detail', args=[match.id])
messages.success(self.request, 'Your Report has been successfully submitted')
if match.type == 'w':
return redirect('wagers:list')
return redirect('matches:detail', pk=pk)
# if match.type == 'w':
# return redirect('wagers:list')
# return redirect('singletournaments:list')
# else:
# messages.error(request, "A report has already been created for this match")
# return redirect('matches:detail', pk=pk)


class MatchDisputeReportCreateView(CreateView):
Expand Down
14 changes: 12 additions & 2 deletions project-templates/staff/teams/team_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
<td class="">Founder</td>
<td><a href="{% url 'profiles:profile' urlusername=team.founder %}">{{ team.founder }}</a></td>
</tr>
<tr>
<td class="">Captains</td>
{% for captain in captains %}
<td><a href="{% url 'profiles:profile' urlusername=captain.name %}">{{ captain.name }}</a>
{% endfor %}
</td>
</tr>
<tr>
<td>Created</td>
<td>{{ team.created }}</td>
Expand Down Expand Up @@ -67,8 +74,11 @@
</tr>
</table>

<a href="{% url 'staff:remove_user' pk %}" onclick="return confirm('Are you sure you want to remove these users from the team')" class="btn btn-danger">Remove users from team</a>
<a href="{% url 'staff:remove_user' pk %}"
onclick="return confirm('Are you sure you want to remove these users from the team')" class="btn btn-danger">Remove
users from team</a>
<br/>
<a href="{% url 'staff:delete_team' pk %}" onclick="return confirm('Are you sure you want to delete this team?')" class="btn btn-danger">Delete Team</a>
<a href="{% url 'staff:delete_team' pk %}"
onclick="return confirm('Are you sure you want to delete this team?')" class="btn btn-danger">Delete Team</a>
</div>
{% endblock %}
16 changes: 14 additions & 2 deletions project-templates/teams/team_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
<td class="">Founder</td>
<td><a href="{% url 'profiles:profile' urlusername=team.founder %}">{{ team.founder }}</a></td>
</tr>
<tr>
<td class="">Captains</td>
{% for captain in captains %}
<td><a href="{% url 'profiles:profile' urlusername=captain.user %}">{{ captain.name }}</a></td>
{% endfor %}
</tr>
<tr>
<td>Created</td>
<td>{{ team.created }}</td>
Expand Down Expand Up @@ -69,7 +75,7 @@
</tr>
<tr>
<td>Players</td>
{% for profile in up %}
{% for profile in players %}

<tr>
<td valign="middle"><img src="{{ profile.country.flag }}"> <a
Expand All @@ -78,7 +84,13 @@
NO{% endif %}</td>
</tr>
{% endfor %}
</tr>
{% if players.count == 0 %}
<tr>
<td>
There are no players on the team yet
</td>
</tr>
{% endif %}
</table>

<a href="{% url 'teams:leave' pk=pk %}">Leave team</a><br>
Expand Down
2 changes: 1 addition & 1 deletion project-templates/teams/team_invite_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<th scope="row">Invite ID</th>
<th>From</th>
<th>Team Name</th>
<th>Team Role</th>
<th>Captain Role</th>
<th>Active?</th>
</thead>
</tr>
Expand Down
8 changes: 6 additions & 2 deletions staff/views/teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ def teams_detail(request, pk):
return render(request, 'staff/permissiondenied.html')
else:
team = Team.objects.get(id=pk)
players = TeamInvite.objects.filter(team=team, accepted=True).order_by('id')
return render(request, 'staff/teams/team_detail.html', {'team': team, 'players': players, 'pk': pk})
players = team.players.all()
captains = team.captain.all()

return render(request, 'staff/teams/team_detail.html',
{'team': team, 'players': players, 'captains': captains, 'pk': pk})


def create_team(request):
Expand Down Expand Up @@ -61,6 +64,7 @@ def delete_team(request, pk):
return redirect('staff:teamindex')


#TODO remove TeamInvite object
def remove_user(request, pk):
user = UserProfile.objects.get(user__username=request.user.username)
allowed = ['superadmin', 'admin']
Expand Down
Loading

0 comments on commit d5e854a

Please sign in to comment.