New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Save config on build model #4749
Changes from 7 commits
6891db8
b1b3620
27d8251
ab5ea04
5f4fb8c
9d70812
13abeda
98d26a4
660c03a
d29b6d9
7fc1e2b
8f62f03
76929ac
7ade872
6f8becc
07efa1b
715e5ed
dab5860
5aacd63
822b793
8c60760
7ed110d
2ffdbf6
5cce5d8
9d8266f
28cd935
433b709
b88837d
4723705
043f4e6
79fbb8e
f33d54a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
from django.conf import settings | ||
from django.core.urlresolvers import reverse | ||
from django.db import models | ||
from django.utils import timezone | ||
from django.utils.encoding import python_2_unicode_compatible | ||
from django.utils.translation import ugettext | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
@@ -137,7 +138,7 @@ def config(self): | |
.order_by('-date') | ||
.first() | ||
) | ||
return last_build.config | ||
return last_build.get_config() | ||
|
||
@property | ||
def commit_name(self): | ||
|
@@ -478,7 +479,7 @@ class Build(models.Model): | |
exit_code = models.IntegerField(_('Exit code'), null=True, blank=True) | ||
commit = models.CharField( | ||
_('Commit'), max_length=255, null=True, blank=True) | ||
config = JSONField(_('Configuration used in the build'), default={}) | ||
config = JSONField(_('Configuration used in the build'), default=dict) | ||
|
||
length = models.IntegerField(_('Build Length'), null=True, blank=True) | ||
|
||
|
@@ -497,6 +498,47 @@ class Meta(object): | |
get_latest_by = 'date' | ||
index_together = [['version', 'state', 'type']] | ||
|
||
@property | ||
def previous(self): | ||
"""Returns the previous build to this one.""" | ||
date = self.date or timezone.now() | ||
if self.project is not None and self.version is not None: | ||
return ( | ||
Build.objects | ||
.filter( | ||
project=self.project, | ||
version=self.version, | ||
date__lt=date, | ||
) | ||
.order_by('-date') | ||
.first() | ||
) | ||
return None | ||
|
||
def get_config(self): | ||
"""Get the config from correct object.""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we are not going to access the The docstring could be more explicit.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the idea of using |
||
if '__config' in self.config: | ||
return Build.objects.get(pk=self.config['__config']).config | ||
return self.config | ||
|
||
def save(self, *args, **kwargs): # noqa | ||
""" | ||
Save object. | ||
|
||
To save space on the db we only save the config if it's different | ||
from the previous one. | ||
|
||
If the config is the same, we save the pk of the object | ||
that has the **real** config under the ``__config`` key. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, it would be good if this is a CONSTANT defined in the class, instead something that it's written many times. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I'm not convinced with the name either, not sure about |
||
""" | ||
previous = self.previous | ||
if previous is not None and self.config == previous.get_config(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you know how deep the Also, I think this comparison needs to be a method because it will be more explicit. Like, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's recursive https://stackoverflow.com/a/17217255/5689214 |
||
previous_pk = previous.pk | ||
if '__config' in previous.config: | ||
previous_pk = previous.config['__config'] | ||
self.config = {'__config': previous_pk} | ||
super(Build, self).save(*args, **kwargs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little worried that we're running this on each Is there a way to set the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think there is an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, I'm just trying to think if there is a way to run this logic just when we are trying to set a |
||
|
||
def __str__(self): | ||
return ugettext( | ||
'Build {project} for {usernames} ({pk})'.format( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying to be more explicit here,