Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 7f4e63c1fc46ce87acbf14040388d6301b2e9de2 1 parent 3389d04
Jon Rohan authored
Showing with 246 additions and 0 deletions.
  1. +4 −0 README
  2. +28 −0 admin.py
  3. +5 −0 const.py
  4. +124 −0 models.py
  5. +9 −0 urls.py
  6. +76 −0 views.py
View
4 README
@@ -0,0 +1,4 @@
+The code we use to track ping pong games.
+
+This is far from plug and play, just posting the views, models, urls, admin,
+and const we use
View
28 admin.py
@@ -0,0 +1,28 @@
+from django.contrib import admin
+
+from models import Employee, PingPongMatch, PingPongGame, PingPongRecords
+
+class EmployeeAdmin(admin.ModelAdmin):
+ list_display = ["user","date_joined"]
+ fieldsets = [
+ ("Profile", {
+ "fields": ("user", "nick_name", "title","teams","bio","sites","date_joined","birthday"),
+ }),
+ ("Optional", {
+ "fields": ("twitter","facebook","website","skype","aim"),
+ })
+ ]
+
+class PingPongMatchAdmin(admin.ModelAdmin):
+ pass
+
+class PingPongGameAdmin(admin.ModelAdmin):
+ pass
+
+class PingPongRecordsAdmin(admin.ModelAdmin):
+ pass
+
+admin.site.register(Employee, EmployeeAdmin)
+admin.site.register(PingPongMatch, PingPongMatchAdmin)
+admin.site.register(PingPongGame, PingPongGameAdmin)
+admin.site.register(PingPongRecords, PingPongRecordsAdmin)
View
5 const.py
@@ -0,0 +1,5 @@
+
+
+
+PING_PONG_BEST_OF = 3
+PING_PONG_FIRST_TO = 11
View
124 models.py
@@ -0,0 +1,124 @@
+import datetime
+
+from django.contrib.sites.models import Site
+from django.contrib.auth.models import User as DjangoUser
+from django.contrib.auth.models import Group
+from django.db.models import Q
+from django.db import models
+
+class Employee(models.Model):
+ user = models.OneToOneField(DjangoUser,limit_choices_to={'is_staff': True},help_text="Only is_staff django users in this list")
+
+ twitter = models.URLField(blank=True,verify_exists=False,help_text="ie. http://twitter.com/mg")
+
+ facebook = models.URLField(blank=True,verify_exists=False,help_text="ie. http://facebook.com/mg")
+
+ website = models.URLField(blank=True,verify_exists=False)
+
+ skype = models.CharField(max_length=100,blank=True)
+
+ aim = models.CharField(max_length=100,blank=True)
+
+ title = models.CharField(max_length=100)
+
+ nick_name = models.CharField(max_length=100,blank=True,default="")
+
+ bio = models.TextField(blank=True)
+
+ teams = models.ManyToManyField(Group)
+
+ birthday = models.DateField(blank=True, null=True)
+
+ date_joined = models.DateTimeField()
+
+ def __unicode__(self):
+ return unicode(self.user)
+
+ def is_new(self):
+ return (datetime.datetime.now() - self.date_joined) < datetime.timedelta(90)
+
+ def is_birthday(self):
+ if not self.birthday:
+ return False
+ today = datetime.datetime.utcnow()
+
+ return today.day == self.birthday.day and today.month == self.birthday.month
+
+ def full_name(self):
+ return unicode(self.user.first_name + " " + self.user.last_name)
+
+ def is_social(self):
+ return (self.twitter or self.facebook or self.website or self.skype or self.aim)
+
+class PingPongMatch(models.Model):
+
+ player_1 = models.ForeignKey(Employee,related_name="player_1")
+
+ player_2 = models.ForeignKey(Employee,related_name="player_2")
+
+ date = models.DateTimeField(auto_now_add=True)
+
+ def winner(self):
+
+ player1_games = 0
+ player2_games = 0
+
+ games = PingPongGame.objects.filter(match=self)
+ for game in games:
+ if game.player1_points > game.player2_points:
+ player1_games = player1_games + 1
+ else:
+ player2_games = player2_games + 1
+
+ if player1_games > player2_games:
+ return self.player_1
+
+ return self.player_2
+
+
+ def __unicode__(self):
+ return '%s vs. %s' % (self.player_1.full_name(),
+ self.player_2.full_name())
+
+class PingPongGame(models.Model):
+
+ player1_points = models.IntegerField(default=0)
+
+ player2_points = models.IntegerField(default=0)
+ match = models.ForeignKey(PingPongMatch, related_name='games')
+
+ def save(self, *args, **kwargs):
+ """
+ We want to add to PingPongRecords everytime a game is saved.
+ """
+ super(PingPongGame, self).save(*args, **kwargs)
+ player_1_record, status = PingPongRecords.objects.get_or_create(player=self.match.player_1)
+ player_2_record, status = PingPongRecords.objects.get_or_create(player=self.match.player_2)
+ if int(self.player1_points) > int(self.player2_points):
+ player_1_record.game_wins = player_1_record.game_wins + 1 if player_1_record.game_wins else 1
+ player_2_record.game_losses = player_2_record.game_losses + 1 if player_2_record.game_losses else 1
+ else:
+ player_2_record.game_wins = player_2_record.game_wins + 1 if player_2_record.game_wins else 1
+ player_1_record.game_losses = player_1_record.game_losses + 1 if player_1_record.game_losses else 1
+ player_1_record.games_played = player_1_record.games_played + 1
+ player_2_record.games_played = player_2_record.games_played + 1
+ player_1_record.matches.add(self.match)
+ player_2_record.matches.add(self.match)
+ player_1_record.save()
+ player_2_record.save()
+
+ def __unicode__(self):
+ return '%s, %s' % (self.player1_points, self.player2_points)
+
+class PingPongRecords(models.Model):
+ player = models.ForeignKey(Employee)
+ game_wins = models.IntegerField(default=0, blank=True, null=True)
+ game_losses = models.IntegerField(default=0, blank=True, null=True)
+ match_wins = models.IntegerField(default=0, blank=True, null=True)
+ match_losses = models.IntegerField(default=0, blank=True, null=True)
+ games_played = models.IntegerField(default=0, blank=True, null=True)
+ win_ratio = models.FloatField(default=0.00, blank=True, null=True)
+ matches = models.ManyToManyField(PingPongMatch)
+
+ def __unicode__(self):
+ return '%s' % self.player.user.username
View
9 urls.py
@@ -0,0 +1,9 @@
+from django.conf.urls.defaults import *
+
+from views import *
+
+urlpatterns = patterns('',
+ url(r'^pingpong/$', pingpong, name="pingpong"),
+ url(r'^pingpong/(?P<match_id>[0-9]+)/$', match, name="pingpong_match"),
+ url(r'^pingpong/new/$', new_match, name="new_pingpong"),
+)
View
76 views.py
@@ -0,0 +1,76 @@
+from django.contrib.admin.views.decorators import staff_member_required
+from django.template import RequestContext
+from django.shortcuts import render_to_response, get_object_or_404
+from django.core.urlresolvers import reverse
+from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden
+
+from models import *
+from const import PING_PONG_BEST_OF
+
+@staff_member_required
+def new_match(request):
+
+ if request.method == "POST":
+ player1 = Employee.objects.get(id=request.POST.get("player_1",""))
+ player2 = Employee.objects.get(id=request.POST.get("player_2",""))
+ if player1 != player2:
+ match = PingPongMatch(player_1=player1,player_2=player2)
+ match.save()
+ player1_games = 0
+ player2_games = 0
+ for i in [1,2,3]:
+ score1 = request.POST.get("player1_game%i"%i,None)
+ score2 = request.POST.get("player2_game%i"%i,None)
+ if score1 and score2:
+ if int(score1) > int(score2):
+ player1_games = player1_games + 1
+ else:
+ player2_games = player2_games + 1
+ game = PingPongGame(player1_points=score1,player2_points=score2,match=match)
+ game.save()
+
+ player_1_record = PingPongRecords.objects.get(player=player1)
+ player_2_record = PingPongRecords.objects.get(player=player2)
+ if player1_games > player2_games:
+ player_1_record.match_wins = player_1_record.match_wins + 1
+ player_1_record.save()
+ else:
+ player_2_record.match_wins = player_2_record.match_wins + 1
+ player_2_record.save()
+ player_1_record.win_ratio = round((float(player_1_record.game_wins) / float(player_1_record.games_played)) * 100, 2)
+ player_2_record.win_ratio = round((float(player_2_record.game_wins) / float(player_2_record.games_played)) * 100, 2)
+ player_1_record.save()
+ player_2_record.save()
+ return HttpResponseRedirect(reverse("pingpong_match",kwargs={"match_id":match.id}))
+ employees = Employee.objects.all()
+
+ return render_to_response('employees/new_pingpong.html',
+ {
+ "employees":employees,
+ "num_games":PING_PONG_BEST_OF
+ },
+ context_instance=RequestContext(request))
+
+@staff_member_required
+def match(request,match_id):
+
+ match = get_object_or_404(PingPongMatch,id=match_id)
+
+ return render_to_response('employees/pingpong_match.html',
+ {
+ "match":match,
+
+ },
+ context_instance=RequestContext(request))
+
+
+@staff_member_required
+def pingpong(request):
+
+ records = PingPongRecords.objects.all().order_by("game_wins").reverse()
+
+ return render_to_response('employees/pingpong_leaderboard.html',
+ {
+ "records":records
+ },
+ context_instance=RequestContext(request))
Please sign in to comment.
Something went wrong with that request. Please try again.