Skip to content

Commit

Permalink
Refactoring : remplacer datetime par timezone (#952)
Browse files Browse the repository at this point in the history
* Tender set_validated: use timezone

* log: use isoformat

* Replace datetime with timezone
  • Loading branch information
raphodn authored Oct 26, 2023
1 parent f8db4a8 commit 4138de9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
10 changes: 5 additions & 5 deletions lemarche/siaes/management/commands/sync_c1_c4.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import re
from datetime import datetime, timedelta
from datetime import timedelta

import psycopg2
import psycopg2.extras
Expand Down Expand Up @@ -360,8 +360,8 @@ def c4_delist_old_siae(self, dry_run):
- all the ones who have is_active as False
"""
if not dry_run:
date_yesterday = datetime.now() - timedelta(days=1)
Siae.objects.exclude(c1_sync_skip=True).filter(
c1_last_sync_date__lt=timezone.make_aware(date_yesterday)
).update(is_delisted=True)
date_yesterday = timezone.now() - timedelta(days=1)
Siae.objects.exclude(c1_sync_skip=True).filter(c1_last_sync_date__lt=date_yesterday).update(
is_delisted=True
)
Siae.objects.filter(is_active=False).update(is_delisted=True)
6 changes: 3 additions & 3 deletions lemarche/siaes/management/commands/sync_c2_c4.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from datetime import datetime, timedelta
from datetime import timedelta

import psycopg2
import psycopg2.extras
Expand Down Expand Up @@ -64,10 +64,10 @@ def handle(self, dry_run=False, **options):

# count after
siae_etp_count_after = Siae.objects.filter(c2_etp_count__isnull=False).count()
date_yesterday = datetime.now() - timedelta(days=1)
date_yesterday = timezone.now() - timedelta(days=1)
siae_etp_updated = (
Siae.objects.filter(c2_etp_count__isnull=False)
.filter(c2_etp_count_last_sync_date__gte=timezone.make_aware(date_yesterday))
.filter(c2_etp_count_last_sync_date__gte=date_yesterday)
.count()
)

Expand Down
5 changes: 3 additions & 2 deletions lemarche/stats/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime, timedelta
from datetime import timedelta

from django.db import models
from django.utils import timezone

from lemarche.siaes import constants as siae_constants
from lemarche.utils import constants
Expand All @@ -17,7 +18,7 @@ def siae_views_last_3_months(self, siae_slug):
return self.env_prod().filter(
action="load",
page=f"/prestataires/{siae_slug}/",
date_created__gte=datetime.now() - timedelta(days=90),
date_created__gte=timezone.now() - timedelta(days=90),
)

def siae_buyer_views_last_3_months(self, siae_slug):
Expand Down
5 changes: 2 additions & 3 deletions lemarche/tenders/admin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from datetime import datetime

from ckeditor.widgets import CKEditorWidget
from django import forms
from django.contrib import admin
from django.contrib.contenttypes.admin import GenericTabularInline
from django.db import models
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.utils import timezone
from django.utils.html import format_html
from django_admin_filters import MultiChoice
from django_better_admin_arrayfield.admin.mixins import DynamicArrayMixin
Expand Down Expand Up @@ -109,7 +108,7 @@ def restart_send_tender_task(tender: Tender):
# 1) log the tender send restart
log_item = {
"action": "restart_send",
"date": str(datetime.now()),
"date": timezone.now().isoformat(),
}
tender.logs.append(log_item)
tender.save()
Expand Down
4 changes: 2 additions & 2 deletions lemarche/tenders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,11 +601,11 @@ def is_validated(self) -> bool:
return self.validated_at and self.status == self.STATUS_VALIDATED

def set_validated(self, with_save=True):
self.validated_at = datetime.now()
self.validated_at = timezone.now()
self.status = tender_constants.STATUS_VALIDATED
log_item = {
"action": "validate",
"date": str(self.validated_at),
"date": self.validated_at.isoformat(),
}
self.logs.append(log_item)
if with_save:
Expand Down

0 comments on commit 4138de9

Please sign in to comment.