2424)
2525from forum .models import Forum , ForumMessage , ForumTopic
2626from pedagogy .models import UV
27+ from reservation .models import ReservationSlot , Room
2728from subscription .models import Subscription
2829
2930
@@ -40,45 +41,20 @@ def handle(self, *args, **options):
4041
4142 self .stdout .write ("Creating users..." )
4243 users = self .create_users ()
44+ # len(subscribers) is approximately 480
4345 subscribers = random .sample (users , k = int (0.8 * len (users )))
4446 self .stdout .write ("Creating subscriptions..." )
4547 self .create_subscriptions (subscribers )
4648 self .stdout .write ("Creating club memberships..." )
47- users_qs = User .objects .filter (id__in = [s .id for s in subscribers ])
48- subscribers_now = list (
49- users_qs .annotate (
50- filter = Exists (
51- Subscription .objects .filter (
52- member_id = OuterRef ("pk" ), subscription_end__gte = now ()
53- )
54- )
55- )
56- )
57- old_subscribers = list (
58- users_qs .annotate (
59- filter = Exists (
60- Subscription .objects .filter (
61- member_id = OuterRef ("pk" ), subscription_end__lt = now ()
62- )
63- )
64- )
65- )
66- self .make_club (
67- Club .objects .get (id = settings .SITH_MAIN_CLUB_ID ),
68- random .sample (subscribers_now , k = min (30 , len (subscribers_now ))),
69- random .sample (old_subscribers , k = min (60 , len (old_subscribers ))),
70- )
71- self .make_club (
72- Club .objects .get (name = "Troll Penché" ),
73- random .sample (subscribers_now , k = min (20 , len (subscribers_now ))),
74- random .sample (old_subscribers , k = min (80 , len (old_subscribers ))),
75- )
49+ self .create_club_memberships (subscribers )
50+ self .stdout .write ("Creating rooms and reservation..." )
51+ self .create_resources_and_reservations (random .sample (subscribers , k = 40 ))
7652 self .stdout .write ("Creating uvs..." )
7753 self .create_uvs ()
7854 self .stdout .write ("Creating products..." )
7955 self .create_products ()
8056 self .stdout .write ("Creating sales and refills..." )
81- sellers = random . sample ( list (User .objects .all ()), 100 )
57+ sellers = list (User .objects .order_by ( "?" )[: 100 ] )
8258 self .create_sales (sellers )
8359 self .stdout .write ("Creating permanences..." )
8460 self .create_permanences (sellers )
@@ -188,6 +164,99 @@ def zip_roles(users: list[User]) -> Iterator[tuple[User, int]]:
188164 memberships = Membership .objects .bulk_create (memberships )
189165 Membership ._add_club_groups (memberships )
190166
167+ def create_club_memberships (self , users : list [User ]):
168+ users_qs = User .objects .filter (id__in = [s .id for s in users ])
169+ subscribers_now = list (
170+ users_qs .annotate (
171+ filter = Exists (
172+ Subscription .objects .filter (
173+ member_id = OuterRef ("pk" ), subscription_end__gte = now ()
174+ )
175+ )
176+ )
177+ )
178+ old_subscribers = list (
179+ users_qs .annotate (
180+ filter = Exists (
181+ Subscription .objects .filter (
182+ member_id = OuterRef ("pk" ), subscription_end__lt = now ()
183+ )
184+ )
185+ )
186+ )
187+ self .make_club (
188+ Club .objects .get (id = settings .SITH_MAIN_CLUB_ID ),
189+ random .sample (subscribers_now , k = min (30 , len (subscribers_now ))),
190+ random .sample (old_subscribers , k = min (60 , len (old_subscribers ))),
191+ )
192+ self .make_club (
193+ Club .objects .get (name = "Troll Penché" ),
194+ random .sample (subscribers_now , k = min (20 , len (subscribers_now ))),
195+ random .sample (old_subscribers , k = min (80 , len (old_subscribers ))),
196+ )
197+
198+ def create_resources_and_reservations (self , users : list [User ]):
199+ """Generate reservable rooms and reservations slots for those rooms.
200+
201+ Contrary to the other data generator,
202+ this one generates more data than what is expected on the real db.
203+ """
204+ ae = Club .objects .get (id = settings .SITH_MAIN_CLUB_ID )
205+ pdf = Club .objects .get (id = settings .SITH_PDF_CLUB_ID )
206+ troll = Club .objects .get (name = "Troll Penché" )
207+ rooms = [
208+ Room (
209+ name = name ,
210+ club = club ,
211+ address = self .faker .address (),
212+ description = self .faker .text (100 ),
213+ )
214+ for name , club in [
215+ ("Champi" , ae ),
216+ ("Muzik" , ae ),
217+ ("Pôle Tech" , ae ),
218+ ("Jolly" , troll ),
219+ ("Cookut" , pdf ),
220+ ("Lucky" , pdf ),
221+ ]
222+ ]
223+ rooms = Room .objects .bulk_create (rooms )
224+ reservations = []
225+ for room in rooms :
226+ # how much people use this room.
227+ # The higher the number, the more reservations exist,
228+ # the more people are present in a slot,
229+ # the smaller the interval between two slot is,
230+ # and the more future reservations have already been made ahead of time
231+ affluence = random .randint (2 , 6 )
232+ slot_start = make_aware (self .faker .past_datetime ("-5y" ))
233+ generate_until = make_aware (
234+ self .faker .future_datetime (timedelta (days = 1 ) * affluence ** 2 )
235+ )
236+ while slot_start < generate_until :
237+ if slot_start .hour < 8 :
238+ # if a reservation would start in the middle of the night
239+ # make it start the next morning instead
240+ slot_start += timedelta (hours = 10 - slot_start .hour )
241+ duration = timedelta (minutes = 15 ) * (1 + int (random .gammavariate (3 , 2 )))
242+ reservations .append (
243+ ReservationSlot (
244+ room = room ,
245+ nb_people = (
246+ 1 + random .binomialvariate (affluence * 10 , affluence / 10 )
247+ ),
248+ author = random .choice (users ),
249+ start_at = slot_start ,
250+ duration = duration ,
251+ created_at = slot_start - self .faker .time_delta ("+7d" ),
252+ )
253+ )
254+ slot_start += duration + (
255+ timedelta (hours = 1 ) * random .expovariate (affluence / 48 )
256+ )
257+ reservations .sort (key = lambda slot : slot .created_at )
258+ ReservationSlot .objects .bulk_create (reservations )
259+
191260 def create_uvs (self ):
192261 root = User .objects .get (username = "root" )
193262 categories = ["CS" , "TM" , "OM" , "QC" , "EC" ]
@@ -379,7 +448,7 @@ def create_permanences(self, sellers: list[User]):
379448 Permanency .objects .bulk_create (perms )
380449
381450 def create_forums (self ):
382- forumers = random . sample ( list (User .objects .all ()), 100 )
451+ forumers = list (User .objects .order_by ( "?" )[: 100 ] )
383452 most_actives = random .sample (forumers , 10 )
384453 categories = list (Forum .objects .filter (is_category = True ))
385454 new_forums = [
@@ -397,15 +466,15 @@ def create_forums(self):
397466 for _ in range (100 )
398467 ]
399468 ForumTopic .objects .bulk_create (new_topics )
400- topics = list (ForumTopic .objects .all ( ))
469+ topics = list (ForumTopic .objects .values_list ( "id" , flat = True ))
401470
402471 def get_author ():
403472 if random .random () > 0.5 :
404473 return random .choice (most_actives )
405474 return random .choice (forumers )
406475
407476 messages = []
408- for t in topics :
477+ for topic_id in topics :
409478 nb_messages = max (1 , int (random .normalvariate (mu = 90 , sigma = 50 )))
410479 dates = sorted (
411480 [
@@ -417,7 +486,7 @@ def get_author():
417486 messages .extend (
418487 [
419488 ForumMessage (
420- topic = t ,
489+ topic_id = topic_id ,
421490 author = get_author (),
422491 date = d ,
423492 message = "\n \n " .join (
0 commit comments