Skip to content

Commit

Permalink
Tender siae_transactioned: track changes
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Mar 8, 2024
1 parent 236b75e commit 7759d64
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lemarche/siaes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ def __init__(self, *args, **kwargs):
"""
https://stackoverflow.com/a/23363123
"""
super(Siae, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
for field_name in self.TRACK_UPDATE_FIELDS:
setattr(self, f"__previous_{field_name}", getattr(self, field_name))

Expand Down
28 changes: 28 additions & 0 deletions lemarche/tenders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ class Tender(models.Model):
FIELDS_STATS = FIELDS_STATS_COUNT + FIELDS_STATS_TIMESTAMPS + ["marche_benefits"]
READONLY_FIELDS = FIELDS_SURVEY_TRANSACTIONED + FIELDS_STATS

TRACK_UPDATE_FIELDS = [
# set last_updated fields
"siae_transactioned",
]

# used in templates
STATUS_DRAFT = tender_constants.STATUS_DRAFT
STATUS_PUBLISHED = tender_constants.STATUS_PUBLISHED
Expand Down Expand Up @@ -596,6 +601,14 @@ class Meta:
def __str__(self):
return self.title

def __init__(self, *args, **kwargs):
"""
https://stackoverflow.com/a/23363123
"""
super().__init__(*args, **kwargs)
for field_name in self.TRACK_UPDATE_FIELDS:
setattr(self, f"__previous_{field_name}", getattr(self, field_name))

def set_slug(self, with_uuid=False):
"""
The slug field should be unique.
Expand All @@ -605,6 +618,19 @@ def set_slug(self, with_uuid=False):
if with_uuid:
self.slug += f"-{str(uuid4())[:4]}"

def set_last_updated_fields(self):
"""
We track changes on some fields, in order to update their 'last_updated' counterpart.
Where are the '__previous' fields set? In the __init__ method
"""
for field_name in self.TRACK_UPDATE_FIELDS:
previous_field_name = f"__previous_{field_name}"
if getattr(self, field_name) and getattr(self, field_name) != getattr(self, previous_field_name):
try:
setattr(self, f"{field_name}_last_updated", timezone.now())
except AttributeError: # TRACK_UPDATE_FIELDS without last_updated fields
pass

def set_siae_found_list(self):
"""
Where the Tender-Siae matching magic happens!
Expand Down Expand Up @@ -648,10 +674,12 @@ def set_siae_found_list(self):

def save(self, *args, **kwargs):
"""
- update the "last_updated" fields
- update the object stats
- update the object content_fill_dates
- generate the slug field
"""
self.set_last_updated_fields()
try:
self.set_slug()
with transaction.atomic():
Expand Down
4 changes: 4 additions & 0 deletions lemarche/www/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1770,12 +1770,16 @@ def test_only_tender_author_with_sesame_token_can_call_tender_survey_transaction
# full form displayed (but should never happen)

def test_update_tender_stats_on_tender_survey_transactioned_answer_true(self):
self.assertIsNone(Tender.objects.get(id=self.tender.id).survey_transactioned_answer)
self.assertIsNone(Tender.objects.get(id=self.tender.id).siae_transactioned)
self.assertIsNone(Tender.objects.get(id=self.tender.id).siae_transactioned_last_updated)
# load with answer 'True': partial form
url = self.url + self.user_buyer_1_sesame_query_string + "&answer=True"
response = self.client.get(url, follow=True)
self.assertEqual(response.status_code, 200)
self.assertTrue(Tender.objects.get(id=self.tender.id).survey_transactioned_answer)
self.assertTrue(Tender.objects.get(id=self.tender.id).siae_transactioned)
self.assertIsNotNone(Tender.objects.get(id=self.tender.id).siae_transactioned_last_updated)
# fill in form
response = self.client.post(
url, data={"survey_transactioned_amount": 1000, "survey_transactioned_feedback": "Feedback"}, follow=True
Expand Down
22 changes: 9 additions & 13 deletions lemarche/www/tenders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,13 +616,11 @@ def get(self, request, *args, **kwargs):
if survey_transactioned_answer in ["True", "False"]:
# transform survey_transactioned_answer into bool
survey_transactioned_answer = survey_transactioned_answer == "True"
# update survey_transactioned_answer
Tender.objects.filter(id=self.object.id).update(
survey_transactioned_answer=survey_transactioned_answer,
survey_transactioned_answer_date=timezone.now(),
siae_transactioned=survey_transactioned_answer,
updated_at=timezone.now(),
)
# update tender
self.object.survey_transactioned_answer = survey_transactioned_answer
self.object.survey_transactioned_answer_date = timezone.now()
self.object.siae_transactioned = survey_transactioned_answer
self.object.save()
else:
pass
# TODO or not? "answer" should always be passed
Expand Down Expand Up @@ -683,12 +681,10 @@ def get(self, request, *args, **kwargs):
if survey_transactioned_answer in ["True", "False"]:
# transform survey_transactioned_answer into bool
survey_transactioned_answer = survey_transactioned_answer == "True"
# update survey_transactioned_answer
TenderSiae.objects.filter(id=self.object.id).update(
survey_transactioned_answer=survey_transactioned_answer,
survey_transactioned_answer_date=timezone.now(),
updated_at=timezone.now(),
)
# update tender
self.object.survey_transactioned_answer = survey_transactioned_answer
self.object.survey_transactioned_answer_date = timezone.now()
self.object.save()
else:
pass
# TODO or not? "answer" should always be passed
Expand Down

0 comments on commit 7759d64

Please sign in to comment.