Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #916 from yverdon/feature/improve_fixturize
Browse files Browse the repository at this point in the history
improve fixturize to be dynamic and use args
  • Loading branch information
AlexandreJunod authored Jan 17, 2024
2 parents d478d32 + aad04d4 commit 9654517
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 228 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@ docker-compose run web scripts/migrate.sh

**Load demo data**

Edit file `geocity/apps/submissions/management/commands/fixturize_data/fixturize.py`
There is some examples in same folder and you can create new ones
Create a file in `geocity/apps/submissions/management/commands/fixturize_data/`.

There is some examples in same folder and you can create new ones.

The file will automatically appear once it's created.

`docker-compose run web python manage.py fixturize` is interactive and can use some arguments as :

- `--help` : Shows description of command and available arguments
- `--list` : List available files to be used as fixtures
- `--file` : Use a specific file. Example : `--file=agenda`
- no args : Asks if you want to run default fixture file

```bash
# Load demo data
Expand Down
77 changes: 65 additions & 12 deletions geocity/apps/submissions/management/commands/fixturize.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import importlib
import os
import re
import shutil
import sys
import unicodedata
from io import StringIO

from constance import config
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
Expand All @@ -20,9 +24,6 @@
from geocity.apps.reports.models import *
from geocity.apps.submissions.models import *

# import fixturize file
from ..fixturize_data.generic_example import *


def strip_accents(text):
"""
Expand Down Expand Up @@ -114,10 +115,62 @@ def setup_media(images_folder):


class Command(BaseCommand):
help = "Create default datas using fixtures for dev, pre-prod or demo environment."

def add_arguments(self, parser):
parser.add_argument("--file", type=str, help="Define the fixture file to run")
parser.add_argument(
"--list", action="store_true", help="List available fixture files"
)

def handle(self, *args, **options):
if settings.ENV.upper() != "DEV":
self.stdout.write(
self.style.ERROR("Les fixtures ne peuvent être exécutés qu'en DEV")
)
sys.exit()

directory_path = "geocity/apps/submissions/management/fixturize_data"
available_fixtures = [
os.path.splitext(f)[0]
for f in os.listdir(directory_path)
if os.path.isfile(os.path.join(directory_path, f))
]

if options["list"]:
self.stdout.write(self.style.SUCCESS("Liste des fichiers disponibles"))
self.stdout.write("- " + "\n- ".join(available_fixtures))
sys.exit()
elif options["file"]:
file_name = options["file"]
self.stdout.write((f"Fichier spécifié : {file_name}"))

if file_name in available_fixtures:
module = importlib.import_module(
f"..fixturize_data.{file_name}", package=__package__
)
else:
self.stdout.write(self.style.ERROR("Ce fichier n'existe pas"))
sys.exit()
else:
self.stdout.write(
self.style.SUCCESS(
"Aucune option spécifiée. Utilisez --show-help pour afficher les commandes disponibles."
)
)
user_input = input(
"Voulez-vous executer les fixtures par défaut ? [Y/n] : "
)
if user_input.lower() not in {"y", "yes", ""}:
sys.exit()

module = importlib.import_module(
f"..fixturize_data.generic", package=__package__
)

self.stdout.write("Resetting database...")
reset_db()
setup_media(images_folder)
setup_media(module.images_folder)
self.stdout.write("")
self.stdout.write("░██████╗███████╗███████╗██████╗░")
self.stdout.write("██╔════╝██╔════╝██╔════╝██╔══██╗")
Expand All @@ -130,17 +183,17 @@ def handle(self, *args, **options):
with transaction.atomic():
self.stdout.write("Creating default site...")
self.setup_necessary_default_site()
for idx, (domain, entity) in enumerate(entities.items()):
for idx, (domain, entity) in enumerate(module.entities.items()):
self.stdout.write(f"Entity : {entity}")
self.stdout.write(" • Creating site...")
self.setup_site(entity)
self.stdout.write(" • Creating administrative entity...")
administrative_entity = self.create_administrative_entity(
entity, ofs_ids[idx], geoms[idx]
entity, module.ofs_ids[idx], module.geoms[idx]
)
self.stdout.write(" • Creating users...")
integrator_group = self.create_users(
iterations, entity, domain, administrative_entity
module.iterations, entity, domain, administrative_entity
)
self.stdout.write(" • Setting administrative_entity integrator...")
self.setup_administrative_entity_integrator(
Expand All @@ -152,24 +205,24 @@ def handle(self, *args, **options):
" • Setting form, form categories and complementary document type..."
)
self.setup_form_and_form_categories(
form_categories,
module.form_categories,
integrator_group,
form_additional_information,
module.form_additional_information,
administrative_entity,
)
self.stdout.write(" • Creating submissions...")
self.setup_submission(
entity,
iterations.get("user_iterations"),
module.iterations.get("user_iterations"),
administrative_entity,
small_text,
module.small_text,
)
self.stdout.write(" • Creating default report...")
Report.create_default_report(administrative_entity.id)
self.stdout.write("Creating template customizations...")
self.create_template_customization()
self.stdout.write("Setup template customizations...")
self.setup_homepage(entities, iterations)
self.setup_homepage(module.entities, module.iterations)
self.stdout.write("Fixturize succeed ✔")

def setup_necessary_default_site(self):
Expand Down
214 changes: 0 additions & 214 deletions geocity/apps/submissions/management/fixturize_data/fixturize.py

This file was deleted.

0 comments on commit 9654517

Please sign in to comment.