mirror of
				https://gitlab.crans.org/bde/nk20-scripts
				synced 2025-10-31 07:09:52 +01:00 
			
		
		
		
	Compare commits
	
		
			7 Commits
		
	
	
		
			9dcb25723e
			...
			dce51ad261
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | dce51ad261 | ||
|  | 877d2e28d0 | ||
|  | e16629cc70 | ||
|  | dd812e09fc | ||
|  | b9ae701021 | ||
|  | dd8b48c31d | ||
|  | ceb7063f17 | 
							
								
								
									
										39
									
								
								management/commands/check_consistency.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								management/commands/check_consistency.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,39 @@ | |||||||
|  | # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay | ||||||
|  | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
|  | from django.core.management import BaseCommand | ||||||
|  | from django.db.models import Sum, F | ||||||
|  |  | ||||||
|  | from note.models import Note, Transaction | ||||||
|  | from note.templatetags.pretty_money import pretty_money | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class Command(BaseCommand): | ||||||
|  |     def add_arguments(self, parser): | ||||||
|  |         parser.add_argument('--sum-all', '-s', action='store_true', help='Check if the global sum is equal to zero') | ||||||
|  |  | ||||||
|  |     def handle(self, *args, **options): | ||||||
|  |         if options["sum_all"]: | ||||||
|  |             s = Note.objects.aggregate(Sum("balance"))["balance__sum"] | ||||||
|  |             if s: | ||||||
|  |                 self.stderr.write(self.style.NOTICE("LA SOMME DES NOTES NE VAUT PAS ZÉRO : " + pretty_money(s))) | ||||||
|  |                 exit(-1) | ||||||
|  |             else: | ||||||
|  |                 self.stdout.write(self.style.SUCCESS("La somme des notes vaut bien zéro.")) | ||||||
|  |                 exit(0) | ||||||
|  |  | ||||||
|  |         error = False | ||||||
|  |         for note in Note.objects.all(): | ||||||
|  |             balance = note.balance | ||||||
|  |             incoming = Transaction.objects.filter(valid=True, destination=note)\ | ||||||
|  |                 .annotate(total=F("quantity") * F("amount")).aggregate(Sum("total"))["total__sum"] or 0 | ||||||
|  |             outcoming = Transaction.objects.filter(valid=True, source=note)\ | ||||||
|  |                 .annotate(total=F("quantity") * F("amount")).aggregate(Sum("total"))["total__sum"] or 0 | ||||||
|  |             expected_balance = incoming - outcoming | ||||||
|  |             if expected_balance != balance: | ||||||
|  |                 self.stderr.write(self.style.NOTICE("LA SOMME DES TRANSACTIONS DE LA NOTE {} NE CORRESPOND PAS " | ||||||
|  |                                                     "AVEC LE MONTANT RÉEL".format(str(note)))) | ||||||
|  |                 self.stderr.write(self.style.NOTICE("Attendu : {}, calculé : {}" | ||||||
|  |                                                     .format(pretty_money(balance), pretty_money(expected_balance)))) | ||||||
|  |                 error = True | ||||||
|  |         exit(1 if error else 0) | ||||||
| @@ -19,7 +19,7 @@ from member.models import Club, Profile | |||||||
| from ._import_utils import ImportCommand, BulkCreateManager, timed | from ._import_utils import ImportCommand, BulkCreateManager, timed | ||||||
|  |  | ||||||
| M_DURATION = 396 | M_DURATION = 396 | ||||||
| M_START = datetime.date(2019, 8, 31) | M_START = datetime.date(2019, 8, 1) | ||||||
| M_END = datetime.date(2020, 9, 30) | M_END = datetime.date(2020, 9, 30) | ||||||
|  |  | ||||||
| MAP_IDBDE = { | MAP_IDBDE = { | ||||||
| @@ -60,6 +60,19 @@ class Command(ImportCommand): | |||||||
|         Every Model has to be manually created, and no magic `.save()` |         Every Model has to be manually created, and no magic `.save()` | ||||||
|         function is being called. |         function is being called. | ||||||
|         """ |         """ | ||||||
|  |         # Get promotion and date of creation of the account | ||||||
|  |         cur.execute("SELECT idbde, MIN(date) AS created_at, MIN(annee) AS promo FROM adhesions" | ||||||
|  |                     " GROUP BY idbde ORDER BY promo, created_at;") | ||||||
|  |         MAP_IDBDE_PROMOTION = {} | ||||||
|  |         for row in cur: | ||||||
|  |             MAP_IDBDE_PROMOTION[row["idbde"]] = row | ||||||
|  |  | ||||||
|  |         cur.execute("SELECT * FROM comptes WHERE idbde <= 0 ORDER BY idbde;") | ||||||
|  |         for row in cur: | ||||||
|  |             note = Note.objects.get(pk=MAP_IDBDE[row["idbde"]]) | ||||||
|  |             note.balance = row["solde"] | ||||||
|  |             note.save() | ||||||
|  |  | ||||||
|         cur.execute("SELECT * FROM comptes WHERE idbde > 0 ORDER BY idbde;") |         cur.execute("SELECT * FROM comptes WHERE idbde > 0 ORDER BY idbde;") | ||||||
|         pk_club = 3 |         pk_club = 3 | ||||||
|         pk_user = 1 |         pk_user = 1 | ||||||
| @@ -84,7 +97,6 @@ class Command(ImportCommand): | |||||||
|                 "last_negative": None, |                 "last_negative": None, | ||||||
|                 "is_active": True, |                 "is_active": True, | ||||||
|                 "display_image": "pic/default.png", |                 "display_image": "pic/default.png", | ||||||
|                 "created_at": now() |  | ||||||
|             } |             } | ||||||
|             if row["last_negatif"] is not None: |             if row["last_negatif"] is not None: | ||||||
|                 note_dict["last_negative"] = make_aware(row["last_negatif"]) |                 note_dict["last_negative"] = make_aware(row["last_negatif"]) | ||||||
| @@ -95,6 +107,11 @@ class Command(ImportCommand): | |||||||
|                 else: |                 else: | ||||||
|                     passwd_nk15 = '' |                     passwd_nk15 = '' | ||||||
|  |  | ||||||
|  |                 if row["idbde"] not in MAP_IDBDE_PROMOTION: | ||||||
|  |                     # NK12 bug. Applying default values | ||||||
|  |                     MAP_IDBDE_PROMOTION[row["idbde"]] = {"promo": 2014, | ||||||
|  |                                                          "created_at": datetime.datetime(2014, 9, 1, 0, 0, 0)} | ||||||
|  |  | ||||||
|                 obj_dict = { |                 obj_dict = { | ||||||
|                     "pk": pk_user, |                     "pk": pk_user, | ||||||
|                     "username": row["pseudo"], |                     "username": row["pseudo"], | ||||||
| @@ -103,6 +120,7 @@ class Command(ImportCommand): | |||||||
|                     "last_name": row["nom"], |                     "last_name": row["nom"], | ||||||
|                     "email": row["mail"], |                     "email": row["mail"], | ||||||
|                     "is_active": True,  # temporary |                     "is_active": True,  # temporary | ||||||
|  |                     "date_joined": make_aware(MAP_IDBDE_PROMOTION[row["idbde"]]["created_at"]), | ||||||
|                 } |                 } | ||||||
|                 profile_dict = { |                 profile_dict = { | ||||||
|                     "pk": pk_profile, |                     "pk": pk_profile, | ||||||
| @@ -112,7 +130,9 @@ class Command(ImportCommand): | |||||||
|                     "paid": row['normalien'], |                     "paid": row['normalien'], | ||||||
|                     "registration_valid": True, |                     "registration_valid": True, | ||||||
|                     "email_confirmed": True, |                     "email_confirmed": True, | ||||||
|  |                     "promotion": MAP_IDBDE_PROMOTION[row["idbde"]]["promo"], | ||||||
|                 } |                 } | ||||||
|  |                 note_dict["created_at"] = make_aware(MAP_IDBDE_PROMOTION[row["idbde"]]["created_at"]) | ||||||
|                 note_dict["polymorphic_ctype"] = note_user_type |                 note_dict["polymorphic_ctype"] = note_user_type | ||||||
|                 note_user_dict = { |                 note_user_dict = { | ||||||
|                     "pk": pk_note, |                     "pk": pk_note, | ||||||
| @@ -138,6 +158,7 @@ class Command(ImportCommand): | |||||||
|                     "pk": pk_club, |                     "pk": pk_club, | ||||||
|                     "name": row["pseudo"], |                     "name": row["pseudo"], | ||||||
|                     "email": row["mail"], |                     "email": row["mail"], | ||||||
|  |                     "parent_club_id": 1,   # All clubs depends on BDE by default | ||||||
|                     "membership_duration": M_DURATION, |                     "membership_duration": M_DURATION, | ||||||
|                     "membership_start": M_START, |                     "membership_start": M_START, | ||||||
|                     "membership_end": M_END, |                     "membership_end": M_END, | ||||||
| @@ -146,7 +167,7 @@ class Command(ImportCommand): | |||||||
|                 } |                 } | ||||||
|                 note_club_dict = { |                 note_club_dict = { | ||||||
|                     "pk": pk_note, |                     "pk": pk_note, | ||||||
|                     "club_id": pk_club, |                     "club_id": pk_club | ||||||
|                 } |                 } | ||||||
|                 alias_dict = { |                 alias_dict = { | ||||||
|                     "pk": pk_note, |                     "pk": pk_note, | ||||||
| @@ -154,6 +175,7 @@ class Command(ImportCommand): | |||||||
|                     "normalized_name": Alias.normalize(pseudo), |                     "normalized_name": Alias.normalize(pseudo), | ||||||
|                     "note_id": pk_note |                     "note_id": pk_note | ||||||
|                 } |                 } | ||||||
|  |                 note_dict["created_at"] = make_aware(row["previous_report_date"])  # Not perfect, but good approximation | ||||||
|                 note_dict["polymorphic_ctype"] = note_club_type |                 note_dict["polymorphic_ctype"] = note_club_type | ||||||
|                 bulk_mgr.add(Club(**obj_dict), |                 bulk_mgr.add(Club(**obj_dict), | ||||||
|                              Note(**note_dict), |                              Note(**note_dict), | ||||||
|   | |||||||
| @@ -185,6 +185,7 @@ class Command(ImportCommand): | |||||||
|         n = cur.rowcount |         n = cur.rowcount | ||||||
|         pk_membership = 1 |         pk_membership = 1 | ||||||
|         pk_transaction = 1 |         pk_transaction = 1 | ||||||
|  |         kfet_balance = 0 | ||||||
|         for idx, row in enumerate(cur): |         for idx, row in enumerate(cur): | ||||||
|             if save or idx % chunk_size == 0: |             if save or idx % chunk_size == 0: | ||||||
|                 self.update_line(idx, n, row["description"]) |                 self.update_line(idx, n, row["description"]) | ||||||
| @@ -261,6 +262,7 @@ class Command(ImportCommand): | |||||||
|                     } |                     } | ||||||
|                     obj_dict0["amount"] = bde_dict["fee"] |                     obj_dict0["amount"] = bde_dict["fee"] | ||||||
|                     obj_dict["amount"] = kfet_dict["fee"] |                     obj_dict["amount"] = kfet_dict["fee"] | ||||||
|  |                     kfet_balance += kfet_dict["fee"] | ||||||
|                     # BDE membership Transaction is inserted before the Kfet membershipTransaction |                     # BDE membership Transaction is inserted before the Kfet membershipTransaction | ||||||
|                     pk_membership += 1 |                     pk_membership += 1 | ||||||
|                     pk_transaction += 1 |                     pk_transaction += 1 | ||||||
| @@ -291,6 +293,14 @@ class Command(ImportCommand): | |||||||
|             pk_transaction += 1 |             pk_transaction += 1 | ||||||
|         bulk_mgr.done() |         bulk_mgr.done() | ||||||
|  |  | ||||||
|  |         # Update the balance of the BDE and the Kfet club | ||||||
|  |         note_bde = NoteClub.objects.get(pk=5) | ||||||
|  |         note_kfet = NoteClub.objects.get(pk=6) | ||||||
|  |         note_bde.balance -= kfet_balance | ||||||
|  |         note_kfet.balance += kfet_balance | ||||||
|  |         note_bde.save() | ||||||
|  |         note_kfet.save() | ||||||
|  |  | ||||||
|     @timed |     @timed | ||||||
|     def set_roles(self): |     def set_roles(self): | ||||||
|         bulk_mgr = BulkCreateManager(chunk_size=10000) |         bulk_mgr = BulkCreateManager(chunk_size=10000) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user