diff --git a/apps/wrapped/management/commands/generate_wrapped.py b/apps/wrapped/management/commands/generate_wrapped.py new file mode 100644 index 00000000..47848ba7 --- /dev/null +++ b/apps/wrapped/management/commands/generate_wrapped.py @@ -0,0 +1,211 @@ +# Copyright (C) 2028-2024 by BDE ENS Paris-Saclay +# SPDX-License-Identifier: GPL-3.0-or-later + +### Import ### + +from argparse import ArgumentParser +from django.core.management import BaseCommand + +from note.models import Note +from member.models import User, Club +from ...models import Bde + +######################################################################################################## +""" +Cahier des charges : + + - Un script lisible + - Avec beaucoup d'options + - Personnalisable +""" +######################################################################################################## + + +class Command(BaseCommand): + help = "Generate wrapper for the annual BDE change" + + def add_arguments(self, parser: ArgumentParser): + parser.add_argument( + '-b', '--bde', + type=str, + required=False, + help="A list of BDE name, BDE1,BDE2,... (a BDE name cannot have ',')", + dest='bde', + ) + parser.add_argument( + '-i', '--id', + type=str, + required=False, + help="A list of BDE id, id1,id2,...", + dest='bde_id', + ) + parser.add_argument( + '-u', '--users', + type=str, + required=False, + help="""User will have their(s) wrapped generated, + all = all users + adh = all users who have a valid memberships to BDE during the BDE considered + supersuser = all superusers + custom user1,user2,... = a list of username, + custom_id id1,id2,... = a list of user id""", + dest='user', + ) + parser.add_argument( + '-c', '--club', + type=str, + required=False, + help="""Club will have their(s) wrapped generated, + all = all clubs, + active = all clubs with at least one transaction during the BDE mandate considered, + custom club1,club2,... = a list of club name, + custom_id id1,id2,... = a list of club id""", + dest='club', + ) + parser.add_argument( + '-f', '--force', + required=False, + action='store_true', + help="if wrapped already exist delete it and regenerate", + dest='force', + ) + parser.add_argument( + '-d', '--delete', + required=False, + action='store_true', + help="delete all wrapped selectionned and don't regenerate them", + dest='delete', + ) + + + def handle(self, *args, **options): + # useful string for output + red = '\033[31;1m' + yellow = '\033[33;1m' + green = '\033[32;1m' + abort = red + 'ABORT' + warning = yellow + 'WARNING' + success = green + 'SUCCESS' + + ################################### + #### Traitement des paramètres #### + ################################### + verb = options['verbosity'] + bde = [] + if options['bde']: + bde_list = options['bde'].split(',') + bde = [Bde.objects.get(name=bde_name) for bde_name in bde_list] + + if options['bde_id']: + if bde: + if verb >= 1: + print(warning) + print(yellow + 'You already defined bde with their name !') + if verb >= 0: print(abort) + return + bde_id = options['bde_id'].split(',') + bde = [Bde.objects.get(pk=i) for i in bde_id] + + user = [] + if options['user']: + if options['user'] == 'all': + user = ['all',None] + elif options['user'] == 'adh': + user = ['adh',None] + elif options['user'] == 'superuser': + user = ['superuser',None] + elif options['user'].split(' ')[0] == 'custom': + user_list = options['user'].split(' ')[1].split(',') + user = ['custom', [User.objects.get(username=u) for u in user_list]] + elif options['user'].split(' ')[0] == 'custom_id': + user_id = options['user'].split(' ')[1].split(',') + user = ['custom_id', [User.objects.get(pk=u) for u in user_id]] + else: + if verb >= 1: + print(warning) + print(yellow + 'You user option is not recognized') + if verb >= 0: print(abort) + return + + club = [] + if options['club']: + if options['club'] == 'all': + club = ['all',None] + elif options['club'] == 'active': + club = ['active',None] + elif options['club'].split(' ')[0] == 'custom': + club_list = options['club'].split(' ')[1].split(',') + club = ['custom', [Club.objects.get(name=club_name) for club_name in club_list]] + elif options['club'].split(' ')[0] == 'custom_id': + club_id = options['club'].split(' ')[1].split(',') + club = ['custom_id', [Club.objects.get(pk=c) for c in club_id]] + else: + if verb >= 1: + print(warning) + print(yellow + 'You club option is not recognized') + if verb >= 0: print(abort) + return + + force = options['force'] + delete = options['delete'] + + # check if parameters are sufficient for generate wrapped with the desired option + if not bde: + if verb >= 1: + print(warning) + print(yellow + 'You have not selectionned a BDE !') + if verb >= 0 : print(abort) + return + if not (user or club): + if verb >= 1: + print(warning) + print(yellow + 'No club or user selected !') + if verb >= 0 : print(abort) + return + + if verb >= 3: + print('\033[1mOptions:\033[m') + bde_str = '' + for b in bde: bde_str += str(b) + print('BDE: ' + bde_str) + if user: print('User: ' + user[0]) + if club: print('Club: ' + club[0]) + print('force: ' + str(force)) + print('delete: ' + str(delete)) + print('') + if verb >=1 and force: + print(warning) + print(yellow + 'force is set to true, some wrapped may be deleted !') + if verb >=1 and delete: + print(warning) + print(yellow + 'wrapped will be deleted !') + if verb >= 2 or force or delete: + a = str(input('\033[mContinue ? (y/n) ')).lower() + if a in ['n','no','non','0']: + if verb >= 0: print(abort) + return + + # à partir de maintenant si ça foire ce n'est plus de la faute de l'utilisateur... + ######################################### + #### Transform club and user in note #### + ######################################### + + ######################## + #### Delete wrapped #### + ######################## + + ########################################### + #### Global data (in all user wrapped) #### + ########## modify it each year ! ########## + ########################################### + + ################################# + ########## Unique data ########## + ##### modify it each year ! ##### + ################################# + + ############################### + #### Create/Change wrapped #### + ############################### + return +