Skip to content

Commit

Permalink
Merge pull request #101 from NFM-Studios/notifications
Browse files Browse the repository at this point in the history
Notifications - closes #21
  • Loading branch information
mikemaddem authored Aug 28, 2020
2 parents 012cfa0 + 864e878 commit 6803dff
Show file tree
Hide file tree
Showing 22 changed files with 554 additions and 238 deletions.
230 changes: 122 additions & 108 deletions matches/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
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
from profiles.models import Notification, UserProfile
import datetime


class MapPoolDetail(DetailView):
Expand All @@ -23,10 +25,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 +79,140 @@ 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]:
test = Notification(title="A match you're playing in has been disputed")
test.link = 'matches:detail'
test.pk1 = match.pk
test.datetime = datetime.datetime.now()
test.save()
userprofile = UserProfile.objects.get(user=i.user)
userprofile.notifications.add(test)
userprofile.save()
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(fail_silently=True)

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
2 changes: 1 addition & 1 deletion olly/base_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,4 @@
LOGIN_REDIRECT_URL = '/'
LOGIN_URL = '/login/'

SITE_VERSION = "0.9.0"
SITE_VERSION = "0.9.1"
32 changes: 32 additions & 0 deletions profiles/migrations/0012_auto_20200813_1639.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 2.2.14 on 2020-08-13 20:39

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('profiles', '0011_userprofile_activisionid'),
]

operations = [
migrations.CreateModel(
name='Notification',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('datetime', models.DateTimeField(auto_created=True)),
('title', models.CharField(blank=True, max_length=255, null=True)),
('description', models.TextField(default='No description given')),
('sender', models.CharField(default='System', max_length=255)),
('type', models.CharField(choices=[('match', 1), ('tournament', 2), ('league', 3), ('team', 4), ('support', 5), ('news', 6), ('general', 7), ('store', 8)], default='general', max_length=255)),
('link', models.CharField(max_length=255)),
('read', models.BooleanField(default=False)),
('seen', models.BooleanField(default=False)),
],
),
migrations.AddField(
model_name='userprofile',
name='notifications',
field=models.ManyToManyField(blank=True, related_name='user_notifications', to='profiles.Notification'),
),
]
18 changes: 18 additions & 0 deletions profiles/migrations/0013_notification_pk1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.15 on 2020-08-27 22:44

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('profiles', '0012_auto_20200813_1639'),
]

operations = [
migrations.AddField(
model_name='notification',
name='pk1',
field=models.IntegerField(default=0),
),
]
28 changes: 28 additions & 0 deletions profiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@
from django.db.models.signals import post_save
from django_countries.fields import CountryField

NOTIFICATION_TYPES = [
('match', 1),
('tournament', 2),
('league', 3),
('team', 4),
('support', 5),
('news', 6),
('general', 7),
('store', 8),
]


class Notification(models.Model):
title = models.CharField(max_length=255, null=True, blank=True)
description = models.TextField(default="No description given")
# set the default sender of a notification as "System"
sender = models.CharField(max_length=255, default="System")
type = models.CharField(choices=NOTIFICATION_TYPES, default='general', max_length=255)
datetime = models.DateTimeField(auto_created=True)
link = models.CharField(max_length=255)
pk1 = models.IntegerField(default=0)
# has the user marked the notification as read? default to false
read = models.BooleanField(default=False)
# has the user visited the notification list page since the notification was generated? used for stats
seen = models.BooleanField(default=False)


class UserProfile(models.Model):
def __str__(self):
Expand All @@ -12,6 +38,8 @@ def __str__(self):
user = models.OneToOneField(User, related_name='user', on_delete=models.CASCADE)
# xp they have from winning events
xp = models.PositiveSmallIntegerField(default=0)
# all notifications associated with this user
notifications = models.ManyToManyField(Notification, related_name='user_notifications', blank=True)
# credits they own from purchasing things in the store
credits = models.PositiveSmallIntegerField(default=0)
passes = models.PositiveSmallIntegerField(default=0)
Expand Down
3 changes: 3 additions & 0 deletions profiles/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@
path('users/search/', login_required(views.searchusers), name='searchusers'),
path('user/<str:urlusername>/', views.profile, name='profile'),
path('leaderboards/', views.LeaderboardView.as_view(), name='leaderboard'),
path('notifications/', login_required(views.notifications_list), name='notifications'),
path('notifications/<int:pk>/read/', login_required(views.notification_read), name='notification_read'),
path('notifications/<int:pk>/unread/', login_required(views.notification_unread), name='notification_unread'),
]
Loading

0 comments on commit 6803dff

Please sign in to comment.