def report_incorrect_detail(request): if request.method == 'POST': reporting_profile = get_curr_profile(request) incorrect_field = request.POST.get('incorrect_field') fix_suggestion = request.POST.get('fix_suggestion') curr_val = request.POST.get('curr_val') language_validator = LanguageValidator(settings.PROFANITY_SERVICE_URL) logger.info('checking language') if not language_validator.validate(fix_suggestion): logger.info('bad language detected') return HttpResponseBadRequest('NOT SENT - BAD LANGUAGE') logger.info('msg OK, creating new employee request') new_request = EmployeeRequest( sent_time=timezone.now(), type='P', subject='Employee Change Request', text='Field claimed to be incorrect: %s.' ' Current field value is %s; Suggestion is %s' % (incorrect_field, curr_val, fix_suggestion)) new_request.save() # add the employee's manager to the recipients list new_request.issuers.add(reporting_profile) try: get_curr_profile(request).send_mail_to_manager() except EmailWaitError as e: return HttpResponseServerError(e.message) return HttpResponse('Report was sent successfully')
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)
def emp_home(request): profile, business = get_profile_and_business(request) start_week, end_week = get_current_week_range() existing_request = ShiftRequest.objects.filter( employee=get_curr_profile(request), submission_time__range=[start_week, end_week]).first() deadline_date_str = get_current_deadline_date_string( get_curr_profile(request).business.deadline_day) curr_week_string = get_current_week_string() curr_week_sunday = get_curr_week_sunday() generation_status = business.shifts_generated request_enabled = business.slot_request_enabled and deadline_date_str is_first_login = False if not profile.ever_logged_in: logger.info('first login') is_first_login = True profile.ever_logged_in = True profile.save() new_messages = get_curr_profile(request).new_messages open_swap_requests = ShiftSwap.objects.filter( Q(requester=get_curr_profile(request)) | Q(responder=get_curr_profile(request)), accept_step__in=ShiftSwap.open_accept_steps()).count() return render( request, "employee/home.html", { 'got_request_slots': existing_request.requested_slots.all() if existing_request else None, 'request_enabled': request_enabled, 'curr_week_str': curr_week_string, 'deadline_date': deadline_date_str, 'start_date': curr_week_sunday, 'first_login': is_first_login, 'generation': generation_status, 'logo_conf': get_logo_conf(), 'new_messages': new_messages, 'swap_cnt': open_swap_requests })
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)
def get_swap_requests(request): curr_employee = get_curr_profile(request) is_open = True if request.GET.get('state') == 'open' else False if is_open: swap_requests = ShiftSwap.objects.filter( Q(requester=curr_employee) | Q(responder=curr_employee), accept_step__in=ShiftSwap.open_accept_steps()).order_by( '-updated_at') else: key = curr_employee.get_old_swap_requests_cache_key() if key not in cache: swap_requests = ShiftSwap.objects.filter( Q(requester=curr_employee) | Q(responder=curr_employee), accept_step__in=ShiftSwap.closed_accept_steps()).order_by('-updated_at') \ .select_related('requester__user', 'responder__user', 'requested_shift', 'requester_shift', 'requested_shift__slot', 'requester_shift__slot') cache.set(key, list(swap_requests), settings.DURATION_CACHE_TTL) logger.debug('Taking old swap requests from DB: %s', swap_requests) logger.debug('Taking old swap requests from cache') swap_requests = cache.get(key) return render(request, 'employee/swap_requests.html', { 'swap_requests': swap_requests, 'is_open': is_open })
def ask_another_test_try(request): curr_emp = get_curr_profile(request) if request.method == 'GET': response = {} try: existing_request = EmployeeRequest.objects.filter( issuers__in=[curr_emp.pk], type='M').last() response['retry_status'] = existing_request.get_status_display() except AttributeError: response['retry_status'] = 'non-exist' return JsonResponse(response) elif request.method == 'POST': logger.info('got retry employee request from: ' + str(curr_emp)) try: existing_request = EmployeeRequest.objects.filter( issuers__in=[curr_emp.pk], type='M').last() return HttpResponseBadRequest( 'menu test retry request (with status %s) already exist for this employee' % existing_request.get_status_display()) except AttributeError: new_emp_req = EmployeeRequest(type='M') new_emp_req.save() new_emp_req.issuers.add(curr_emp) return JsonResponse({'created': 'ok'})
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)
def get_time_to_next_shift(request): curr_emp = get_curr_profile(request) next_shift = curr_emp.get_next_shift() if not next_shift: logger.warning('no upcoming shift for emp %s', request.user.username) return HttpResponseBadRequest('No upcoming shift was found...') logger.info('next shift for user %s is %s', request.user.username, str(next_shift)) days, hours = get_days_hours_from_delta(next_shift.slot.get_datetime() - datetime.now()) return HttpResponse('%s days, %s hours' % (days, hours))
def get_manager_messages(request): curr_emp_profile = get_curr_profile(request) is_new_str = request.GET.get('new') is_new = True if is_new_str == 'true' else False manager_messages = curr_emp_profile.get_manger_msgs_of_employee(is_new) logger.debug('flushing new messages cache for emp %s', str(curr_emp_profile)) curr_emp_profile.flush_new_messages() return render(request, "employee/manager_messages.html", { 'manager_msgs': manager_messages, 'is_new': is_new })
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)
def export_shifts_csv(request): curr_emp = get_curr_profile(request) prev_shifts = curr_emp.get_previous_shifts() response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="prev_shifts.csv"' writer = csv.writer(response) writer.writerow(['shift date', 'tip']) for shift in prev_shifts: writer.writerow( [shift.slot.get_datetime_str(), shift.calculate_employee_tip()]) return response
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)
def get_prev_shifts(request): curr_emp = get_curr_profile(request) curr_business = get_curr_business(request) is_manager = request.user.groups.filter(name='Managers').exists() if is_manager: prev_shifts = curr_business.get_previous_shifts() else: prev_shifts = curr_emp.get_previous_shifts() if len(prev_shifts) == 0: logger.warning('no previous shifts for emp %s', request.user.username) return HttpResponseBadRequest('No previous shifts was found...') logger.info('Found %d previous shifts', len(prev_shifts)) return render(request, 'previous_shifts.html', { 'prev_shifts': prev_shifts, 'manager': is_manager })
def ask_shift_swap(request): try: responder = EmployeeProfile.objects.get( user__username=request.POST.get('requested_employee')) requested_shift = ShiftSlot.objects.get( id=int(request.POST.get('requested_shift'))).shift requester_shift = ShiftSlot.objects.get( id=int(request.POST.get('requester_shift'))).shift logger.info('new swap request for shift %d to shift %d', requester_shift.id, requested_shift.id) ShiftSwap.objects.create(requester=get_curr_profile(request), responder=responder, requested_shift=requested_shift, requester_shift=requester_shift) return HttpResponse('ok') except (ValueError, IntegrityError) as e: return HttpResponseBadRequest('bad request: %s' % e.message)
def edit_profile_form(request): if request.method == 'GET': employee_username = request.GET.get('username', None) is_manager = request.user.groups.filter(name='Managers').exists() if not is_manager: logger.info( 'employee profile - creating employee edit profile form') initial_form = EditProfileForm(instance=request.user.profile, is_manager=False) else: logger.info('manager profile - creating manager edit profile form') initial_form = EditProfileForm( instance=EmployeeProfile.objects.get( user__username=employee_username if employee_username else request.user.username), is_manager=True) return render(request, 'ajax_form.html', {'form': initial_form}) else: profile = EmployeeProfile.objects.get(user=request.POST.get('user')) is_manager = request.user.groups.filter(name='Managers').exists() form = EditProfileForm(request.POST, instance=profile, is_manager=is_manager) if form.is_valid(): edited_profile = form.save() is_edited_other = is_manager and request.user.profile != edited_profile messages.success(request, message='successfully edited %s' % (edited_profile.user.username if is_edited_other else 'yourself')) logger.info('going to delete cached ETA duration...') try: cache.delete(get_curr_profile(request).get_eta_cache_key()) except ConnectionError as e: logger.warn('NO REDIS CONNECTION: ', e.message) return redirect( 'manage_employees' if is_edited_other else 'edit_profile') else: logger.error(str(form.errors)) messages.error(request, message='couldn\'t edit profile: %s' % str(form.errors.as_text())) return redirect('/')
def get_employee_requests(request): curr_manager = get_curr_profile(request) is_pending_str = request.GET.get('pending') is_pending = True if is_pending_str == 'true' else False if is_pending: emp_requests = get_employee_requests_with_status(curr_manager, 'P') else: key = curr_manager.get_old_employee_requests_cache_key() if key not in cache: logger.debug('Taking old employee requests from DB') emp_requests = get_employee_requests_with_status( curr_manager, 'A', 'R') cache.set(key, list(emp_requests), settings.DURATION_CACHE_TTL) else: logger.debug('Taking old employee requests from cache') emp_requests = cache.get(key) return render(request, 'manager/emp_requests.html', { 'requests': emp_requests, 'is_pending': is_pending })
def submit_slots_request(request): next_week_no = get_next_week_num() curr_business = get_curr_business(request) curr_profile = get_curr_profile(request) start_week, end_week = get_current_week_range() existing_request = ShiftRequest.objects.filter( employee=curr_profile, submission_time__range=[start_week, end_week]).last() if request.method == 'GET': form = SelectSlotsForm(instance=existing_request, business=curr_business, week=next_week_no) existing_slots = existing_request.requested_slots.all( ) if existing_request else [] request_enabled = curr_business.slot_request_enabled return render( request, 'employee/slot_list.html', { 'form': form, 'existing_slots': existing_slots, 'request_enabled': request_enabled }) else: form = SelectSlotsForm(request.POST, business=curr_business, week=next_week_no, instance=existing_request) if form.is_valid(): shifts_request = save_shifts_request(form, curr_profile) logger.info('slots chosen are: %s', str(shifts_request.requested_slots.all())) messages.success(request, 'request saved') else: logger.error('couldn\'t save slots request: %s', str(form.errors.as_text())) messages.error(request, message='couldn\'t save slots request: %s' % str(form.errors.as_text())) return HttpResponseRedirect('/')
def get_work_duration_data(request): curr_profile = get_curr_profile(request) curr_business = get_curr_business(request) key = curr_profile.get_eta_cache_key() if key not in cache: home_address = curr_profile.home_address work_address = curr_business.address arrival_method = curr_profile.arriving_method duration_client = DurationApiClient(home_address, work_address) if not home_address or not work_address: return HttpResponseBadRequest( 'can\'t get distance data - work address or home address are not set' ) duration_data = duration_client.get_dist_data(arrival_method) driving_duration = duration_data['driving'] walking_duration = duration_data['walking'] if not walking_duration and not driving_duration: return HttpResponseBadRequest( 'cant find home to work durations - make sure both business' 'and home addresses are available') logger.debug('getting duration from API service') cache.set(key, duration_data, settings.DURATION_CACHE_TTL) else: logger.debug('getting duration from cache') duration_data = cache.get(key) driving_duration = duration_data['driving'] walking_duration = duration_data['walking'] logger.info( 'found distance data: driving duration is %s and walking duration is %s', driving_duration, walking_duration) return JsonResponse(duration_data)