27:9fa79edc2e45
Anton Shestakov <av6@dwimlabs.net>, Thu, 16 Jun 2016 07:31:39 +0800
viewer: use new get_duration() method to format time taken by a check

next change 34:0a5f37c8562d
previous change 26:8c1ce7d12350

candolint/models.py

Permissions: -rw-r--r--

Other formats: Feeds:
import peewee as pw
database = pw.SqliteDatabase(None, journal_mode='WAL')
class BaseModel(pw.Model):
class Meta:
database = database
@classmethod
def create_or_get(cls, **kwargs):
try:
with cls._meta.database.atomic():
return cls.create(**kwargs), True
except pw.IntegrityError:
# a variation on https://github.com/coleifer/peewee/pull/681
query = []
compound_unique = set()
for field_names, unique in cls._meta.indexes:
if unique:
compound_unique.update(field_names)
for field_name, value in kwargs.items():
field = getattr(cls, field_name)
if field.unique or field.primary_key or field_name in compound_unique:
query.append(field == value)
return cls.get(*query), False
class Project(BaseModel):
url = pw.CharField(unique=True)
domain = pw.CharField()
user = pw.CharField(null=True)
name = pw.CharField()
class Meta:
indexes = (
(('domain', 'user', 'name'), True),
)
def get_url(self, request=None):
if request is not None:
base = '{}://{}'.format(request.protocol, request.host)
return base + self.get_url()
else:
user = self.user if self.user is not None else '-'
return '/{}/{}/{}'.format(self.domain, user, self.name)
class Change(BaseModel):
rev = pw.IntegerField()
node = pw.CharField()
branch = pw.CharField()
date = pw.DateTimeField()
author = pw.CharField()
message = pw.CharField()
project = pw.ForeignKeyField(Project, related_name='changes')
class Meta:
indexes = (
(('rev', 'node', 'project'), True),
)
class Check(BaseModel):
ordinal = pw.IntegerField()
errors = pw.IntegerField()
warnings = pw.IntegerField()
raw = pw.TextField()
lines = pw.TextField()
success = pw.BooleanField()
started = pw.DateTimeField()
finished = pw.DateTimeField()
project = pw.ForeignKeyField(Project, related_name='checks')
change = pw.ForeignKeyField(Change, related_name='checks')
class Meta:
order_by = ('-finished', '-ordinal')
indexes = (
(('ordinal', 'project'), True),
)
def get_url(self, request=None):
return '{}/{}'.format(self.project.get_url(request), self.ordinal)
def get_duration(self):
d = self.finished - self.started
minutes, seconds = divmod(int(d.total_seconds()), 60)
if minutes:
return '{} min {} sec'.format(minutes, seconds)
else:
return '{} sec'.format(seconds)