From 6f0bfb175b323e394b87453973cf626bc530a20e Mon Sep 17 00:00:00 2001 From: Pierre-antoine Comby Date: Sat, 2 May 2020 16:08:40 +0200 Subject: [PATCH] fix time error and explicit select query --- management/commands/import_nk15.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/management/commands/import_nk15.py b/management/commands/import_nk15.py index 7425963..e667f04 100644 --- a/management/commands/import_nk15.py +++ b/management/commands/import_nk15.py @@ -3,16 +3,19 @@ import json import datetime import re +import pytz + +import psycopg2 as pg +import psycopg2.extras as pge from django.core.management.base import BaseCommand from django.core.management import call_command -import psycopg2 as pg -import psycopg2.extras as pge from django.db import transaction from django.core.exceptions import ValidationError from django.utils.timezone import make_aware from django.db import IntegrityError from django.contrib.auth.models import User + from activity.models import ActivityType, Activity, Guest, Entry, GuestTransaction from note.models import Note from note.models import Alias @@ -159,16 +162,22 @@ def import_boutons(cur): @transaction.atomic def import_transaction(cur): idmin = 58770 - cur.execute("SELECT *, transactions.date AS transac_date\ - FROM transactions\ - LEFT JOIN adhesions ON transactions.id = adhesions.id\ + bde = Club.objects.get(name="BDE") + kfet = Club.objects.get(name="Kfet") + cur.execute("SELECT t.date AS transac_date, t.type, t.emetteur, t.destinataire, t.quantite, t.montant, t.description, t.valide, t.cantinvalidate, t.categorie\ + a.idbde,a.annee, a.wei, a.date AS adh_date, a.section + FROM transactions AS t\ + LEFT JOIN adhesions AS a ON t.id = a.idtransaction\ WHERE transactions.id> {}\ ORDER BY transactions.id;".format(idmin)) n = cur.rowcount for idx, row in enumerate(cur): update_line(idx, n, row["description"]) - # some date are set to None, use the previous one - date = row["transac_date"] + try: + date = make_aware(row["transac_date"]) + except (pytz.NonExistentTimeError, pytz.AmbiguousTimeError): + date = make_aware(row["transac_date"] + datetime.timedelta(hours=1) + obj_dict = { # "pk": row["id"], "destination_id": MAP_IDBDE[row["destinataire"]],