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
Merged

Conversation

jkmarx
Copy link
Member

@jkmarx jkmarx commented Jan 4, 2017

Resolves #1477

def save(self, *args, **kwargs):
if len(self.name) == 0:
logger.error("Group name cannot be empty.")
return
raise ValidationError('Group name cannot be empty')
elif self.duplicate_group_name_exists():
Copy link
Member

@hackdna hackdna Jan 4, 2017

Choose a reason for hiding this comment

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

Need to re-evaluate this logic because in Group model: name = models.CharField(_('name'), max_length=80, unique=True)

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed the elif because the DB returns an integrityError, but the if return does not return an exception. Please see latest.

:returns: Boolean based on if a duplicate name exists
"""
# Catches empty strings & removes trail/lead white spaces
if self.name:
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if we need this extra check here. This may have only been applicable to duplicate slug checking

Copy link
Member Author

Choose a reason for hiding this comment

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

No longer require the method.

@codecov-io
Copy link

codecov-io commented Jan 5, 2017

Current coverage is 34.92% (diff: 16.66%)

Merging #1573 into develop will increase coverage by 0.01%

@@            develop      #1573   diff @@
==========================================
  Files           340        340          
  Lines         23456      23576   +120   
  Methods           0          0          
  Messages          0          0          
  Branches       1251       1251          
==========================================
+ Hits           8187       8233    +46   
- Misses        15269      15343    +74   
  Partials          0          0          

Powered by Codecov. Last update 4d62db7...1bec1ce

@@ -1875,7 +1875,7 @@ def get_managed_group(self):
def save(self, *args, **kwargs):
Copy link
Member

@hackdna hackdna Jan 5, 2017

Choose a reason for hiding this comment

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

It looks like the preferred way to validate model fields is via full_clean() and clean() model methods instead of overriding save(): https://docs.djangoproject.com/en/1.7/ref/models/instances/#django.db.models.Model.clean

Copy link
Member

Choose a reason for hiding this comment

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

Also, the group name cannot be blank already: name = models.CharField(_('name'), max_length=80, unique=True), so no additional checking for that is necessary. It might be a good idea to strip whitespace characters though.

@@ -1845,6 +1845,13 @@ class ExtendedGroup(Group):
can_edit = False
manager_group_uuid = None

def clean(self):
# Don't allow only white spaces.
if len(self.name) == 0:
Copy link
Member

Choose a reason for hiding this comment

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

As I mentioned earlier, it looks like the group name cannot be blank already: name = models.CharField(_('name'), max_length=80, unique=True), so no additional checking for that is necessary. It might be a good idea to strip whitespace characters though.

if len(self.name) == 0:
logger.error("Group name cannot be empty.")
raise ValidationError("Group name cannot be empty")
return self.name.strip()
Copy link
Member

Choose a reason for hiding this comment

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

return
else:
super(ExtendedGroup, self).save(*args, **kwargs)
self.name = self.clean()
Copy link
Member

Choose a reason for hiding this comment

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

The clean() method does not return a value (https://docs.djangoproject.com/en/1.7/_modules/django/db/models/base/#Model.clean). Also, validation should be done using the full_clean() method (https://docs.djangoproject.com/en/1.7/ref/models/instances/#django.db.models.Model.full_clean). Finally, overriding the save() method might cause the validation to be run twice in some cases and not run at all in others (http://stackoverflow.com/a/23684110).

Copy link
Member Author

Choose a reason for hiding this comment

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

https://docs.djangoproject.com/en/1.7/ref/models/instances/#django.db.models.Model.full_clean - "Note that full_clean() will not be called automatically when you call your model’s save() method. " That's only an issue with ModelForm. With ModelForms, the clean() methods do return cleaned_data according to Two Scoops 11.7.

@jkmarx
Copy link
Member Author

jkmarx commented Jan 6, 2017

Please note, this will not be merged until the next milestone (post-code review of course).


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.

@hackdna
Copy link
Member

hackdna commented Jan 10, 2017

@jkmarx jkmarx added this to the TBD Milestone milestone Jan 12, 2017
@jkmarx jkmarx merged commit 4f7c3eb into develop Jan 17, 2017
@jkmarx jkmarx deleted the jkmarx/group-name-duplicate-error branch January 17, 2017 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants