Skip to content

Commit a85b782

Browse files
committed
Fix Python3 str/unicode for all Django models
1 parent a5a121a commit a85b782

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

codespeed/models.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
from django.core.exceptions import ValidationError
88
from django.core.urlresolvers import reverse
9-
from django.db import models
109
from django.conf import settings
10+
from django.db import models
11+
from django.utils.encoding import python_2_unicode_compatible
1112

1213
from .commits.github import GITHUB_URL_RE
1314

1415

16+
@python_2_unicode_compatible
1517
class Project(models.Model):
1618
NO_LOGS = 'N'
1719
GIT = 'G'
@@ -38,7 +40,7 @@ class Project(models.Model):
3840
blank=True, max_length=200)
3941
track = models.BooleanField("Track changes", default=True)
4042

41-
def __unicode__(self):
43+
def __str__(self):
4244
return self.name
4345

4446
@property
@@ -71,18 +73,20 @@ def save(self, *args, **kwargs):
7173
super(Project, self).save(*args, **kwargs)
7274

7375

76+
@python_2_unicode_compatible
7477
class Branch(models.Model):
7578
name = models.CharField(max_length=20)
7679
project = models.ForeignKey(Project, related_name="branches")
7780

78-
def __unicode__(self):
81+
def __str__(self):
7982
return self.project.name + ":" + self.name
8083

8184
class Meta:
8285
unique_together = ("name", "project")
8386
verbose_name_plural = "branches"
8487

8588

89+
@python_2_unicode_compatible
8690
class Revision(models.Model):
8791
# git and mercurial's SHA-1 length is 40
8892
commitid = models.CharField(max_length=42)
@@ -100,7 +104,7 @@ def get_short_commitid(self):
100104
def get_browsing_url(self):
101105
return self.branch.project.commit_browsing_url.format(**self.__dict__)
102106

103-
def __unicode__(self):
107+
def __str__(self):
104108
if self.date is None:
105109
date = None
106110
else:
@@ -123,6 +127,7 @@ def clean(self):
123127
raise ValidationError("Invalid SVN commit id %s" % self.commitid)
124128

125129

130+
@python_2_unicode_compatible
126131
class Executable(models.Model):
127132
name = models.CharField(max_length=30)
128133
description = models.CharField(max_length=200, blank=True)
@@ -131,10 +136,11 @@ class Executable(models.Model):
131136
class Meta:
132137
unique_together = ('name', 'project')
133138

134-
def __unicode__(self):
139+
def __str__(self):
135140
return self.name
136141

137142

143+
@python_2_unicode_compatible
138144
class Benchmark(models.Model):
139145
B_TYPES = (
140146
('C', 'Cross-project'),
@@ -154,7 +160,7 @@ class Benchmark(models.Model):
154160
default_on_comparison = models.BooleanField(
155161
"Default on comparison page", default=True)
156162

157-
def __unicode__(self):
163+
def __str__(self):
158164
return self.name
159165

160166
def clean(self):
@@ -164,17 +170,19 @@ def clean(self):
164170
"'default_on_comparison' first.")
165171

166172

173+
@python_2_unicode_compatible
167174
class Environment(models.Model):
168175
name = models.CharField(unique=True, max_length=100)
169176
cpu = models.CharField(max_length=100, blank=True)
170177
memory = models.CharField(max_length=100, blank=True)
171178
os = models.CharField(max_length=100, blank=True)
172179
kernel = models.CharField(max_length=100, blank=True)
173180

174-
def __unicode__(self):
181+
def __str__(self):
175182
return self.name
176183

177184

185+
@python_2_unicode_compatible
178186
class Result(models.Model):
179187
value = models.FloatField()
180188
std_dev = models.FloatField(blank=True, null=True)
@@ -186,13 +194,14 @@ class Result(models.Model):
186194
benchmark = models.ForeignKey(Benchmark, related_name="results")
187195
environment = models.ForeignKey(Environment, related_name="results")
188196

189-
def __unicode__(self):
197+
def __str__(self):
190198
return u"%s: %s" % (self.benchmark.name, self.value)
191199

192200
class Meta:
193201
unique_together = ("revision", "executable", "benchmark", "environment")
194202

195203

204+
@python_2_unicode_compatible
196205
class Report(models.Model):
197206
revision = models.ForeignKey(Revision, related_name="reports")
198207
environment = models.ForeignKey(Environment, related_name="reports")
@@ -201,7 +210,7 @@ class Report(models.Model):
201210
colorcode = models.CharField(max_length=10, default="none")
202211
_tablecache = models.TextField(blank=True)
203212

204-
def __unicode__(self):
213+
def __str__(self):
205214
return u"Report for %s" % self.revision
206215

207216
class Meta:
@@ -534,3 +543,4 @@ def _get_tablecache(self):
534543
if self._tablecache == '':
535544
return {}
536545
return json.loads(self._tablecache)
546+

codespeed/tests/test_models.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,17 @@ class TestProject(TestCase):
1111

1212
def setUp(self):
1313
self.github_project = Project(
14-
repo_type='H', repo_path='https://github.com/tobami/codespeed.git')
15-
self.git_project = Project(repo_type='G',
16-
repo_path='/home/foo/codespeed')
14+
name='Some Project',
15+
repo_type='H',
16+
repo_path='https://github.com/tobami/codespeed.git'
17+
)
18+
self.git_project = Project(
19+
repo_type='G',
20+
repo_path='/home/foo/codespeed'
21+
)
22+
23+
def test_str(self):
24+
self.assertEqual(str(self.github_project), 'Some Project')
1725

1826
def test_repo_name(self):
1927
"""Test that only projects with local repositories have a repo_name attribute
File renamed without changes.

0 commit comments

Comments
 (0)