def make_book_laboratory(rooms): content = {} for labroom in rooms: labobj = [[ _("Furniture"), _("Shelf"), _("Code"), _("Name"), _("Quantity"), _("Units"), _("Object type"), _("CAS ID"), _("IMDG code") ]] for furniture in labroom.furniture_set.all(): for obj in furniture.get_objects(): labobj.append([ furniture.name, obj.shelf.name, obj.object.code, obj.object.name, obj.quantity, obj.get_measurement_unit_display(), obj.object.get_type_display(), get_cas(obj.object, ""), str(get_imdg(obj.object, "")) ]) if labobj: content[labroom.name] = labobj return content
def report_reactive_precursor_objects(request, *args, **kwargs): template = get_template('pdf/reactive_precursor_objects_pdf.html') lab = kwargs.get('lab_pk') try: all_labs = int(request.GET.get('all_labs', '0')) except: all_labs = 0 if lab and not all_labs: rpo = Object.objects.filter( shelfobject__shelf__furniture__labroom__laboratory__pk=lab) else: rpo = Object.objects.all() rpo = rpo.filter(type=Object.REACTIVE, sustancecharacteristics__is_precursor=True) for obj in rpo: clentry = CLInventory.objects.filter( cas_id_number=get_cas(obj, 0)).first() setattr(obj, 'clinventory_entry', clentry) fileformat = request.GET.get('format', 'pdf') if fileformat in ['xls', 'xlsx', 'ods']: return django_excel.make_response_from_book_dict( make_book_objects(rpo, summary=True, type_id='0'), fileformat, file_name="reactive_precursor.%s" % (fileformat, )) context = { 'verbose_name': "Reactive precursor objects", 'rpo': rpo, 'datetime': timezone.now(), 'request': request, 'laboratory': lab } html = template.render(context=context) response = HttpResponse(content_type='application/pdf') response[ 'Content-Disposition'] = 'attachment; filename="report_reactive_precursor_objects.pdf"' pisaStatus = pisa.CreatePDF(html, dest=response, link_callback=link_callback, encoding='utf-8') if pisaStatus.err: return HttpResponse('We had some errors with code %s <pre>%s</pre>' % (pisaStatus.err, html)) return response
def make_book_objects(objects, summary=False, type_id=None): description = [ _("Code"), _("Name"), _("Type"), _("Quantity total"), _('Measurement units') ] if type_id == '0': description += [ _("Molecular formula"), _("CAS id number"), _("Is precursor?"), _("IMDG type") ] content = { 'objects': [ description, ] } objects = objects.annotate( quantity_total=Sum('shelfobject__quantity'), measurement_unit=Min('shelfobject__measurement_unit')) for object in objects: obj_info = [ object.code, object.name, object.get_type_display(), object.quantity_total, ShelfObject.get_units(object.measurement_unit) ] if type_id == '0': obj_info += [ get_molecular_formula(object), get_cas(object, ''), object.is_precursor, str(get_imdg(object, '')) ] content['objects'].append(obj_info) if not summary: for shelfobj in object.shelfobject_set.all(): content['objects'].append([ '', shelfobj.shelf.furniture.name, shelfobj.shelf.name, shelfobj.quantity, shelfobj.get_measurement_unit_display() ]) return content
def get_queryset(self): try: self.all_labs = int(self.request.GET.get('all_labs', '0')) except: self.all_labs = 0 query = super(ReactivePrecursorObjectList, self).get_queryset() query = query.filter(type=Object.REACTIVE, sustancecharacteristics__is_precursor=True) if not self.all_labs: query = query.filter( shelfobject__shelf__furniture__labroom__laboratory=self.lab ).distinct() for obj in query: clentry = CLInventory.objects.filter( cas_id_number=get_cas(obj, 0)).first() setattr(obj, 'clinventory_entry', clentry) return query
def prepare_results(self, qs): json_data = [] for item in qs: is_bioaccumulable = hasattr(item, 'sustancecharacteristics')\ and item.sustancecharacteristics and item.sustancecharacteristics.bioaccumulable is_precursor = hasattr(item, 'sustancecharacteristics')\ and item.sustancecharacteristics and item.sustancecharacteristics.is_precursor warning_precursor = 'fa fa-check-circle fa-fw text-success' if is_precursor else \ 'fa fa-times-circle fa-fw text-warning' warning_bioaccumulable = 'fa fa-leaf fa-fw text-success' if is_bioaccumulable else \ 'fa fa-flask text-warning' warning_is_public = 'fa fa-users fa-fw text-success' if item.is_public \ else 'fa fa-user-times fa-fw text-warning' precursor = '<i class="{0}" title="{1} {2}"></i>'.format(warning_precursor, _('Is precursor?'), _("Yes") if is_precursor else _("No") ) bioaccumulable = '<i class="{0}" title="{1} {2}"></i>'.format(warning_bioaccumulable, _('Is bioaccumulable?'), _("Yes") if is_bioaccumulable else _("No")) is_public = '<i class="{0}" title="{1} {2}" aria-hidden="true"></i>'.format( warning_is_public, _('Is public?'), _("Yes") if item.is_public else _("No")) name_url = """<a href="{0}" title="{1}">{2}</a>""".format( reverse('laboratory:sustance_manage', kwargs={'pk': item.id}), item.synonym or item.name, item.name) delete = """<a href="{0}" title="{1}" class="pull-right"><i class="fa fa-trash-o" style="color:red"></i></a>"""\ .format(reverse('laboratory:sustance_delete', kwargs={'pk': item.id}), _('Delete sustance')) if hasattr(item, 'sustancecharacteristics') and item.sustancecharacteristics and \ item.sustancecharacteristics.security_sheet: download = """<a href="{0}" title="{1}"><i class="fa fa-download" ></i></a>""" \ .format(item.sustancecharacteristics.security_sheet.url, _("Download security sheet")) delete = download+delete json_data.append([ is_public+precursor+bioaccumulable, name_url, get_cas(item, ''), delete ]) return json_data
def report_objects(request, *args, **kwargs): var = request.GET.get('pk') type_id = None if var is None: if 'lab_pk' in kwargs: filters = Q( shelfobject__shelf__furniture__labroom__laboratory__pk=kwargs. get('lab_pk')) if 'type_id' in request.GET: type_id = request.GET.get('type_id', '') if type_id: filters = filters & Q(type=type_id) if 'lab_pk' in kwargs and 'type_id' in request.GET: objects = Object.objects.filter(filters) else: objects = Object.objects.all() else: objects = Object.objects.filter(pk=var) try: detail = bool(int(request.GET.get('details', 0))) except: detail = False fileformat = request.GET.get('format', 'pdf') if fileformat in ['xls', 'xlsx', 'ods']: return django_excel.make_response_from_book_dict( make_book_objects(objects, summary=detail, type_id=type_id), fileformat, file_name="objects.%s" % (fileformat, )) for obj in objects: clentry = CLInventory.objects.filter( cas_id_number=get_cas(obj, 0)).first() setattr(obj, 'clinventory_entry', clentry) template = get_template('pdf/object_pdf.html') verbose_name = 'Reactives report' if type_id == "1": verbose_name = 'Materials report' if type_id == "2": verbose_name = 'Equipments report' context = { 'verbose_name': verbose_name, 'object_list': objects, 'datetime': timezone.now(), 'request': request, 'laboratory': kwargs.get('lab_pk') } response = HttpResponse(content_type='application/pdf') html = template.render(context=context) response[ 'Content-Disposition'] = 'attachment; filename="report_objects.pdf"' pisaStatus = pisa.CreatePDF(html, dest=response, link_callback=link_callback, encoding='utf-8') if pisaStatus.err: return HttpResponse('We had some errors with code %s <pre>%s</pre>' % (pisaStatus.err, html)) return response