Anton Shestakov <av6@dwimlabs.net>, Sat, 23 Sep 2017 12:42:03 +0800
viewer: use order_by() explicitly, helps with keeping indices up-to-date
candolint/models.py
Permissions: -rw-r--r--
from __future__ import absolute_import from candolint import adapters database = pw.SqliteDatabase(None, journal_mode='WAL') class BaseModel(pw.Model): def manual_upsert(cls, **kwargs): with cls._meta.database.atomic(): return cls.create(**kwargs), True except pw.IntegrityError: # a variation on https://github.com/coleifer/peewee/pull/681 for field_names, unique in cls._meta.indexes: 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) instance = cls.get(*query) for field_name, value in kwargs.items(): setattr(instance, field_name, value) class Project(BaseModel): url = pw.CharField(unique=True) user = pw.CharField(null=True) (('domain', 'user', 'name'), True), def get_url(self, request=None): base = '{}://{}'.format(request.protocol, request.host) return base + self.get_url() user = self.user if self.user is not None else '-' return '/{}/{}/{}'.format(self.domain, user, self.name) if self.domain == 'bitbucket.org': return adapters.BitbucketAdapter(self) elif self.domain == 'github.com': return adapters.GithubAdapter(self) elif self.domain.startswith('hg.') or 'mercurial' in self.domain: return adapters.HgwebAdapter(self) return adapters.HostingAdapter(self) rev = pw.IntegerField(null=True) date = pw.DateTimeField() project = pw.ForeignKeyField(Project, related_name='changes') (('node', 'project'), True), ordinal = pw.IntegerField() errors = pw.IntegerField() warnings = pw.IntegerField() success = pw.BooleanField() started = pw.DateTimeField() finished = pw.DateTimeField() project = pw.ForeignKeyField(Project, related_name='checks') change = pw.ForeignKeyField(Change, related_name='checks') (('ordinal', 'project'), True), (('project', 'finished', 'ordinal'), True) d = self.finished - self.started minutes, seconds = divmod(int(d.total_seconds()), 60) result.append('{} min'.format(minutes)) if seconds or not minutes: result.append('{} sec'.format(seconds)) def get_next_ordinal(cls, project): .select(pw.fn.COALESCE(pw.fn.MAX(cls.ordinal), 0)) .where(cls.project == project)) def get_light_fields(cls): field for field in cls._meta.fields.values() if not isinstance(field, pw.TextField)