Пример #1
0
def get_quiz(request):
    if request.method == 'GET':
        recent_score = get_curr_profile(request).menu_score
        if recent_score is not None:
            logger.warning('recent score detected: %d', recent_score)
            return HttpResponseBadRequest(
                'You\'ve recently done menu test with score %d.' %
                recent_score)
        if request.user.profile.role == 'MA':
            quiz = Quiz.objects.filter(
                business=get_curr_business(request)).first()
            is_preview = True
            if not quiz:
                return HttpResponse(status=503)
        else:
            quiz = Quiz.objects.filter(business=get_curr_business(request), role=get_curr_profile(request).role). \
                first()
            is_preview = False
            if not quiz:
                return HttpResponseNotFound(
                    'There are not any quizzes for %s yet' %
                    get_curr_profile(request).get_role_display())
        response = quiz.serialize(is_preview)

        logger.info('data is ' + str(response))
        return JsonResponse(response)

    return wrong_method(request)
Пример #2
0
def get_calendar_current_week_shifts(request):
    if request.method == 'GET':
        shifts_json = []

        current_week_slots = ShiftSlot.objects.filter(
            week=get_curr_week_num(), business=get_curr_business(request))

        for slot in current_week_slots:
            if not slot.was_shift_generated():
                continue

            bg_color, text_color = slot.get_calendar_colors(
                get_curr_profile(request))

            jsoned_shift = json.dumps({
                'id': str(slot.id),
                'title': slot.name,
                'start': slot.start_time_str(),
                'end': slot.end_time_str(),
                'backgroundColor': bg_color,
                'textColor': text_color
            })
            shifts_json.append(jsoned_shift)
        return JsonResponse(json.dumps(shifts_json), safe=False)

    return wrong_method(request)
Пример #3
0
def generate_shifts(request):
    def execute_shift_generation(business):

        business.set_shift_generation_pending()
        business.save()

        if settings.CELERY:
            tasks.generate_next_week_shifts.delay(
                business.business_name,
                settings.SHIFT_GENERATION_ALGORITHM_LEVEL)
        else:
            tasks.generate_next_week_shifts(
                business.business_name,
                settings.SHIFT_GENERATION_ALGORITHM_LEVEL)

    if request.method == 'POST':
        next_week_slots = ShiftSlot.objects.filter(
            week=get_next_week_num(), business=get_curr_business(request))
        if not len(next_week_slots):
            messages.error(request, 'No slots next week !')
            return HttpResponseBadRequest('No slots next week !')
        else:
            execute_shift_generation(get_curr_business(request))

            create_manager_msg(
                recipients=get_curr_business(request).get_employees(),
                subject='New Shifts',
                text='Your manager has generated shifts for next week',
                wait_for_mail_results=False)
            messages.success(request,
                             'Shifts generation requested successfully')
            return HttpResponse('Request triggered')

    return wrong_method(request)
Пример #4
0
def delete_question(request, question_id):
    if request.method == 'DELETE':
        question_to_delete = Question.objects.get(id=question_id)
        question_to_delete.delete()
        return JsonResponse({})

    return wrong_method(request)
Пример #5
0
def try_again(request):
    if request.method == 'POST':
        curr_emp = get_curr_profile(request)
        remove_score_from_emp(curr_emp)
        remove_prev_emp_request(curr_emp)
        return JsonResponse({'can_do_again': 'ok'})

    return wrong_method(request)
Пример #6
0
def submit_question_details(request):
    if request.method == 'POST':
        try:
            deserialize_objects(request.body)
            return JsonResponse({})
        except AttributeError as e:
            return HttpResponseBadRequest('Illegal question data: ' + str(e))

    return wrong_method(request)
Пример #7
0
def submit_quiz_settings(request):
    if request.method == 'POST':
        settings_data = json.loads(request.body)
        quiz_id, quiz_name, new_min_score, new_max_time = settings_data.get('id'), settings_data.get('name'), \
                                                          settings_data.get('score'), settings_data.get('time')

        quiz = Quiz.objects.get(id=quiz_id)
        quiz.name, quiz.score_to_pass, quiz.time_to_pass = quiz_name, new_min_score, new_max_time
        quiz.save()
        return JsonResponse({})

    return wrong_method(request)
Пример #8
0
def submit_question_only(request):
    if request.method == 'POST':
        try:
            question_data = json.loads(request.body)
            ques = Question.objects.create(
                quiz=Quiz.objects.get(id=question_data['quiz']),
                content=question_data['name'])
            return JsonResponse({'question_id': ques.id})
        except AttributeError as e:
            return HttpResponseBadRequest('Illegal question data: ' + str(e))

    return wrong_method(request)
Пример #9
0
def quiz_submission(request):
    if request.method == 'POST':
        logger.info('quiz submission data is: ' + str(request.body))
        quiz_score = get_quiz_score(request.body)
        logger.info('QUIZ SCORE IS %d, going to save it in profile',
                    quiz_score)
        curr_profile = get_curr_profile(request)
        curr_profile.menu_score = quiz_score

        logger.info('updating %s rate...', curr_profile)
        curr_profile.rate += quiz_score - curr_profile.menu_score

        curr_profile.save()
        return JsonResponse(build_quiz_result(request.body, quiz_score))

    return wrong_method(request)
Пример #10
0
def get_specific_quiz(request, role):
    if request.method == 'GET':
        try:
            short_role = EmployeeProfile.get_roles_reversed()[role]
        except KeyError:
            return HttpResponseBadRequest('no such role: %s' % role)

        quiz = Quiz.objects.filter(business=get_curr_business(request),
                                   role=short_role).first()

        if not quiz:
            quiz = Quiz.objects.create(role=short_role,
                                       business=get_curr_business(request))

        response = quiz.serialize(True)
        logger.info('data is ' + str(response))
        return JsonResponse(response)
    return wrong_method(request)
Пример #11
0
def get_slot_employees(request, slot_id):
    if request.method == 'GET':
        curr_employee = get_curr_profile(request)
        requested_slot = ShiftSlot.objects.get(id=slot_id)
        if requested_slot.was_shift_generated():
            shift = requested_slot.shift
            curr_emp_future_slots = curr_employee.get_current_week_slots()
            offer_swap = (len(curr_emp_future_slots) > 0) and (
                not get_curr_profile(request) in shift.employees.all())
            return render(
                request, 'slot_request_emp_list.html', {
                    'emps': shift.employees.select_related('user').all(),
                    'empty_msg': 'No employees in this shift :(',
                    'curr_emp': get_curr_profile(request),
                    'future_slots': curr_emp_future_slots,
                    'offer_swap': offer_swap
                })
        else:
            logger.warning(
                'Can\'t get employees for slot id %s, shift was not generated',
                slot_id)
            return HttpResponse('Cant find shift for this slot')

    return wrong_method(request)