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,101 @@ 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+ location = location ,
212+ description = self .faker .text (100 ),
213+ )
214+ for name , club , location in [
215+ ("Champi" , ae , "BELFORT" ),
216+ ("Muzik" , ae , "BELFORT" ),
217+ ("Pôle Tech" , ae , "BELFORT" ),
218+ ("Jolly" , troll , "BELFORT" ),
219+ ("Cookut" , pdf , "BELFORT" ),
220+ ("Lucky" , pdf , "BELFORT" ),
221+ ("Potards" , pdf , "SEVENANS" ),
222+ ("Bureau AE" , ae , "SEVENANS" ),
223+ ]
224+ ]
225+ rooms = Room .objects .bulk_create (rooms )
226+ reservations = []
227+ for room in rooms :
228+ # how much people use this room.
229+ # The higher the number, the more reservations exist,
230+ # the more people are present in a slot,
231+ # the smaller the interval between two slot is,
232+ # and the more future reservations have already been made ahead of time
233+ affluence = random .randint (2 , 6 )
234+ slot_start = make_aware (self .faker .past_datetime ("-5y" ))
235+ generate_until = make_aware (
236+ self .faker .future_datetime (timedelta (days = 1 ) * affluence ** 2 )
237+ )
238+ while slot_start < generate_until :
239+ if slot_start .hour < 8 :
240+ # if a reservation would start in the middle of the night
241+ # make it start the next morning instead
242+ slot_start += timedelta (hours = 10 - slot_start .hour )
243+ duration = timedelta (minutes = 15 ) * (1 + int (random .gammavariate (3 , 2 )))
244+ reservations .append (
245+ ReservationSlot (
246+ room = room ,
247+ nb_people = (
248+ 1 + random .binomialvariate (affluence * 10 , affluence / 10 )
249+ ),
250+ author = random .choice (users ),
251+ start_at = slot_start ,
252+ duration = duration ,
253+ created_at = slot_start - self .faker .time_delta ("+7d" ),
254+ )
255+ )
256+ slot_start += duration + (
257+ timedelta (hours = 1 ) * random .expovariate (affluence / 48 )
258+ )
259+ reservations .sort (key = lambda slot : slot .created_at )
260+ ReservationSlot .objects .bulk_create (reservations )
261+
191262 def create_uvs (self ):
192263 root = User .objects .get (username = "root" )
193264 categories = ["CS" , "TM" , "OM" , "QC" , "EC" ]
@@ -379,7 +450,7 @@ def create_permanences(self, sellers: list[User]):
379450 Permanency .objects .bulk_create (perms )
380451
381452 def create_forums (self ):
382- forumers = random . sample ( list (User .objects .all ()), 100 )
453+ forumers = list (User .objects .order_by ( "?" )[: 100 ] )
383454 most_actives = random .sample (forumers , 10 )
384455 categories = list (Forum .objects .filter (is_category = True ))
385456 new_forums = [
@@ -397,15 +468,15 @@ def create_forums(self):
397468 for _ in range (100 )
398469 ]
399470 ForumTopic .objects .bulk_create (new_topics )
400- topics = list (ForumTopic .objects .all ( ))
471+ topics = list (ForumTopic .objects .values_list ( "id" , flat = True ))
401472
402473 def get_author ():
403474 if random .random () > 0.5 :
404475 return random .choice (most_actives )
405476 return random .choice (forumers )
406477
407478 messages = []
408- for t in topics :
479+ for topic_id in topics :
409480 nb_messages = max (1 , int (random .normalvariate (mu = 90 , sigma = 50 )))
410481 dates = sorted (
411482 [
@@ -417,7 +488,7 @@ def get_author():
417488 messages .extend (
418489 [
419490 ForumMessage (
420- topic = t ,
491+ topic_id = topic_id ,
421492 author = get_author (),
422493 date = d ,
423494 message = "\n \n " .join (
0 commit comments