-
-
Notifications
You must be signed in to change notification settings - Fork 616
Expand file tree
/
Copy path0002_migrate_data.py
More file actions
55 lines (49 loc) · 1.73 KB
/
0002_migrate_data.py
File metadata and controls
55 lines (49 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from django.db import migrations
def migrate(apps, schema_editor):
Job = apps.get_model("api_app", "Job")
Analyzable = apps.get_model("analyzables_manager", "Analyzable")
for job in Job.objects.all().order_by("received_request_time"):
if job.is_sample:
obj, created = Analyzable.objects.get_or_create(
md5=job.md5,
defaults={
"file": job.file,
"mimetype": job.file_mimetype,
"name": job.file_name,
"md5": job.md5,
"classification": "sample",
"discovery_date": job.received_request_time,
},
)
if created:
p = job.file.path
try:
p.rename(p.parent / job.md5)
except Exception:
...
else:
job.file.name = job.md5
else:
obj, created = Analyzable.objects.get_or_create(
md5=job.md5,
defaults={
"name": job.observable_name,
"md5": job.md5,
"classification": job.observable_classification,
"discovery_date": job.received_request_time,
},
)
if created:
obj.full_clean()
obj.save()
job.analyzable = obj
job.save()
class Migration(migrations.Migration):
dependencies = [
("contenttypes", "0002_remove_content_type_name"),
("api_app", "0067_add_analyzable"),
("analyzables_manager", "0001_initial"),
]
operations = [
migrations.RunPython(migrate, migrations.RunPython.noop),
]