def test_progresscolor(self): self.assertEqual(get_progresscolor(91), "danger") self.assertEqual(get_progresscolor(90), "warning") self.assertEqual(get_progresscolor(59), "success")
def get_widget_data(user, widgetlist=[], departments=None): context = {} context["today"] = datetime.date.today() if departments is None: # so we can run Queryset.difference() later departments = Department.objects.none() if "statistics" in widgetlist: if departments: devices = Device.active().filter(department__in=departments) else: devices = Device.active() context['device_all'] = devices.count() if context['device_all'] != 0: context['device_available'] = Device.active().filter(currentlending=None).count() context["device_percent"] = 100 - int((float(context["device_available"] ) / context["device_all"]) * 100) context["device_percentcolor"] = get_progresscolor(context["device_percent"]) context['ipaddress_all'] = IpAddress.objects.all().count() if context['ipaddress_all'] != 0: context['ipaddress_available'] = IpAddress.objects.filter(device=None).count() context["ipaddress_percent"] = 100 - int((float(context["ipaddress_available"] ) / context["ipaddress_all"]) * 100) context["ipaddress_percentcolor"] = get_progresscolor(context["ipaddress_percent"]) if "edithistory" in widgetlist: # using exclude(...=other_deps) is much faster than filter(...=departments).distinct() other_deps = Department.objects.all().difference(departments) context['revisions'] = Revision.objects.select_related('user') \ .prefetch_related('version_set', 'version_set__content_type') \ .exclude(user__departments__in=other_deps) \ .order_by("-date_created")[:20] if "newestdevices" in widgetlist: if departments: devices = Device.objects.filter(department__in=departments) else: devices = Device.objects.all() context['newest_devices'] = devices.order_by("-pk")[:10] if "overdue" in widgetlist: if departments: lendings = Lending.objects.select_related("device", "owner").filter(Q(device__department__in=departments) | Q(owner__main_department__in=departments)) else: lendings = Lending.objects.select_related("device", "owner") context["overdue"] = lendings.filter(duedate__lt=context["today"], returndate=None).order_by("duedate")[:10] if "groups" in widgetlist: context["groups"] = Devicegroup.objects.all() if "sections" in widgetlist: context["sections"] = Section.objects.all() if "recentlendings" in widgetlist: if departments: lendings = Lending.objects.select_related("device", "owner").filter(Q(device__department__in=departments) | Q(owner__main_department__in=departments)) else: lendings = Lending.objects.select_related("device", "owner") context["recentlendings"] = lendings.all().order_by("-pk")[:10] if "shorttermdevices" in widgetlist: context['shorttermdevices'] = Device.objects.filter(templending=True)[:10] if "bookmarks" in widgetlist: context["bookmarks"] = user.bookmarks.all()[:10] if "returnsoon" in widgetlist: soon = context["today"] + datetime.timedelta(days=10) if departments: lendings = Lending.objects.select_related("device", "owner").filter(Q(device__department__in=departments) | Q(owner__main_department__in=departments)) else: lendings = Lending.objects.select_related("device", "owner") context["returnsoon"] = lendings.filter(duedate__lte=soon, duedate__gt=context["today"], returndate=None).order_by( "duedate")[:10] return context
def get_widget_data(user, widgetlist=[], departments=None): context = {} context["today"] = datetime.date.today() if "statistics" in widgetlist: if departments: devices = Device.active().filter(department__in=departments) else: devices = Device.active() context['device_all'] = devices.count() if context['device_all'] != 0: context['device_available'] = Device.active().filter( currentlending=None).count() context["device_percent"] = 100 - int( (float(context["device_available"]) / context["device_all"]) * 100) context["device_percentcolor"] = get_progresscolor( context["device_percent"]) context['ipaddress_all'] = IpAddress.objects.all().count() if context['ipaddress_all'] != 0: context['ipaddress_available'] = IpAddress.objects.filter( device=None).count() context["ipaddress_percent"] = 100 - int( (float(context["ipaddress_available"]) / context["ipaddress_all"]) * 100) context["ipaddress_percentcolor"] = get_progresscolor( context["ipaddress_percent"]) if "edithistory" in widgetlist: context['revisions'] = Revision.objects.select_related( "user").prefetch_related( "version_set", "version_set__content_type").filter( user__departments__in=departments).order_by( "-date_created")[:20] if "newestdevices" in widgetlist: if departments: devices = Device.objects.filter(department__in=departments) else: devices = Device.objects.all() context['newest_devices'] = devices.order_by("-pk")[:10] if "overdue" in widgetlist: if departments: lendings = Lending.objects.select_related( "device", "owner").filter( Q(device__department__in=departments) | Q(owner__main_department__in=departments)) else: lendings = Lending.objects.select_related("device", "owner") context["overdue"] = lendings.filter( duedate__lt=context["today"], returndate=None).order_by("duedate")[:10] if "groups" in widgetlist: context["groups"] = Devicegroup.objects.all() if "sections" in widgetlist: context["sections"] = Section.objects.all() if "recentlendings" in widgetlist: if departments: lendings = Lending.objects.select_related( "device", "owner").filter( Q(device__department__in=departments) | Q(owner__main_department__in=departments)) else: lendings = Lending.objects.select_related("device", "owner") context["recentlendings"] = lendings.all().order_by("-pk")[:10] if "shorttermdevices" in widgetlist: context['shorttermdevices'] = Device.objects.filter( templending=True)[:10] if "bookmarks" in widgetlist: context["bookmarks"] = user.bookmarks.all()[:10] if "returnsoon" in widgetlist: soon = context["today"] + datetime.timedelta(days=10) if departments: lendings = Lending.objects.select_related( "device", "owner").filter( Q(device__department__in=departments) | Q(owner__main_department__in=departments)) else: lendings = Lending.objects.select_related("device", "owner") context["returnsoon"] = lendings.filter( duedate__lte=soon, duedate__gt=context["today"], returndate=None).order_by("duedate")[:10] return context