Skip to content

Commit

Permalink
fix bug 1059222 - refresh flat_exclusions on save
Browse files Browse the repository at this point in the history
  • Loading branch information
Cameron Dawson committed Sep 5, 2014
1 parent f93c3e3 commit 0be2592
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ nosetests.xml

#local settings
treeherder/settings/local.py
treeherder/settings/pycharm_django_settings.py
htmlcov/

#static files in deployment
Expand Down
31 changes: 9 additions & 22 deletions treeherder/model/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,13 +566,18 @@ class ExclusionProfile(models.Model):
"""
name = models.CharField(max_length=255, unique=True)
is_default = models.BooleanField(default=False)
exclusions = models.ManyToManyField(JobExclusion, related_name="profiles", through="ExclusionProfileExclusions")
exclusions = models.ManyToManyField(JobExclusion, related_name="profiles")
flat_exclusion = JSONField(blank=True, default={})
author = models.ForeignKey(User, related_name="exclusion_profiles_authored")

def save(self, *args, **kwargs):
super(ExclusionProfile, self).save(*args, **kwargs)

# update the old default profile
if self.is_default:
ExclusionProfile.objects.filter(is_default=True).exclude(id=self.id).update(is_default=False)

def update_flat_exclusions(self):
# prepare the nested defaultdict structure for the flat exclusions
# options should be stored in a set but sets are not serializable.
# using a list instead
Expand All @@ -585,18 +590,12 @@ def save(self, *args, **kwargs):
combo = tuple(itertools.product(exclusion.info['repos'], exclusion.info['platforms'],
exclusion.info['job_types'], exclusion.info['options']))
for repo, platform, job_type, option in combo:
# strip the job type symbol appended in the ui
job_type = job_type[:job_type.rfind(" (")]
flat_exclusions[repo][platform][job_type][option] = 1

self.flat_exclusion = flat_exclusions
kwargs["force_insert"] = False
kwargs["force_update"] = True
super(ExclusionProfile, self).save(*args, **kwargs)
if cmp(self.flat_exclusion, flat_exclusions) != 0:
self.flat_exclusion = flat_exclusions
super(ExclusionProfile, self).save(force_insert=False, force_update=True)

# update the old default profile
if self.is_default:
ExclusionProfile.objects.filter(is_default=True).exclude(id=self.id).update(is_default=False)

class Meta:
db_table = 'exclusion_profile'
Expand All @@ -616,18 +615,6 @@ class Meta:
db_table = 'user_exclusion_profile'


class ExclusionProfileExclusions(models.Model):
"""
A many to many entity for exclusion profiles and job exclusions
"""

exclusionprofile = models.ForeignKey(ExclusionProfile)
jobexclusion = models.ForeignKey(JobExclusion)

class Meta:
db_table = 'exclusion_profile_exclusions'


class ReferenceDataSignatures(models.Model):
"""
A collection of all the possible combinations of reference data,
Expand Down
6 changes: 6 additions & 0 deletions treeherder/webapp/api/refdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

from django.contrib.auth.models import User

import itertools
from collections import defaultdict

from treeherder.model import models
from treeherder.model.derived import RefDataManager
from treeherder.webapp.api import serializers as th_serializers
Expand Down Expand Up @@ -162,3 +165,6 @@ def create(self, request, *args, **kwargs):
if "author" not in request.DATA:
request.DATA["author"] = request.user.id
return super(ExclusionProfileViewSet, self).create(request, *args, **kwargs)

def post_save(self, obj, created=False):
obj.update_flat_exclusions()

0 comments on commit 0be2592

Please sign in to comment.