Command

Créer une Commande

Help

Entrées & sorties

www/core/management/commands/populate_medical_center.py

  from django.core.management.base import BaseCommand

  from core.models.medical_center import MedicalCenter

  class Command(BaseCommand):
      """
      Usage: python manage.py populate_medical_center
      """
      help = 'Populate the database with init data for Medical Centers.'

      def handle(self, *args, **options):
          MedicalCenter.objects.all().delete()

          self.medical_center(
              name='CHU Nice',
              address_name='CHU DE NICE – Hôpital Pasteur 1',
              address_name_comp=(
                  'Service de Pneumologie – Oncologie Thoracique '
                  'et Soins Intensifs Respiratoires'
              ),
              address_text='30 Voie Romaine, 06000 Nice',
              finess='060785003',
              phone='04 92 03 77 67',
          )
          self.medical_center(
              name='Centre de radiologie Nice',
              address_text='Vallon de La Lauvette, 06300 Nice',
          )
          self.stdout.write('')

      def medical_center(self, **kwargs):
          MedicalCenter(**kwargs).save()

          self.stdout.write('.', ending='')
  

Paramètres

www/core/management/commands/testset_user_unvalidate.py

  from django.core.management.base import BaseCommand

  from core.models.user import User


  class Command(BaseCommand):
      help = 'Mark a user account as waiting validation'

      def add_arguments(self, parser):
          parser.add_argument(
              '--email',
              type=str,
              default='jacques-boutros@dacapo.fr',
              help='Run for the given email',
          )

      def handle(self, *args, **options):
          email = options['email']
          if not email:
              return

          try:
              user = User.objects.get(email=email)
          except User.DoesNotExist:
              self.stderr.write(f"Please create the user '{email}'")
              return

          self._update(user)

      def _update(self, user):
          user.is_active = False
          user.is_waiting_validation = True
          user.save(update_fields=[
              'is_active',
              'is_waiting_validation',
          ])

          self.stdout.write(f"'{user.email}' (#{user.id}) is waiting validation.")
  

Progress bar

www/lib/study/management/commands/resave_study.py

  from tqdm import tqdm

  from django.core.management.base import BaseCommand

  from lib.study.models import Study


  class Command(BaseCommand):
      help = 'Resave all studies to process pre/post save hooks again'

      def add_arguments(self, parser):
          parser.add_argument(
              '--ids',
              nargs='+',
              type=int,
              default=None,
              help='Run only for the given IDs',
          )

      def handle(self, *args, **options):
          if ids := options['ids']:
              qs = Study.objects.filter(id__in=ids)
          else:
              qs = Study.objects.all()

          for record in tqdm(qs, desc='Update study'):
              record.save()
  

Écraser une commande

www/core/management/commands/makemessages.py

  from django.core.management.commands import makemessages


  class Command(makemessages.Command):
      msgmerge_options = ['-q', '--previous', '--no-fuzzy-matching']
  

assertStatus

Django-admin

Cheatsheet commandes django-admin