mirror of
				https://gitlab.crans.org/bde/nk20-scripts
				synced 2025-11-04 08:32:10 +01:00 
			
		
		
		
	import activities and entry fixes
This commit is contained in:
		@@ -2,103 +2,95 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import psycopg2 as pg
 | 
					import psycopg2 as pg
 | 
				
			||||||
import psycopg2.extras as pge
 | 
					import psycopg2.extras as pge
 | 
				
			||||||
import pytz
 | 
					 | 
				
			||||||
import datetime
 | 
					import datetime
 | 
				
			||||||
import copy
 | 
					import copy
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from django.utils.timezone import make_aware
 | 
					from django.utils.timezone import make_aware
 | 
				
			||||||
from django.db import transaction
 | 
					from django.db import transaction
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from activity.models import ActivityType, Activity
 | 
					from activity.models import ActivityType, Activity, Guest, Entry
 | 
				
			||||||
from member.models import Club
 | 
					from member.models import Club
 | 
				
			||||||
 | 
					from note.models import Note
 | 
				
			||||||
 | 
					from ._import_utils import ImportCommand, BulkCreateManager, timed
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from ._import_utils import ImportCommand, BulkCreateManager
 | 
					MAP_ACTIVITY = dict()
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Command(ImportCommand):
 | 
					class Command(ImportCommand):
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    Import command for Activities Base Data (Comptes, and Aliases)
 | 
					    Import command for Activities Base Data (Comptes, and Aliases)
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def add_arguments(self, parser):
 | 
					 | 
				
			||||||
        pass
 | 
					 | 
				
			||||||
    @timed
 | 
					    @timed
 | 
				
			||||||
    @transaction.atomic
 | 
					    @transaction.atomic
 | 
				
			||||||
    def import_activities(self, cur, chunk_size):
 | 
					    def import_activities(self, cur, chunk_size):
 | 
				
			||||||
        cur.execute("SELECT * FROM activites ORDER by id")
 | 
					        cur.execute("SELECT * FROM activites ORDER by id")
 | 
				
			||||||
        n = cur.rowcount
 | 
					        n = cur.rowcount
 | 
				
			||||||
        bulk_mgr = BulkCreateManager(chunk_size=chunk_size)
 | 
					        bulk_mgr = BulkCreateManager(chunk_size=chunk_size)
 | 
				
			||||||
        activity_type = ActivityType.objects.get(name="Pot")  # Need to be fixed manually
 | 
					        activity_type_id = ActivityType.objects.get(name="Pot").pk  # Need to be fixed manually
 | 
				
			||||||
        kfet = Club.objects.get(name="Kfet")
 | 
					        kfet = Club.objects.get(name="Kfet")
 | 
				
			||||||
 | 
					        pk_activity = 1
 | 
				
			||||||
        for idx, row in enumerate(cur):
 | 
					        for idx, row in enumerate(cur):
 | 
				
			||||||
            update_line(idx, n, row["alias"])
 | 
					            self.update_line(idx, n, row["titre"])
 | 
				
			||||||
 | 
					            note = self.MAP_IDBDE[row["responsable"]]
 | 
				
			||||||
 | 
					            if note == 6244:
 | 
				
			||||||
 | 
					                 # Licorne magique ne doit pas utiliser son compte club pour proposer des activités
 | 
				
			||||||
 | 
					                note = Note.objects.get(pk=self.MAP_IDBDE[6524])
 | 
				
			||||||
 | 
					                note = note.user_id
 | 
				
			||||||
            organizer = Club.objects.filter(name=row["signature"])
 | 
					            organizer = Club.objects.filter(name=row["signature"])
 | 
				
			||||||
            if organizer.exists():
 | 
					            if organizer.exists():
 | 
				
			||||||
                # Try to find the club that organizes the activity. If not found, assume it's Kfet (fix manually)
 | 
					                # Try to find the club that organizes the activity.
 | 
				
			||||||
 | 
					                # If not found, assume it's Kfet (fix manually)
 | 
				
			||||||
                organizer = organizer.get()
 | 
					                organizer = organizer.get()
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                organizer = kfet
 | 
					                organizer = kfet
 | 
				
			||||||
            obj_dict = {
 | 
					            obj_dict = {
 | 
				
			||||||
                "pk": row["id"]
 | 
					                "pk": pk_activity,
 | 
				
			||||||
                "name": row["titre"],
 | 
					                "name": row["titre"],
 | 
				
			||||||
                "description": row["description"],
 | 
					                "description": row["description"],
 | 
				
			||||||
                "activity_type": activity_type,  # By default Pot
 | 
					                "activity_type_id": activity_type_id,  # By default Pot
 | 
				
			||||||
                "creater": self.MAP_IDBDE[row["responsable"]],
 | 
					                "creater_id": note,
 | 
				
			||||||
                "organizer": organizer,
 | 
					                "organizer_id": organizer.pk,
 | 
				
			||||||
                "attendees_club": kfet,  # Maybe fix manually
 | 
					                "attendees_club_id": kfet.pk,  # Maybe fix manually
 | 
				
			||||||
                "date_start": row["debut"],
 | 
					                "date_start": make_aware(row["debut"]),
 | 
				
			||||||
                "date_end": row["fin"],
 | 
					                "date_end": make_aware(row["fin"]),
 | 
				
			||||||
                "valid": row["validepar"] is not None,
 | 
					                "valid": row["validepar"] is not None,
 | 
				
			||||||
                "open": row["open"],  # Should always be False
 | 
					                "open": row["ouvert"],  # Should always be False
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            # WARNING: Fields lieu, liste, listeimprimee are missing
 | 
					            # WARNING: Fields lieu, liste, listeimprimee are missing
 | 
				
			||||||
            #
 | 
					            MAP_ACTIVITY[row["id"]] = pk_activity
 | 
				
			||||||
 | 
					            pk_activity +=1
 | 
				
			||||||
            bulk_mgr.add(Activity(**obj_dict))
 | 
					            bulk_mgr.add(Activity(**obj_dict))
 | 
				
			||||||
            MAP_NAMEACTIVITY[activity.name] = activity
 | 
					 | 
				
			||||||
        bulk_mgr.done()
 | 
					        bulk_mgr.done()
 | 
				
			||||||
        return MAP_IDACTIVITY, MAP_NAMEACTIVITY
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @timed
 | 
					    @timed
 | 
				
			||||||
    @transaction.atomic
 | 
					    @transaction.atomic
 | 
				
			||||||
    def import_activity_entries(cur):
 | 
					    def import_activities_entries(self, cur):
 | 
				
			||||||
        bulk_mgr = BulkCreateManager()
 | 
					        bulk_mgr = BulkCreateManager()
 | 
				
			||||||
        map_idguests = set()
 | 
					 | 
				
			||||||
        cur.execute("SELECT * FROM invites ORDER by id")
 | 
					        cur.execute("SELECT * FROM invites ORDER by id")
 | 
				
			||||||
        n = cur.rowcount
 | 
					        n = cur.rowcount
 | 
				
			||||||
        for idx, row in enumerate(cur):
 | 
					        for idx, row in enumerate(cur):
 | 
				
			||||||
            update_line(idx, n, row["nom"] + " " + row["prenom"])
 | 
					            self.update_line(idx, n, f"{row['nom']} {row['prenom']}")
 | 
				
			||||||
            obj_dict = {
 | 
					            obj_dict = {
 | 
				
			||||||
                "pk": row["id"],
 | 
					                "pk": row["id"],
 | 
				
			||||||
                "activity_id": row["activity"],
 | 
					                "activity_id": MAP_ACTIVITY[row["activite"]],
 | 
				
			||||||
                "last_name": row["nom"],
 | 
					                "last_name": row["nom"],
 | 
				
			||||||
                "first_name": row["prenom"],
 | 
					                "first_name": row["prenom"],
 | 
				
			||||||
                "inviter": self.MAP_IDBDE[row["responsable"]],
 | 
					                "inviter_id": self.MAP_IDBDE[row["responsable"]],
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            bulk_mgr.add(Guest(**obj_dict))
 | 
					            bulk_mgr.add(Guest(**obj_dict))
 | 
				
			||||||
        bulk_mgr.done()
 | 
					        bulk_mgr.done()
 | 
				
			||||||
        cur.execute("SELECT * FROM entree_activites ORDER by id")
 | 
					        cur.execute("SELECT * FROM entree_activites ORDER by id")
 | 
				
			||||||
        n = cur.rowcount
 | 
					        n = cur.rowcount
 | 
				
			||||||
        for idx, row in enumerate(cur):
 | 
					        for idx, row in enumerate(cur):
 | 
				
			||||||
            update_line(idx, n, row["nom"] + " " + row["prenom"])
 | 
					            self.update_line(idx, n, f"{row['idbde']} {row['responsable']}")
 | 
				
			||||||
            guest = None
 | 
					 | 
				
			||||||
            if row["est_invite"]:
 | 
					 | 
				
			||||||
                for g in map_idguests[row["id"]]:
 | 
					 | 
				
			||||||
                    if g.activity.pk == activity.pk:
 | 
					 | 
				
			||||||
                        guest = g
 | 
					 | 
				
			||||||
                        break
 | 
					 | 
				
			||||||
                    if not guest:
 | 
					 | 
				
			||||||
            obj_dict = {
 | 
					            obj_dict = {
 | 
				
			||||||
                "activity": row["activity"],
 | 
					                "activity_id": MAP_ACTIVITY[row["activite"]],
 | 
				
			||||||
                "time": make_aware(row["heure_entree"]),
 | 
					                "time": make_aware(row["heure_entree"]),
 | 
				
			||||||
                "note": guest.inviter if guest else MAP_IDBDE[row["idbde"]],
 | 
					                "note_id": self.MAP_IDBDE[row["responsable"]] if row['est_invite'] else row["idbde"],
 | 
				
			||||||
                "guest": guest,
 | 
					                "guest_id": row["idbde"] if row['est_invite'] else None,
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            try:
 | 
					            bulk_mgr.add(Entry(**obj_dict))
 | 
				
			||||||
                with transaction.atomic():
 | 
					        bulk_mgr.done()
 | 
				
			||||||
                    Entry.objects.get_or_create(**obj_dict)
 | 
					 | 
				
			||||||
            except IntegrityError as e:
 | 
					 | 
				
			||||||
                raise e
 | 
					 | 
				
			||||||
           
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle(self, *args, **kwargs):
 | 
					    def handle(self, *args, **kwargs):
 | 
				
			||||||
        # default args, provided by ImportCommand.
 | 
					        # default args, provided by ImportCommand.
 | 
				
			||||||
@@ -108,5 +100,6 @@ class Command(ImportCommand):
 | 
				
			|||||||
        cur = conn.cursor(cursor_factory=pge.DictCursor)
 | 
					        cur = conn.cursor(cursor_factory=pge.DictCursor)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if kwargs["map"]:
 | 
					        if kwargs["map"]:
 | 
				
			||||||
            self.load(kwargs["map"])
 | 
					            self.load_map(kwargs["map"])
 | 
				
			||||||
        self.import_activities(cur, chunk_size)
 | 
					        self.import_activities(cur, kwargs["chunk"])
 | 
				
			||||||
 | 
					        self.import_activities_entries(cur)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user