migrate.py 54.9 KB
Newer Older
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
#!/usr/bin/env python3
# -*- coding:utf-8 -*
#
# Copyright 2016,2017
# - Skia <skia@libskia.so>
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License a published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
#
#

Skia's avatar
Skia committed
26 27 28 29
import MySQLdb
import os
import django
import random
30
import datetime
Skia's avatar
Skia committed
31
from io import StringIO
32
from pytz import timezone
Skia's avatar
Skia committed
33
from os import listdir
Skia's avatar
Skia committed
34 35

os.environ["DJANGO_SETTINGS_MODULE"] = "sith.settings"
Sli's avatar
Sli committed
36
os.environ["DJANGO_COLORS"] = "nocolor"
Skia's avatar
Skia committed
37 38 39 40
django.setup()

from django.db import IntegrityError
from django.conf import settings
Skia's avatar
Skia committed
41 42
from django.core.management import call_command
from django.db import connection
43
from django.forms import ValidationError
Skia's avatar
Skia committed
44
from django.core.files import File
Skia's avatar
Skia committed
45

Skia's avatar
Skia committed
46

Skia's avatar
Skia committed
47
from core.models import User, SithFile
Skia's avatar
Skia committed
48
from core.utils import doku_to_markdown, bbcode_to_markdown
Sli's avatar
Sli committed
49
from club.models import Club, Membership, Mailing, MailingSubscription
Sli's avatar
Sli committed
50 51 52 53 54 55 56 57 58 59
from counter.models import (
    Customer,
    Counter,
    Selling,
    Refilling,
    Product,
    ProductType,
    Permanency,
    Eticket,
)
Skia's avatar
Skia committed
60
from subscription.models import Subscription
61
from eboutic.models import Invoice, InvoiceItem
Sli's avatar
Sli committed
62 63 64 65 66 67 68 69 70 71
from accounting.models import (
    BankAccount,
    ClubAccount,
    GeneralJournal,
    Operation,
    AccountingType,
    Company,
    SimplifiedAccountingType,
    Label,
)
Skia's avatar
Skia committed
72
from sas.models import Album, Picture, PeoplePictureRelation
Sli's avatar
Sli committed
73 74 75 76 77 78 79
from forum.models import (
    Forum,
    ForumTopic,
    ForumMessage,
    ForumMessageMeta,
    ForumUserInfo,
)
Skia's avatar
Skia committed
80

Skia's avatar
Skia committed
81
db = MySQLdb.connect(**settings.OLD_MYSQL_INFOS)
82 83
start = datetime.datetime.now()

Sli's avatar
Sli committed
84

Skia's avatar
Skia committed
85 86 87 88 89
def reset_index(*args):
    sqlcmd = StringIO()
    call_command("sqlsequencereset", *args, stdout=sqlcmd)
    cursor = connection.cursor()
    cursor.execute(sqlcmd.getvalue())
Skia's avatar
Skia committed
90

Sli's avatar
Sli committed
91

Skia's avatar
Skia committed
92 93
def to_unicode(s):
    if s:
Sli's avatar
Sli committed
94
        return bytes(s, "cp1252", errors="replace").decode("utf-8", errors="replace")
Skia's avatar
Skia committed
95 96
    return ""

Skia's avatar
Skia committed
97

98 99
def migrate_core():
    def migrate_users():
Sli's avatar
Sli committed
100
        SEX = {"1": "MAN", "2": "WOMAN", None: "MAN"}
101
        TSHIRT = {
Sli's avatar
Sli committed
102 103 104 105 106 107 108 109 110 111 112
            None: "-",
            "": "-",
            "NULL": "-",
            "XS": "XS",
            "S": "S",
            "M": "M",
            "L": "L",
            "XL": "XL",
            "XXL": "XXL",
            "XXXL": "XXXL",
        }
113
        ROLE = {
Sli's avatar
Sli committed
114 115 116 117 118 119 120 121 122
            "doc": "DOCTOR",
            "etu": "STUDENT",
            "anc": "FORMER STUDENT",
            "ens": "TEACHER",
            "adm": "ADMINISTRATIVE",
            "srv": "SERVICE",
            "per": "AGENT",
            None: "",
        }
123
        DEPARTMENTS = {
Sli's avatar
Sli committed
124 125 126 127 128 129 130 131 132 133 134 135 136
            "tc": "TC",
            "gi": "GI",
            "gesc": "GESC",
            "na": "NA",
            "mc": "MC",
            "imap": "IMAP",
            "huma": "HUMA",
            "edim": "EDIM",
            "ee": "EE",
            "imsi": "IMSI",
            "truc": "NA",
            None: "NA",
        }
137 138

        def get_random_free_email():
Skia's avatar
Skia committed
139
            email = "no_email_%s@git.an" % random.randrange(4000, 40000)
140 141 142 143 144
            while User.objects.filter(email=email).exists():
                email = "no_email_%s@git.an" % random.randrange(4000, 40000)
            return email

        c = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
145 146
        c.execute(
            """
147 148 149 150 151 152 153 154 155 156 157
        SELECT *
        FROM utilisateurs utl
        LEFT JOIN utl_etu ue
        ON ue.id_utilisateur = utl.id_utilisateur
        LEFT JOIN utl_etu_utbm ueu
        ON ueu.id_utilisateur = utl.id_utilisateur
        LEFT JOIN utl_extra uxtra
        ON uxtra.id_utilisateur = utl.id_utilisateur
        LEFT JOIN loc_ville ville
        ON utl.id_ville = ville.id_ville
        -- WHERE utl.id_utilisateur = 9360
Sli's avatar
Sli committed
158 159
        """
        )
160 161 162 163
        User.objects.filter(id__gt=0).delete()
        print("Users deleted")

        for u in c:
Skia's avatar
Skia committed
164
            try:
165
                new = User(
Sli's avatar
Sli committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
                    id=u["id_utilisateur"],
                    last_name=to_unicode(u["nom_utl"]) or "Bou",
                    first_name=to_unicode(u["prenom_utl"]) or "Bi",
                    email=u["email_utl"],
                    second_email=u["email_utbm"] or "",
                    date_of_birth=u["date_naissance_utl"],
                    last_update=u["date_maj_utl"],
                    nick_name=to_unicode(u["surnom_utbm"]),
                    sex=SEX[u["sexe_utl"]],
                    tshirt_size=TSHIRT[u["taille_tshirt_utl"]],
                    role=ROLE[u["role_utbm"]],
                    department=DEPARTMENTS[u["departement_utbm"]],
                    dpt_option=to_unicode(u["filiere_utbm"]),
                    semester=u["semestre_utbm"] or 0,
                    quote=to_unicode(u["citation"]),
                    school=to_unicode(u["nom_ecole_etudiant"]),
                    promo=u["promo_utbm"] or 0,
                    forum_signature=to_unicode(u["signature_utl"]),
                    address=(
                        to_unicode(u["addresse_utl"])
                        + ", "
                        + to_unicode(u["cpostal_ville"])
                        + " "
                        + to_unicode(u["nom_ville"])
                    ),
                    parent_address=(
                        to_unicode(u["adresse_parents"])
                        + ", "
                        + to_unicode(u["cpostal_parents"])
                        + " "
                        + to_unicode(u["ville_parents"])
                    ),
                    phone=u["tel_portable_utl"] or "",
                    parent_phone=u["tel_parents"] or "",
                    is_subscriber_viewable=bool(u["publique_utl"]),
201 202 203 204 205 206 207 208 209 210
                )
                new.generate_username()
                new.set_password(str(random.randrange(1000000, 10000000)))
                new.save()
            except IntegrityError as e:
                if "Key (email)" in repr(e):
                    new.email = get_random_free_email()
                    new.save()
                    print("New email generated")
                else:
Sli's avatar
Sli committed
211
                    print("FAIL for user %s: %s" % (u["id_utilisateur"], repr(e)))
Skia's avatar
Skia committed
212
            except Exception as e:
Sli's avatar
Sli committed
213
                print("FAIL for user %s: %s" % (u["id_utilisateur"], repr(e)))
214 215
        c.close()
        print("Users migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
216
        print("Running time: %s" % (datetime.datetime.now() - start))
217 218 219 220 221 222 223 224

    def migrate_profile_pict():
        PROFILE_ROOT = "/data/matmatronch/"

        profile = SithFile.objects.filter(parent=None, name="profiles").first()
        profile.children.all().delete()
        print("Profiles pictures deleted")
        for filename in listdir(PROFILE_ROOT):
Sli's avatar
Sli committed
225
            if filename.split(".")[-2] != "mini":
226
                try:
Sli's avatar
Sli committed
227
                    uid = filename.split(".")[0].split("-")[0]
228 229
                    user = User.objects.filter(id=int(uid)).first()
                    if user:
Sli's avatar
Sli committed
230 231 232 233 234 235 236 237 238 239 240 241
                        f = File(open(PROFILE_ROOT + "/" + filename, "rb"))
                        f.name = f.name.split("/")[-1]
                        t = filename.split(".")[1]
                        new_file = SithFile(
                            parent=profile,
                            name=filename,
                            file=f,
                            owner=user,
                            is_folder=False,
                            mime_type="image/jpeg",
                            size=f.size,
                        )
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
                        if t == "identity":
                            new_file.save()
                            user.profile_pict = new_file
                            user.save()
                        elif t == "blouse":
                            new_file.save()
                            user.scrub_pict = new_file
                            user.save()
                        else:
                            new_file.save()
                            user.avatar_pict = new_file
                            user.save()
                except Exception as e:
                    print(repr(e))
        print("Profile pictures migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
257
        print("Running time: %s" % (datetime.datetime.now() - start))
Skia's avatar
Skia committed
258

259 260
    migrate_users()
    migrate_profile_pict()
Skia's avatar
Skia committed
261 262


263 264 265
def migrate_club():
    def migrate_clubs():
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
266 267
        cur.execute(
            """
268 269 270 271 272
        SELECT *
        FROM asso asso
        WHERE nom_unix_asso <> "ae"
            AND nom_unix_asso <> "bdf"
            AND nom_unix_asso <> "laverie"
Sli's avatar
Sli committed
273 274
        """
        )
275 276 277 278 279 280 281
        # club = cur.fetchone()
        # for k,v in club.items():
        #     print("%40s | %40s" % (k, v))

        for c in cur:
            try:
                new = Club(
Sli's avatar
Sli committed
282 283 284 285 286
                    id=c["id_asso"],
                    name=to_unicode(c["nom_asso"]),
                    unix_name=to_unicode(c["nom_unix_asso"]),
                    address=to_unicode(c["adresse_postale"]),
                )
Skia's avatar
Skia committed
287
                new.save()
288
            except Exception as e:
Sli's avatar
Sli committed
289 290 291
                print("FAIL for club %s: %s" % (c["nom_unix_asso"], repr(e)))
        cur.execute(
            """
292 293
        SELECT *
        FROM asso
Sli's avatar
Sli committed
294 295
        """
        )
296
        for c in cur:
Sli's avatar
Sli committed
297 298
            club = Club.objects.filter(id=c["id_asso"]).first()
            parent = Club.objects.filter(id=c["id_asso_parent"]).first()
299 300 301 302
            club.parent = parent
            club.save()
        cur.close()
        print("Clubs migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
303
        print("Running time: %s" % (datetime.datetime.now() - start))
304 305 306

    def migrate_club_memberships():
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
307 308
        cur.execute(
            """
309 310
        SELECT *
        FROM asso_membre
Sli's avatar
Sli committed
311 312
        """
        )
313 314 315 316 317

        Membership.objects.all().delete()
        print("Memberships deleted")
        for m in cur:
            try:
Sli's avatar
Sli committed
318 319
                club = Club.objects.filter(id=m["id_asso"]).first()
                user = User.objects.filter(id=m["id_utilisateur"]).first()
320 321
                if club and user:
                    new = Membership(
Sli's avatar
Sli committed
322 323 324 325 326 327 328 329
                        id=Membership.objects.count() + 1,
                        club=club,
                        user=user,
                        start_date=m["date_debut"],
                        end_date=m["date_fin"],
                        role=m["role"],
                        description=to_unicode(m["desc_role"]),
                    )
330 331
                    new.save()
            except Exception as e:
Sli's avatar
Sli committed
332
                print("FAIL for club membership %s: %s" % (m["id_asso"], repr(e)))
333 334
        cur.close()
        print("Clubs memberships migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
335
        print("Running time: %s" % (datetime.datetime.now() - start))
336

Skia's avatar
Skia committed
337
    # migrate_clubs()
338
    migrate_club_memberships()
Skia's avatar
Skia committed
339

Sli's avatar
Sli committed
340

Skia's avatar
Skia committed
341
def migrate_subscriptions():
Sli's avatar
Sli committed
342
    LOCATION = {5: "SEVENANS", 6: "BELFORT", 9: "MONTBELIARD", None: "SEVENANS"}
Skia's avatar
Skia committed
343
    TYPE = {
Sli's avatar
Sli committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357
        0: "un-semestre",
        1: "deux-semestres",
        2: "cursus-tronc-commun",
        3: "cursus-branche",
        4: "membre-honoraire",
        5: "assidu",
        6: "amicale/doceo",
        7: "reseau-ut",
        8: "crous",
        9: "sbarro/esta",
        10: "cursus-alternant",
        None: "un-semestre",
    }
    PAYMENT = {1: "CHECK", 2: "CARD", 3: "CASH", 4: "OTHER", 5: "EBOUTIC", 0: "OTHER"}
358
    cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
359 360
    cur.execute(
        """
Skia's avatar
Skia committed
361 362
    SELECT *
    FROM ae_cotisations
Sli's avatar
Sli committed
363 364
    """
    )
Skia's avatar
Skia committed
365 366 367 368 369

    Subscription.objects.all().delete()
    print("Subscriptions deleted")
    Customer.objects.all().delete()
    print("Customers deleted")
Skia's avatar
Skia committed
370
    for r in cur:
Skia's avatar
Skia committed
371
        try:
Sli's avatar
Sli committed
372
            user = User.objects.filter(id=r["id_utilisateur"]).first()
Skia's avatar
Skia committed
373 374
            if user:
                new = Subscription(
Sli's avatar
Sli committed
375 376 377 378 379 380 381 382
                    id=r["id_cotisation"],
                    member=user,
                    subscription_start=r["date_cotis"],
                    subscription_end=r["date_fin_cotis"],
                    subscription_type=TYPE[r["type_cotis"]],
                    payment_method=PAYMENT[r["mode_paiement_cotis"]],
                    location=LOCATION[r["id_comptoir"]],
                )
Skia's avatar
Skia committed
383 384
                new.save()
        except Exception as e:
Sli's avatar
Sli committed
385
            print("FAIL for subscription %s: %s" % (r["id_cotisation"], repr(e)))
Skia's avatar
Skia committed
386
    cur.close()
387
    print("Subscriptions migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
388 389
    print("Running time: %s" % (datetime.datetime.now() - start))

390 391 392 393

def migrate_counter():
    def update_customer_account():
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
394 395
        cur.execute(
            """
396 397 398 399
        SELECT *
        FROM ae_carte carte
        JOIN ae_cotisations cotis
        ON carte.id_cotisation = cotis.id_cotisation
Sli's avatar
Sli committed
400 401
        """
        )
402 403
        for r in cur:
            try:
Sli's avatar
Sli committed
404
                user = Customer.objects.filter(user_id=r["id_utilisateur"]).first()
405
                if user:
Sli's avatar
Sli committed
406
                    user.account_id = str(r["id_carte_ae"]) + r["cle_carteae"].lower()
407 408
                    user.save()
            except Exception as e:
Sli's avatar
Sli committed
409 410 411 412
                print(
                    "FAIL to update customer account for %s: %s"
                    % (r["id_cotisation"], repr(e))
                )
413 414
        cur.close()
        print("Customer accounts migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
415
        print("Running time: %s" % (datetime.datetime.now() - start))
416 417 418

    def migrate_counters():
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
419 420
        cur.execute(
            """
421 422
        SELECT *
        FROM cpt_comptoir
Sli's avatar
Sli committed
423 424
        """
        )
425 426 427
        Counter.objects.all().delete()
        for r in cur:
            try:
Sli's avatar
Sli committed
428
                club = Club.objects.filter(id=r["id_assocpt"]).first()
429
                new = Counter(
Sli's avatar
Sli committed
430 431 432 433 434
                    id=r["id_comptoir"],
                    name=to_unicode(r["nom_cpt"]),
                    club=club,
                    type="OFFICE",
                )
435 436
                new.save()
            except Exception as e:
Sli's avatar
Sli committed
437
                print("FAIL to migrate counter %s: %s" % (r["id_comptoir"], repr(e)))
438 439 440 441 442
        cur.close()
        eboutic = Counter.objects.filter(id=3).first()
        eboutic.type = "EBOUTIC"
        eboutic.save()
        print("Counters migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
443
        print("Running time: %s" % (datetime.datetime.now() - start))
444 445 446 447 448 449 450 451 452 453 454 455

    def reset_customer_amount():
        Refilling.objects.all().delete()
        Selling.objects.all().delete()
        Invoice.objects.all().delete()
        for c in Customer.objects.all():
            c.amount = 0
            c.save()
        print("Customer amount reset")

    def migrate_refillings():
        BANK = {
Sli's avatar
Sli committed
456 457 458 459 460 461 462 463 464 465 466 467 468 469
            0: "OTHER",
            1: "SOCIETE-GENERALE",
            2: "BANQUE-POPULAIRE",
            3: "BNP",
            4: "CAISSE-EPARGNE",
            5: "CIC",
            6: "CREDIT-AGRICOLE",
            7: "CREDIT-MUTUEL",
            8: "CREDIT-LYONNAIS",
            9: "LA-POSTE",
            100: "OTHER",
            None: "OTHER",
        }
        PAYMENT = {2: "CARD", 1: "CASH", 0: "CHECK"}
470
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
471 472
        cur.execute(
            """
473 474
        SELECT *
        FROM cpt_rechargements
Sli's avatar
Sli committed
475 476
        """
        )
477 478 479 480 481 482 483
        root_cust = Customer.objects.filter(user__id=0).first()
        mde = Counter.objects.filter(id=1).first()
        Refilling.objects.all().delete()
        print("Refillings deleted")
        fail = 100
        for r in cur:
            try:
Sli's avatar
Sli committed
484 485
                cust = Customer.objects.filter(user__id=r["id_utilisateur"]).first()
                user = User.objects.filter(id=r["id_utilisateur"]).first()
486 487 488 489
                if not cust:
                    if not user:
                        cust = root_cust
                    else:
Sli's avatar
Sli committed
490 491 492 493 494
                        cust = Customer(
                            user=user,
                            amount=0,
                            account_id=Customer.generate_account_id(fail),
                        )
495 496
                        cust.save()
                        fail += 1
Sli's avatar
Sli committed
497 498
                op = User.objects.filter(id=r["id_utilisateur_operateur"]).first()
                counter = Counter.objects.filter(id=r["id_comptoir"]).first()
499
                new = Refilling(
Sli's avatar
Sli committed
500 501 502 503 504 505 506 507 508
                    id=r["id_rechargement"],
                    counter=counter or mde,
                    customer=cust or root_cust,
                    operator=op or root_cust.user,
                    amount=r["montant_rech"] / 100,
                    payment_method=PAYMENT[r["type_paiement_rech"]],
                    bank=BANK[r["banque_rech"]],
                    date=r["date_rech"].replace(tzinfo=timezone("Europe/Paris")),
                )
509 510
                new.save()
            except Exception as e:
Sli's avatar
Sli committed
511 512 513 514
                print(
                    "FAIL to migrate refilling %s for %s: %s"
                    % (r["id_rechargement"], r["id_utilisateur"], repr(e))
                )
515 516
        cur.close()
        print("Refillings migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
517
        print("Running time: %s" % (datetime.datetime.now() - start))
518 519 520

    def migrate_typeproducts():
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
521 522
        cur.execute(
            """
523 524
        SELECT *
        FROM cpt_type_produit
Sli's avatar
Sli committed
525 526
        """
        )
527 528 529 530 531
        ProductType.objects.all().delete()
        print("Product types deleted")
        for r in cur:
            try:
                new = ProductType(
Sli's avatar
Sli committed
532 533 534 535
                    id=r["id_typeprod"],
                    name=to_unicode(r["nom_typeprod"]),
                    description=to_unicode(r["description_typeprod"]),
                )
536 537
                new.save()
            except Exception as e:
Sli's avatar
Sli committed
538 539 540
                print(
                    "FAIL to migrate product type %s: %s" % (r["nom_typeprod"], repr(e))
                )
541 542
        cur.close()
        print("Product types migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
543
        print("Running time: %s" % (datetime.datetime.now() - start))
544 545 546

    def migrate_products():
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
547 548
        cur.execute(
            """
549 550
        SELECT *
        FROM cpt_produits
Sli's avatar
Sli committed
551 552
        """
        )
553 554 555 556
        Product.objects.all().delete()
        print("Product deleted")
        for r in cur:
            try:
Sli's avatar
Sli committed
557 558
                type = ProductType.objects.filter(id=r["id_typeprod"]).first()
                club = Club.objects.filter(id=r["id_assocpt"]).first()
559
                new = Product(
Sli's avatar
Sli committed
560 561 562 563 564 565 566 567 568 569 570 571
                    id=r["id_produit"],
                    product_type=type,
                    name=to_unicode(r["nom_prod"]),
                    description=to_unicode(r["description_prod"]),
                    code=to_unicode(r["cbarre_prod"]),
                    purchase_price=r["prix_achat_prod"] / 100,
                    selling_price=r["prix_vente_prod"] / 100,
                    special_selling_price=r["prix_vente_barman_prod"] / 100,
                    club=club,
                    limit_age=r["mineur"] or 0,
                    tray=bool(r["plateau"]),
                )
572 573
                new.save()
            except Exception as e:
Sli's avatar
Sli committed
574
                print("FAIL to migrate product %s: %s" % (r["nom_prod"], repr(e)))
575 576
        cur.close()
        print("Product migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
577
        print("Running time: %s" % (datetime.datetime.now() - start))
578 579 580 581 582

    def migrate_product_pict():
        FILE_ROOT = "/data/files/"

        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
583 584
        cur.execute(
            """
585 586 587
        SELECT *
        FROM cpt_produits
        WHERE id_file IS NOT NULL
Sli's avatar
Sli committed
588 589
        """
        )
590 591
        for r in cur:
            try:
Sli's avatar
Sli committed
592
                prod = Product.objects.filter(id=r["id_produit"]).first()
593
                if prod:
Sli's avatar
Sli committed
594
                    f = File(open(FILE_ROOT + "/" + str(r["id_file"]) + ".1", "rb"))
595 596 597 598 599 600
                    f.name = prod.name
                    prod.icon = f
                    prod.save()
            except Exception as e:
                print(repr(e))
        print("Product pictures migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
601
        print("Running time: %s" % (datetime.datetime.now() - start))
602 603 604

    def migrate_products_to_counter():
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
605 606
        cur.execute(
            """
607 608
        SELECT *
        FROM cpt_mise_en_vente
Sli's avatar
Sli committed
609 610
        """
        )
611 612
        for r in cur:
            try:
Sli's avatar
Sli committed
613 614
                product = Product.objects.filter(id=r["id_produit"]).first()
                counter = Counter.objects.filter(id=r["id_comptoir"]).first()
615 616 617
                counter.products.add(product)
                counter.save()
            except Exception as e:
Sli's avatar
Sli committed
618 619 620 621
                print(
                    "FAIL to set product %s in counter %s: %s"
                    % (product, counter, repr(e))
                )
622 623
        cur.close()
        print("Product in counters migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
624
        print("Running time: %s" % (datetime.datetime.now() - start))
625 626 627

    def migrate_invoices():
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
628 629
        cur.execute(
            """
630 631 632 633 634
        SELECT *
        FROM cpt_vendu ven
        LEFT JOIN cpt_debitfacture fac
        ON ven.id_facture = fac.id_facture
        WHERE fac.mode_paiement = 'SG'
Sli's avatar
Sli committed
635 636
        """
        )
637 638 639 640 641 642 643 644 645
        Invoice.objects.all().delete()
        print("Invoices deleted")
        Refilling.objects.filter(payment_method="CARD").delete()
        print("Card refillings deleted")
        Selling.objects.filter(payment_method="CARD").delete()
        print("Card sellings deleted")
        root = User.objects.filter(id=0).first()
        for r in cur:
            try:
Sli's avatar
Sli committed
646 647 648 649 650
                product = Product.objects.filter(id=r["id_produit"]).first()
                user = User.objects.filter(id=r["id_utilisateur_client"]).first()
                i = Invoice.objects.filter(id=r["id_facture"]).first() or Invoice(
                    id=r["id_facture"]
                )
651 652 653 654
                i.user = user or root
                for f in i._meta.local_fields:
                    if f.name == "date":
                        f.auto_now = False
Sli's avatar
Sli committed
655
                i.date = r["date_facture"].replace(tzinfo=timezone("Europe/Paris"))
656
                i.save()
Sli's avatar
Sli committed
657 658 659 660 661 662 663 664
                InvoiceItem(
                    invoice=i,
                    product_id=product.id,
                    product_name=product.name,
                    type_id=product.product_type.id,
                    product_unit_price=r["prix_unit"] / 100,
                    quantity=r["quantite"],
                ).save()
665 666 667
            except ValidationError as e:
                print(repr(e) + " for %s (%s)" % (customer, customer.user.id))
            except Exception as e:
Sli's avatar
Sli committed
668
                print("FAIL to migrate invoice %s: %s" % (r["id_facture"], repr(e)))
669 670
        cur.close()
        for i in Invoice.objects.all():
671 672 673
            for f in i._meta.local_fields:
                if f.name == "date":
                    f.auto_now = False
674 675
            i.validate()
        print("Invoices migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
676
        print("Running time: %s" % (datetime.datetime.now() - start))
677 678 679

    def migrate_sellings():
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
680 681
        cur.execute(
            """
682 683 684 685 686
        SELECT *
        FROM cpt_vendu ven
        LEFT JOIN cpt_debitfacture fac
        ON ven.id_facture = fac.id_facture
        WHERE fac.mode_paiement = 'AE'
Sli's avatar
Sli committed
687 688
        """
        )
689 690 691 692 693 694 695 696 697 698 699
        Selling.objects.filter(payment_method="SITH_ACCOUNT").delete()
        print("Sith account selling deleted")
        for c in Customer.objects.all():
            c.amount = sum([r.amount for r in c.refillings.all()])
            c.save()
        print("Customer amount reset to sum of refillings")
        ae = Club.objects.filter(unix_name="ae").first()
        mde = Counter.objects.filter(id=1).first()
        root = User.objects.filter(id=0).first()
        beer = Product.objects.filter(id=1).first()
        for r in cur:
Skia's avatar
Skia committed
700
            try:
Sli's avatar
Sli committed
701 702 703 704 705 706 707 708
                product = Product.objects.filter(id=r["id_produit"]).first() or beer
                club = Club.objects.filter(id=r["id_assocpt"]).first() or ae
                counter = Counter.objects.filter(id=r["id_comptoir"]).first() or mde
                op = User.objects.filter(id=r["id_utilisateur"]).first() or root
                customer = (
                    Customer.objects.filter(user__id=r["id_utilisateur_client"]).first()
                    or root.customer
                )
709
                new = Selling(
Sli's avatar
Sli committed
710 711 712 713 714 715 716 717 718 719 720
                    label=product.name or "Produit inexistant",
                    counter=counter,
                    club=club,
                    product=product,
                    seller=op,
                    customer=customer,
                    unit_price=r["prix_unit"] / 100,
                    quantity=r["quantite"],
                    payment_method="SITH_ACCOUNT",
                    date=r["date_facture"].replace(tzinfo=timezone("Europe/Paris")),
                )
721 722 723 724
                new.save()
            except ValidationError as e:
                print(repr(e) + " for %s (%s)" % (customer, customer.user.id))
            except Exception as e:
Sli's avatar
Sli committed
725
                print("FAIL to migrate selling %s: %s" % (r["id_facture"], repr(e)))
726 727
        cur.close()
        print("Sellings migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
728
        print("Running time: %s" % (datetime.datetime.now() - start))
729 730 731

    def migrate_permanencies():
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
732 733
        cur.execute(
            """
734 735
        SELECT *
        FROM cpt_tracking
Sli's avatar
Sli committed
736 737
        """
        )
738 739 740
        Permanency.objects.all().delete()
        print("Permanencies deleted")
        for r in cur:
Skia's avatar
Skia committed
741
            try:
Sli's avatar
Sli committed
742 743
                counter = Counter.objects.filter(id=r["id_comptoir"]).first()
                user = User.objects.filter(id=r["id_utilisateur"]).first()
744
                new = Permanency(
Sli's avatar
Sli committed
745 746 747 748 749 750
                    user=user,
                    counter=counter,
                    start=r["logged_time"].replace(tzinfo=timezone("Europe/Paris")),
                    activity=r["logged_time"].replace(tzinfo=timezone("Europe/Paris")),
                    end=r["closed_time"].replace(tzinfo=timezone("Europe/Paris")),
                )
751
                new.save()
Skia's avatar
Skia committed
752
            except Exception as e:
753 754 755
                print("FAIL to migrate permanency: %s" % (repr(e)))
        cur.close()
        print("Permanencies migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
756
        print("Running time: %s" % (datetime.datetime.now() - start))
Skia's avatar
Skia committed
757

Skia's avatar
Skia committed
758 759 760 761 762 763 764 765 766 767 768
    update_customer_account()
    migrate_counters()
    migrate_permanencies()
    migrate_typeproducts()
    migrate_products()
    migrate_product_pict()
    migrate_products_to_counter()
    reset_customer_amount()
    migrate_invoices()
    migrate_refillings()
    migrate_sellings()
769

Sli's avatar
Sli committed
770

Skia's avatar
Skia committed
771 772
def check_accounts():
    cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
773 774
    cur.execute(
        """
Skia's avatar
Skia committed
775 776
    SELECT *
    FROM utilisateurs
Sli's avatar
Sli committed
777 778
    """
    )
Skia's avatar
Skia committed
779
    mde = Counter.objects.filter(id=1).first()
Sli's avatar
Sli committed
780
    ae = Club.objects.filter(unix_name="ae").first()
Skia's avatar
Skia committed
781 782
    root = User.objects.filter(id=0).first()
    for r in cur:
Sli's avatar
Sli committed
783
        if r["montant_compte"] and r["montant_compte"] > 0:
Skia's avatar
Skia committed
784
            try:
Sli's avatar
Sli committed
785 786 787 788 789 790
                cust = Customer.objects.filter(user__id=r["id_utilisateur"]).first()
                if int(cust.amount * 100) != r["montant_compte"]:
                    print(
                        "Adding %s to %s's account"
                        % (float(cust.amount) - (r["montant_compte"] / 100), cust.user)
                    )
Skia's avatar
Skia committed
791
                    new = Selling(
Sli's avatar
Sli committed
792 793 794 795 796 797 798 799 800 801
                        label="Ajustement migration base de donnée",
                        counter=mde,
                        club=ae,
                        product=None,
                        seller=root,
                        customer=cust,
                        unit_price=float(cust.amount) - (r["montant_compte"] / 100.0),
                        quantity=1,
                        payment_method="SITH_ACCOUNT",
                    )
Skia's avatar
Skia committed
802 803 804
                    new.save()
            except Exception as e:
                print("FAIL to adjust user account: %s" % (repr(e)))
Sli's avatar
Sli committed
805 806


807 808
### Accounting

Sli's avatar
Sli committed
809

810 811 812
def migrate_accounting():
    def migrate_companies():
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
813 814
        cur.execute(
            """
815 816
        SELECT *
        FROM entreprise
Sli's avatar
Sli committed
817 818
        """
        )
819 820 821 822 823
        Company.objects.all().delete()
        print("Company deleted")
        for r in cur:
            try:
                new = Company(
Sli's avatar
Sli committed
824 825 826 827 828 829 830 831 832 833
                    id=r["id_ent"],
                    name=to_unicode(r["nom_entreprise"]),
                    street=to_unicode(r["rue_entreprise"]),
                    city=to_unicode(r["ville_entreprise"]),
                    postcode=to_unicode(r["cpostal_entreprise"]),
                    country=to_unicode(r["pays_entreprise"]),
                    phone=to_unicode(r["telephone_entreprise"]),
                    email=to_unicode(r["email_entreprise"]),
                    website=to_unicode(r["siteweb_entreprise"]),
                )
834 835 836 837 838
                new.save()
            except Exception as e:
                print("FAIL to migrate company: %s" % (repr(e)))
        cur.close()
        print("Companies migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
839
        print("Running time: %s" % (datetime.datetime.now() - start))
840 841 842

    def migrate_bank_accounts():
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
843 844
        cur.execute(
            """
845 846
        SELECT *
        FROM cpta_cpbancaire
Sli's avatar
Sli committed
847 848
        """
        )
849 850
        BankAccount.objects.all().delete()
        print("Bank accounts deleted")
Sli's avatar
Sli committed
851
        ae = Club.objects.filter(unix_name="ae").first()
852 853 854
        for r in cur:
            try:
                new = BankAccount(
Sli's avatar
Sli committed
855 856
                    id=r["id_cptbc"], club=ae, name=to_unicode(r["nom_cptbc"])
                )
857 858 859 860 861
                new.save()
            except Exception as e:
                print("FAIL to migrate bank account: %s" % (repr(e)))
        cur.close()
        print("Bank accounts migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
862
        print("Running time: %s" % (datetime.datetime.now() - start))
863 864 865

    def migrate_club_accounts():
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
866 867
        cur.execute(
            """
868 869
        SELECT *
        FROM cpta_cpasso
Sli's avatar
Sli committed
870 871
        """
        )
872 873 874 875 876
        ClubAccount.objects.all().delete()
        print("Club accounts deleted")
        ae = Club.objects.filter(id=1).first()
        for r in cur:
            try:
Sli's avatar
Sli committed
877 878
                club = Club.objects.filter(id=r["id_asso"]).first() or ae
                bank_acc = BankAccount.objects.filter(id=r["id_cptbc"]).first()
879
                new = ClubAccount(
Sli's avatar
Sli committed
880 881 882 883 884
                    id=r["id_cptasso"],
                    club=club,
                    name=club.name[:30],
                    bank_account=bank_acc,
                )
885 886 887 888 889
                new.save()
            except Exception as e:
                print("FAIL to migrate club account: %s" % (repr(e)))
        cur.close()
        print("Club accounts migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
890
        print("Running time: %s" % (datetime.datetime.now() - start))
891 892 893

    def migrate_journals():
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
894 895
        cur.execute(
            """
896 897
        SELECT *
        FROM cpta_classeur
Sli's avatar
Sli committed
898 899
        """
        )
900 901 902 903
        GeneralJournal.objects.all().delete()
        print("General journals deleted")
        for r in cur:
            try:
Sli's avatar
Sli committed
904
                club_acc = ClubAccount.objects.filter(id=r["id_cptasso"]).first()
905
                new = GeneralJournal(
Sli's avatar
Sli committed
906 907 908 909 910 911 912
                    id=r["id_classeur"],
                    club_account=club_acc,
                    name=to_unicode(r["nom_classeur"]),
                    start_date=r["date_debut_classeur"],
                    end_date=r["date_fin_classeur"],
                    closed=bool(r["ferme"]),
                )
913 914 915 916 917
                new.save()
            except Exception as e:
                print("FAIL to migrate general journal: %s" % (repr(e)))
        cur.close()
        print("General journals migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
918
        print("Running time: %s" % (datetime.datetime.now() - start))
919 920

    def migrate_accounting_types():
Sli's avatar
Sli committed
921
        MOVEMENT = {-1: "DEBIT", 0: "NEUTRAL", 1: "CREDIT"}
922
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
923 924
        cur.execute(
            """
925 926
        SELECT *
        FROM cpta_op_plcptl
Sli's avatar
Sli committed
927 928
        """
        )
929 930 931 932 933
        AccountingType.objects.all().delete()
        print("Accounting types deleted")
        for r in cur:
            try:
                new = AccountingType(
Sli's avatar
Sli committed
934 935 936 937 938
                    id=r["id_opstd"],
                    code=str(r["code_plan"]),
                    label=to_unicode(r["libelle_plan"]).capitalize(),
                    movement_type=MOVEMENT[r["type_mouvement"]],
                )
939 940 941 942 943
                new.save()
            except Exception as e:
                print("FAIL to migrate accounting type: %s" % (repr(e)))
        cur.close()
        print("Accounting types migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
944
        print("Running time: %s" % (datetime.datetime.now() - start))
945 946 947

    def migrate_simpleaccounting_types():
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
948 949
        cur.execute(
            """
950 951 952
        SELECT *
        FROM cpta_op_clb
        WHERE id_asso IS NULL
Sli's avatar
Sli committed
953 954
        """
        )
955 956 957 958
        SimplifiedAccountingType.objects.all().delete()
        print("Simple accounting types deleted")
        for r in cur:
            try:
Sli's avatar
Sli committed
959
                at = AccountingType.objects.filter(id=r["id_opstd"]).first()
960
                new = SimplifiedAccountingType(
Sli's avatar
Sli committed
961 962 963 964
                    id=r["id_opclb"],
                    label=to_unicode(r["libelle_opclb"]).capitalize(),
                    accounting_type=at,
                )
965 966 967 968 969
                new.save()
            except Exception as e:
                print("FAIL to migrate simple type: %s" % (repr(e)))
        cur.close()
        print("Simple accounting types migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
970
        print("Running time: %s" % (datetime.datetime.now() - start))
971

Skia's avatar
Skia committed
972 973
    def migrate_labels():
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
974 975
        cur.execute(
            """
Skia's avatar
Skia committed
976 977 978
        SELECT *
        FROM cpta_libelle
        WHERE id_asso IS NOT NULL
Sli's avatar
Sli committed
979 980
        """
        )
Skia's avatar
Skia committed
981 982 983 984
        Label.objects.all().delete()
        print("Labels deleted")
        for r in cur:
            try:
Sli's avatar
Sli committed
985
                club_accounts = ClubAccount.objects.filter(club__id=r["id_asso"]).all()
Skia's avatar
Skia committed
986
                for ca in club_accounts:
Sli's avatar
Sli committed
987
                    new = Label(club_account=ca, name=to_unicode(r["nom_libelle"]))
Skia's avatar
Skia committed
988 989 990 991 992
                    new.save()
            except Exception as e:
                print("FAIL to migrate label: %s" % (repr(e)))
        cur.close()
        print("Labels migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
993
        print("Running time: %s" % (datetime.datetime.now() - start))
Skia's avatar
Skia committed
994

995 996
    def migrate_operations():
        MODE = {
Sli's avatar
Sli committed
997 998 999 1000 1001 1002 1003 1004
            1: "CHECK",
            2: "CASH",
            3: "TRANSFERT",
            4: "CARD",
            0: "CASH",
            None: "CASH",
        }
        MOVEMENT_TYPE = {-1: "DEBIT", 0: "NEUTRAL", 1: "CREDIT", None: "NEUTRAL"}
1005
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
1006 1007
        cur.execute(
            """
1008 1009 1010 1011
        SELECT *
        FROM cpta_operation op
        LEFT JOIN cpta_op_clb clb
        ON op.id_opclb = clb.id_opclb
Skia's avatar
Skia committed
1012 1013
        LEFT JOIN cpta_libelle lab
        ON op.id_libelle = lab.id_libelle
Sli's avatar
Sli committed
1014 1015
        """
        )
1016 1017 1018 1019 1020 1021
        Operation.objects.all().delete()
        print("Operation deleted")
        for r in cur:
            try:
                simple_type = None
                accounting_type = None
Skia's avatar
Skia committed
1022
                label = None
Sli's avatar
Sli committed
1023 1024 1025 1026 1027 1028 1029 1030
                if r["id_opclb"]:
                    simple_type = SimplifiedAccountingType.objects.filter(
                        id=r["id_opclb"]
                    ).first()
                if r["id_opstd"]:
                    accounting_type = AccountingType.objects.filter(
                        id=r["id_opstd"]
                    ).first()
1031 1032 1033
                if not accounting_type and simple_type:
                    accounting_type = simple_type.accounting_type
                if not accounting_type:
Sli's avatar
Sli committed
1034 1035 1036 1037 1038 1039 1040 1041 1042
                    accounting_type = AccountingType.objects.filter(
                        movement_type=MOVEMENT_TYPE[r["type_mouvement"]]
                    ).first()
                journal = GeneralJournal.objects.filter(id=r["id_classeur"]).first()
                if r["id_libelle"]:
                    label = journal.club_account.labels.filter(
                        name=to_unicode(r["nom_libelle"])
                    ).first()

1043
                def get_target_type():
Sli's avatar
Sli committed
1044
                    if r["id_utilisateur"]:
1045
                        return "USER"
Sli's avatar
Sli committed
1046
                    if r["id_asso"]:
1047
                        return "CLUB"
Sli's avatar
Sli committed
1048
                    if r["id_ent"]:
1049
                        return "COMPANY"
Sli's avatar
Sli committed
1050
                    if r["id_classeur"]:
1051
                        return "ACCOUNT"
Sli's avatar
Sli committed
1052

1053
                def get_target_id():
Sli's avatar
Sli committed
1054 1055 1056 1057 1058 1059
                    return (
                        int(
                            r["id_utilisateur"]
                            or r["id_asso"]
                            or r["id_ent"]
                            or r["id_classeur"]
1060
                        )
Sli's avatar
Sli committed
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
                        or None
                    )

                new = Operation(
                    id=r["id_op"],
                    journal=journal,
                    amount=r["montant_op"] / 100,
                    date=r["date_op"] or journal.end_date,
                    remark=to_unicode(r["commentaire_op"]),
                    mode=MODE[r["mode_op"]],
                    cheque_number=str(r["num_cheque_op"]),
                    done=bool(r["op_effctue"]),
                    simpleaccounting_type=simple_type,
                    accounting_type=accounting_type,
                    target_type=get_target_type(),
                    target_id=get_target_id(),
                    target_label="-",
                    label=label,
                )
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
                try:
                    new.clean()
                except:
                    new.target_id = get_target_id()
                    new.target_type = "OTHER"
                new.save()
            except Exception as e:
                print("FAIL to migrate operation: %s" % (repr(e)))
        cur.close()
        print("Operations migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
1090
        print("Running time: %s" % (datetime.datetime.now() - start))
1091 1092 1093

    def make_operation_links():
        cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
1094 1095
        cur.execute(
            """
1096 1097
        SELECT *
        FROM cpta_operation
Sli's avatar
Sli committed
1098 1099
        """
        )
1100
        for r in cur:
Sli's avatar
Sli committed
1101
            if r["id_op_liee"]:
1102
                try:
Sli's avatar
Sli committed
1103 1104
                    op1 = Operation.objects.filter(id=r["id_op"]).first()
                    op2 = Operation.objects.filter(id=r["id_op_liee"]).first()
1105 1106 1107 1108 1109 1110 1111 1112
                    op1.linked_operation = op2
                    op1.save()
                    op2.linked_operation = op1
                    op2.save()
                except Exception as e:
                    print("FAIL to link operations: %s" % (repr(e)))
        cur.close()
        print("Operations links migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
1113
        print("Running time: %s" % (datetime.datetime.now() - start))
1114

1115
    migrate_companies()
Skia's avatar
Skia committed
1116 1117 1118 1119
    migrate_accounting_types()
    migrate_simpleaccounting_types()
    migrate_bank_accounts()
    migrate_club_accounts()
Skia's avatar
Skia committed
1120
    migrate_labels()
Skia's avatar
Skia committed
1121 1122
    migrate_journals()
    migrate_operations()
Skia's avatar
Skia committed
1123
    make_operation_links()
1124

Sli's avatar
Sli committed
1125

Skia's avatar
Skia committed
1126 1127
def migrate_godfathers():
    cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
1128 1129
    cur.execute(
        """
Skia's avatar
Skia committed
1130 1131
    SELECT *
    FROM parrains
Sli's avatar
Sli committed
1132 1133
    """
    )
Skia's avatar
Skia committed
1134 1135
    for r in cur:
        try:
Sli's avatar
Sli committed
1136 1137
            father = User.objects.filter(id=r["id_utilisateur"]).first()
            child = User.objects.filter(id=r["id_utilisateur_fillot"]).first()
Skia's avatar
Skia committed
1138 1139 1140 1141 1142 1143
            father.godchildren.add(child)
            father.save()
        except Exception as e:
            print("FAIL to migrate godfathering: %s" % (repr(e)))
    cur.close()
    print("Godfathers migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
1144 1145
    print("Running time: %s" % (datetime.datetime.now() - start))

Skia's avatar
Skia committed
1146

Skia's avatar
Skia committed
1147 1148 1149
def migrate_etickets():
    FILE_ROOT = "/data/files/"
    cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
1150 1151
    cur.execute(
        """
Skia's avatar
Skia committed
1152 1153
    SELECT *
    FROM cpt_etickets
Sli's avatar
Sli committed
1154 1155
    """
    )
Skia's avatar
Skia committed
1156
    Eticket.objects.all().delete()
Skia's avatar
Skia committed
1157
    print("Etickets deleted")
Skia's avatar
Skia committed
1158 1159
    for r in cur:
        try:
Sli's avatar
Sli committed
1160
            p = Product.objects.filter(id=r["id_produit"]).first()
Skia's avatar
Skia committed
1161
            try:
Sli's avatar
Sli committed
1162
                f = File(open(FILE_ROOT + "/" + str(r["banner"]) + ".1", "rb"))
Skia's avatar
Skia committed
1163 1164 1165
            except:
                f = None
            e = Eticket(
Sli's avatar
Sli committed
1166 1167
                product=p, secret=to_unicode(r["secret"]), banner=f, event_title=p.name
            )
Skia's avatar
Skia committed
1168
            e.save()
Sli's avatar
Sli committed
1169
            e.secret = to_unicode(r["secret"])
Skia's avatar
Skia committed
1170 1171 1172 1173 1174
            e.save()
        except Exception as e:
            print("FAIL to migrate eticket: %s" % (repr(e)))
    cur.close()
    print("Etickets migrated at %s" % datetime.datetime.now())
Sli's avatar
Sli committed
1175 1176
    print("Running time: %s" % (datetime.datetime.now() - start))

Skia's avatar
Skia committed
1177

Skia's avatar
Skia committed
1178 1179 1180 1181
def migrate_sas():
    album_link = {}
    picture_link = {}
    FILE_ROOT = "/data/sas/"
Skia's avatar
Skia committed
1182
    SithFile.objects.filter(id__gte=18892).delete()
Skia's avatar
Skia committed
1183
    print("Album/Pictures deleted")
Sli's avatar
Sli committed
1184
    reset_index("core", "sas")
Skia's avatar
Skia committed
1185
    cur = db.cursor(MySQLdb.cursors.SSDictCursor)
Sli's avatar
Sli committed
1186 1187
    cur.execute(
        """
Skia's avatar
Skia committed
1188 1189
    SELECT *
    FROM sas_cat_photos
Sli's avatar
Sli committed
1190 1191
    """
    )
Skia's avatar
Skia committed
1192 1193 1194
    root = User.objects.filter(username="root").first()
    for r in cur:
        try:
Sli's avatar
Sli committed
1195 1196 1197 1198 1199 1200
            a = Album(
                name=to_unicode(r["nom_catph"]),
                owner=root,
                is_moderated=True,
                parent=None,
            )
Skia's avatar
Skia committed
1201
            a.save()
Sli's avatar
Sli committed
1202
            album_link[str(r["id_catph"])] = a.id
Skia's avatar
Skia committed
1203 1204
        except Exception as e:
            print("FAIL to migrate Album: %s" % (repr(e)))
Skia's avatar
Skia committed
1205
    print("Album moved, need to make the tree")
Sli's avatar
Sli committed
1206 1207
    cur.execute(
        """
Skia's avatar
Skia committed
1208 1209
    SELECT *
    FROM sas_cat_photos
Sli's avatar
Sli committed
1210 1211
    """
    )
Skia's avatar
Skia committed
1212 1213
    for r in cur:
        try:
Sli's avatar
Sli committed
1214 1215
            p = Album.objects.filter(id=album_link[str(r["id_catph_parent"])]).first()
            a = Album.objects.filter(id=album_link[str(r["id_catph"])]).first()
Skia's avatar
Skia committed
1216 1217
            a.parent = p
            a.save()