Exemplo n.º 1
0
def detail(request, user_id):
    try:
        user = User.objects.get(id=user_id)
        up_user = UserProfile.objects.get(user=user)
        # 오늘부터 14일 이내 만나기로 한 날짜 포함되는 스케쥴 정보 가져오기. 가까운 날짜 순으로 정렬
        today = date.today()
        # 스케줄객체, 본인, 오늘이후부터, 14일 이내, acceptance==True, 만나는 날짜 순으로 정렬
        # 이 객체만 제외하고 다 Available로 처리하면 될듯
        # 스케줄 객체는 언제 생성되는가 - 누군가가 나에게 리퀘스트를 보냈을때 생긴다. acceptance가 False인채로
        schedules = Schedule.objects.filter(guide_id=user). \
            filter(meetdate__gte=today).filter(meetdate__lte=date.today()+timedelta(days=14)).filter(acceptance=True)\
            .order_by('meetdate')
        # schedules가 Queryset이라 리스트로 형변화
        schedules = list(schedules)
        # 현재 유저, 만남 있는 날짜만 모은 리스트
        current_meetdate = [i.meetdate for i in schedules]
        # 오늘로부터 2주 날짜 객체 리스트
        weeks = [today+timedelta(days=day) for day in range(14)]
        schedule_final = []
        i = 0
        for each in weeks:
            if each in current_meetdate:
                schedule_final.append(schedules[i])
                i += 1
            else:
                schedule = Schedule()
                schedule.meetdate = each
                schedule.acceptance = False
                schedule_final.append(schedule)
        data = serializers.serialize('json', schedule_final)

    except ObjectDoesNotExist:
        return index

    return render(request, 'accounts/schedule_detail.html', {'up': up_user, 'data': data})
Exemplo n.º 2
0
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'))
Exemplo n.º 3
0
def club_schedule(request, id):
	club = Club.objects.get(id=id)
	path = reverse("club_page", kwargs={"id":club.id})
	schedule_form = ScheduleForm(request.POST or None, prefix='schedule')

	if request.method == 'POST':
		if schedule_form.is_valid():
		    schedule_text = schedule_form.cleaned_data['text']
		    schedule_duedate = schedule_form.cleaned_data['duedate']
		    
		    new_form = Schedule()
		    new_form.user = request.user
		    new_form.club = club
		    new_form.text = schedule_text
		    new_form.duedate = schedule_duedate
		    new_form.choice = 'club'
		    new_form.save()
		    

		    response_data = {}
		    					
		    response_data['response'] = True


		    new_data = json.dumps(response_data)
		    
		    return HttpResponse(new_data, content_type='application/json')
		else:
			data = {}
			data['response'] = False
			new_data = json.dumps(data)
			return HttpResponse(new_data, content_type='application/json')
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'], })
Exemplo n.º 5
0
    def validate(self, request, **kwargs):
        date = request.data['date']
        start_time = request.data['start_time']
        end_time = request.data['end_time']

        if Schedule.is_schedule_conflict(date, start_time, end_time):
            return Response({'detail': 'Horário não disponivel'},
                            status=status.HTTP_400_BAD_REQUEST)
        return func(self, request, **kwargs)
Exemplo n.º 6
0
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')
Exemplo n.º 7
0
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)
Exemplo n.º 8
0
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')
Exemplo n.º 9
0
def add_schedule(request):
    '''
    新增上线计划
    '''   
    servers = Server.objects.order_by('saltid')
    softforms = SoftForm.objects.order_by('soft_type')
    apppackages = AppPackage.objects.order_by('package_name')
    actiongroupss = ActionGroup.objects.order_by('name')
    testcases = Testcase.objects.order_by('name')
    if request.POST:
        schedule_id = request.POST.get('schedule_id', None)  # 上线计划单号。唯一标识
        service_name = request.POST.get('service_name', None)  # 服务名称。唯一标识
        department = request.POST.get('department', None)  # 上线部署部门
        department_dep = request.POST.get('department_dep', None)  # 研发部门
        contact = request.POST.get('contact', None)  # 联系人
        contact_info = request.POST.get('contact_info', None)  # 联系信息
        auto_rollback = request.POST.get('auto_rollback', None)  # 自动回滚标识。 0:自动回滚;1:不自动回滚;
        
        server_ids = request.POST.getlist('server_ids', None)  # 服务器组 
        softform_ids = request.POST.getlist('softform_ids', None)  # 基础应用模板组
        apppackage_ids = request.POST.getlist('apppackage_ids', None)  # 应用服务组
        actiongroup_ids = request.POST.getlist('actiongroup_ids', None)  # 操作流程
        testcase_ids = request.POST.getlist('testcase_ids', None)  # 测试用例组


        
        schedule = Schedule.objects.filter(schedule_id__exact = schedule_id)
        if schedule:
            return HttpResponse(simplejson.dumps({"statusCode": 302,
                                                  "navTabId": request.POST.get('navTabId', 'scheduleindex'),
                                                  "callbackType": request.POST.get('callbackType', None),
                                                  "message": u'此上线计划已存在,不能添加',
                                                  "info": u'此上线计划已存在,不能添加',
                                                  "result": u'此上线计划已存在,不能添加'}),
                                mimetype='application/json')


        schedule = Schedule(schedule_id = schedule_id, service_name=service_name,
                            user_name = request.user.username, 
                            department = department, department_dep = department_dep, 
                            contact = contact, contact_info = contact_info, 
                            auto_rollback = auto_rollback, executive_state = 1)
        schedule.save()        
        if server_ids != None and len(server_ids) != 0:
            for server_id in server_ids:
                server = Server.objects.get(id__exact = server_id)
                schedule.server_group.add(server)
        if softform_ids != None and len(softform_ids) != 0:
            for softform_id in softform_ids:
                softform = SoftForm.objects.get(id__exact = softform_id)
                schedule.softform_group.add(softform)
        if apppackage_ids != None and len(apppackage_ids) != 0:
            for apppackage_id in apppackage_ids:
                apppackage = AppPackage.objects.get(id__exact = apppackage_id)
                schedule.apppackage_group.add(apppackage)
        if actiongroup_ids != None and len(actiongroup_ids) != 0:
            for actiongroup_id in actiongroup_ids:
                actiongroup = ActionGroup.objects.get(id__exact = actiongroup_id)
                schedule.actiongroups.add(actiongroup)
        if testcase_ids != None and len(testcase_ids) != 0:
            for testcase_id in testcase_ids:
                testcase = Testcase.objects.get(id__exact = testcase_id)
                schedule.testcase_group.add(testcase)                            
        
        Log(username = request.user.username,
            content = u"上线计划" + schedule_id + u"添加成功").save()
        return HttpResponse(simplejson.dumps({"statusCode": 200,
                                              "navTabId": request.POST.get('navTabId', 'scheduleindex'),
                                              "callbackType": request.POST.get('callbackType', 'closeCurrent'),
                                              "message": u'上线计划' + schedule_id + u'添加成功'}),
                            mimetype='application/json')
    return render_to_response('schedule/add.html', {'servers': servers, 
                                                    'softforms': softforms, 
                                                    'apppackages': apppackages, 
                                                    'actiongroupss': actiongroupss, 
                                                    'testcases': testcases})
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()
Exemplo n.º 11
0
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()