|
2 | 2 |
|
3 | 3 | from django.db import migrations, models |
4 | 4 | # import django.db.models.deletion |
5 | | -from tagging.registry import register as tag_register |
6 | 5 | from django.forms.models import model_to_dict |
7 | 6 | import tagulous.models.fields |
8 | 7 | import tagulous.models.models |
|
12 | 11 |
|
13 | 12 |
|
14 | 13 | class Migration(migrations.Migration): |
15 | | - |
16 | | - def copy_existing_tags_to_tags_from_django_tagging_field(apps, schema_editor): |
17 | | - # We can't import the models directly as it may be a newer |
18 | | - # version than this migration expects. We use the historical version. |
19 | | - logger.info('Migrating tags from django-tagging to django-tagulous step1. Enable DEBUG logging to find out more.') |
20 | | - import tagulous.utils |
21 | | - # for model_name in ['Product']: |
22 | | - for model_name in ['Product', 'test', 'finding', 'engagement', 'endpoint', 'finding_template', 'app_Analysis', 'objects']: |
23 | | - model_class = apps.get_model('dojo', model_name) |
24 | | - # the get_model returns a fake class proxy, which is not registered with django-tagging |
25 | | - tag_register(model_class) |
26 | | - |
27 | | - for obj in model_class.objects.all(): |
28 | | - # logger.debug('%s:%s:%s', model_class, obj.id, obj) |
29 | | - if obj.tags: |
30 | | - tags_as_string = tagulous.utils.render_tags(obj.tags.all()) |
31 | | - logger.debug('%s:%s:%s: found tags: %s', model_class, obj.id, obj, tags_as_string) |
32 | | - obj.tags_from_django_tagging = tags_as_string |
33 | | - # obj.description = tags_as_string |
34 | | - # finding.save() doesn't look at push_all_jira_issue, so we should be good |
35 | | - # if model_name == 'finding2': |
36 | | - # obj.save(dedupe_option=False, rules_option=False, issue_updater_option=False, push_to_jira=False) |
37 | | - # else: |
38 | | - try: |
39 | | - if hasattr(obj, 'prod_type_id') and obj.prod_type_id == 0: |
40 | | - logger.warning('product found without product type (prod_type==0), changing to: "_tag migration lost and found" product type') |
41 | | - Product_Type_Model = apps.get_model('dojo', 'Product_Type') |
42 | | - prod_type_lost_and_found, created = Product_Type_Model.objects.get_or_create(name='_tag migration lost and found') |
43 | | - obj.prod_type = prod_type_lost_and_found |
44 | | - obj.save() |
45 | | - logger.warning('product type successfully changed to %i', prod_type_lost_and_found.id) |
46 | | - |
47 | | - obj.save() |
48 | | - except Exception as e: |
49 | | - logger.error('Error saving old existing django-tagging tags to new string field') |
50 | | - logger.error('Details of object:') |
51 | | - logger.error(vars(obj)) |
52 | | - logger.error('Model to dict:') |
53 | | - logger.error(model_to_dict(obj)) |
54 | | - |
55 | | - def copy_tags_from_django_tagging_field_to_new_tagulous_tags_field(apps, schema_editor): |
56 | | - # We can't import the models directly as it may be a newer |
57 | | - # version than this migration expects. We use the historical version. |
58 | | - logger.info('Migrating tags from django-tagging to django-tagulous step2. Enable DEBUG logging to find out more.') |
59 | | - # for model_name in ['Product']: |
60 | | - for model_name in ['Product', 'test', 'finding', 'engagement', 'endpoint', 'finding_template', 'app_Analysis', 'objects_product']: |
61 | | - model_class = apps.get_model('dojo', model_name) |
62 | | - |
63 | | - for obj in model_class.objects.all(): |
64 | | - # logger.debug('%s:%s:%s', model_class, obj.id, obj) |
65 | | - if obj.tags_from_django_tagging: |
66 | | - logger.debug('%s:%s:%s: found tags: %s', model_class, obj.id, obj, obj.tags_from_django_tagging) |
67 | | - obj.tags = obj.tags_from_django_tagging |
68 | | - |
69 | | - try: |
70 | | - obj.save() |
71 | | - except Exception as e: |
72 | | - logger.error('Error saving tags to new tagulous m2m field') |
73 | | - logger.error('Details of object:') |
74 | | - logger.error(vars(obj)) |
75 | | - logger.error('Model to dict:') |
76 | | - logger.error(model_to_dict(obj)) |
77 | | - |
78 | 14 | dependencies = [ |
79 | 15 | ('dojo', '0065_delete_empty_jira_project_configs'), |
80 | 16 | ] |
@@ -121,8 +57,6 @@ def copy_tags_from_django_tagging_field_to_new_tagulous_tags_field(apps, schema_ |
121 | 57 | field=models.TextField(blank=True, editable=False, help_text='Temporary archive with tags from the previous tagging library we used'), |
122 | 58 | ), |
123 | 59 |
|
124 | | - migrations.RunPython(copy_existing_tags_to_tags_from_django_tagging_field, migrations.RunPython.noop), |
125 | | - |
126 | 60 | migrations.RenameModel('Objects', 'Objects_Product'), |
127 | 61 |
|
128 | 62 | migrations.CreateModel( |
@@ -309,6 +243,4 @@ def copy_tags_from_django_tagging_field_to_new_tagulous_tags_field(apps, schema_ |
309 | 243 | name='match_field', |
310 | 244 | field=models.CharField(choices=[('id', 'id'), ('title', 'title'), ('date', 'date'), ('cwe', 'cwe'), ('cve', 'cve'), ('cvssv3', 'cvssv3'), ('url', 'url'), ('severity', 'severity'), ('description', 'description'), ('mitigation', 'mitigation'), ('impact', 'impact'), ('steps_to_reproduce', 'steps_to_reproduce'), ('severity_justification', 'severity_justification'), ('references', 'references'), ('test', 'test'), ('is_template', 'is_template'), ('active', 'active'), ('verified', 'verified'), ('false_p', 'false_p'), ('duplicate', 'duplicate'), ('duplicate_finding', 'duplicate_finding'), ('out_of_scope', 'out_of_scope'), ('under_review', 'under_review'), ('review_requested_by', 'review_requested_by'), ('under_defect_review', 'under_defect_review'), ('defect_review_requested_by', 'defect_review_requested_by'), ('is_Mitigated', 'is_Mitigated'), ('thread_id', 'thread_id'), ('mitigated', 'mitigated'), ('mitigated_by', 'mitigated_by'), ('reporter', 'reporter'), ('numerical_severity', 'numerical_severity'), ('last_reviewed', 'last_reviewed'), ('last_reviewed_by', 'last_reviewed_by'), ('line_number', 'line_number'), ('sourcefilepath', 'sourcefilepath'), ('sourcefile', 'sourcefile'), ('param', 'param'), ('payload', 'payload'), ('hash_code', 'hash_code'), ('line', 'line'), ('file_path', 'file_path'), ('component_name', 'component_name'), ('component_version', 'component_version'), ('static_finding', 'static_finding'), ('dynamic_finding', 'dynamic_finding'), ('created', 'created'), ('scanner_confidence', 'scanner_confidence'), ('sonarqube_issue', 'sonarqube_issue'), ('unique_id_from_tool', 'unique_id_from_tool'), ('vuln_id_from_tool', 'vuln_id_from_tool'), ('sast_source_object', 'sast_source_object'), ('sast_sink_object', 'sast_sink_object'), ('sast_source_line', 'sast_source_line'), ('sast_source_file_path', 'sast_source_file_path'), ('nb_occurences', 'nb_occurences')], max_length=200), |
311 | 245 | ), |
312 | | - |
313 | | - migrations.RunPython(copy_tags_from_django_tagging_field_to_new_tagulous_tags_field, migrations.RunPython.noop), |
314 | 246 | ] |
0 commit comments