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

Fix Pylint warnings in migrations #8869

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cvat/apps/engine/migrations/0024_auto_20191023_1025.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def migrate_task_data(db_task_id, db_data_id, original_video, original_images, s
compressed_chunk_path = os.path.join(compressed_cache_dir, '{}.zip'.format(chunk_idx))
compressed_chunk_writer.save_as_chunk(chunk_images, compressed_chunk_path)

preview = reader.get_preview()
preview = reader.get_preview(0)
preview.save(os.path.join(db_data_dir, 'preview.jpeg'))
else:
original_chunk_writer = ZipChunkWriter(100)
Expand Down Expand Up @@ -146,7 +146,7 @@ def migrate_task_data(db_task_id, db_data_id, original_video, original_images, s
original_chunk_path = os.path.join(original_cache_dir, '{}.zip'.format(chunk_idx))
original_chunk_writer.save_as_chunk(chunk_images, original_chunk_path)

preview = reader.get_preview()
preview = reader.get_preview(0)
preview.save(os.path.join(db_data_dir, 'preview.jpeg'))
shutil.rmtree(old_db_task_dir)
return_dict[db_task_id] = (True, '')
Expand Down
12 changes: 6 additions & 6 deletions cvat/apps/engine/migrations/0034_auto_20201125_1426.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import django.db.models.deletion

def create_profile(apps, schema_editor):
User = apps.get_model('auth', 'User')
Profile = apps.get_model('engine', 'Profile')
for user in User.objects.all():
profile = Profile()
profile.user = user
profile.save()
User = apps.get_model('auth', 'User')
Profile = apps.get_model('engine', 'Profile')
for user in User.objects.all():
profile = Profile()
profile.user = user
profile.save()
Comment on lines +9 to +14
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Prevent potential profile duplication issues
Creating a profile for every user without checking if a user profile already exists can lead to collisions or integrity errors, especially if some users already have associated profiles. Consider verifying the existence of a profile before creating a new one.

Below is a possible fix:

 def create_profile(apps, schema_editor):
     User = apps.get_model('auth', 'User')
     Profile = apps.get_model('engine', 'Profile')
     for user in User.objects.all():
-        profile = Profile()
-        profile.user = user
-        profile.save()
+        if not Profile.objects.filter(user=user).exists():
+            Profile.objects.create(user=user)
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
User = apps.get_model('auth', 'User')
Profile = apps.get_model('engine', 'Profile')
for user in User.objects.all():
profile = Profile()
profile.user = user
profile.save()
User = apps.get_model('auth', 'User')
Profile = apps.get_model('engine', 'Profile')
for user in User.objects.all():
if not Profile.objects.filter(user=user).exists():
Profile.objects.create(user=user)


class Migration(migrations.Migration):

Expand Down
2 changes: 1 addition & 1 deletion cvat/apps/engine/migrations/0038_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def migrate2manifest(apps, shema_editor):

if db_data.storage == StorageChoice.SHARE:
def _get_frame_step(str_):
match = search("step\s*=\s*([1-9]\d*)", str_)
match = search(r"step\s*=\s*([1-9]\d*)", str_)
return int(match.group(1)) if match else 1
logger.info('Data is located on the share, metadata update has been started')
manifest.step = _get_frame_step(db_data.frame_filter)
Expand Down
Loading