예제 #1
0
def habitsandpractices(request, pk,
                      template_name='data_group/habitsandpractices.html'):
    doc = get_object_or_404(DataDocument, pk=pk, )
    script = Script.objects.get(title='Manual (dummy)', script_type='EX')
    extext, created = ExtractedText.objects.get_or_create(data_document=doc,
                                                    extraction_script=script)
    if created:
        extext.doc_date = 'please add...'
    ExtractedTextForm, HnPFormSet = create_detail_formset(doc.data_group.type)
    ext_form = ExtractedTextForm(request.POST or None, instance=extext)
    hp_formset = HnPFormSet(request.POST or None,
                            instance=extext, prefix='habits')
    if request.method == 'POST' and 'save' in request.POST:
        if hp_formset.is_valid() and ext_form.is_valid():
            if not doc.extracted:
                doc.extracted = True
                doc.save()
            hp_formset.save()
            ext_form.save()
            return HttpResponseRedirect(f'/habitsandpractices/{doc.pk}')
    context = {   'doc'         : doc,
                  'ext_form'    : ext_form,
                  'hp_formset'  : hp_formset,
                  }
    return render(request, template_name, context)
    def test_product_surveyed_field(self):
        self.objects.gt.code = "HP"
        self.objects.gt.save()
        _, HnPFormSet = create_detail_formset(self.objects.doc)
        data = {
            "habits-TOTAL_FORMS": "2",
            "habits-INITIAL_FORMS": "1",
            "habits-MIN_NUM_FORMS": "0",
            "habits-MAX_NUM_FORMS": "1000",
            "habits-0-id": self.objects.ehp.pk,
            "habits-0-product_surveyed": "",
        }
        hp_formset = HnPFormSet(data, prefix="habits")
        self.assertFalse(hp_formset.is_valid())

        data = {
            "habits-TOTAL_FORMS": "2",
            "habits-INITIAL_FORMS": "1",
            "habits-MIN_NUM_FORMS": "0",
            "habits-MAX_NUM_FORMS": "1000",
            "habits-0-id": self.objects.ehp.pk,
            "habits-0-product_surveyed": "monster trucks",
        }
        hp_formset = HnPFormSet(data, prefix="habits")

        self.assertTrue(hp_formset.is_valid())
예제 #3
0
def data_document_detail(request, pk):
    template_name = "data_document/data_document_detail.html"
    doc = get_object_or_404(DataDocument, pk=pk)
    if doc.data_group.group_type.code == "SD":
        messages.info(
            request,
            f'"{doc}" has no detail page. GroupType is "{doc.data_group.group_type}"',
        )
        return redirect(reverse("data_group_detail", args=[doc.data_group_id]))
    ParentForm, _ = create_detail_formset(doc)
    Parent, Child = get_extracted_models(doc.data_group.group_type.code)
    ext = Parent.objects.filter(pk=doc.pk).first()
    chemicals = Child.objects.filter(
        extracted_text__data_document=doc).prefetch_related("dsstox")
    if Child == ExtractedListPresence:
        chemicals = chemicals.prefetch_related("tags")
    lp = ExtractedListPresence.objects.filter(
        extracted_text=ext if ext else None).first()
    tag_form = ExtractedListPresenceTagForm()
    context = {
        "doc": doc,
        "extracted_text": ext,
        "chemicals": chemicals,
        "edit_text_form":
        ParentForm(instance=ext),  # empty form if ext is None
        "list_presence_tag_form": tag_form if lp else None,
    }
    if doc.data_group.group_type.code == "CO":
        script_chem = chemicals.filter(script__isnull=False).first()
        context[
            "cleaning_script"] = script_chem.script if script_chem else None
    return render(request, template_name, context)
예제 #4
0
def save_ext_form(request, pk):
    doc = get_object_or_404(DataDocument, pk=pk)
    ExtractedTextForm, _ = create_detail_formset(doc.data_group.type)
    extracted_text = doc.extractedtext.pull_out_cp()
    ext_text_form = ExtractedTextForm(request.POST, instance=extracted_text)
    if ext_text_form.is_valid() and ext_text_form.has_changed():
        ext_text_form.save()
    return redirect('data_document', pk=pk)
예제 #5
0
def save_ext_form(request, pk):
    referer = request.POST.get("referer", "data_document")
    doc = get_object_or_404(DataDocument, pk=pk)
    ExtractedTextForm, _ = create_detail_formset(doc)
    extracted_text = ExtractedText.objects.get_subclass(pk=pk)
    ext_text_form = ExtractedTextForm(request.POST, instance=extracted_text)
    if ext_text_form.is_valid() and ext_text_form.has_changed():
        ext_text_form.save()
    return redirect(referer, pk=pk)
예제 #6
0
 def test_every_extractedtext(self):
     """'Loop through all the ExtractedText objects and confirm that the new
     create_detail_formset method returns forms based on the correct models
     """
     for et in ExtractedText.objects.all():
         dd = et.data_document
         ParentForm, ChildForm = create_detail_formset(dd, EXTRA)
         child_formset = ChildForm(instance=et)
         # Compare the model of the child formset's QuerySet to the model
         # of the ExtractedText object's child objects
         dd_child_model = get_extracted_models(
             dd.data_group.group_type.code)[1]
         childform_model = child_formset.__dict__.get(
             "queryset").__dict__.get("model")
         self.assertEqual(dd_child_model, childform_model)
예제 #7
0
def extracted_text_edit(request, pk):
    doc = get_object_or_404(DataDocument, pk=pk)
    ParentForm, _ = create_detail_formset(doc, extra=0, can_delete=False)
    model = ParentForm.Meta.model
    script = Script.objects.get(title__icontains="Manual (dummy)",
                                script_type="EX")
    try:
        extracted_text = model.objects.get_subclass(data_document_id=pk)
    except ExtractedText.DoesNotExist:
        extracted_text = model(data_document_id=pk, extraction_script=script)
    form = ParentForm(request.POST, instance=extracted_text)
    if form.is_valid():
        form.save()
        doc.save()
        return redirect("data_document", pk=doc.pk)
    else:
        extracted_text.delete()
        return HttpResponse("Houston, we have a problem.")
예제 #8
0
def data_document_detail(
        request, pk, template_name='data_document/data_document_detail.html'):
    doc = get_object_or_404(
        DataDocument,
        pk=pk,
    )
    ParentForm, ChildForm = create_detail_formset(doc.data_group.type,
                                                  extra=0,
                                                  can_delete=False)

    document_type_form = DocumentTypeForm(request.POST or None, instance=doc)
    qs = DocumentType.objects.filter(group_type=doc.data_group.group_type)
    document_type_form.fields['document_type'].queryset = qs

    context = {'doc': doc, 'document_type_form': document_type_form}

    try:
        extracted_text = ExtractedText.objects.get(data_document=doc)

    except ExtractedText.DoesNotExist:
        #print('No ExtractedText object found for DataDocument: %s' % doc )
        extracted_text = None

    else:
        #print('ExtractedText object found: %s' % extracted_text )
        extracted_text = extracted_text.pull_out_cp()  #get CP if exists
        extracted_text_form = ParentForm(instance=extracted_text)
        child_formset = ChildForm(instance=extracted_text)

        for form in child_formset.forms:
            for field in form.fields:
                form.fields[field].widget.attrs['disabled'] = True

        context.update({
            'extracted_text': extracted_text,
            'detail_formset': child_formset
        })

        colors = ['#d6d6a6', '#dfcaa9', '#d8e5bf'] * 47
        color = (hex for hex in colors)
        for form in child_formset.forms:
            form.color = next(color)
    return render(request, template_name, context)
예제 #9
0
def habitsandpractices(request,
                       pk,
                       template_name="data_group/habitsandpractices.html"):
    doc = get_object_or_404(DataDocument, pk=pk)
    script = Script.objects.get(title="Manual (dummy)", script_type="EX")
    extext, created = ExtractedText.objects.get_or_create(
        data_document=doc, extraction_script=script)
    if created:
        extext.doc_date = "please add..."
    ExtractedTextForm, HPFormSet = create_detail_formset(doc)
    ext_form = ExtractedTextForm(request.POST or None, instance=extext)
    hp_formset = HPFormSet(request.POST or None,
                           instance=extext,
                           prefix="habits")
    context = {"doc": doc, "ext_form": ext_form, "hp_formset": hp_formset}
    if request.method == "POST" and "save" in request.POST:
        if hp_formset.is_valid():
            hp_formset.save()
        if ext_form.is_valid():
            ext_form.save()
        doc.save()
        context = {"doc": doc, "ext_form": ext_form, "hp_formset": hp_formset}
    return render(request, template_name, context)
예제 #10
0
    def test_product_surveyed_field(self):
        self.objects.gt.code = 'HP'
        self.objects.gt.save()
        _, HnPFormSet = create_detail_formset(self.objects.dg.type)
        data = {'habits-TOTAL_FORMS':'2',
                'habits-INITIAL_FORMS':'1',
                'habits-MIN_NUM_FORMS':'0',
                'habits-MAX_NUM_FORMS':'1000',
                'habits-0-id': self.objects.ehp.pk,
                'habits-0-product_surveyed':'',
        }
        hp_formset = HnPFormSet(data, prefix='habits')
        self.assertFalse(hp_formset.is_valid())

        data = {'habits-TOTAL_FORMS':'2',
                'habits-INITIAL_FORMS':'1',
                'habits-MIN_NUM_FORMS':'0',
                'habits-MAX_NUM_FORMS':'1000',
                'habits-0-id': self.objects.ehp.pk,
                'habits-0-product_surveyed':'monster trucks',
        }
        hp_formset = HnPFormSet(data, prefix='habits')

        self.assertTrue(hp_formset.is_valid())
예제 #11
0
def extracted_text_qa(request,
                      pk,
                      template_name='qa/extracted_text_qa.html',
                      nextid=0):
    """
    Detailed view of an ExtractedText object, where the user can approve the
    record, edit its ExtractedChemical objects, skip to the next ExtractedText
    in the QA group, or exit to the index page
    """
    extext = get_object_or_404(ExtractedText, pk=pk)
    # The related DataDocument has the same pk as the ExtractedText object
    doc = DataDocument.objects.get(pk=pk)
    exscript = extext.extraction_script
    # when not coming from extraction_script page, we don't necessarily have a qa_group created
    if not extext.qa_group:
        # create the qa group with the optional ExtractedText pk argument
        # so that the ExtractedText gets added to the QA group even if the
        # group uses a random sample
        qa_group = exscript.create_qa_group(pk)
        exscript.qa_begun = True
        exscript.save()
        extext.qa_group = qa_group
        extext.save()
    # get the next unapproved Extracted Text object
    # Its ID will populate the URL for the "Skip" button
    if extext.qa_checked:  # if ExtractedText object's QA process done, use 0
        nextid = 0
    else:
        nextid = extext.next_extracted_text_in_qa_group()
    # derive number of approved records and remaining unapproved in QA Group

    a = extext.qa_group.get_approved_doc_count()
    r = ExtractedText.objects.filter(qa_group=extext.qa_group).count() - a
    stats = '%s document(s) approved, %s documents remaining' % (a, r)
    referer = 'data_document' if 'datadocument' in request.path else 'extraction_script_qa'

    # Create the formset factory for the extracted records
    # The model used for the formset depends on whether the
    # extracted text object matches a data document()

    ParentForm, ChildForm = create_detail_formset(doc.data_group.type, EXTRA)
    extext = extext.pull_out_cp()  #get CP if exists
    ext_form = ParentForm(instance=extext)
    detail_formset = ChildForm(instance=extext)
    # Add CSS selector classes to each form
    for form in detail_formset:
        for field in form.fields:
            form.fields[field].widget.attrs.update({
                'class':
                f'detail-control form-control %s' % doc.data_group.type
            })

    note, created = QANotes.objects.get_or_create(extracted_text=extext)
    notesform = QANotesForm(instance=note)

    context = {
        'extracted_text': extext,
        'doc': doc,
        'script': exscript,
        'stats': stats,
        'nextid': nextid,
        'detail_formset': detail_formset,
        'notesform': notesform,
        'ext_form': ext_form,
        'referer': referer
    }

    if request.method == 'POST' and 'save' in request.POST:
        #print(request.__dict__)

        ParentForm, ChildForm = create_detail_formset(doc.data_group.type,
                                                      EXTRA)
        extext = extext.pull_out_cp()  #get CP if exists
        ext_form = ParentForm(request.POST, instance=extext)
        detail_formset = ChildForm(request.POST, instance=extext)

        notesform = QANotesForm(request.POST, instance=note)
        notesform.save()
        if detail_formset.has_changed() or ext_form.has_changed():
            if detail_formset.is_valid() and ext_form.is_valid():
                detail_formset.save()
                ext_form.save()
                extext.qa_edited = True
                extext.save()
        # rebuild the formset after saving it
        detail_formset = ChildForm(instance=extext)
        context['detail_formset'] = detail_formset
        context['ext_form'] = ext_form
        context.update({'notesform': notesform})  # calls the clean method? y?
        # Add CSS selector classes to each form
        for form in detail_formset:
            for field in form.fields:
                form.fields[field].widget.attrs.update({
                    'class':
                    f'detail-control form-control %s' % doc.data_group.type
                })

    elif request.method == 'POST' and 'approve' in request.POST:  # APPROVAL
        notesform = QANotesForm(request.POST, instance=note)
        context['notesform'] = notesform
        nextpk = extext.next_extracted_text_in_qa_group()
        if notesform.is_valid():
            extext.qa_approved_date = timezone.now()
            extext.qa_approved_by = request.user
            extext.qa_checked = True
            extext.save()
            notesform.save()
            if referer == 'data_document':
                return HttpResponseRedirect(reverse(referer, kwargs={'pk':
                                                                     pk}))
            elif not nextpk == 0:
                return HttpResponseRedirect(
                    reverse('extracted_text_qa', args=[(nextpk)]))
            elif nextpk == 0:
                return HttpResponseRedirect(reverse('qa'))
    return render(request, template_name, context)