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

fixed server to check default_attribute and values for checkbox #7889

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from

Conversation

KrishavRajSingh
Copy link
Contributor

@KrishavRajSingh KrishavRajSingh commented May 14, 2024

Fixes #2927

I checked the values of default_value and values and threw error if it wasn't true or false for checkbox
Testing video

Checklist

  • [*] I submit my changes into the develop branch
  • I have created a changelog fragment
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • [*] I have linked related issues (see GitHub docs)
  • I have increased versions of npm packages if it is necessary
    (cvat-canvas,
    cvat-core,
    cvat-data and
    cvat-ui)

License

  • [*] I submit my code changes under the same MIT License that covers the project.
    Feel free to contact the maintainers if that's a concern.

Summary by CodeRabbit

  • Bug Fixes
    • Added validation for checkbox attributes to ensure values are either 'true' or 'false', including default values.

Copy link
Contributor

coderabbitai bot commented May 14, 2024

Walkthrough

The recent update ensures that checkbox attributes and their default values are validated to be 'true' or 'false' across different components, aiming to resolve issues related to updating labels with multiple attributes, including 'expression'.

Changes

Files/Paths Change Summary
changelog.d/20240514_153646_krishavrajsingh_server.md Added a fix for checkbox attribute validation in the server.
cvat/apps/engine/views.py Implemented validation for checkbox attributes in the perform_update method.
cvat/apps/engine/migrations/0079_fix_checkbox_values.py Introduced a migration to correct checkbox attribute values in Django.
cvat/apps/engine/serializers.py Updated AttributeSerializer to validate checkbox attributes.

Assessment against linked issues

Objective Addressed Explanation
Ensure label attributes with checkboxes have values 'true' or 'false' (#2927)
Fix the issue where labels with multiple attributes including 'expression' fail to update (#2927)

In the code's realm where checkboxes reside,
Values now 'true' or 'false' abide.
No more errors, updates will flow,
With labels fixed, the bugs must go.
🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Out of diff range and nitpick comments (3)
cvat/apps/engine/views.py (3)

Line range hint 44-44: Remove unused import statement.

- import cvat.apps.dataset_manager.views  # pylint: disable=unused-import

Line range hint 1967-1967: Convert f-string to regular string.

-        return Response("{} is an invalid directory".format(directory_param),
+        return Response(f"{directory_param} is an invalid directory",

Line range hint 2723-2723: Convert f-string to regular string.

-        return Response(data='unknown data type {}.'.format(self.type),
+        return Response(data=f'unknown data type {self.type}.',
Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between f4ee3f9 and b85c048.
Files selected for processing (1)
  • cvat/apps/engine/views.py (1 hunks)
Additional Context Used
Ruff (3)
cvat/apps/engine/views.py (3)

44-44: cvat.apps.dataset_manager.views imported but unused


1967-1967: f-string without any placeholders


2723-2723: f-string without any placeholders

cvat/apps/engine/views.py Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between b85c048 and bc17226.
Files selected for processing (1)
  • changelog.d/20240514_153646_krishavrajsingh_server.md (1 hunks)
Additional Context Used
LanguageTool (1)
changelog.d/20240514_153646_krishavrajsingh_server.md (1)

Near line 1: Possible typo: you repeated a word
Context: ### Fixed - fixed server to check default_attribute and v...

changelog.d/20240514_153646_krishavrajsingh_server.md Outdated Show resolved Hide resolved
changelog.d/20240514_153646_krishavrajsingh_server.md Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

Out of diff range and nitpick comments (2)
cvat/apps/engine/views.py (2)

Line range hint 1967-1967: Convert f-string to regular string.

The f-string on this line does not contain any placeholders and should be converted to a regular string.

-        return Response("{} is an invalid directory".format(directory_param),
+        return Response(f"{directory_param} is an invalid directory",

Line range hint 2723-2723: Convert f-string to regular string.

The f-string on this line does not contain any placeholders and should be converted to a regular string.

-        return Response(data='unknown data type {}.'.format(self.type),
+        return Response(data=f'unknown data type {self.type}.',
Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between bc17226 and e288d1b.
Files selected for processing (2)
  • changelog.d/20240514_153646_krishavrajsingh_server.md (1 hunks)
  • cvat/apps/engine/views.py (1 hunks)
Additional Context Used
LanguageTool (1)
changelog.d/20240514_153646_krishavrajsingh_server.md (1)

Near line 4: You might be missing the article “the” here.
Context: ### Fixed - Server now checks default_attribute and values...

Ruff (3)
cvat/apps/engine/views.py (3)

44-44: cvat.apps.dataset_manager.views imported but unused


1967-1967: f-string without any placeholders


2723-2723: f-string without any placeholders

Additional comments not posted (1)
cvat/apps/engine/views.py (1)

Line range hint 44-44: Remove unused import statement.

The import statement import cvat.apps.dataset_manager.views is not used anywhere in the file and can be safely removed.

changelog.d/20240514_153646_krishavrajsingh_server.md Outdated Show resolved Hide resolved
changelog.d/20240514_153646_krishavrajsingh_server.md Outdated Show resolved Hide resolved
cvat/apps/engine/views.py Outdated Show resolved Hide resolved
KrishavRajSingh and others added 2 commits May 14, 2024 17:12
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between e288d1b and 7574663.
Files selected for processing (1)
  • changelog.d/20240514_153646_krishavrajsingh_server.md (1 hunks)
Additional Context Used
LanguageTool (1)
changelog.d/20240514_153646_krishavrajsingh_server.md (1)

Near line 3: You might be missing the article “the” here.
Context: ### Fixed - Server now checks default_attribute and values...

changelog.d/20240514_153646_krishavrajsingh_server.md Outdated Show resolved Hide resolved
@KrishavRajSingh
Copy link
Contributor Author

Should I move the logic to LabelSerializer??

Comment on lines 2286 to 2294
attributes = self.request.data.get('attributes', [])
for attribute in attributes:
if attribute.get('input_type') == 'checkbox':
def_val = attribute.get('default_value')
values = attribute.get('values', [])
if values and values[0] not in ['true', 'false']:
raise ValidationError('checkbox values must be either true or false')
if def_val and def_val not in ['true','false']:
raise ValidationError("default_value for checkbox must be either true or false")
Copy link
Member

@bsekachev bsekachev May 20, 2024

Choose a reason for hiding this comment

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

Hello,

  • We also need this check when we create attributes, not only when update.
  • Also code from these views not always executed. For example when we importing a task from a backup, it does not use this code (please, see: cvat/apps/engine/backup.py:_ImporterBase:_create_labels). That is why we should move all these Validation checks to AttributeSerializer.
  • On the other hand, incorrect values may already be saved in the database. And in this case we need a data migration file to fix the issue.

The issue is not so trivial as it may look. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will proceed with this:
[] Move the validation logic to 'AttributeSerializer'
[
] Make a data migration to fix existing invalid checkbox values in database.

Thanks for your detailed feedback. If you have furthur suggestions, please let me know.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 7574663 and fed500d.
Files selected for processing (3)
  • changelog.d/20240514_153646_krishavrajsingh_server.md (1 hunks)
  • cvat/apps/engine/migrations/0079_fix_checkbox_values.py (1 hunks)
  • cvat/apps/engine/serializers.py (1 hunks)
Additional Context Used
Ruff (2)
cvat/apps/engine/serializers.py (2)

728-728: f-string without any placeholders


1163-1163: f-string without any placeholders

Markdownlint (2)
changelog.d/20240514_153646_krishavrajsingh_server.md (2)

1: Expected: 1; Actual: 0; Below
Headings should be surrounded by blank lines


2: null
Lists should be surrounded by blank lines

Additional comments not posted (1)
cvat/apps/engine/migrations/0079_fix_checkbox_values.py (1)

19-25: The migration setup looks correct and well-structured.

Comment on lines +243 to +252
def validate(self, attr):
if attr.get('input_type') == 'checkbox':
def_val = attr.get('default_value')
value = attr.get('values')
attr_name = attr.get('name')
if value not in ['true', 'false']:
raise serializers.ValidationError(f"Checkbox value for attribute '{attr_name}' must be either true or false")
if def_val not in ['true', 'false']:
raise serializers.ValidationError(f"Default_value of checkbox for attribute '{attr_name}' must be either true or false")
return attr
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure consistent error messages for checkbox validation.

- raise serializers.ValidationError(f"Checkbox value for attribute '{attr_name}' must be either true or false")
- raise serializers.ValidationError(f"Default_value of checkbox for attribute '{attr_name}' must be either true or false")
+ raise serializers.ValidationError(f"Checkbox value for attribute '{attr_name}' must be 'true' or 'false'")
+ raise serializers.ValidationError(f"Default value of checkbox for attribute '{attr_name}' must be 'true' or 'false'")

The error messages should consistently use the format 'true' or 'false' with quotes to match the expected string values. Also, use "Default value" instead of "Default_value" for better readability.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
def validate(self, attr):
if attr.get('input_type') == 'checkbox':
def_val = attr.get('default_value')
value = attr.get('values')
attr_name = attr.get('name')
if value not in ['true', 'false']:
raise serializers.ValidationError(f"Checkbox value for attribute '{attr_name}' must be either true or false")
if def_val not in ['true', 'false']:
raise serializers.ValidationError(f"Default_value of checkbox for attribute '{attr_name}' must be either true or false")
return attr
def validate(self, attr):
if attr.get('input_type') == 'checkbox':
def_val = attr.get('default_value')
value = attr.get('values')
attr_name = attr.get('name')
if value not in ['true', 'false']:
raise serializers.ValidationError(f"Checkbox value for attribute '{attr_name}' must be 'true' or 'false'")
if def_val not in ['true', 'false']:
raise serializers.ValidationError(f"Default value of checkbox for attribute '{attr_name}' must be 'true' or 'false'")
return attr

@@ -0,0 +1,3 @@
### Fixed
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a blank line before the heading for better readability.

+ 
### Fixed

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
### Fixed
+
### Fixed

@@ -0,0 +1,3 @@
### Fixed
- Server now checks default_attribute and values for checkbox
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a blank line before the list for better readability.

+ 
- Server now checks default_attribute and values for checkbox

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
- Server now checks default_attribute and values for checkbox
- Server now checks default_attribute and values for checkbox

for attribute in Attribute.objects.filter(input_type='checkbox'):
if attribute.default_value not in ['true', 'false']:
attribute.default_value = 'false'
if attribute.values not in ['true', 'false']:
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure correct comparison for list values.

- if attribute.values not in ['true', 'false']:
+ if all(val not in ['true', 'false'] for val in attribute.values):

This change ensures that each value in the list is checked individually against 'true' or 'false'.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if attribute.values not in ['true', 'false']:
if all(val not in ['true', 'false'] for val in attribute.values):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

If a label has 2+ attributes, one of which 'expression', they will fail to update
2 participants