Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from policorp.models import Availability, Booking 

2 

3class Schedule: 

4 

5 def __init__(self, date, location, availabilities, bookings): 

6 self.date = date 

7 self.location = location 

8 self.availabilities = availabilities 

9 self.bookings = bookings 

10 

11 def toSortedScheduleArray(self): 

12 arr1 = sorted(self.availabilities, key=lambda a: a.when) 

13 arr2 = sorted(self.bookings, key=lambda b: b.availability.when) 

14 n1 = len(arr1) 

15 n2 = len(arr2) 

16 arr3 = [None]*(n1 + n2) 

17 i = 0 

18 j = 0 

19 k = 0 

20 

21 # Traverse both array 

22 while i < n1 and j < n2: 

23 # Check if current element of first array is smaller than current element of 

24 # second array. If yes, store first array element and increment first array 

25 # index. Otherwise do same with second array 

26 if arr1[i].when < arr2[j].availability.when: 

27 arr3[k] = arr1[i] 

28 k = k + 1 

29 i = i + 1 

30 else: 

31 arr3[k] = arr2[j] 

32 k = k + 1 

33 j = j + 1 

34 

35 # Store remaining elements of first array 

36 while i < n1: 

37 arr3[k] = arr1[i] 

38 k = k + 1 

39 i = i + 1 

40 

41 # Store remaining elements of second array 

42 while j < n2: 

43 arr3[k] = arr2[j] 

44 k = k + 1 

45 j = j + 1 

46 

47 return arr3 

48 

49 def json(self): 

50 

51 def json_sch(x): 

52 json = {} 

53 if isinstance(x, Booking): 

54 json = {'booking': x.json()} 

55 elif isinstance(x, Availability): 

56 json = {'availability': x.json()} 

57 return json 

58 

59 return { 

60 'date': self.date.isoformat(), 

61 'location': self.location.json(), 

62 'schedule': [ json_sch(s) for s in self.toSortedScheduleArray()] 

63 }