Skip to content
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

Jkmarx/group name duplicate error #1573

Merged
merged 9 commits into from
Jan 17, 2017
9 changes: 9 additions & 0 deletions refinery/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from django.template import loader, Context
from django.core.cache import cache
from django.core.signing import Signer
from django.forms import ValidationError
from guardian.shortcuts import get_objects_for_user, get_objects_for_group
from guardian.models import GroupObjectPermission
from tastypie import fields
Expand Down Expand Up @@ -2018,6 +2019,14 @@ def obj_create(self, bundle, **kwargs):
user = bundle.request.user
data = json.loads(bundle.request.body)
new_ext_group = ExtendedGroup(name=data['name'])

new_ext_group.name = new_ext_group.name.strip()
try:
new_ext_group.full_clean()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to call full_clean() if there is no custom clean() method. The save() method will raise an exception if the group name is blank (a DB constraint) but it will not check for group names that contain whitespace chars. Is that the desired behavior?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The save() method does not raise an exception if the group name is blank.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, that's correct. Now, should we allow group names that contain whitespace chars only? What about leading and/or trailing whitespace chars?

except ValidationError as e:
raise ImmediateHttpResponse(HttpBadRequest(
'Invalid group creation request: %s.' % e
))
new_ext_group.save()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While save() does not perform validation checks, it can still raise IntegrityError for duplicate names, so that exception should be handled here.

new_ext_group.user_set.add(user)
new_ext_group.manager_group.user_set.add(user)
Expand Down
7 changes: 0 additions & 7 deletions refinery/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1872,13 +1872,6 @@ def get_managed_group(self):
except:
return None

def save(self, *args, **kwargs):
if len(self.name) == 0:
logger.error("Group name cannot be empty.")
return
else:
super(ExtendedGroup, self).save(*args, **kwargs)


# automatic creation of a managed group when an extended group is created:
def create_manager_group(sender, instance, created, **kwargs):
Expand Down