# Path: floreal/models.py
# class IdentifiedBySlug(models.Model):
#     class Meta:
# class Mapped(models.Model):
#     class Meta:
# class FlorealUser(IdentifiedBySlug, Mapped):
# class NetworkSubgroup(IdentifiedBySlug):
#     class Meta:
# class NetworkMembership(models.Model):
#     class Meta:
# class Network(IdentifiedBySlug, Mapped):
#     class Meta:
# class Delivery(IdentifiedBySlug):
#     class Meta:
# class Product(models.Model):
#     class Meta:
# class Purchase(models.Model):
#     class Meta:
# class Bestof(models.Model):
#     class Meta:
# class JournalEntry(models.Model):
# class AdminMessage(models.Model):
#     def slug_prefix(self):
#     def save(self, **kwargs):
#     def __str__(self):
#     def slug_prefix(self):
#     def display_name(self):
#     def display_number(self):
#     def uri(self):
#     def has_some_admin_rights(self):
#     def __str__(self) -> str:
#     def __str__(self):
#     def __str__(self):
#     def description_text(self):
#     def grouped(self):
#     def save(self, **kwargs):
#     def __str__(self):
#     def state_name(self):
#     def description_text(self):
#     def freeze_overdue_deliveries(cls):
#     def __str__(self):
#     def _normalize_unit(cls, u):
#     def save(
#         self, force_insert=False, force_update=False, using=None, update_fields=None
#     ):
#     def left(self):
#     def description_text(self):
#     def price(self):
#     def weight(self):
#     def packages(self):
#     def out_of_package(self):
#     def max_quantity(self):
#     def __str__(self, specify_user=False):
#     def __str__(self):
#     def update(cls):
#     def log(cls, u, fmt, *args, **kwargs):
#     def __str__(self):
#     def __str__(self):
#     (PREPARATION, ORDERING_ALL, ORDERING_ADMIN, FROZEN, TERMINATED) = "ABCDE"
#     STATE_CHOICES = {
#         PREPARATION: "En préparation",
#         ORDERING_ALL: "Ouverte",
#         ORDERING_ADMIN: "Admins",
#         FROZEN: "Gelée",
#         TERMINATED: "Terminée",
#     }
#     UNIT_AUTO_TRANSLATE = {
#         "1": "pièce",
#         "piece": "pièce",
#         "kilo": "kg",
#         "gr": "g",
#         "unité": "pièce",
#         "unite": "pièce",
#     }
#     UNIT_TRANSLATE_REGEXP = re.compile(r"""
#         ^
#         ([0-9]+) 
#         (?:  [,.]  ([0-9]+)  )?
#         \s*
#         ([A-Za-z]+)
#         $""", re.VERBOSE)


from django.core.management.base import BaseCommand
from types import SimpleNamespace
from floreal import models as m
from markdown import markdown
from django.db import IntegrityError, DataError
import json
import sys


def parse_pc(dump):
    # purchase: user, product, quantity
    print("\n * purchases")
    n = len(dump.purchase)
    p = int(n / 100)
    for i, pc in enumerate(dump.purchase.values()):
        if i % p == 0:
            sys.stdout.write(f"({i}/{n}) ")
            sys.stdout.flush()
        u = dump.user[pc.user]
        if not hasattr(u, "new_id"):
            continue  # deleted user
        pc_db, _ = m.Purchase.objects.get_or_create(
            user_id=u.new_id,
            product_id=dump.product[pc.product].new_id,
            defaults={"quantity": pc.quantity},
        )
        pc.new_id = pc_db.id
    print(" Done")

def parse_cd(dump):
    print(f" * candidacies")
    for cd in dump.candidacy.values():
        u = dump.user[cd.user]
        if not hasattr(u, "new_id"):
            continue  # deleted user
        sg = dump.subgroup[cd.subgroup]
        new_nw_id = dump.network[sg.network].new_id

# Path: floreal/models.py
# class IdentifiedBySlug(models.Model):
#     class Meta:
# class Mapped(models.Model):
#     class Meta:
# class FlorealUser(IdentifiedBySlug, Mapped):
# class NetworkSubgroup(IdentifiedBySlug):
#     class Meta:
# class NetworkMembership(models.Model):
#     class Meta:
# class Network(IdentifiedBySlug, Mapped):
#     class Meta:
# class Delivery(IdentifiedBySlug):
#     class Meta:
# class Product(models.Model):
#     class Meta:
# class Purchase(models.Model):
#     class Meta:
# class Bestof(models.Model):
#     class Meta:
# class JournalEntry(models.Model):
# class AdminMessage(models.Model):
#     def slug_prefix(self):
#     def save(self, **kwargs):
#     def __str__(self):
#     def slug_prefix(self):
#     def display_name(self):
#     def display_number(self):
#     def uri(self):
#     def has_some_admin_rights(self):
#     def __str__(self) -> str:
#     def __str__(self):
#     def __str__(self):
#     def description_text(self):
#     def grouped(self):
#     def save(self, **kwargs):
#     def __str__(self):
#     def state_name(self):
#     def description_text(self):
#     def freeze_overdue_deliveries(cls):
#     def __str__(self):
#     def _normalize_unit(cls, u):
#     def save(
#         self, force_insert=False, force_update=False, using=None, update_fields=None
#     ):
#     def left(self):
#     def description_text(self):
#     def price(self):
#     def weight(self):
#     def packages(self):
#     def out_of_package(self):
#     def max_quantity(self):
#     def __str__(self, specify_user=False):
#     def __str__(self):
#     def update(cls):
#     def log(cls, u, fmt, *args, **kwargs):
#     def __str__(self):
#     def __str__(self):
#     (PREPARATION, ORDERING_ALL, ORDERING_ADMIN, FROZEN, TERMINATED) = "ABCDE"
#     STATE_CHOICES = {
#         PREPARATION: "En préparation",
#         ORDERING_ALL: "Ouverte",
#         ORDERING_ADMIN: "Admins",
#         FROZEN: "Gelée",
#         TERMINATED: "Terminée",
#     }
#     UNIT_AUTO_TRANSLATE = {
#         "1": "pièce",
#         "piece": "pièce",
#         "kilo": "kg",
#         "gr": "g",
#         "unité": "pièce",
#         "unite": "pièce",
#     }
#     UNIT_TRANSLATE_REGEXP = re.compile(r"""
#         ^
#         ([0-9]+) 
#         (?:  [,.]  ([0-9]+)  )?
#         \s*
#         ([A-Za-z]+)
#         $""", re.VERBOSE)


# Path: floreal/management/commands/from_civam.py
from django.core.management.base import BaseCommand
from types import SimpleNamespace
from floreal import models as m
from markdown import markdown
from django.db import IntegrityError, DataError
import json
import sys



def parse_pc(dump):
    # purchase: user, product, quantity
    print("\n * purchases")
    n = len(dump.purchase)
    p = int(n / 100)
    for i, pc in enumerate(dump.purchase.values()):
        if i % p == 0:
            sys.stdout.write(f"({i}/{n}) ")
            sys.stdout.flush()
        u = dump.user[pc.user]
        if not hasattr(u, "new_id"):
            continue  # deleted user
        pc_db, _ = m.Purchase.objects.get_or_create(
            user_id=u.new_id,
            product_id=dump.product[pc.product].new_id,
            defaults={"quantity": pc.quantity},
        )
        pc.new_id = pc_db.id
    print(" Done")

def parse_cd(dump):
    print(f" * candidacies")
    for cd in dump.candidacy.values():
        u = dump.user[cd.user]
        if not hasattr(u, "new_id"):
            continue  # deleted user
        sg = dump.subgroup[cd.subgroup]
        new_nw_id = dump.network[sg.network].new_id

        nm, _ = m.NetworkMembership.objects.get_or_create(