Skip to content

Commit

Permalink
Fix according to review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
ycheng-aa committed Oct 16, 2015
1 parent 642b656 commit ed0ad07
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 35 deletions.
31 changes: 0 additions & 31 deletions pdc/apps/component/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,40 +541,13 @@ class Meta:
fields = ('id', 'type', 'from_component', 'to_component')


def _check_role_count(component_contact_model, component, role):
current_role_count = component_contact_model.objects.filter(component=component, role=role).count()
if role.count_limit != ContactRole.UNLIMITED and current_role_count >= role.count_limit:
raise serializers.ValidationError({
'detail': 'Exceed contact role limit for the component. The limit is %d' % role.count_limit})


def validate_component_role_count(component_contact_model, value, instance):
role = value.get('role')
component = value.get('component')
# POST with role
if not instance and role:
_check_role_count(component_contact_model, component, role)

# PUT or PATCH
else:
if (role and role != instance.role) or (component and component != instance.component):
# with model unique restriction, it will increase 1 to destination component's role count.
role = role if role else instance.role
component = component if component else instance.component
_check_role_count(component_contact_model, component, role)


class GlobalComponentContactSerializer(StrictSerializerMixin, serializers.ModelSerializer):
component = serializers.SlugRelatedField(slug_field='name', read_only=False,
queryset=GlobalComponent.objects.all())
role = serializers.SlugRelatedField(slug_field='name', read_only=False,
queryset=ContactRole.objects.all())
contact = ContactField()

def validate(self, value):
validate_component_role_count(GlobalComponentContact, value, self.instance)
return value

class Meta:
model = GlobalComponentContact
fields = ('id', 'component', 'role', 'contact')
Expand All @@ -587,10 +560,6 @@ class ReleaseComponentContactSerializer(StrictSerializerMixin, serializers.Model
queryset=ContactRole.objects.all())
contact = ContactField()

def validate(self, value):
validate_component_role_count(ReleaseComponentContact, value, self.instance)
return value

class Meta:
model = ReleaseComponentContact
fields = ('id', 'component', 'role', 'contact')
27 changes: 24 additions & 3 deletions pdc/apps/contact/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.db import models
from django.db.models.query import QuerySet
from django.forms.models import model_to_dict
from django.core.exceptions import ValidationError


class ContactRole(models.Model):
Expand Down Expand Up @@ -144,7 +145,27 @@ def create(self, **kwargs):
return self.get_queryset().create(**create_kwargs)


class GlobalComponentContact(models.Model):
class ValidateRoleCountMixin(object):

def _check_role_count(self):
current_role_count = type(self).objects.filter(component=self.component, role=self.role).count()
if self.role.count_limit != ContactRole.UNLIMITED and current_role_count >= self.role.count_limit:
raise ValidationError(
{'detail': 'Exceed contact role limit for the component. The limit is %d' % self.role.count_limit})

def clean(self):
# Create
if not self.pk:
self._check_role_count()
# Update
else:
old_instance = type(self).objects.get(pk=self.pk)
if self.role != old_instance.role or self.component != old_instance.component:
# with model unique restriction, it will increase 1 to destination component's role count.
self._check_role_count()


class GlobalComponentContact(ValidateRoleCountMixin, models.Model):

role = models.ForeignKey(ContactRole, on_delete=models.PROTECT)
contact = models.ForeignKey(Contact, on_delete=models.PROTECT)
Expand All @@ -165,15 +186,15 @@ def export(self, fields=None):
}


class ReleaseComponentContact(models.Model):
class ReleaseComponentContact(ValidateRoleCountMixin, models.Model):

role = models.ForeignKey(ContactRole, on_delete=models.PROTECT)
contact = models.ForeignKey(Contact, on_delete=models.PROTECT)
component = models.ForeignKey('component.ReleaseComponent',
on_delete=models.PROTECT)

def __unicode__(self):
return u'%s: %s: %s' % (unicode(self.component), self.contact_role, unicode(self.contact))
return u'%s: %s: %s' % (unicode(self.component), self.role, unicode(self.contact))

This comment has been minimized.

Copy link
@lubomir

lubomir Oct 16, 2015

Member

GlobalComponentContact has the same problem, it's PDC-1103.


class Meta:
unique_together = (('role', 'component', 'contact'), )
Expand Down
2 changes: 1 addition & 1 deletion pdc/apps/contact/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ContactRoleSerializer(StrictSerializerMixin,

class Meta:
model = ContactRole
fields = ('name', )
fields = ('name', 'count_limit')


class PersonSerializer(DynamicFieldsSerializerMixin,
Expand Down

0 comments on commit ed0ad07

Please sign in to comment.