Skip to content

Commit

Permalink
Fix Python3 str/unicode for all Django models
Browse files Browse the repository at this point in the history
  • Loading branch information
tobami committed May 24, 2016
1 parent a5a121a commit a85b782
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
28 changes: 19 additions & 9 deletions codespeed/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django.db import models
from django.conf import settings
from django.db import models
from django.utils.encoding import python_2_unicode_compatible

from .commits.github import GITHUB_URL_RE


@python_2_unicode_compatible
class Project(models.Model):
NO_LOGS = 'N'
GIT = 'G'
Expand All @@ -38,7 +40,7 @@ class Project(models.Model):
blank=True, max_length=200)
track = models.BooleanField("Track changes", default=True)

def __unicode__(self):
def __str__(self):
return self.name

@property
Expand Down Expand Up @@ -71,18 +73,20 @@ def save(self, *args, **kwargs):
super(Project, self).save(*args, **kwargs)


@python_2_unicode_compatible
class Branch(models.Model):
name = models.CharField(max_length=20)
project = models.ForeignKey(Project, related_name="branches")

def __unicode__(self):
def __str__(self):
return self.project.name + ":" + self.name

class Meta:
unique_together = ("name", "project")
verbose_name_plural = "branches"


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

def __unicode__(self):
def __str__(self):
if self.date is None:
date = None
else:
Expand All @@ -123,6 +127,7 @@ def clean(self):
raise ValidationError("Invalid SVN commit id %s" % self.commitid)


@python_2_unicode_compatible
class Executable(models.Model):
name = models.CharField(max_length=30)
description = models.CharField(max_length=200, blank=True)
Expand All @@ -131,10 +136,11 @@ class Executable(models.Model):
class Meta:
unique_together = ('name', 'project')

def __unicode__(self):
def __str__(self):
return self.name


@python_2_unicode_compatible
class Benchmark(models.Model):
B_TYPES = (
('C', 'Cross-project'),
Expand All @@ -154,7 +160,7 @@ class Benchmark(models.Model):
default_on_comparison = models.BooleanField(
"Default on comparison page", default=True)

def __unicode__(self):
def __str__(self):
return self.name

def clean(self):
Expand All @@ -164,17 +170,19 @@ def clean(self):
"'default_on_comparison' first.")


@python_2_unicode_compatible
class Environment(models.Model):
name = models.CharField(unique=True, max_length=100)
cpu = models.CharField(max_length=100, blank=True)
memory = models.CharField(max_length=100, blank=True)
os = models.CharField(max_length=100, blank=True)
kernel = models.CharField(max_length=100, blank=True)

def __unicode__(self):
def __str__(self):
return self.name


@python_2_unicode_compatible
class Result(models.Model):
value = models.FloatField()
std_dev = models.FloatField(blank=True, null=True)
Expand All @@ -186,13 +194,14 @@ class Result(models.Model):
benchmark = models.ForeignKey(Benchmark, related_name="results")
environment = models.ForeignKey(Environment, related_name="results")

def __unicode__(self):
def __str__(self):
return u"%s: %s" % (self.benchmark.name, self.value)

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


@python_2_unicode_compatible
class Report(models.Model):
revision = models.ForeignKey(Revision, related_name="reports")
environment = models.ForeignKey(Environment, related_name="reports")
Expand All @@ -201,7 +210,7 @@ class Report(models.Model):
colorcode = models.CharField(max_length=10, default="none")
_tablecache = models.TextField(blank=True)

def __unicode__(self):
def __str__(self):
return u"Report for %s" % self.revision

class Meta:
Expand Down Expand Up @@ -534,3 +543,4 @@ def _get_tablecache(self):
if self._tablecache == '':
return {}
return json.loads(self._tablecache)

14 changes: 11 additions & 3 deletions codespeed/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@ class TestProject(TestCase):

def setUp(self):
self.github_project = Project(
repo_type='H', repo_path='https://github.com/tobami/codespeed.git')
self.git_project = Project(repo_type='G',
repo_path='/home/foo/codespeed')
name='Some Project',
repo_type='H',
repo_path='https://github.com/tobami/codespeed.git'
)
self.git_project = Project(
repo_type='G',
repo_path='/home/foo/codespeed'
)

def test_str(self):
self.assertEqual(str(self.github_project), 'Some Project')

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

0 comments on commit a85b782

Please sign in to comment.