def get_feedback(staff,
                 week_of=datetime.date.today() -
                 datetime.timedelta(days=datetime.date.today().weekday())):
    end_week = week_of + datetime.timedelta(days=7)
    week_orders = Order.objects.filter(added_on__range=[week_of, end_week])

    avg = 0
    sum = 0
    rev_count = 0
    if is_in_group(staff, "Administrators"):
        return "N/A"
    elif is_in_group(staff, ['Technicians', 'Radiologists']):
        if is_in_group(staff, 'Radiologists'):
            team = Team.objects.filter(radiologists__in=User.objects.filter(
                id=staff.id))
        else:
            team = Team.objects.filter(technicians__in=User.objects.filter(
                id=staff.id))

        survey_list = Survey.objects.filter(order__in=week_orders.filter(
            team__in=team))
        rev_count = len(survey_list)
        for survey in survey_list:
            sum += survey.team_rating

    if rev_count > 0:
        avg = sum / rev_count

    return f'{avg:.1f} / 10 [{rev_count} Reviews]'
示例#2
0
def index(request):
    """ Displays dashboard tables, depending on group membership of logged in user. """

    # Determine if current user can see all sections
    see_all = is_in_group(request.user, "Administrators")

    # Set up empty context to pass to template
    context = {}

    # Check if administrator or physician
    if see_all or is_in_group(request.user, "Physicians"):
        # Grab active orders and completed orders from database
        active_orders = Order.objects.filter(level_id__lt=4)
        complete_orders = Order.objects.filter(level_id=4)

        # If we are not an administrator, limit active and complete orders to
        # the logged in users' patients.
        if not see_all:
            active_orders = active_orders.filter(patient__doctor=request.user)
            complete_orders = complete_orders.filter(
                patient__doctor=request.user)

        # Add the orders we grabbed to our template context
        context['active_orders'] = active_orders
        context['complete_orders'] = complete_orders

        # Add the patient lookup form to our context
        context['patient_lookup'] = PatientLookupForm()

    # Check if administrator or receptionist
    if see_all or is_in_group(request.user, "Receptionists"):
        # Find today's appts. To filter by today's appointments, we find the datetime for today at midnight,
        # and today at 11:59 PM. We then find all appts between those two ranges. Then we add it to the context.
        today_min = datetime.datetime.combine(datetime.date.today(),
                                              datetime.time.min)
        today_max = datetime.datetime.combine(datetime.date.today(),
                                              datetime.time.max)
        context['todays_orders'] = Order.objects.filter(
            level_id=1, appointment__range=(today_min, today_max))

        # Find unscheduled appointments
        context['unsched_orders'] = Order.objects.filter(
            level_id=1, appointment__isnull=True)

    # Check if administrator or technician
    if see_all or is_in_group(request.user, "Technicians"):
        # Pass into context all checked in orders for any team where the logged in user is a technician.
        context['checked_in_orders'] = Order.objects.filter(
            level_id=2, team__technicians=request.user)

    if see_all or is_in_group(request.user, "Radiologists"):
        # Pass into context all imaging complete orders for teams where logged in user is a radiologist.
        context['radiologist_orders'] = Order.objects.filter(
            level_id=3, team__radiologists=request.user)

    # Render the dashoboard with any context we've passed in.
    return render(request, 'index.html', context)
def patient_lookup(request):
    """ Handles patient lookup and order creation """

    # Prevent patients from viewing the staff version of the patient forms
    if is_in_group(request.user, ['Patient']):
        return redirect('index')

    # Grab a data object from our DateWidget
    dob = datetime.datetime.strptime(request.POST['birth_date'],
                                     '%m/%d/%Y').date()

    if dob > datetime.date.today():
        messages = {
            'headline1': 'Birth date must be in the past',
            'headline2': 'Please try again.',
            'headline3': f""
        }
        return show_message(request, messages)

    # Grab a list of patients with that DOB from DB
    patient_list = Patient.objects.filter(birth_date=dob)

    # Prepare empty lookup form
    new_form = PatientLookupForm(initial={'birth_date': dob})

    # prepare context for our page and then render it
    context = {
        'patient_list': patient_list,
        'date_selected': dob.strftime('%m/%d/%Y'),
        'new_patient_form': PatientInfoForm(),
        'patient_lookup': new_form,
    }
    return render(request, 'patient_lookup.html', context)
def new_order(request, pat_id):
    """ Handles creation of a new order """

    # Prevent patients from viewing the staff version of the patient form
    if is_in_group(request.user, ['Patient']):
        return redirect('index')

    # if not post request, redirect to 404
    if request.method == 'POST':
        # Copy form data and assign patient to order
        form_data = request.POST.copy()
        form_data['patient'] = pat_id

        # Set up form with our copied data
        new_form = NewOrderForm(data=form_data)

        # Check validity. If valid, save order and set workflow. Otherwise, reload page with errors.
        if new_form.is_valid():
            new_order = new_form.save()
            new_order.level_id = 1
            new_order.save()

            return redirect('order', order_id=new_order.pk)
    else:
        new_form = NewOrderForm()

    # Either we're reloading form with errors, or we didn't have a post request.
    # If it's not a post request, we'll load a blank form. Otherwise, load error form.
    context = {
        'new_order_form': new_form,
        'patient': Patient.objects.get(pk=pat_id),
    }
    return render(request, 'new_order.html', context)
def patient(request, pat_id=None):
    """ Displays the patient info and orders """

    # Grab patient from the database
    patient_rec = Patient.objects.get(pk=pat_id)

    # Prevent patients from viewing the staff version of the patient form
    if is_in_group(request.user, ['Patient']):
        return redirect('index')

    # Check if it is a post request. If so, build our form with the post data.
    if request.method == 'POST':
        form = PatientInfoForm(data=request.POST, instance=patient_rec)

        # Ensure form is valid. If so, save. If not, show error.
        if form.is_valid():
            form.save()
        else:
            messages = {
                'headline1': 'Invalid Form',
                'headline2': 'Please try again.',
                'headline3': f"{form.errors}"
            }
            return show_message(request, messages)

    # Set up variables for our template and render it
    context = {
        'patient_info': patient_rec,
        'form': PatientInfoForm(instance=patient_rec),
        'active_orders': patient_rec.orders.filter(level_id__lt=4),
        'complete_orders': patient_rec.orders.filter(level_id__gte=4),
    }
    return render(request, 'patient.html', context)
def download_excel(request, user_id, week_of):
    """ Download the generated Productivity Report """
    # Ensure user request comes from an admin
    if not is_in_group(request.user, "Administrators"):
        raise Http404

    return generate_excel(user_id, week_of)
def reports(request):
    """ Determines Context for the Employee Productivity Reports page """
    if not is_in_group(request.user, "Administrators"):
        return Http404

    date = datetime.date.today()
    cur_week = date - datetime.timedelta(date.weekday())

    group_list = list(Group.objects.all().values_list('name', flat=True))
    if 'Patient' in group_list:
        group_list.remove('Patient')

    prev_weeks = list(WeeklyHours.objects.all().values_list(
        'week_of', flat=True).distinct())
    if cur_week in prev_weeks:
        prev_weeks.remove(cur_week)
    prev_weeks = reversed(prev_weeks)

    start_week = date - datetime.timedelta(days=date.weekday(), weeks=0)
    end_week = start_week + datetime.timedelta(days=7)
    entries = Survey.objects.filter(order__in=Order.objects.filter(
        added_on__range=[start_week, end_week]))

    context = {
        'faculty': User.objects.filter(groups__name__in=group_list).distinct(),
        'weeks': WeeklyHours.objects.all(),
        'cur_week': cur_week,
        'prev_weeks': prev_weeks,
        'dates': [start_week, end_week],
        'entries': entries
    }
    # Render the timesheet with any context we've passed in.
    return render(request, 'reports.html', context)
示例#8
0
def get_patient_count(staff, week_of=datetime.date.today() - datetime.timedelta(days=datetime.date.today().weekday())):
    # try:
    end_week = week_of + datetime.timedelta(days=7)
    week_orders = Order.objects.filter(added_on__range=[week_of, end_week])

    if is_in_group(staff, "Administrators"):
        return "N/A"
    elif is_in_group(staff, "Receptionists"):
        return len(week_orders.filter(receptionist=staff))
    elif is_in_group(staff, ['Technicians', 'Radiologists']):
        if is_in_group(staff, 'Radiologists'):
            team = Team.objects.filter(radiologists__in=User.objects.filter(id=staff.id))
        else:
            team = Team.objects.filter(technicians__in=User.objects.filter(id=staff.id))
        return len(week_orders.filter(team__in=team))
    elif is_in_group(staff, "Physicians"):
        return len(week_orders.filter(patient__doctor=staff))

    return 'Error'
    # except:
    #     return 0
def render_to_pdf(request, order_id):
    """ Generate a PDF for the provided Order """
    # Attempt to grab order via order_id from url. 404 if not found.
    try:
        cur_order = Order.objects.get(pk=order_id)
    except Order.DoesNotExist:
        raise Http404

    # Ensure only patients can submit surveys, and that they can only submit surveys on their own orders
    if not is_in_group(
            request.user,
            "Patient") or cur_order.patient.pk != request.user.user_obj.pk:
        return Http404

    # Define which user groups can see medical info, add to context
    medical_groups = ['Technicians', 'Radiologists', 'Physicians']
    context = {
        'cur_order': cur_order,
        'user': request.user,
        'url': request.build_absolute_uri('/')[:-1],
        'show_medical': is_in_group(request.user, medical_groups)
    }

    # Send thumbnails into context and render HTML
    context['thumbnails'] = get_image_files(cur_order.images.all())

    template = get_template('pdf_temp.html')
    html = template.render(context)
    result = BytesIO()

    # Free PythonAnywhere accounts do not have access to this type of file download...
    try:
        pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
    except:
        return None

    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')
    return None
def submit_survey(request, order_id):
    """ Handles creation of a new Survey """

    # set up new patient request form with POST data
    new_form = SurveyForm(data=request.POST)

    # Attempt to grab order via order_id from url. 404 if not found.
    try:
        cur_order = Order.objects.get(pk=order_id)
    except Order.DoesNotExist:
        raise Http404

    # Ensure only patients can submit surveys, and that they can only submit surveys on their own orders
    if not is_in_group(
            request.user,
            "Patient") or cur_order.patient.pk != request.user.user_obj.pk:
        return Http404

    # Check if form is valid. If so, assign doctor and save, the redir to a new order. Otherwise, show error.
    if new_form.is_valid():
        new_survey = new_form.save()

        cur_order = Order.objects.get(pk=order_id)

        new_survey.order = cur_order
        new_survey.completed_time = timezone.now()
        new_survey.save()

        cur_order.survey = new_survey
        cur_order.save()

        request.session['just_submitted'] = 0
        return redirect('index')

    else:
        # Grab active orders and completed orders from database
        active_orders = Order.objects.filter(level_id__lt=4)
        complete_orders = Order.objects.filter(level_id=4)

        active_orders = active_orders.filter(patient=request.user.user_obj)
        complete_orders = complete_orders.filter(patient=request.user.user_obj)

        context = {
            'new_survey_form': new_form,
            'order_id': order_id,
            'show_modal': True,
            'active_orders': active_orders,
            'complete_orders': complete_orders,
        }
        return render(request, 'index.html', context)
def order_survey(request, order_id):
    """ Construct basis for Patient Survey """
    # if post request, redirect to 404
    if request.method == 'POST':
        raise Http404

    # Attempt to grab order via order_id from url. 404 if not found.
    try:
        cur_order = Order.objects.get(pk=order_id)
    except Order.DoesNotExist:
        raise Http404

    if not is_in_group(request.user, "Patient"):
        return Http404

    return redirect('submit_survey', order_id=order_id)
示例#12
0
def save_order(request, order_id):
    """ Saves radiology report but does not complete order """

    # Attempt to grab order via order_id from url. 404 if not found.
    try:
        cur_order = Order.objects.get(pk=order_id)
    except Order.DoesNotExist:
        raise Http404

    # Ensure request method is POST
    if request.method == 'POST':
        # Check if Order is at radiologist level and request user is a radiologist and is on the order's team.
        if cur_order.level_id == 3 and is_in_group(request.user, ['Radiologists']):
            if request.user in cur_order.team.radiologists.all():
                # Set up form with our data and save if valid
                form = AnalysisForm(data=request.POST, instance=cur_order)
                if form.is_valid():
                    form.save()

    # Always redirect to specified order
    return redirect('order', order_id=order_id)
示例#13
0
def order(request, order_id):

    # Attempt to grab order via order_id from url. 404 if not found.
    try:
        cur_order = Order.objects.get(pk=order_id)
        pat_id = cur_order.get_patient_id()
        print(cur_order.modality)
        pat = Patient.objects.get(pk=pat_id)
        print(pat.pat_email)
    except Order.DoesNotExist:
        raise Http404

    # Check if we have a POST request
    if request.method == 'POST':
        print('request pot')
        print(cur_order.level_id, request.user)
        # Check if level and permissions for the logged in user are both receptionists or admins
        if cur_order.level_id == 1 and is_in_group(
                request.user, ['Receptionists', 'Administrators']):
            print('permission tocheck')
            # Assign POST data to selection form, check if it's valid, and save if so
            form = TeamSelectionForm(data=request.POST, instance=cur_order)
            print(form)
            if form.is_valid():
                modal = cur_order.modality
                time = "30"
                calc = PriceCaculator(modal, time)
                price = calc.calcPrice()

                #send email that shows current account balance
                send_mail(
                    subject='Account Balance',
                    message=
                    'Thank you for your visit! Your account balance is currently:'
                    + ' ' + '$' + str(price),
                    from_email='*****@*****.**',
                    recipient_list=["*****@*****.**"],
                    fail_silently=False)
                form.save()
            else:
                # Show errors
                messages = {
                    'headline1': 'Invalid Form',
                    'headline2': 'Please try again.',
                    'headline3': f"{form.errors}"
                }
                return show_message(request, messages)

        # Check if level and permissions for the logged in user are both technicians
        elif cur_order.level_id == 2 and is_in_group(
                request.user, ['Technicians', 'Radiologists']):
            if request.user in cur_order.team.technicians.all(
            ) | cur_order.team.technicians.all():
                # Save image complete info
                cur_order.imaged = request.user.get_username()
                cur_order.imaged_time = timezone.now()
                cur_order.save()
            else:
                # Show auth error
                messages = {
                    'headline1': 'Not Authorized',
                    'headline2': '',
                    'headline3': '',
                }
                return show_message(request, messages)

        # Check if level and permissions for the logged in user are both radiology
        elif cur_order.level_id == 3 and is_in_group(request.user,
                                                     ['Radiologists']):
            if request.user in cur_order.team.radiologists.all():
                # Set up data in our form and check validity of data.
                form = AnalysisForm(data=request.POST, instance=cur_order)
                if form.is_valid():
                    print("completed")
                    # Save form, then grab saved item
                    form.save()
                    cur_order.refresh_from_db()

                    # Add completed user and completed time to record, then save
                    cur_order.completed = request.user.get_username()
                    cur_order.completed_time = timezone.now()
                    cur_order.save()

                else:
                    # Show form errors
                    messages = {
                        'headline1': 'Invalid Form',
                        'headline2': 'Please try again.',
                        'headline3': f"{form.errors}"
                    }
                    return show_message(request, messages)
            else:
                # Show auth error
                messages = {
                    'headline1': 'Not Authorized',
                    'headline2': '',
                    'headline3': '',
                }
                return show_message(request, messages)
        else:
            # Show invalid request error
            messages = {
                'headline1': 'Order already complete.',
                'headline2': '',
                'headline3': '',
            }
            return show_message(request, messages)

        # If we've made it to there, that means we've successfully submitted the order.
        # Therefore, we'll re-grab it from the DB and increment it's level by one.
        print(cur_order)
        cur_order.refresh_from_db()
        cur_order.level_id += 1
        cur_order.save()

        # Send an email notification to the correct user(s)
        send_notification.now(order_id)

    # Set up the variables for our template
    context = {
        "cur_order": cur_order,
    }

    # Check for user permission and level order. Add appropriate elements for template rendering.
    if cur_order.level_id == 1 and is_in_group(
            request.user, ['Receptionists', 'Administrators']):
        # Add scheduler form if not yet checked in
        context['schedule_form'] = ScheduleForm(instance=cur_order)
        context['checkin_form'] = TeamSelectionForm(instance=cur_order)
    elif cur_order.level_id == 2 and is_in_group(
            request.user, ['Technicians', 'Radiologists']):
        # Prepare context for template if at checked in step
        if request.user in cur_order.team.radiologists.all(
        ) | cur_order.team.technicians.all():
            context['image_form'] = ImageUploadForm(instance=cur_order)
    elif cur_order.level_id == 3 and is_in_group(request.user,
                                                 ['Radiologists']):
        # Prepare context for template if at imaging complete step
        if request.user in cur_order.team.radiologists.all():
            context['analysis_form'] = AnalysisForm(instance=cur_order)
    elif cur_order.level_id == 4:
        # Prepare context for template if at analysis complete step
        pass
    elif cur_order.level_id == 5:
        # Prepare context for template if archived
        pass

    # Define which user groups can see medical info, add to context
    medical_groups = ['Technicians', 'Radiologists', 'Physicians']
    context['show_medical'] = is_in_group(request.user, medical_groups)

    # Send thumbnails into context and render HTML
    context['thumbnails'] = get_image_files(cur_order.images.all())
    return render(request, 'order.html', context)
示例#14
0
def order(request, order_id):

    # Attempt to grab order via order_id from url. 404 if not found.
    try:
        cur_order = Order.objects.get(pk=order_id)
    except Order.DoesNotExist:
        raise Http404

    # Check if we have a POST request
    if request.method == 'POST':

        # Check if level and permissions for the logged in user are both receptionists or admins
        if cur_order.level_id == 1 and is_in_group(
                request.user, ['Receptionists', 'Administrators']):

            # Assign POST data to selection form, check if it's valid, and save if so
            form = TeamSelectionForm(data=request.POST, instance=cur_order)
            if form.is_valid():
                form.save()
            else:
                # Show errors
                messages = {
                    'headline1': 'Invalid Form',
                    'headline2': 'Please try again.',
                    'headline3': f"{form.errors}"
                }
                return show_message(request, messages)

        # Check if level and permissions for the logged in user are both technicians
        elif cur_order.level_id == 2 and is_in_group(
                request.user, ['Technicians', 'Radiologists']):
            if request.user in cur_order.team.technicians.all(
            ) | cur_order.team.technicians.all():
                # Save image complete info
                cur_order.imaged = request.user.get_username()
                cur_order.imaged_time = timezone.now()
                cur_order.save()
            else:
                # Show auth error
                messages = {
                    'headline1': 'Not Authorized',
                    'headline2': '',
                    'headline3': '',
                }
                return show_message(request, messages)

        # Check if level and permissions for the logged in user are both radiology
        elif cur_order.level_id == 3 and is_in_group(request.user,
                                                     ['Radiologists']):
            if request.user in cur_order.team.radiologists.all():
                # Set up data in our form and check validity of data.
                # send invoice

                form = AnalysisForm(data=request.POST, instance=cur_order)

                if form.is_valid():

                    # Save form, then grab saved item
                    form.save()
                    cur_order.refresh_from_db()

                    # Add completed user and completed time to record, then save
                    cur_order.completed = request.user.get_username()
                    cur_order.completed_time = timezone.now()
                    cur_order.save()

                    if cur_order.modality == ModalityOption.objects.get(
                            name='MRI'):
                        price = 2611

                    if cur_order.modality == ModalityOption.objects.get(
                            name='CAT Scan'):
                        price = 1200

                    if cur_order.modality == ModalityOption.objects.get(
                            name='X-Ray'):
                        price = 460

                    a = Invoice(order=cur_order,
                                patient=cur_order.patient,
                                total=price)
                    a.save()

                else:
                    # Show form errors
                    messages = {
                        'headline1': 'Invalid Form',
                        'headline2': 'Please try again.',
                        'headline3': f"{form.errors}"
                    }
                    return show_message(request, messages)
            else:
                # Show auth error
                messages = {
                    'headline1': 'Not Authorized',
                    'headline2': '',
                    'headline3': '',
                }
                return show_message(request, messages)
        else:
            # Show invalid request error
            messages = {
                'headline1': 'Order already complete.',
                'headline2': '',
                'headline3': '',
            }
            return show_message(request, messages)

        # If we've made it to there, that means we've successfully submitted the order.
        # Therefore, we'll re-grab it from the DB and increment it's level by one.
        cur_order.refresh_from_db()
        cur_order.level_id += 1
        cur_order.save()

        # Send an email notification to the correct user(s)
        send_notification.now(order_id)

    # Set up the variables for our template
    context = {
        "cur_order": cur_order,
    }

    # Check for user permission and level order. Add appropriate elements for template rendering.
    if cur_order.level_id == 1 and is_in_group(
            request.user, ['Receptionists', 'Administrators']):
        # Add scheduler form if not yet checked in
        context['schedule_form'] = ScheduleForm(instance=cur_order)
        context['checkin_form'] = TeamSelectionForm(instance=cur_order)
    elif cur_order.level_id == 2 and is_in_group(
            request.user, ['Technicians', 'Radiologists']):
        # Prepare context for template if at checked in step
        if request.user in cur_order.team.radiologists.all(
        ) | cur_order.team.technicians.all():
            context['image_form'] = ImageUploadForm(instance=cur_order)
    elif cur_order.level_id == 3 and is_in_group(request.user,
                                                 ['Radiologists']):
        # Prepare context for template if at imaging complete step
        if request.user in cur_order.team.radiologists.all():
            context['analysis_form'] = AnalysisForm(instance=cur_order)
    elif cur_order.level_id == 4:
        # Prepare context for template if at analysis complete step
        pass
    elif cur_order.level_id == 5:
        # Prepare context for template if archived
        pass

    # Define which user groups can see medical info, add to context
    medical_groups = ['Technicians', 'Radiologists', 'Physicians']
    context['show_medical'] = is_in_group(request.user, medical_groups)

    # Send thumbnails into context and render HTML
    context['thumbnails'] = get_image_files(cur_order.images.all())
    return render(request, 'order.html', context)
def index(request):
    """ Displays dashboard tables, depending on group membership of logged in user. """

    # Determine if current user can see all sections
    see_all = is_in_group(request.user, "Administrators")

    # Set up empty context to pass to template
    context = {}

    # Check if administrator or physician
    if see_all or is_in_group(request.user, "Physicians"):
        # Grab active orders and completed orders from database
        active_orders = Order.objects.filter(level_id__lt=4)
        complete_orders = Order.objects.filter(level_id=4)

        # If we are not an administrator, limit active and complete orders to
        # the logged in users' patients.
        if not see_all:
            active_orders = active_orders.filter(patient__doctor=request.user)
            complete_orders = complete_orders.filter(
                patient__doctor=request.user)

        # Add the orders we grabbed to our template context
        context['active_orders'] = active_orders
        context['complete_orders'] = complete_orders

        # Add the patient lookup form to our context
        context['patient_lookup'] = PatientLookupForm()

    # Check if administrator or receptionist
    if see_all or is_in_group(request.user, "Receptionists"):
        # Find today's appts. To filter by today's appointments, we find the datetime for today at midnight,
        # and today at 11:59 PM. We then find all appts between those two ranges. Then we add it to the context.
        today_min = datetime.datetime.combine(datetime.date.today(),
                                              datetime.time.min)
        today_max = datetime.datetime.combine(datetime.date.today(),
                                              datetime.time.max)
        context['todays_orders'] = Order.objects.filter(
            level_id=1, appointment__range=(today_min, today_max))

        # Find unscheduled appointments
        context['unsched_orders'] = Order.objects.filter(
            level_id=1, appointment__isnull=True)

    # Check if administrator or technician
    if see_all or is_in_group(request.user, "Technicians"):
        # Pass into context all checked in orders for any team where the logged in user is a technician.
        context['checked_in_orders'] = Order.objects.filter(
            level_id=2, team__technicians=request.user)

    if see_all or is_in_group(request.user, "Radiologists"):
        # Pass into context all imaging complete orders for teams where logged in user is a radiologist.
        context['radiologist_orders'] = Order.objects.filter(
            level_id=3, team__radiologists=request.user)

    if is_in_group(request.user, "Patient"):
        # Grab patient from the database
        patient_rec = Patient.objects.get(pk=request.user.user_obj.pk)
        # Set up variables for our template and render it
        context['user'] = request.user
        context['patient_info'] = patient_rec
        context['active_orders'] = patient_rec.orders.filter(level_id__lt=4)
        context['complete_orders'] = patient_rec.orders.filter(
            level_id__gte=4).order_by('-added_on')
        context['order_id'] = list(context['complete_orders'])[0].pk

        new_form = SurveyForm(data=request.POST)
        context['new_survey_form'] = new_form
        # Determine if a survey was just submitted, and only allow the notification to show once
        if 'just_submitted' in request.session:
            if request.session['just_submitted'] >= 1:
                del request.session['just_submitted']
            elif request.session['just_submitted'] < 0:
                request.session['just_submitted'] = 0
            else:
                request.session['just_submitted'] = request.session.get(
                    'just_submitted') + 1

    # Render the dashoboard with any context we've passed in.
    return render(request, 'index.html', context)
def timesheet(request):
    """ Backend control of the Employee Timesheet """

    # Prevent patients from accessing timesheet application
    if is_in_group(request.user, "Patient"):
        return Http404

    if request.is_ajax():
        values = dict(request.POST)
        returnJson = {}
        if 'time' in values:
            values = request.POST.get('time')
            if values:
                values = json.loads(values)

            if values == {}:
                return JsonResponse({
                    'msg':
                    'Error',
                    'error_msg':
                    'You must specify your hours before submitting.'
                })

            # Most recent monday value
            monday = datetime.date.today() + datetime.timedelta(
                days=-datetime.date.today().weekday())
            cur_week, created = WeeklyHours.objects.get_or_create(
                employee=request.user, week_of=monday)

            for key, value in values.items():
                TimeCategory.objects.update_or_create(defaults={
                    'mon_hours': value[0],
                    'tues_hours': value[1],
                    'wed_hours': value[2],
                    'thur_hours': value[3],
                    'fri_hours': value[4],
                    'sat_hours': value[5],
                    'sun_hours': value[6]
                },
                                                      week=cur_week,
                                                      name=key)

        elif 'copy' in values:
            if values['copy'][0] == '1':
                # Most recent monday value
                monday = datetime.date.today() + datetime.timedelta(
                    days=-datetime.date.today().weekday(), weeks=-1)
                try:
                    returnJson = list(
                        WeeklyHours.objects.get(
                            employee=request.user,
                            week_of=monday).week.all().values(
                                'name', 'mon_hours', 'tues_hours', 'wed_hours',
                                'thur_hours', 'fri_hours', 'sat_hours',
                                'sun_hours'))
                except WeeklyHours.DoesNotExist:
                    return JsonResponse({
                        'msg':
                        'Error',
                        'error_msg':
                        'No timesheet records were found for last week.'
                    })

        elif 'offset' in values:
            offset = int(values['offset'][0])
            monday = datetime.date.today() + datetime.timedelta(
                days=-datetime.date.today().weekday(), weeks=offset)
            try:
                returnJson = list(
                    WeeklyHours.objects.get(employee=request.user,
                                            week_of=monday).week.all().values(
                                                'name', 'mon_hours',
                                                'tues_hours', 'wed_hours',
                                                'thur_hours', 'fri_hours',
                                                'sat_hours', 'sun_hours'))
            except WeeklyHours.DoesNotExist:
                returnJson = ['DNE']
                values['return'] = 'DNE'

        dates = []
        for i in range(0, 7):
            day = monday + datetime.timedelta(days=i)
            dates.append(day.strftime('%b %d'))
        values['week_dates'] = dates

        return JsonResponse({
            'msg': 'Success',
            'json_data': str(values),
            'returnJson': returnJson
        })

    date_tuple = dt.today().isocalendar()
    WEEK = date_tuple[1] - 2
    startdate = time.asctime(
        time.strptime(f'{date_tuple[0]} {WEEK} 0', '%Y %W %w'))
    startdate = dt.strptime(startdate, '%a %b %d %H:%M:%S %Y')
    dates = []
    for i in range(1, 8):
        day = startdate + datetime.timedelta(days=i)
        dates.append(day.strftime('%b %d'))

    context = {
        'init_cats': ['Vacation', 'Project X', 'Lead App', 'Office', 'Break'],
        'week': dates,
    }

    try:
        context['cur_hours'] = list(
            WeeklyHours.objects.get(
                employee=request.user,
                week_of=startdate +
                datetime.timedelta(days=1)).week.all().values(
                    'name', 'mon_hours', 'tues_hours', 'wed_hours',
                    'thur_hours', 'fri_hours', 'sat_hours', 'sun_hours'))
    except WeeklyHours.DoesNotExist:
        context['cur_hours'] = [{
            'name': 'Vacation'
        }, {
            'name': 'Meetings'
        }, {
            'name': 'Training'
        }, {
            'name': 'Break'
        }]

    # Render the timesheet with any context we've passed in.
    return render(request, 'timesheet.html', context)