def commit_cal(events): shift_objects = {} print 'Deleting shift events...' se = ShiftEvent.objects.all() se.delete() for shift in set(SHIFTS + SAT_SHIFTS): shift_objects[shift] = Shift.objects.get(code=shift) dummy = User.objects.get(id=16) # default to some resident username = None # with ShiftEvent.delayed as d: for event in events: last_name = event[2] try: username = RESIDENTS[last_name.lower()] # do the lower() here so I can store the un-lower() as resident try: user = User.objects.get(username=username) except User.DoesNotExist: user = dummy except KeyError: subject = 'Misspelled last name: "' + last_name + '"' send_mail(subject, subject, '*****@*****.**', ['*****@*****.**'], fail_silently=False) user = dummy # TODO: If date upcoming soon, send email to everyone about the error and the name # d.insert(dict(date = event[0], shift=shift_objects[event[1]], resident=event[2], user=user)) print 'Saving %s...' % (str(event)) ShiftEvent.objects.create(date=event[0], shift=shift_objects[event[1]], resident=last_name, user=user) get_conflict_ids(force_refresh=True) get_last_update_time()
def update_cal(events): shift_objects = {} for shift in set(SHIFTS + SAT_SHIFTS): shift_objects[shift] = Shift.objects.get(code=shift) # with ShiftEvent.delayed as d: for event in events: last_name = event[2] shift = shift_objects[event[1]] if not last_name: # blank cell in the Excel file try: target_event = ShiftEvent.objects.get(date=event[0], shift=shift) target_event.delete() # delete so it doesn't show up on the calendar print "Deleted...%s" % (str(target_event)) except ShiftEvent.DoesNotExist: # most blanks are permanently blank and there will be no existing event # unfortunately I had to add this even though it will result in exceptions being raised # to account for when they decide to eliminate a shift, such as the Hillman Saturdays print 'This shift event does not exist' pass else: if '/' in last_name: # take the first name if two are given to assign at least one user last_name = last_name.split('/')[0] username = RESIDENTS.get(last_name.lower().translate(None, '0123456789,').strip(), 'unknown') # do the lower() here so I can store the un-lower() as resident try: user = User.objects.get(username=username) except User.DoesNotExist: user = None if username == 'unknown': subject = 'Misspelled last name: "' + last_name + '"' send_mail(subject, subject, '*****@*****.**', ['*****@*****.**'], fail_silently=False) user = None # TODO: If date upcoming soon, send email to everyone about the error and the name created = False try: target_event, created = ShiftEvent.objects.get_or_create(date=event[0], shift=shift) except ShiftEvent.MultipleObjectsReturned: shifts = ShiftEvent.objects.filter(date=event[0], shift=shift) target_event = shifts[0] for s in shifts[1:]: body = str(s.date) + ' - ' + s.shift.name + ' - ' + s.resident send_mail('Shift event deleted', body, '*****@*****.**', ['*****@*****.**'], fail_silently=False) s.delete() if not created: print 'Updating %s...' % (str(target_event)) target_event.resident=event[2] # the exact string from each cell in Excel target_event.user=user target_event.start = timezone.make_aware(datetime.combine(event[0], shift.start_time), timezone.get_default_timezone()) target_event.end = timezone.make_aware(datetime.combine(event[0], shift.end_time), timezone.get_default_timezone()) target_event.save() if not created: print 'To %s...' % (str(target_event)) else: print 'Created...%s' % (str(target_event)) # d.insert(dict(date = event[0], shift=shift_objects[event[1]], resident=event[2], user=user)) get_conflict_ids(force_refresh=True) get_last_update_time() cache.delete('se_json')