Skip to content

Commit d975b32

Browse files
committed
Converted models to use abstract models
Added Game and GameDefinition fields which contain all fields and constants common to Match/Tournament/MoranProcess & their corresponding definition classes. These classes instead inherit from the abstract models.
1 parent 5c8ec41 commit d975b32

File tree

1 file changed

+24
-76
lines changed

1 file changed

+24
-76
lines changed

api/core/models.py

Lines changed: 24 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,22 @@ class Game(Model):
4141
results = JSONField()
4242

4343
class Meta:
44-
managed = False
44+
abstract = True
4545

4646

47-
class Tournament(Model):
47+
class GameDefinition(Model):
48+
created = DateTimeField(auto_now_add=True, editable=False)
49+
last_updated = DateTimeField(auto_now=True, editable=False)
50+
turns = IntegerField()
51+
noise = FloatField()
52+
player_list = ManyToManyField('InternalStrategy')
4853

49-
PENDING = 0
50-
RUNNING = 1
51-
SUCCESS = 2
52-
FAILED = 3
54+
class Meta:
55+
abstract = True
5356

54-
STATUS_CHOICES = (
55-
(PENDING, 'PENDING'),
56-
(RUNNING, 'RUNNING'),
57-
(SUCCESS, 'SUCCESS'),
58-
(FAILED, 'FAILED'),
59-
)
6057

61-
# Fields
62-
created = DateTimeField(auto_now_add=True, editable=False)
63-
last_updated = DateTimeField(auto_now=True, editable=False)
64-
status = IntegerField(choices=STATUS_CHOICES, default=PENDING)
65-
results = JSONField(null=True)
58+
class Tournament(Game):
59+
6660
definition = ForeignKey('TournamentDefinition')
6761

6862
def run(self, strategies):
@@ -80,37 +74,14 @@ def run(self, strategies):
8074
return results
8175

8276

83-
class TournamentDefinition(Model):
84-
# Fields
77+
class TournamentDefinition(GameDefinition):
8578
name = CharField(max_length=255)
86-
created = DateTimeField(auto_now_add=True, editable=False)
87-
last_updated = DateTimeField(auto_now=True, editable=False)
88-
turns = IntegerField()
8979
repetitions = IntegerField()
90-
noise = FloatField()
9180
with_morality = BooleanField()
92-
player_list = ManyToManyField('InternalStrategy')
9381

9482

95-
class Match(Model):
83+
class Match(Game):
9684

97-
PENDING = 0
98-
RUNNING = 1
99-
SUCCESS = 2
100-
FAILED = 3
101-
102-
STATUS_CHOICES = (
103-
(PENDING, 'PENDING'),
104-
(RUNNING, 'RUNNING'),
105-
(SUCCESS, 'SUCCESS'),
106-
(FAILED, 'FAILED'),
107-
)
108-
109-
# Fields
110-
created = DateTimeField(auto_now_add=True, editable=False)
111-
last_updated = DateTimeField(auto_now=True, editable=False)
112-
status = IntegerField(choices=STATUS_CHOICES, default=PENDING)
113-
results = JSONField(null=True)
11485
definition = ForeignKey('MatchDefinition')
11586

11687
def run(self, strategies):
@@ -125,33 +96,14 @@ def run(self, strategies):
12596
return match
12697

12798

128-
class MatchDefinition(Model):
129-
created = DateTimeField(auto_now_add=True, editable=False)
130-
last_updated = DateTimeField(auto_now=True, editable=False)
131-
turns = IntegerField()
132-
noise = FloatField()
133-
player_list = ManyToManyField('InternalStrategy')
99+
class MatchDefinition(GameDefinition):
134100

101+
class Meta:
102+
managed = True
135103

136-
class MoranProcess(Model):
137104

138-
PENDING = 0
139-
RUNNING = 1
140-
SUCCESS = 2
141-
FAILED = 3
105+
class MoranProcess(Game):
142106

143-
STATUS_CHOICES = (
144-
(PENDING, 'PENDING'),
145-
(RUNNING, 'RUNNING'),
146-
(SUCCESS, 'SUCCESS'),
147-
(FAILED, 'FAILED'),
148-
)
149-
150-
# Fields
151-
created = DateTimeField(auto_now_add=True, editable=False)
152-
last_updated = DateTimeField(auto_now=True, editable=False)
153-
status = IntegerField(choices=STATUS_CHOICES, default=PENDING)
154-
results = JSONField(null=True)
155107
definition = ForeignKey('MoranDefinition')
156108

157109
def run(self, strategies):
@@ -168,23 +120,19 @@ def run(self, strategies):
168120
return mp
169121

170122

171-
class MoranDefinition(Model):
172-
created = DateTimeField(auto_now_add=True, editable=False)
173-
last_updated = DateTimeField(auto_now=True, editable=False)
174-
turns = IntegerField()
175-
noise = FloatField()
123+
class MoranDefinition(GameDefinition):
176124
mode = CharField(max_length=2)
177-
player_list = ManyToManyField('InternalStrategy')
178125

179126

180127
class InternalStrategy(Model):
128+
"""
129+
This model is used to represent strategies in an internal
130+
database table. Games reference this in a ManyToMany
131+
to store the strategies in their player_list field. This is
132+
necessary to facilitate normalization of the database.
133+
"""
181134
id = TextField(primary_key=True)
182135
created = DateTimeField(auto_now_add=True, editable=False)
183136
last_updated = DateTimeField(auto_now=True, editable=False)
184137

185138

186-
class Result(Model):
187-
created = DateTimeField(auto_now_add=True, editable=False)
188-
last_updated = DateTimeField(auto_now=True, editable=False)
189-
type = CharField(max_length=255)
190-
result = JSONField()

0 commit comments

Comments
 (0)