Skip to content

Commit

Permalink
Jkmarx/group name duplicate error (#1573)
Browse files Browse the repository at this point in the history
* Error handling in the group api.

* Prevent duplicate group names.

* Fix name duplication check.

* Double quote strings.

* Remove duplicate error since db returns integrityError.

* Create and call clean method for stripping white spaces.

* Utilize full_clean for param validation.

* Remove unnessary methods due to using full_clean.
  • Loading branch information
jkmarx committed Jan 17, 2017
1 parent e6f4f22 commit 4f7c3eb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
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()
except ValidationError as e:
raise ImmediateHttpResponse(HttpBadRequest(
'Invalid group creation request: %s.' % e
))
new_ext_group.save()
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

0 comments on commit 4f7c3eb

Please sign in to comment.