def area_access(request): """ Presents a page that displays audit records for all areas. """ now = timezone.now().astimezone() today = now.strftime('%m/%d/%Y') yesterday = (now - timedelta(days=1)).strftime('%m/%d/%Y') area_id = '' dictionary = { 'today': reverse('area_access') + '?' + urlencode({'start': today, 'end': today}), 'yesterday': reverse('area_access') + '?' + urlencode({'start': yesterday, 'end': yesterday}), } try: start, end = parse_start_and_end_date(request.GET['start'], request.GET['end']) area_id = request.GET.get('area') dictionary['start'] = start dictionary['end'] = end area_access_records = AreaAccessRecord.objects.filter(start__gte=start, start__lt=end, staff_charge=None) if area_id: area_id = int(area_id) filter_areas = Area.objects.get(pk=area_id).get_descendants(include_self=True) area_access_records = area_access_records.filter(area__in=filter_areas) dictionary['area_id'] = area_id area_access_records = area_access_records.order_by('area__name') area_access_records.query.add_ordering(F('end').desc(nulls_first=True)) area_access_records.query.add_ordering(F('start').desc()) dictionary['access_records'] = area_access_records except: pass dictionary['area_select_field'] = TreeNodeChoiceField(Area.objects.filter(area_children_set__isnull=True).only('name'), empty_label="All").widget.render('area', area_id) return render(request, 'area_access/area_access.html', dictionary)
def area_access(request): """ Presents a page that displays audit records for all NanoFab areas. """ now = timezone.now().astimezone() today = now.strftime('%m/%d/%Y') yesterday = (now - timedelta(days=1)).strftime('%m/%d/%Y') dictionary = { 'today': reverse('area_access') + '?' + urlencode({'start': today, 'end': today}), 'yesterday': reverse('area_access') + '?' + urlencode({'start': yesterday, 'end': yesterday}), 'areas': Area.objects.all(), } try: start, end = parse_start_and_end_date(request.GET['start'], request.GET['end']) area = request.GET.get('area') dictionary['start'] = start dictionary['end'] = end area_access_records = AreaAccessRecord.objects.filter(start__gte=start, start__lt=end, staff_charge=None) if area: area_access_records = area_access_records.filter(area__name=area) dictionary['area_name'] = area area_access_records = area_access_records.order_by('area__name') area_access_records.query.add_ordering(F('end').desc(nulls_first=True)) area_access_records.query.add_ordering(F('start').desc()) dictionary['access_records'] = area_access_records except: pass return render(request, 'area_access/area_access.html', dictionary)
def area_access(request): """ Presents a page that displays audit records for all access-controlled areas. """ today = timezone.now().strftime('%m/%d/%Y') yesterday = (timezone.now() - timedelta(days=1)).strftime('%m/%d/%Y') dictionary = { 'today': reverse('area_access') + '?' + urlencode({ 'start': today, 'end': today }), 'yesterday': reverse('area_access') + '?' + urlencode({ 'start': yesterday, 'end': yesterday }), } try: start, end = parse_start_and_end_date(request.GET['start'], request.GET['end']) dictionary['start'] = start dictionary['end'] = end dictionary['access_records'] = AreaAccessRecord.objects.filter( start__gte=start, start__lt=end, staff_charge=None) except: pass return render(request, 'area_access/area_access.html', dictionary)
def date_parameters_dictionary(request): if request.GET.get('start_date') and request.GET.get('end_date'): start_date, end_date = parse_start_and_end_date(request.GET.get('start_date'), request.GET.get('end_date')) else: start_date, end_date = get_month_timeframe() kind = request.GET.get("type") identifier = request.GET.get("id") customer = request.GET.get("customer") tool = request.GET.get("tool") dictionary = { 'month_list': month_list(), 'start_date': start_date, 'end_date': end_date, 'kind': kind, 'identifier': identifier, 'tab_url': get_url_for_other_tab(request), 'customer': None, 'tool': None, 'billing_service': get_billing_service().get('available', False), } if request.user.is_staff: dictionary['users'] = User.objects.all() dictionary['tools'] = Tool.objects.all() try: if customer: dictionary['customer'] = User.objects.get(id=customer) if tool: dictionary['tool'] = Tool.objects.get(id=tool) except: pass return dictionary, start_date, end_date, kind, identifier
def date_parameters_dictionary(request): if request.GET.get('start_date') and request.GET.get('end_date'): start_date, end_date = parse_start_and_end_date( request.GET.get('start_date'), request.GET.get('end_date')) else: start_date, end_date = get_month_timeframe() kind = request.GET.get("type") identifier = request.GET.get("id") dictionary = { 'month_list': month_list(), 'start_date': start_date, 'end_date': end_date, 'kind': kind, 'identifier': identifier, 'tab_url': get_url_for_other_tab(request), 'billing_service': False if not hasattr(settings, 'BILLING_SERVICE') or not settings.BILLING_SERVICE['available'] else True, } return dictionary, start_date, end_date, kind, identifier
def billing(request): """ Presents a page that displays billing. """ dictionary = {} try: start, end = parse_start_and_end_date(request.GET['start'], request.GET['end']) dictionary['start'] = start dictionary['end'] = end dictionary['billing_result'] = get_billing_data(start, end) except: pass return render(request, 'billing.html', dictionary)
def date_parameters_dictionary(request): if request.GET.get('start_date') and request.GET.get('end_date'): start_date, end_date = parse_start_and_end_date(request.GET.get('start_date'), request.GET.get('end_date')) else: start_date, end_date = get_month_timeframe() kind = request.GET.get("type") identifier = request.GET.get("id") dictionary = { 'month_list': month_list(), 'start_date': start_date, 'end_date': end_date, 'kind': kind, 'identifier': identifier, 'tab_url': get_url_for_other_tab(request), 'billing_service': get_billing_service().get('available', False), } return dictionary, start_date, end_date, kind, identifier
def date_parameters_dictionary(request): dates = False if request.GET.get('start_date') and request.GET.get('end_date'): start_date, end_date = parse_start_and_end_date( request.GET.get('start_date'), request.GET.get('end_date')) dates = True else: start_date, end_date = get_month_timeframe( request.GET.get('timeframe')) dictionary = { 'month_list': month_list(), 'timeframe': request.GET.get('timeframe') or start_date.strftime('%B, %Y'), 'start_date': start_date, 'end_date': end_date, 'dates': dates, 'tab_url': get_url_for_other_tab(request) } return dictionary, start_date, end_date
def billingxls(request): dictionary = {} try: start, end = parse_start_and_end_date(request.GET['start'], request.GET['end']) fn = "billing_" + start.strftime("%Y%m%d") + "_" + end.strftime("%Y%m%d") + ".xlsx" billing_result = get_billing_data(start, end) response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response['Content-Disposition'] = 'attachment; filename = "%s"' % fn book = Workbook(response, {'in_memory': True}) sheet = book.add_worksheet('billing') bold = book.add_format() bold.set_bold() money=book.add_format() money.set_num_format('$0.00') redgreen = book.add_format() redgreen.set_num_format('[Green]General;[Red]-General;General') rgmoney = book.add_format() rgmoney.set_num_format('[Green]$0.00;[Red]-$0.00;$0.00') fields = ['Username', 'Name', 'Email', 'PI', 'Type', 'Billable Days', 'Adjustments', 'Rate', 'Usage Bill', 'Stockroom Bill', 'Staff Charge Bill', 'Final Adjustments', 'Total Bill'] sheet.write_row('A1', fields, bold) iter = 1 for r in billing_result: days_cell = xl_rowcol_to_cell(iter,5) adj_cell = xl_rowcol_to_cell(iter,7) if r['user_type'] == 'Internal - Unlimited': usage_eq = 1125 elif r['user_type'] == 'Internal - Full'or r['user_type'] == 'Internal - Packaging' or r['user_type'] == 'Internal - SMP': usage_eq = f'=min({xl_rowcol_to_cell(iter,5)}+{xl_rowcol_to_cell(iter,6)},10)*{xl_rowcol_to_cell(iter,7)}+max(-10+{xl_rowcol_to_cell(iter,5)}+{xl_rowcol_to_cell(iter,6)},0)*45' else: usage_eq = f'=({xl_rowcol_to_cell(iter,5)}+{xl_rowcol_to_cell(iter,6)})*{xl_rowcol_to_cell(iter,7)}' total_eq = f'=sum({xl_range(iter, 8, iter, 11)})' row = [r['username'], r['name'], r['email'], r['PI'], r['user_type'], r['billable_days'], "", r['rate'], usage_eq, r['stockroom_bill'], r['staff_charge_bill'], "", total_eq] sheet.write_row(iter,0,row) iter +=1 sheet.set_column('H:M', None, money) sheet.set_column('G:G', None, redgreen) sheet.set_column('L:L', None, rgmoney) book.close() return response except: return render(request, 'billing.html', dictionary)
def remote_work(request): if request.GET.get("start_date") and request.GET.get("end_date"): start_date, end_date = parse_start_and_end_date( request.GET.get("start_date"), request.GET.get("end_date")) else: start_date, end_date = get_month_timeframe() operator = request.GET.get("operator") if operator: if operator == "all staff": operator = None else: operator = get_object_or_404(User, id=operator) else: operator = request.user project = request.GET.get("project") if project and project != "all projects": project = get_object_or_404(Project, id=project) else: project = None usage_events = UsageEvent.objects.filter( operator__is_staff=True, start__gte=start_date, start__lte=end_date).exclude(operator=F("user")) staff_charges = StaffCharge.objects.filter(start__gte=start_date, start__lte=end_date) if operator: usage_events = usage_events.exclude(~Q(operator_id=operator.id)) staff_charges = staff_charges.exclude(~Q(staff_member_id=operator.id)) if project: usage_events = usage_events.filter(project=project) staff_charges = staff_charges.filter(project=project) csv_export = bool(request.GET.get("csv", False)) if csv_export: table_result = BasicDisplayTable() TYPE, ID, ITEM, STAFF, CUSTOMER, PROJECT, START, END = ( "item_type", "item_id", "item", "staff_member", "customer", "project", "start_date", "end_date", ) table_result.headers = [ (TYPE, "Item Type"), (ID, "Item Id"), (ITEM, "Item"), (STAFF, "Staff"), (CUSTOMER, "Customer"), (PROJECT, "Project"), (START, "Start"), (END, "End"), ] for usage in usage_events: table_result.add_row({ ID: usage.tool.id, TYPE: "Tool Usage", ITEM: usage.tool, STAFF: usage.operator, CUSTOMER: usage.user, START: usage.start.astimezone( timezone.get_current_timezone()).strftime( "%m/%d/%Y @ %I:%M %p"), END: usage.end.astimezone(timezone.get_current_timezone()).strftime( "%m/%d/%Y @ %I:%M %p") if usage.end else "", PROJECT: usage.project, }) for staff_charge in staff_charges: for access in staff_charge.areaaccessrecord_set.all(): table_result.add_row({ ID: access.area.id, TYPE: "Area Access", ITEM: access.area, STAFF: staff_charge.staff_member, CUSTOMER: access.customer, START: access.start.astimezone( timezone.get_current_timezone()).strftime( "%m/%d/%Y @ %I:%M %p"), END: access.end.astimezone(timezone.get_current_timezone()). strftime("%m/%d/%Y @ %I:%M %p") if access.end else "", PROJECT: access.project, }) table_result.add_row({ ID: staff_charge.id, TYPE: "Staff Charge", ITEM: "Staff Charge", STAFF: staff_charge.staff_member, CUSTOMER: staff_charge.customer, START: staff_charge.start.astimezone( timezone.get_current_timezone()).strftime( "%m/%d/%Y @ %I:%M %p"), END: staff_charge.end.astimezone(timezone.get_current_timezone()). strftime("%m/%d/%Y @ %I:%M %p") if staff_charge.end else "", PROJECT: staff_charge.project, }) response = table_result.to_csv() filename = f"remote_work_{start_date.strftime('%m_%d_%Y')}_to_{end_date.strftime('%m_%d_%Y')}.csv" response["Content-Disposition"] = f'attachment; filename="{filename}"' return response dictionary = { "usage": usage_events, "staff_charges": staff_charges, "staff_list": User.objects.filter(is_staff=True), "project_list": Project.objects.filter(active=True), "start_date": start_date, "end_date": end_date, "month_list": month_list(), "selected_staff": operator.id if operator else "all staff", "selected_project": project.id if project else "all projects", } return render(request, "remote_work.html", dictionary)