-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmeasures.py
93 lines (83 loc) · 3.88 KB
/
measures.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import time
from django.db import connection
from common.models.transactions import Transaction
from common.models.utils import override_current_transaction
from open_data.models import ReportMeasure
from open_data.models import ReportMeasureCondition
def update_measure_components(verbose):
# Unless there is a current transaction, reading the latest description will fail in a misterious way
# Because this is called in a command, there is no transaction set"""
counter = 0
tx = Transaction.objects.last()
start = time.time()
if verbose:
print("Updating measure components")
with override_current_transaction(tx):
measures_qs = (
ReportMeasure.objects.filter(sid__gte=20000000)
.only("trackedmodel_ptr")
.select_related("trackedmodel_ptr")
)
component_list = []
for measure in measures_qs:
counter += 1
if counter % 1000 == 0:
print(f"Measure count {counter}")
# comp_counter = 0
for (
component
) in (
measure.trackedmodel_ptr.conditions.latest_approved().with_reference_price_string()
):
# comp_counter += 1
# print(f" Condition count {comp_counter}")
component_list.append(
ReportMeasureCondition(
trackedmodel_ptr_id=component.trackedmodel_ptr_id,
sid=component.sid,
component_sequence_number=component.component_sequence_number,
duty_amount=component.duty_amount,
action_id=component.action_id,
condition_code_id=component.condition_code_id,
condition_measurement_id=component.condition_measurement_id,
dependent_measure_id=measure.trackedmodel_ptr_id,
monetary_unit_id=component.monetary_unit_id,
required_certificate_id=component.required_certificate_id,
reference_price=component.reference_price_string,
),
)
ReportMeasureCondition.objects.bulk_create(component_list)
print("Completed Measure condition creation")
# The required_certificate_id is not updated when the certificate is updated
# In the UI it works because the certificate is selected using the SID and
# 'approved to last Transaction'. In data workspace works because when a
# certificate is updated, only the validity is changed so even if the data is not read from the latest,
# the SID is correct. I am not sure what is the best way to fix this!!!
# I'll try patching the required_certificate_id and hope for the best
fk_query_list = ReportMeasureCondition.update_fk_queries()
if fk_query_list:
with connection.cursor() as cursor:
for query in fk_query_list:
cursor.execute(query)
if verbose:
print(f"Elapsed time {time.time() - start}")
def update_measure(verbose):
# Unless there is a current transaction, reading the latest description will fail in a misterious way
# Because this is called in a command, there is no transaction set"""
tx = Transaction.objects.last()
start = time.time()
if verbose:
print("Updating measure")
with override_current_transaction(tx):
measures_qs = (
ReportMeasure.objects.filter(sid__gte=20000000)
.only("trackedmodel_ptr", "duty_sentence")
.select_related("trackedmodel_ptr")
)
for measure in measures_qs:
duty_sentence = measure.trackedmodel_ptr.duty_sentence
if duty_sentence:
measure.duty_sentence = duty_sentence
measure.save()
if verbose:
print(f"Elapsed time {time.time() - start}")