def add_schedule(request): play = request.POST['play'] studio = request.POST['studio'] show_time = request.POST['show_time'] ticket_money = request.POST['money'] str_showtime = show_time.replace( 'T', ' ') # 把时间2019-05-05T13:30格式化为2019-05-05 13:30 datetime_showtime = datetime.datetime.strptime( str_showtime, "%Y-%m-%d %H:%M") # 把字符串转化为datetime时间,因为下面要进行加法计算 play_obj = get_object_or_404(Play, id=play) datetime_endtime = datetime_showtime + datetime.timedelta( minutes=play_obj.length) end_time = datetime_endtime.strftime( "%Y-%m-%dT%H:%M") # datetime时间转化为字符串时间 studio_obj = get_object_or_404(Studio, id=studio) ticket_counts = studio_obj.seat_rows * studio_obj.seat_cols ticket_all_counts = studio_obj.seat_rows * studio_obj.seat_cols # print(play, studio, show_time, ticket_money, ticket_counts, '---------------') schedule = Schedule(play=play_obj, studio=studio_obj, show_time=show_time, end_time=end_time, ticket_counts=ticket_counts, ticket_money=float(ticket_money), ticket_all_counts=ticket_all_counts) schedule.save() return redirect(reverse('schedule_list'))
def check_for_status_update(request): if request.method == 'GET': if (Schedule.objects.last() is None) or (Schedule.objects.last().date != datetime.date.today()): schedule_object = Schedule() else: schedule_object = Schedule.objects.all().order_by('-date')[:1][0] return JsonResponse({ 'status_1' : schedule_object.slot_1['status'], 'status_2' : schedule_object.slot_2['status'], 'status_3' : schedule_object.slot_3['status'], 'status_4' : schedule_object.slot_4['status'], 'status_5' : schedule_object.slot_5['status'], 'status_6' : schedule_object.slot_6['status'], 'status_7' : schedule_object.slot_7['status'], 'status_8' : schedule_object.slot_8['status'], 'status_9' : schedule_object.slot_9['status'], 'status_10': schedule_object.slot_10['status'], 'status_11': schedule_object.slot_11['status'], 'status_12': schedule_object.slot_12['status'], 'status_13': schedule_object.slot_13['status'], 'status_14': schedule_object.slot_14['status'], 'status_15': schedule_object.slot_15['status'], 'status_16': schedule_object.slot_16['status'], 'status_17': schedule_object.slot_17['status'], 'status_18': schedule_object.slot_18['status'], 'status_19': schedule_object.slot_19['status'], 'status_20': schedule_object.slot_20['status'], 'status_21': schedule_object.slot_21['status'], 'status_22': schedule_object.slot_22['status'], 'status_23': schedule_object.slot_23['status'], 'status_24': schedule_object.slot_24['status'], 'status_25': schedule_object.slot_25['status'], 'status_26': schedule_object.slot_26['status'], 'status_27': schedule_object.slot_27['status'], 'status_28': schedule_object.slot_28['status'], 'status_29': schedule_object.slot_29['status'], 'status_30': schedule_object.slot_30['status'], 'status_31': schedule_object.slot_31['status'], 'status_32': schedule_object.slot_32['status'], 'status_33': schedule_object.slot_33['status'], 'status_34': schedule_object.slot_34['status'], 'status_35': schedule_object.slot_35['status'], 'status_36': schedule_object.slot_36['status'], 'status_37': schedule_object.slot_37['status'], 'status_38': schedule_object.slot_38['status'], 'status_39': schedule_object.slot_39['status'], 'status_40': schedule_object.slot_40['status'], 'status_41': schedule_object.slot_41['status'], 'status_42': schedule_object.slot_42['status'], 'status_43': schedule_object.slot_43['status'], 'status_44': schedule_object.slot_44['status'], 'status_45': schedule_object.slot_45['status'], 'status_46': schedule_object.slot_46['status'], 'status_47': schedule_object.slot_47['status'], 'status_48': schedule_object.slot_48['status'], })
def create_edit_slot(request, id=None): if not request.user.has_perm( 'schedule.add_user') and request.user.has_perm( 'schedule.edit_schedule'): return HttpResponseForbidden() user = request.user studentChecked = None if id: slot = get_object_or_404(Schedule, id=id) if slot.creator != request.user: return HttpResponseForbidden() studentChecked = slot.student.all().values_list('matricule', flat=True) else: slot = Schedule(creator=user) classes = Classes.objects.all() if request.method == 'POST': form = SlotForm(request.POST, instance=slot) if form.is_valid(): slot_instance = form.save(commit=False) if id: slot_instance.last_user = user slot_instance.save() slot.teacher.clear() for teacher in form.cleaned_data['teacher']: slot.teacher.add(teacher) slot.student.clear() for student in form.cleaned_data['student']: slot.student.add(student) return redirect('schedule:schedule_main') else: print('SlotForm is invalid') print('errors: ', form.errors) else: form = SlotForm(instance=slot) variables = { 'studentChecked': studentChecked, 'classes': classes, 'form': form, } template = 'schedule/slot_form.html' return render(request, template, variables)
def update(request, schedule_id): schedule = Schedule() schedule.Id = request.POST['id'] schedule.Name = request.POST['name'] schedule.StartDateDisplay = request.POST['startdate'] schedule.StartDate = time.strptime(schedule.StartDateDisplay, "%d/%m/%Y") schedule.StatusTypeId = request.POST['statustype'] schedule.StatusDateDisplay = request.POST.get('statusdate', '') schedule.StatusDate = time.strptime(schedule.StatusDateDisplay, "%d/%m/%Y") if schedule.StatusDateDisplay != '' else None schedule = UpdateWorkingDays(request, schedule) scheduleService = ScheduleService if schedule_id != 0: scheduleService.Update(schedule) else: scheduleService.Add(schedule) return HttpResponseRedirect('/schedule')
def add_schedule(request): if request.POST: time_frame = request.POST.get('time_frame') description = request.POST.get('description') priority = request.POST.get('priority') is_done = request.POST.get('is_done') user_id = request.POST.get('user_id') print(user_id) print(is_done) try: sch_obj = Schedule(time_frame=time_frame, description=description, priority=priority, is_done=is_done, user_id=user_id) sch_obj.save() return render(request, 'schedule/add.html', {'message': '时间表添加成功'}) except: print('fail to add') return render(request, 'schedule/add.html', {'message': '时间表添加失败'}) else: return render(request, 'schedule/add.html')
def schedule(request): if request.method == 'GET': if (Schedule.objects.last() is None) or (Schedule.objects.last().date != datetime.date.today()): form_1 = ScheduleForm(initial=get_default_json(1)) form_2 = ScheduleForm(initial=get_default_json(2)) form_3 = ScheduleForm(initial=get_default_json(3)) form_4 = ScheduleForm(initial=get_default_json(4)) form_5 = ScheduleForm(initial=get_default_json(5)) form_6 = ScheduleForm(initial=get_default_json(6)) form_7 = ScheduleForm(initial=get_default_json(7)) form_8 = ScheduleForm(initial=get_default_json(8)) form_9 = ScheduleForm(initial=get_default_json(9)) form_10 = ScheduleForm(initial=get_default_json(10)) form_11 = ScheduleForm(initial=get_default_json(11)) form_12 = ScheduleForm(initial=get_default_json(12)) form_13 = ScheduleForm(initial=get_default_json(13)) form_14 = ScheduleForm(initial=get_default_json(14)) form_15 = ScheduleForm(initial=get_default_json(15)) form_16 = ScheduleForm(initial=get_default_json(16)) form_17 = ScheduleForm(initial=get_default_json(17)) form_18 = ScheduleForm(initial=get_default_json(18)) form_19 = ScheduleForm(initial=get_default_json(19)) form_20 = ScheduleForm(initial=get_default_json(20)) form_21 = ScheduleForm(initial=get_default_json(21)) form_22 = ScheduleForm(initial=get_default_json(22)) form_23 = ScheduleForm(initial=get_default_json(23)) form_24 = ScheduleForm(initial=get_default_json(24)) form_25 = ScheduleForm(initial=get_default_json(25)) form_26 = ScheduleForm(initial=get_default_json(26)) form_27 = ScheduleForm(initial=get_default_json(27)) form_28 = ScheduleForm(initial=get_default_json(28)) form_29 = ScheduleForm(initial=get_default_json(29)) form_30 = ScheduleForm(initial=get_default_json(30)) form_31 = ScheduleForm(initial=get_default_json(31)) form_32 = ScheduleForm(initial=get_default_json(32)) form_33 = ScheduleForm(initial=get_default_json(33)) form_34 = ScheduleForm(initial=get_default_json(34)) form_35 = ScheduleForm(initial=get_default_json(35)) form_36 = ScheduleForm(initial=get_default_json(36)) form_37 = ScheduleForm(initial=get_default_json(37)) form_38 = ScheduleForm(initial=get_default_json(38)) form_39 = ScheduleForm(initial=get_default_json(39)) form_40 = ScheduleForm(initial=get_default_json(40)) form_41 = ScheduleForm(initial=get_default_json(41)) form_42 = ScheduleForm(initial=get_default_json(42)) form_43 = ScheduleForm(initial=get_default_json(43)) form_44 = ScheduleForm(initial=get_default_json(44)) form_45 = ScheduleForm(initial=get_default_json(45)) form_46 = ScheduleForm(initial=get_default_json(46)) form_47 = ScheduleForm(initial=get_default_json(47)) form_48 = ScheduleForm(initial=get_default_json(48)) else: schedule_object = Schedule.objects.all().order_by('-date')[:1][0] objects = [schedule_object.slot_1, schedule_object.slot_2, schedule_object.slot_3, schedule_object.slot_4, schedule_object.slot_5, schedule_object.slot_6, schedule_object.slot_7, schedule_object.slot_8, schedule_object.slot_9, schedule_object.slot_10, schedule_object.slot_11, schedule_object.slot_12, schedule_object.slot_13, schedule_object.slot_14, schedule_object.slot_15, schedule_object.slot_16, schedule_object.slot_17, schedule_object.slot_18, schedule_object.slot_19, schedule_object.slot_20, schedule_object.slot_21, schedule_object.slot_22, schedule_object.slot_23, schedule_object.slot_24, schedule_object.slot_25, schedule_object.slot_26, schedule_object.slot_27, schedule_object.slot_28, schedule_object.slot_29, schedule_object.slot_30, schedule_object.slot_31, schedule_object.slot_32, schedule_object.slot_33, schedule_object.slot_34, schedule_object.slot_35, schedule_object.slot_36, schedule_object.slot_37, schedule_object.slot_38, schedule_object.slot_39, schedule_object.slot_40, schedule_object.slot_41, schedule_object.slot_42, schedule_object.slot_43, schedule_object.slot_44, schedule_object.slot_45, schedule_object.slot_46, schedule_object.slot_47, schedule_object.slot_48, ] form_1 = ScheduleForm(initial=objects[0]) form_2 = ScheduleForm(initial=objects[1]) form_3 = ScheduleForm(initial=objects[2]) form_4 = ScheduleForm(initial=objects[3]) form_5 = ScheduleForm(initial=objects[4]) form_6 = ScheduleForm(initial=objects[5]) form_7 = ScheduleForm(initial=objects[6]) form_8 = ScheduleForm(initial=objects[7]) form_9 = ScheduleForm(initial=objects[8]) form_10 = ScheduleForm(initial=objects[9]) form_11 = ScheduleForm(initial=objects[10]) form_12 = ScheduleForm(initial=objects[11]) form_13 = ScheduleForm(initial=objects[12]) form_14 = ScheduleForm(initial=objects[13]) form_15 = ScheduleForm(initial=objects[14]) form_16 = ScheduleForm(initial=objects[15]) form_17 = ScheduleForm(initial=objects[16]) form_18 = ScheduleForm(initial=objects[17]) form_19 = ScheduleForm(initial=objects[18]) form_20 = ScheduleForm(initial=objects[19]) form_21 = ScheduleForm(initial=objects[20]) form_22 = ScheduleForm(initial=objects[21]) form_23 = ScheduleForm(initial=objects[22]) form_24 = ScheduleForm(initial=objects[23]) form_25 = ScheduleForm(initial=objects[24]) form_26 = ScheduleForm(initial=objects[25]) form_27 = ScheduleForm(initial=objects[26]) form_28 = ScheduleForm(initial=objects[27]) form_29 = ScheduleForm(initial=objects[28]) form_30 = ScheduleForm(initial=objects[29]) form_31 = ScheduleForm(initial=objects[30]) form_32 = ScheduleForm(initial=objects[31]) form_33 = ScheduleForm(initial=objects[32]) form_34 = ScheduleForm(initial=objects[33]) form_35 = ScheduleForm(initial=objects[34]) form_36 = ScheduleForm(initial=objects[35]) form_37 = ScheduleForm(initial=objects[36]) form_38 = ScheduleForm(initial=objects[37]) form_39 = ScheduleForm(initial=objects[38]) form_40 = ScheduleForm(initial=objects[39]) form_41 = ScheduleForm(initial=objects[40]) form_42 = ScheduleForm(initial=objects[41]) form_43 = ScheduleForm(initial=objects[42]) form_44 = ScheduleForm(initial=objects[43]) form_45 = ScheduleForm(initial=objects[44]) form_46 = ScheduleForm(initial=objects[45]) form_47 = ScheduleForm(initial=objects[46]) form_48 = ScheduleForm(initial=objects[47]) context = { 'date': datetime.date.today(), 'form_dict': {'form_1' : form_1, 'form_2' : form_2, 'form_3' : form_3, 'form_4' : form_4, 'form_5' : form_5, 'form_6' : form_6, 'form_7' : form_7, 'form_8' : form_8, 'form_9' : form_9, 'form_10': form_10, 'form_11': form_11, 'form_12': form_12, 'form_13': form_13, 'form_14': form_14, 'form_15': form_15, 'form_16': form_16, 'form_17': form_17, 'form_18': form_18, 'form_19': form_19, 'form_20': form_20, 'form_21': form_21, 'form_22': form_22, 'form_23': form_23, 'form_24': form_24, 'form_25': form_25, 'form_26': form_26, 'form_27': form_27, 'form_28': form_28, 'form_29': form_29, 'form_30': form_30, 'form_31': form_31, 'form_32': form_32, 'form_33': form_33, 'form_34': form_34, 'form_35': form_35, 'form_36': form_36, 'form_37': form_37, 'form_38': form_38, 'form_39': form_39, 'form_40': form_40, 'form_41': form_41, 'form_42': form_42, 'form_43': form_43, 'form_44': form_44, 'form_45': form_45, 'form_46': form_46, 'form_47': form_47, 'form_48': form_48, }} return render(request, 'schedule.html', context=context) elif request.method == 'POST': if 'notify' in request.POST: encoder = { 1: date.month * date.day * 217, 2: date.month * date.day * 94, 3: date.month * date.day * 33, 4: date.month * date.day * 65, 5: date.month * date.day * 210, 6: date.month * date.day * 38, 7: date.month * date.day * 51, 8: date.month * date.day * 176, 9: date.month * date.day * 154, 10: date.month * date.day * 77, 11: date.month * date.day * 66, 12: date.month * date.day * 114, 13: date.month * date.day * 177, 14: date.month * date.day * 111, 15: date.month * date.day * 86, 16: date.month * date.day * 74, 17: date.month * date.day * 104, 18: date.month * date.day * 186, 19: date.month * date.day * 78, 20: date.month * date.day * 37, 21: date.month * date.day * 222, 22: date.month * date.day * 12, 23: date.month * date.day * 75, 24: date.month * date.day * 55, 25: date.month * date.day * 46, 26: date.month * date.day * 57, 27: date.month * date.day * 95, 28: date.month * date.day * 60, 29: date.month * date.day * 59, 30: date.month * date.day * 63, 31: date.month * date.day * 99, 32: date.month * date.day * 170, 33: date.month * date.day * 44, 34: date.month * date.day * 32, 35: date.month * date.day * 61, 36: date.month * date.day * 151, 37: date.month * date.day * 189, 38: date.month * date.day * 147, 39: date.month * date.day * 80, 40: date.month * date.day * 234, 41: date.month * date.day * 85, 42: date.month * date.day * 35, 43: date.month * date.day * 43, 44: date.month * date.day * 202, 45: date.month * date.day * 81, 46: date.month * date.day * 93, 47: date.month * date.day * 228, 48: date.month * date.day * 24, } schedule_object = Schedule.objects.all().order_by('-date')[:1][0] json_dicts = [schedule_object.slot_1, schedule_object.slot_2, schedule_object.slot_3, schedule_object.slot_4, schedule_object.slot_5, schedule_object.slot_6, schedule_object.slot_7, schedule_object.slot_8, schedule_object.slot_9, schedule_object.slot_10, schedule_object.slot_11, schedule_object.slot_12, schedule_object.slot_13, schedule_object.slot_14, schedule_object.slot_15, schedule_object.slot_16, schedule_object.slot_17, schedule_object.slot_18, schedule_object.slot_19, schedule_object.slot_20, schedule_object.slot_21, schedule_object.slot_22, schedule_object.slot_23, schedule_object.slot_24, schedule_object.slot_25, schedule_object.slot_26, schedule_object.slot_27, schedule_object.slot_28, schedule_object.slot_29, schedule_object.slot_30, schedule_object.slot_31, schedule_object.slot_32, schedule_object.slot_33, schedule_object.slot_34, schedule_object.slot_35, schedule_object.slot_36, schedule_object.slot_37, schedule_object.slot_38, schedule_object.slot_39, schedule_object.slot_40, schedule_object.slot_41, schedule_object.slot_42, schedule_object.slot_43, schedule_object.slot_44, schedule_object.slot_45, schedule_object.slot_46, schedule_object.slot_47, schedule_object.slot_48, ] slot = int(request.POST['form_indicator'][1:]) # Slice it because it begins with '_' name = request.POST['name'] modality = request.POST['modality'] phone = request.POST['phone'] email = request.POST['email'] # Change the indicator to a semi-random number, the 48-set of which change every day, so that no one can meddle slot_encoded = encoder[slot] # Use regex to limit spaces to 1, split the resulting string on whitespaces, then capitalize each segment name = ' '.join([x.capitalize() for x in re.sub('\s{2,}', ' ', name).split(' ')]) # Keep 'there' lower-case when no name is provided in the form. if name == 'There': name = 'there' slot_json = json_dicts[slot - 1] slot_json['status'] = 'Sent' schedule_object.save() def send_email(): email_body = """\ <html> <head></head> <body style="border-radius: 20px; padding: 1rem; color: black; font-size: 1.1rem; background-color: #d5e9fb"> <h2>Hello {0},</h2> <h3>We are now ready for you!</h3> </br> <p>Please use the button below to indicate that you are on your way. Thank you!</p> </br> <a href="appointmentalert.herokuapp.com/{1}/confirm"><input type="button" style="border-radius: 9px; padding: 1rem; margin: 1rem 0; color: white; background-color: #1F45FC;" value="Confirm Appointment"></a> </br> </body> </html> """.format(name, slot_encoded) email_message = EmailMessage('Ready for your appointment!', email_body, from_email='NBRHC Alerts <*****@*****.**>', to=[email]) # It wants a tuple or a list email_message.content_subtype = "html" # this is the crucial part email_message.send() def send_text(): to = '+1' + phone client = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN) response = client.messages.create( body='\nHello {0},\n\nWe are now ready for you!\n\nPlease use the link below to indicate that you are on your way.\n\nappointmentalert.herokuapp.com/{1}/confirm\n\nThank you!'.format(name, slot_encoded), to=to, from_=settings.TWILIO_PHONE_NUMBER) if modality == 'Email': send_email() elif modality == 'Phone': send_text() elif modality == 'Phone & Email': send_email() send_text() else: if Schedule.objects.last() is None: schedule_object = Schedule() set_default_slots = True elif Schedule.objects.last().date == datetime.date.today(): schedule_object = Schedule.objects.all().order_by('-date')[:1][0] set_default_slots = False else: schedule_object = Schedule() set_default_slots = True json_dicts = [schedule_object.slot_1, schedule_object.slot_2, schedule_object.slot_3, schedule_object.slot_4, schedule_object.slot_5, schedule_object.slot_6, schedule_object.slot_7, schedule_object.slot_8, schedule_object.slot_9, schedule_object.slot_10, schedule_object.slot_11, schedule_object.slot_12, schedule_object.slot_13, schedule_object.slot_14, schedule_object.slot_15, schedule_object.slot_16, schedule_object.slot_17, schedule_object.slot_18, schedule_object.slot_19, schedule_object.slot_20, schedule_object.slot_21, schedule_object.slot_22, schedule_object.slot_23, schedule_object.slot_24, schedule_object.slot_25, schedule_object.slot_26, schedule_object.slot_27, schedule_object.slot_28, schedule_object.slot_29, schedule_object.slot_30, schedule_object.slot_31, schedule_object.slot_32, schedule_object.slot_33, schedule_object.slot_34, schedule_object.slot_35, schedule_object.slot_36, schedule_object.slot_37, schedule_object.slot_38, schedule_object.slot_39, schedule_object.slot_40, schedule_object.slot_41, schedule_object.slot_42, schedule_object.slot_43, schedule_object.slot_44, schedule_object.slot_45, schedule_object.slot_46, schedule_object.slot_47, schedule_object.slot_48, ] # Run this code if a new object has been created if set_default_slots == True: slot_counter = 0 for slot_json in json_dicts: slot_json['slot'] = slots[slot_counter] slot_counter+=1 # Get the specific slot to update based on the POST request slot_json = json_dicts[int(request.POST['form_indicator'][1:]) - 1] slot_json['slot'] = request.POST['slot'] slot_json['name'] = request.POST['name'].capitalize() slot_json['modality'] = request.POST['modality'] slot_json['phone'] = request.POST['phone'] slot_json['email'] = request.POST['email'] slot_json['status'] = request.POST['status'] schedule_object.save() return HttpResponse()
def start_button(request): schedule = Schedule() schedule.start = timezone.now() schedule.finish = None schedule.worker = User.objects.get(username__exact=request.user.username) schedule.save()