-
Notifications
You must be signed in to change notification settings - Fork 20
/
__init__.py
1220 lines (1027 loc) · 43.8 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
fedocallib - Back-end library for the fedocal project.
Copyright (C) 2012-2014 Pierre-Yves Chibon
Author: Pierre-Yves Chibon <pingou@pingoured.fr>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at
your option) any later version.
See http://www.gnu.org/copyleft/gpl.html for the full text of the
license.
"""
import copy
import vobject
import pytz
import operator
from datetime import datetime
from datetime import date
from datetime import time
from datetime import timedelta
from dateutil import zoneinfo
from dateutil.relativedelta import relativedelta
import dateutil.rrule as rrule
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import scoped_session
from fedocal.fedocallib.week import Week
from fedocal.fedocallib.model import (
CalendarStatus, Calendar, Reminder, Meeting)
import dbaction as dbaction
from fedocal.fedocallib.exceptions import UserNotAllowed, InvalidMeeting
from fedocal.fedocallib.fedora_calendar import FedocalCalendar
HOURS = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09',
'10', '11', '12', '13', '14', '15', '16', '17', '18', '19',
'20', '21', '22', '23', '24']
def convert_time(timeobj, tzfrom, tzto):
""" Convert a given datetime object from a specified timezone to
the other specified.
:arg timeobj: a datetime object to convert
:arg tzfrom: the timezone from which to convert
:arg tzto: the timezone to which to convert
"""
timez_from = pytz.timezone(tzfrom)
timez_to = pytz.timezone(tzto)
timeobj_from = timez_from.localize(timeobj)
timeobj_to = timeobj_from.astimezone(timez_to)
return timeobj_to
def convert_meeting_timezone(meeting, tzfrom, tzto):
""" Convert a given meeting from one specified timezone to another
specified one.
:arg meeting: a Meeting object representing the meeting to convert
from one timezone to the other.
:arg tzfrom: the timezone from which to convert
:arg tzto: the timezone to which to convert
"""
# Prevents the actual SQLAlchemy object from being changed
meeting = copy.copy(meeting)
meeting_start = convert_time(
datetime(
meeting.meeting_date.year,
meeting.meeting_date.month,
meeting.meeting_date.day,
meeting.meeting_time_start.hour,
meeting.meeting_time_start.minute),
tzfrom, tzto)
meeting_stop = convert_time(
datetime(
meeting.meeting_date_end.year,
meeting.meeting_date_end.month,
meeting.meeting_date_end.day,
meeting.meeting_time_stop.hour,
meeting.meeting_time_stop.minute),
tzfrom, tzto)
meeting.meeting_date = meeting_start.date()
meeting.meeting_date_end = meeting_stop.date()
meeting.meeting_time_start = meeting_start.time()
meeting.meeting_time_stop = meeting_stop.time()
return meeting
def create_session(db_url, debug=False, pool_recycle=3600):
""" Create the Session object to use to query the database.
:arg db_url: URL used to connect to the database. The URL contains
information with regards to the database engine, the host to connect
to, the user and password and the database name.
ie: <engine>://<user>:<password>@<host>/<dbname>
:arg debug: a boolean specifying wether we should have the verbose
output of sqlalchemy or not.
:return a Session that can be used to query the database.
"""
engine = create_engine(db_url, echo=debug, pool_recycle=pool_recycle)
scopedsession = scoped_session(sessionmaker(bind=engine))
return scopedsession
def get_calendars(session):
""" Retrieve the list of Calendar from the database. """
return Calendar.get_all(session)
def get_start_week(year=None, month=None, day=None):
""" For a given date, retrieve the day the week started.
For any missing parameters (ie: None), use the value of the current
day.
:kwarg year: year to consider when searching a week.
:kwarg month: month to consider when searching a week.
:kwarg day: day to consider when searching a week.
:return a Date of the day the week started either based on the
current utc date or based on the information.
"""
now = datetime.utcnow()
if not year:
year = now.year
if not month:
month = now.month
if not day:
day = now.day
week_day = date(year, month, day)
week_start = week_day - timedelta(days=week_day.weekday())
return week_start
def get_stop_week(year=None, month=None, day=None):
""" For a given date, retrieve the day the week stops.
For any missing parameters (ie: None), use the value of the current
day.
:kwarg year: year to consider when searching a week.
:kwarg month: month to consider when searching a week.
:kwarg day: day to consider when searching a week.
:return a Date of the day the week started either based on the
current utc date or based on the information.
"""
week_start = get_start_week(year, month, day)
week_stop = week_start + timedelta(days=6)
return week_stop
def get_next_week(year=None, month=None, day=None):
""" For a given date, retrieve the day when the next week starts.
For any missing parameters (ie: None), use the value of the current
day.
:kwarg year: year to consider when searching a week.
:kwarg month: month to consider when searching a week.
:kwarg day: day to consider when searching a week.
:return a Date of the day the week started either based on the
current utc date or based on the information.
"""
week_start = get_start_week(year, month, day)
next_week_start = week_start + timedelta(days=7)
return next_week_start
def get_previous_week(year=None, month=None, day=None):
""" For a given date, retrieve the day when the previous week starts.
For any missing parameters (ie: None), use the value of the current
day.
:kwarg year: year to consider when searching a week.
:kwarg month: month to consider when searching a week.
:kwarg day: day to consider when searching a week.
:return a Date of the day the week started either based on the
current utc date or based on the information.
"""
week_start = get_start_week(year, month, day)
previous_week_start = week_start - timedelta(days=7)
return previous_week_start
def get_week(session, calendar, year=None, month=None, day=None):
""" For a given date, retrieve the corresponding week.
For any missing parameters (ie: None), use the value of the current
day.
:arg session: the database session to use
:arg calendar: the Calendar object of interest.
:kwarg year: year to consider when searching a week.
:kwarg month: month to consider when searching a week.
:kwarg day: day to consider when searching a week.
:return a Week object corresponding to the week asked either based
on the current utc date of based on the information specified.
"""
week_start = get_start_week(year, month, day)
week = Week(session, calendar, week_start)
return week
def get_week_of_location(session, location, year=None, month=None, day=None):
""" For a given date, retrieve the corresponding week.
For any missing parameters (ie: None), use the value of the current
day.
:arg session: the database session to use
:arg calendar: the Calendar object of interest.
:kwarg year: year to consider when searching a week.
:kwarg month: month to consider when searching a week.
:kwarg day: day to consider when searching a week.
:return a Week object corresponding to the week asked either based
on the current utc date of based on the information specified.
"""
week_start = get_start_week(year, month, day)
week = Week(session, None, week_start, location)
return week
def get_week_days(year=None, month=None, day=None):
""" For a given date, retrieve the corresponding week and return the
list of all the days in the week with their dates.
For any missing parameters (ie: None), use the value of the current
day.
This function provides the 'Day date' string used at the header of
the agenda table.
:kwarg year: year to consider when searching a week.
:kwarg month: month to consider when searching a week.
:kwarg day: day to consider when searching a week.
:return a list of 'Day date' string corresponding to the week asked
either based on the current utc date or based on the information
specified.
"""
week_start = get_start_week(year, month, day)
weekdays = []
for i in range(0, 7):
curday = week_start + timedelta(days=i)
weekdays.append(curday)
return weekdays
def get_week_day_index(year=None, month=None, day=None):
""" For a specified date, find the index of this day in the week.
This function provides the 'Day index' string used to highlight the
current day in the calendar view.
:kwarg year: year to consider when searching a week.
:kwarg month: month to consider when searching a week.
:kwarg day: day to consider when searching a week.
:return a list of 'Day date' string corresponding to the week asked
either based on the current utc date or based on the information
specified.
"""
today = date.today()
if not year:
year = today.year
if not month:
month = today.month
if not day:
day = today.day
return date(year, month, day).isoweekday()
def format_full_day_meeting(meeting_list, week_start):
""" Return a list of list corresponding to an entry per day and for
each day the list of full_day meeting associated.
"""
meetings = []
for cnt in range(7):
meetings.append([])
for meeting in meeting_list:
idx = meeting.meeting_date - week_start
if idx.days < 0:
# Skip meetings finishing exactly at 00:00 on the day the week
# starts
if meeting.meeting_date_end == week_start + timedelta(days=7) \
and meeting.meeting_time_stop.hour == 0:
continue
idx = idx + timedelta(days=abs(idx.days))
meetings[int(idx.days)].append(meeting)
start_day = meeting.meeting_date
# If the meeting starts before the beginning of the week, consider
# the beginning of the week as the start day.
if start_day < week_start:
start_day = week_start
end_day = meeting.meeting_date_end
# If the end day is not the last day of the week, show it one day
# shorter (otherwise it show on day X while the meeting ends on
# day X at 00:00)
if end_day < start_day + timedelta(days=6):
end_day = end_day - timedelta(days=1)
duration = end_day - start_day
if abs(duration.days) > 0:
cnt = int(idx.days)
for day in range(1, duration.days + 1):
key = cnt + day
if key < len(meetings):
meetings[key].append(meeting)
return meetings
def format_week_meeting(meeting_list, tzone, week_start):
""" Return a dictionnary representing the meeting of the week in the
appropriate format for the meeting provided in the meeting_list.
"""
meetings = {}
for hour in HOURS[:-1]:
for key in ['%sh00', '%sh30']:
key = key % (hour)
# pylint: disable=W0612
meetings[key] = [None for cnt2 in range(0, 7)]
week_start = pytz.timezone(tzone).localize(
datetime(week_start.year, week_start.month, week_start.day, 0, 0,))
fmt = '%Hh%M'
#week_start = convert_time(week_start, 'UTC', tzone)
for meeting in meeting_list:
meeting = convert_meeting_timezone(
meeting, meeting.meeting_timezone, tzone)
start_delta = 0
if meeting.meeting_time_start.minute < 15:
start_delta = - meeting.meeting_time_start.minute
elif 15 <= meeting.meeting_time_start.minute <= 45:
start_delta = 30 - meeting.meeting_time_start.minute
elif meeting.meeting_time_start.minute > 45:
start_delta = 60 - meeting.meeting_time_start.minute
stop_delta = 0
if meeting.meeting_time_stop.minute < 15:
stop_delta = - meeting.meeting_time_stop.minute
elif 15 <= meeting.meeting_time_stop.minute <= 45:
stop_delta = 30 - meeting.meeting_time_stop.minute
elif meeting.meeting_time_stop.minute > 45:
stop_delta = 60 - meeting.meeting_time_stop.minute
startdt = datetime(
meeting.meeting_date.year,
meeting.meeting_date.month, meeting.meeting_date.day,
meeting.meeting_time_start.hour,
meeting.meeting_time_start.minute, 0
) + timedelta(minutes=start_delta)
# Required to add the tz info, does not actually convert
startdt = convert_time(
startdt, meeting.meeting_timezone, meeting.meeting_timezone)
stopdt = datetime(
meeting.meeting_date_end.year,
meeting.meeting_date_end.month,
meeting.meeting_date_end.day,
meeting.meeting_time_stop.hour,
meeting.meeting_time_stop.minute, 0
) + timedelta(minutes=stop_delta)
# Required to add the tz info
stopdt = convert_time(
stopdt, meeting.meeting_timezone, meeting.meeting_timezone)
if stopdt < startdt: # pragma: no cover
stopdt = stopdt + timedelta(days=1)
t_time = startdt
while t_time < stopdt:
if t_time < week_start \
or t_time >= (week_start + timedelta(days=7)
): # pragma: no cover
# Skip meeting start or ending in another week
t_time = t_time + timedelta(minutes=30)
continue
day = t_time.weekday()
key = t_time.strftime(fmt)
if key in meetings:
if meetings[key][day]:
meetings[key][day].append(meeting)
else:
meetings[key][day] = [meeting]
t_time = t_time + timedelta(minutes=30)
return meetings
def is_date_in_future(indate, start_time):
""" Return whether the date is in the future or the past.
:arg indate: a datetime object of the date to check
(ie: '2012-09-01')
:arg start_time: a string of the starting time of the meeting
(ie: '08')
"""
today = datetime.utcnow()
if today.date() > indate:
return False
elif today.date() == indate and today.hour > int(start_time.hour):
return False
else:
return True
def get_past_meeting_of_user(
session, username, tzone='UTC', from_date=date.today()):
""" Return all past meeting which specified username is among the
managers.
:arg session: the database session to use
:arg username: the FAS user name that you would like to have the
past meetings for.
:kwarg tzone: the time-zone to which to convert the meetings.
Defaults to 'UTC'.
:kwarg from_date: the date from which the futur meetings should be
retrieved. Defaults to today
"""
meetings_tmp = Meeting.expand_regular_meetings(
Meeting.get_past_meeting_of_user(session, username, from_date),
end_date=from_date)
meetings = []
for meeting in meetings_tmp:
meetings.append(convert_meeting_timezone(
meeting, meeting.meeting_timezone, tzone))
meetings.sort(key=operator.attrgetter('meeting_date'))
return meetings
# pylint: disable=C0103
def get_future_single_meeting_of_user(
session, username, tzone='UTC', from_date=date.today()):
""" Return all future meeting which specified username is among the
managers.
:arg session: the database session to use
:arg username: the FAS user name that you would like to have the
past meetings for.
:kwarg tzone: the time-zone to which to convert the meetings.
Defaults to 'UTC'.
:kwarg from_date: the date from which the futur meetings should be
retrieved. Defaults to today
"""
meetings_tmp = Meeting.get_future_single_meeting_of_user(
session, username, from_date)
meetings = []
for meeting in meetings_tmp:
meetings.append(convert_meeting_timezone(
meeting, meeting.meeting_timezone, tzone))
return meetings
# pylint: disable=C0103
def get_future_regular_meeting_of_user(
session, username, tzone='UTC', from_date=date.today()):
""" Return all future recursive meeting which specified username is
among the managers.
:arg session: the database session to use.
:arg username: the FAS user name that you would like to have the
past meetings for.
:kwarg tzone: the time-zone to which to convert the meetings.
Defaults to 'UTC'.
:kwarg from_date: the date from which the futur meetings should be
retrieved. Defaults to today.
"""
meetings_tmp = Meeting.get_future_regular_meeting_of_user(
session, username, from_date)
meetings = []
for meeting in meetings_tmp:
mtg_conv = convert_meeting_timezone(
meeting, meeting.meeting_timezone, tzone)
last_rec = mtg_conv.recursion_ends - timedelta(
days=mtg_conv.recursion_frequency)
if last_rec >= from_date:
meetings.append(mtg_conv)
return meetings
def agenda_is_free(
session, calendarobj,
meeting_date,
meeting_date_end):
"""Check if there is already someting planned in this agenda at that
time on that day.
:arg session: the database session to use
:arg calendar: the name of the calendar of interest.
:arg meeting_date: the date of the meeting (as Datetime object)
:arg meeting_date_end: the end date of the meeting (as Datetime
object)
:arg time_start: the time at which the meeting starts (as int)
:arg time_stop: the time at which the meeting stops (as int)
"""
meetings = Meeting.get_overlaping_meetings(
session, calendarobj,
meeting_date.date(),
meeting_date_end.date())
agenda_free = True
for meeting in set(meetings):
meeting_start_date_time = datetime(
meeting.meeting_date.year,
meeting.meeting_date.month,
meeting.meeting_date.day,
meeting.meeting_time_start.hour,
meeting.meeting_time_start.minute,
tzinfo=pytz.timezone(meeting.meeting_timezone))
meeting_stop_date_time = datetime(
meeting.meeting_date_end.year,
meeting.meeting_date_end.month,
meeting.meeting_date_end.day,
meeting.meeting_time_stop.hour,
meeting.meeting_time_stop.minute,
tzinfo=pytz.timezone(meeting.meeting_timezone))
if meeting_date <= meeting_start_date_time \
and meeting_date_end > meeting_start_date_time:
agenda_free = False
elif meeting_date < meeting_stop_date_time \
and meeting_date_end >= meeting_stop_date_time:
agenda_free = False
elif meeting_date < meeting_start_date_time \
and meeting_date_end > meeting_stop_date_time:
agenda_free = False
elif meeting_date > meeting_start_date_time \
and meeting_date_end < meeting_stop_date_time:
agenda_free = False
elif meeting_date == meeting_start_date_time \
and meeting_date_end == meeting_stop_date_time:
agenda_free = False
return agenda_free
def is_user_managing_in_calendar(session, calendar_name, fas_user):
""" Returns True if the user is in a group set as editor of the
calendar and False otherwise. It will also return True if there are
no groups set as editor the calendar.
:arg session: the database session to use
:arg calendar_name: the name of the calendar of interest.
:arg fas_user: a FAS user object with all the info.
"""
editor_groups = Calendar.get_editor_groups(session, calendar_name)
admin_groups = Calendar.get_admin_groups(session, calendar_name)
if not editor_groups:
return True
else:
return len(
set(editor_groups).intersection(set(fas_user.groups))
) >= 1 or len(
set(admin_groups).intersection(set(fas_user.groups))
) >= 1
def delete_recursive_meeting(
session, meeting, del_date=None, all_meetings=False):
""" Delete from the database any future meetings associated with this
recursion. For recursive meeting, deletion = set the end date to
today.
:arg session: the database session to use.
:arg meeting: the Meeting object from which are removed all further
meetings.
"""
if not del_date:
del_date = date.today()
if not meeting.recursion_frequency \
or meeting.recursion_ends < del_date:
return
meeting_date = meeting.meeting_date
meeting_date_end = meeting.meeting_date_end
cnt = 0
while meeting_date < del_date:
if meeting.recursion_ends < meeting_date + \
timedelta(
days=meeting.recursion_frequency * cnt
): # pragma: no cover
break
meeting_date = meeting.meeting_date + \
timedelta(
days=meeting.recursion_frequency * cnt)
meeting_date_end = meeting.meeting_date_end + \
timedelta(
days=meeting.recursion_frequency * cnt)
cnt = cnt + 1
if all_meetings:
# If the meeting_date is before the meeting.meeting_date, just delete
# the meeting
if meeting.meeting_date >= meeting_date:
meeting.delete(session)
else:
# End the recursion at the desired date
meeting.recursion_ends = meeting_date - timedelta(days=1)
meeting.save(session)
session.commit()
else:
original_rec_end = meeting.recursion_ends
# End recursion
meeting.recursion_ends = meeting_date - timedelta(days=1)
meeting.save(session)
# Re-create after deletion has occured
new_meeting = meeting.copy()
new_meeting.add_manager(session, meeting.meeting_manager)
new_meeting.meeting_date = meeting_date + timedelta(
days=meeting.recursion_frequency)
new_meeting.meeting_date_end = meeting_date_end + timedelta(
days=meeting.recursion_frequency)
new_meeting.recursion_ends = original_rec_end
new_meeting.save(session)
# pylint: disable=C0103
def _generate_date_rounded_to_the_hour(meetingdate, offset):
""" For a given date, return a new date to which the given offset in
hours has been added and the time rounded to the hour.
:arg meetingdate: a datetime object to which to add the offset
(in hours) and to round to the hour.
:arg offset: an integer representing the number of hour to add to
the given date.
"""
delta = timedelta(hours=offset)
new_date = meetingdate + delta
new_date = new_date - timedelta(seconds=new_date.second,
microseconds=new_date.microsecond)
return new_date
def retrieve_meeting_to_remind(session, offset=30):
""" Retrieve all the meetings for which we have to send a reminder.
:arg session: the database session to use.
:kwarg offset: the frequency at which the cron job is ran in order
to avoid sending twice the same reminder.
"""
today = datetime.utcnow()
meetings = []
for reminder_time in [12, 24, 48, 168]:
# Retrieve meeting planned in less than X hours
new_date = _generate_date_rounded_to_the_hour(today,
reminder_time)
end_date = new_date + timedelta(minutes=offset)
if new_date.date() != end_date.date():
# in case the end_date is day after (ie: 23:45 + 30min), make
# the end_date as start_date at 23:59
end_date = datetime(
new_date.year, new_date.month, new_date.day, 23, 59)
meetings.extend(Meeting.get_meeting_with_reminder(
session, new_date.date(), new_date.time(), end_date.time(),
'H-%s' % reminder_time))
return meetings
def add_meeting_to_vcal(ical, meeting):
""" Convert a Meeting object into iCal object and add it to the
provided calendar.
:arg ical: the iCal calendar object to which the meetings should
be added.
:arg meeting: a single fedocal.model.Meeting object to convert to
iCal and add to the provided calendar.
"""
entry = ical.add('vevent')
entry.add('summary').value = meeting.meeting_name
if meeting.meeting_information:
entry.add('description').value = meeting.meeting_information
entry.add('organizer').value = ', '.join(meeting.meeting_manager)
if meeting.meeting_location:
entry.add('location').value = meeting.meeting_location
start = entry.add('dtstart')
stop = entry.add('dtend')
if meeting.full_day:
start.value = meeting.meeting_date
stop.value = meeting.meeting_date_end
entry.add('transp').value = 'TRANSPARENT'
else:
tz = zoneinfo.gettz(meeting.meeting_timezone)
dti_start = datetime.combine(
meeting.meeting_date, meeting.meeting_time_start)
start.value = dti_start.replace(tzinfo=tz)
dti_end = datetime.combine(
meeting.meeting_date_end, meeting.meeting_time_stop)
stop.value = dti_end.replace(tzinfo=tz)
if meeting.recursion_frequency and meeting.recursion_ends:
newrule = rrule.rruleset()
freq = 1
if meeting.recursion_frequency == 14:
freq = 2
newrule.rrule(
rrule.rrule(
freq=rrule.WEEKLY,
interval=freq,
dtstart=start.value,
until=meeting.recursion_ends))
entry.rruleset = newrule
def add_meetings_to_vcal(ical, meetings):
""" Convert the Meeting objects into iCal object and add them to
the provided calendar.
:arg ical: the iCal calendar object to which the meetings should
be added.
:arg meetings: a list of fedocal.model.Meeting object to convert to
iCal and add to the provided calendar.
"""
for meeting in meetings:
add_meeting_to_vcal(ical, meeting)
def get_html_monthly_cal(
day=None, month=None, year=None, calendar_name=None, loc_name=None,
busy_days=[]):
""" Display a monthly calendar as HTML.
:kwarg day: optionnal day (as int). Defaults to current day
:kwarg month: optionnal month (as int). Defaults to current month
:kwarg year: optionnal year. Defaults to current year.
:kwarg calendar_name: the name of the calendar to which the links
should point.
:kwarg loc_name: the name of the location to which the links
should point.
"""
cur_date = date.today()
if not year:
year = cur_date.year
if not month:
month = cur_date.month
if not day:
day = cur_date.day
htmlcal = FedocalCalendar(day=day, year=year, month=month,
calendar_name=calendar_name,
loc_name=loc_name,
busy_days=busy_days)
curmonth_cal_nf = htmlcal.formatmonth()
return curmonth_cal_nf
def get_by_date(session, calendarobj, start_date, end_date, tzone='UTC',
extended=True):
""" Returns all the meetings in a given time period.
Recursive meetings are expanded as if each was a single meeting.
:arg session: the database session to use
:arg calendarobj: the calendar (object) of interest.
:arg start_date: a Date object representing the beginning of the
period
:arg start_date: a Date object representing the ending of the period
:kwarg tzone: the timezone in which the meetings should be displayed
defaults to UTC.
"""
meetings_utc = Meeting.get_by_date(
session, calendarobj, start_date, end_date, no_recursive=extended)
if extended:
meetings_utc.extend(
Meeting.get_regular_meeting_by_date(
session, calendarobj, start_date, end_date))
else:
meetings_utc.extend(
Meeting.get_active_regular_meeting_by_date(
session, calendarobj, start_date))
meetings = list(set(meetings_utc))
meetings.sort(key=operator.attrgetter('meeting_date'))
return meetings
def get_meetings_by_date_and_location(
session, calendar, start_date, end_date, location):
""" Return a list of meetings which have or will occur in between
the two provided dates.
:arg session: the database session to use
:arg calendar: the name of the calendar of interest.
:arg start_date: the date from which we would like to retrieve the
meetings (this day is included in the selection).
:arg start_date: the date until which we would like to retrieve the
meetings (this day is excluded from the selection).
:arg location: the location in which the meetings occurs.
"""
calendar = Calendar.by_id(session, calendar)
return Meeting.get_by_date_and_location(session, calendar, start_date,
end_date, location)
def get_by_date_at_location(
session, location, start_date, end_date, tzone='UTC'):
""" Returns all the meetings in a given time period at a given location.
Recursive meetings are expanded as if each was a single meeting.
:arg session: the database session to use
:arg calendarobj: the calendar (object) of interest.
:arg start_date: a Date object representing the beginning of the
period
:arg start_date: a Date object representing the ending of the period
:kwarg tzone: the timezone in which the meetings should be displayed
defaults to UTC.
"""
meetings_utc = Meeting.get_by_date_at_location(
session, location, start_date, end_date, no_recursive=True)
meetings_utc.extend(Meeting.get_regular_meeting_by_date_at_location(
session, location, start_date, end_date))
meetings = list(set(meetings_utc))
meetings.sort(key=operator.attrgetter('meeting_date'))
return meetings
# pylint: disable=R0913,R0914
def add_meeting(
session, calendarobj, fas_user,
meeting_name, meeting_date, meeting_date_end,
meeting_time_start, meeting_time_stop, comanager,
meeting_information,
meeting_location, tzone,
frequency, end_repeats,
remind_when, remind_who,
full_day,
admin=False):
""" When a user wants to add a meeting to the database, we need to
perform a number of test first checking that the input is valid
and then add the desired meeting.
"""
if not is_user_managing_in_calendar(
session, calendarobj.calendar_name, fas_user
) and not admin: # pragma: no cover
raise UserNotAllowed(
'You are not allowed to add a meeting to this calendar')
if meeting_date_end is None:
meeting_date_end = meeting_date
if full_day:
meeting_time_start = time(0, 0)
meeting_time_stop = time(0, 0)
tzone = 'UTC'
meeting_time_start = datetime(
meeting_date.year, meeting_date.month, meeting_date.day,
meeting_time_start.hour, meeting_time_start.minute)
meeting_time_stop = datetime(
meeting_date_end.year, meeting_date_end.month, meeting_date_end.day,
meeting_time_stop.hour, meeting_time_stop.minute)
if meeting_time_start.date() > meeting_time_stop.date():
raise InvalidMeeting(
'The start date of your meeting is later than the stop date.')
if meeting_time_start > meeting_time_stop:
raise InvalidMeeting(
'The start time of your meeting is later than the stop time.')
if full_day:
meeting_time_stop = meeting_time_stop + timedelta(days=1)
if meeting_time_start == meeting_time_stop:
raise InvalidMeeting(
'The start date of your meeting exactly the same as the stop date.'
)
reminder = None
if remind_when and remind_who:
reminder = dbaction.add_reminder(
session=session,
remind_when=remind_when,
remind_who=remind_who)
reminder_id = None
if reminder:
reminder_id = reminder.reminder_id
managers = '%s,' % fas_user.username
if comanager:
managers = managers + comanager
meeting = dbaction.add_meeting(
session=session,
meeting_name=meeting_name,
meeting_manager=managers,
meeting_date=meeting_time_start.date(),
meeting_date_end=meeting_time_stop.date(),
meeting_time_start=meeting_time_start,
meeting_time_stop=meeting_time_stop,
meeting_timezone=tzone,
meeting_information=meeting_information,
calendarobj=calendarobj,
reminder_id=reminder_id,
meeting_location=meeting_location,
recursion_frequency=frequency,
recursion_ends=end_repeats,
full_day=full_day)
session.commit()
return meeting
def edit_meeting(
session, meeting, calendarobj, fas_user,
meeting_name, meeting_date, meeting_date_end,
meeting_time_start, meeting_time_stop, comanager,
meeting_information,
meeting_location, tzone,
recursion_frequency, recursion_ends,
remind_when, remind_who,
full_day,
edit_all_meeting=True,
admin=False):
""" When a user wants to edit a meeting to the database, we need to
perform a number of test first checking that the input is valid
and then edit the desired meeting.
"""
if not is_user_managing_in_calendar(
session, calendarobj.calendar_name, fas_user) and not admin:
raise UserNotAllowed(
'You are not allowed to add a meeting to this calendar')
if not meeting_date_end:
meeting_date_end = meeting_date
if full_day:
meeting_time_start = time(0, 0)
meeting_time_stop = time(0, 0)
tzone = 'UTC'
meeting_time_start = datetime(
meeting_date.year, meeting_date.month, meeting_date.day,
meeting_time_start.hour, meeting_time_start.minute)
meeting_time_stop = datetime(
meeting_date_end.year, meeting_date_end.month, meeting_date_end.day,
meeting_time_stop.hour, meeting_time_stop.minute)
if meeting_time_start.date() > meeting_time_stop.date():
raise InvalidMeeting(
'The start date of your meeting is later than the stop date.')
if meeting_time_start > meeting_time_stop:
raise InvalidMeeting(
'The start time of your meeting is later than the stop time.')
if full_day and meeting_time_start == meeting_time_stop:
meeting_time_stop = meeting_time_start + timedelta(days=1)
if meeting_time_start == meeting_time_stop:
raise InvalidMeeting(
'The start date of your meeting exactly the same as the stop date.'
)
if meeting.calendar_name != calendarobj.calendar_name:
meeting.calendar_name = calendarobj.calendar_name
## The information are correct
## What we do now:
# a) the meeting is not recursive -> edit the information as provided
# b) the meeting is recursive and we update all the meetings
# -> recursion_end = today
# -> copy meeting to new object
# -> update new object
# c) the meeting is recursive and the update only one meeting
# -> recursion_end = today
# -> copy meeting to new object w/o recursion
# -> update new object
# -> copy meeting to new object w/ recursion and date = date + offset
remove_recursion = False
if meeting.recursion_frequency:
if meeting_date > meeting.meeting_date:
old_meeting = meeting.copy()
old_meeting.add_manager(session, meeting.meeting_manager)
old_meeting.recursion_ends = meeting_date - timedelta(days=1)
if old_meeting.recursion_ends > old_meeting.meeting_date:
old_meeting.save(session)
if not edit_all_meeting:
remove_recursion = True
new_meeting = meeting.copy()
new_meeting.add_manager(session, meeting.meeting_manager)
new_meeting.meeting_date = meeting_date + timedelta(
days=meeting.recursion_frequency)
new_meeting.meeting_date_end = meeting_date_end + timedelta(
days=meeting.recursion_frequency)