From 0a27bbf9ce1f5b1a52bc62ff4d9e5c2f960b030c Mon Sep 17 00:00:00 2001 From: Michal Zimniewicz Date: Sun, 9 Oct 2016 11:27:06 +0200 Subject: Initial commit --- ql/orm/__init__.py | 0 ql/orm/models.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ ql/orm/utils.py | 7 +++++++ 3 files changed, 61 insertions(+) create mode 100644 ql/orm/__init__.py create mode 100644 ql/orm/models.py create mode 100644 ql/orm/utils.py (limited to 'ql/orm') diff --git a/ql/orm/__init__.py b/ql/orm/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ql/orm/models.py b/ql/orm/models.py new file mode 100644 index 0000000..be31137 --- /dev/null +++ b/ql/orm/models.py @@ -0,0 +1,54 @@ +from django.db import models + + +class Team(models.Model): + + class Meta: + db_table = 'teams' + + name = models.CharField(db_column='fullname', max_length=50) + + +class Player(models.Model): + + class Meta: + db_table = 'players' + + first_name = models.CharField(db_column='gname', max_length=30) + last_name = models.CharField(db_column='sname', max_length=30) + team = models.ForeignKey(Team, on_delete=models.PROTECT, related_name='players', db_column='team') + + @property + def info(self): + return '%s %s' % (self.first_name, self.last_name) + + def __str__(self): + return '%s %s (%s)' % (self.first_name, self.last_name, self.team.name) + + +class Segment(models.Model): + + ''' This class has no single primary key, so not all standard ORM API will work. ''' + + class Meta: + db_table = 'segments' + + round = models.IntegerField(db_column='rnd', primary_key=True) + segment = models.IntegerField(db_column='segment', primary_key=True) + table = models.IntegerField(db_column='tabl', primary_key=True) + home_team = models.ForeignKey(Team, on_delete=models.PROTECT, related_name='+', db_column='homet') + away_team = models.ForeignKey(Team, on_delete=models.PROTECT, related_name='+', db_column='visit') + openN = models.ForeignKey(Player, on_delete=models.PROTECT, related_name='+', db_column='openN') + openS = models.ForeignKey(Player, on_delete=models.PROTECT, related_name='+', db_column='openS') + openE = models.ForeignKey(Player, on_delete=models.PROTECT, related_name='+', db_column='openE') + openW = models.ForeignKey(Player, on_delete=models.PROTECT, related_name='+', db_column='openW') + closeN = models.ForeignKey(Player, on_delete=models.PROTECT, related_name='+', db_column='closeN') + closeS = models.ForeignKey(Player, on_delete=models.PROTECT, related_name='+', db_column='closeS') + closeE = models.ForeignKey(Player, on_delete=models.PROTECT, related_name='+', db_column='closeE') + closeW = models.ForeignKey(Player, on_delete=models.PROTECT, related_name='+', db_column='closeW') + + def update(self, **kwargs): + affected = Segment.objects.filter(round=self.round, segment=self.segment, table=self.table).update(**kwargs) + assert affected == 1 + for field, value in kwargs.items(): + setattr(self, field, value) diff --git a/ql/orm/utils.py b/ql/orm/utils.py new file mode 100644 index 0000000..e5d96e2 --- /dev/null +++ b/ql/orm/utils.py @@ -0,0 +1,7 @@ +from .models import Team + + +def get_num_of_tables(): + num_of_teams = Team.objects.count() + assert num_of_teams % 2 == 0 + return int(num_of_teams / 2) -- cgit v1.2.3