57:ceaa32d9dfca
Anton Shestakov <av6@dwimlabs.net>, Thu, 23 Jun 2016 16:53:45 +0800
linters: yamllint config

next change 66:db31e73bde12
previous change 46:0f45e02848a1

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)
def get_title(self):
owner = self.user if self.user is not None else self.domain
return '{}/{}'.format(owner, 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)
result = []
if minutes:
result.append('{} min'.format(minutes))
if seconds or not minutes:
result.append('{} sec'.format(seconds))
return ' '.join(result)