2025-02-12 00:00:23 +01:00
|
|
|
# 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
|
2025-02-13 02:39:33 +01:00
|
|
|
from django.db.models import Q
|
2025-02-12 00:00:23 +01:00
|
|
|
|
|
|
|
from note.models import Note
|
|
|
|
from member.models import User, Club
|
2025-02-13 02:39:33 +01:00
|
|
|
from ...models import Bde, Wrapped
|
2025-02-12 00:00:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
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(
|
2025-02-13 02:39:33 +01:00
|
|
|
'-f', '--force-change',
|
2025-02-12 00:00:23 +01:00
|
|
|
required=False,
|
|
|
|
action='store_true',
|
2025-02-13 02:39:33 +01:00
|
|
|
help="if wrapped already exist change data_json",
|
|
|
|
dest='change',
|
2025-02-12 00:00:23 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2025-02-13 02:39:33 +01:00
|
|
|
'-n', '--no-creation',
|
2025-02-12 00:00:23 +01:00
|
|
|
required=False,
|
2025-02-13 02:39:33 +01:00
|
|
|
action='store_false',
|
|
|
|
help="if wrapped don't already exist, don't generate it",
|
|
|
|
dest='create',
|
2025-02-12 00:00:23 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2025-02-13 02:39:33 +01:00
|
|
|
change = options['change']
|
|
|
|
create = options['create']
|
2025-02-12 00:00:23 +01:00
|
|
|
|
|
|
|
# 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])
|
2025-02-13 02:39:33 +01:00
|
|
|
print('change: ' + str(change))
|
|
|
|
print('create: ' + str(create))
|
2025-02-12 00:00:23 +01:00
|
|
|
print('')
|
2025-02-13 02:39:33 +01:00
|
|
|
if not (change or create):
|
|
|
|
if verb >= 1:
|
|
|
|
print(warning)
|
|
|
|
print(yellow + 'change and create is set to false, none wrapped will be created')
|
|
|
|
if verb >= 0:
|
|
|
|
print(abort)
|
|
|
|
return
|
|
|
|
if verb >=1 and change:
|
2025-02-12 00:00:23 +01:00
|
|
|
print(warning)
|
2025-02-13 02:39:33 +01:00
|
|
|
print(yellow + 'change is set to true, some wrapped may be replaced !')
|
|
|
|
if verb >=1 and not create:
|
2025-02-12 00:00:23 +01:00
|
|
|
print(warning)
|
2025-02-13 02:39:33 +01:00
|
|
|
print(yellow + 'create is set to false, wrapped will not be created !')
|
|
|
|
if verb >= 2 or change or not create:
|
2025-02-12 00:00:23 +01:00
|
|
|
a = str(input('\033[mContinue ? (y/n) ')).lower()
|
|
|
|
if a in ['n','no','non','0']:
|
|
|
|
if verb >= 0: print(abort)
|
|
|
|
return
|
|
|
|
|
2025-02-13 02:39:33 +01:00
|
|
|
note = self.convert_to_note(user=user, club=club, verb=verb)
|
|
|
|
if verb >= 1: print("\033[32mUser and/or Club given has successfully convert in their note\033[m")
|
2025-02-12 00:00:23 +01:00
|
|
|
|
2025-02-13 02:39:33 +01:00
|
|
|
global_data = self.global_data(bde, verb=verb)
|
|
|
|
if verb >= 1: print("\033[32mGlobal data has been successfully generated\033[m")
|
2025-02-12 00:00:23 +01:00
|
|
|
|
2025-02-13 02:39:33 +01:00
|
|
|
unique_data = self.unique_data(bde, note, change, create, global_data=global_data, verb=verb)
|
|
|
|
if verb >= 1: print("\033[32mUnique data has been successfully generated\033[m")
|
|
|
|
|
|
|
|
self.make_wrapped(unique_data, note, bde, change, create, verb=verb)
|
|
|
|
if verb >= 1: print(green + "The wrapped has been generated !")
|
|
|
|
if verb >= 0: print(success)
|
2025-02-12 00:00:23 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
|
2025-02-13 02:39:33 +01:00
|
|
|
def convert_to_note(self, user=None, club=None, verb=1):
|
|
|
|
query = Q(pk=-1)
|
|
|
|
if user:
|
|
|
|
if 'custom' in user[0]:
|
|
|
|
for u in user[1]:
|
|
|
|
query |= Q(noteuser__user=u)
|
|
|
|
elif user[0] == 'all':
|
|
|
|
query |= Q(noteuser__user__pk__gte=-1)
|
|
|
|
elif user[0] == 'adh':
|
|
|
|
# TODO some complex query
|
|
|
|
query |= query
|
|
|
|
elif user[0] == 'superuser':
|
|
|
|
query |= Q(noteuser__user__is_superuser=True)
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
if club:
|
|
|
|
if 'custom' in club[0]:
|
|
|
|
for c in club[1]:
|
|
|
|
query |= Q(noteclub__club=c)
|
|
|
|
elif club[0] == 'all':
|
|
|
|
query |= Q(noteclub__club__pk__gte=-1)
|
|
|
|
elif club[0] == 'active':
|
|
|
|
# TODO some complex query
|
|
|
|
query |= query
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
if verb >= 3: print('\033[mQuery: ' + str(query))
|
|
|
|
note = Note.objects.filter(query)
|
|
|
|
|
|
|
|
return note
|
|
|
|
|
|
|
|
def global_data(self, bde, verb=1):
|
|
|
|
data = {}
|
|
|
|
for b in bde:
|
|
|
|
if b.name == 'Rave Part[list]':
|
|
|
|
# TODO
|
|
|
|
data = {}
|
|
|
|
else:
|
|
|
|
# make your wrapped or reuse previous wrapped
|
|
|
|
raise NotImplementedError("The BDE: {bde_name} has not personalized wrapped, make it !"
|
|
|
|
.format(bde_name=b.name))
|
|
|
|
return data
|
|
|
|
|
|
|
|
def unique_data(self, bde, note, change, create, global_data=None, verb=1):
|
|
|
|
data = {}
|
|
|
|
for i in range(len(bde)):
|
|
|
|
if verb >= 2: print('\033[mlen(note) = {nb}'.format(nb=len(note)))
|
|
|
|
note_filtered = self.filter_note(bde[i], note, change, create, verb=verb)
|
|
|
|
if verb >= 2: print('\033[mlen(note_after_filter) = {nb}'.format(nb=len(note_filtered)))
|
|
|
|
if bde[i].name == 'Rave Part[list]':
|
|
|
|
# TODO
|
|
|
|
data = {}
|
|
|
|
else:
|
|
|
|
# make your wrapped or reuse previous wrapped
|
|
|
|
raise NotImplementedError("The BDE: {bde_name} has not personalized wrapped, make it !"
|
|
|
|
.format(bde_name=bde[i].name))
|
|
|
|
return data
|
|
|
|
|
|
|
|
def make_wrapped(self, unique_data, note, bde, change, create, verb=1):
|
|
|
|
for i in range(len(bde)):
|
|
|
|
for j in len(note[i]):
|
|
|
|
if create and not Wrapped.objects.filter(bde=bde[i], note=note[i][j]):
|
|
|
|
Wrapped(bde=bde[i],
|
|
|
|
note=note[i][j],
|
|
|
|
data_json=unique_data[i][j],
|
|
|
|
public=False,
|
|
|
|
generated=True).save()
|
|
|
|
elif change:
|
|
|
|
w = Wrapped.objects.get(bde=bde[i], note=note[i][j])
|
|
|
|
w.data_json = unique_data[i][j]
|
|
|
|
w.save()
|
|
|
|
return
|
|
|
|
|
|
|
|
def filter_note(self, bde, note, change, create, verb=1):
|
|
|
|
if change and create:
|
|
|
|
return note
|
|
|
|
if change and not create:
|
|
|
|
note_new = []
|
|
|
|
for n in note:
|
|
|
|
if Wrapped.objects.filter(bde=bde, note=n):
|
|
|
|
note_new.append(n)
|
|
|
|
return note_new
|
|
|
|
if not change and create:
|
|
|
|
note_new = []
|
|
|
|
for n in note:
|
|
|
|
if not Wrapped.objects.filter(bde=bde, note=n):
|
|
|
|
note_new.append(n)
|
|
|
|
return note_new
|