Tuesday, 27 April 2021

Django - How to check if the user has uploaded an excel file or input text and validate the form

Currently I have the following view, it checks if the user has uploaded an excel file and if everything is ok, some logic will run:

def search_variant(request):
   context = {
      'current_tab': '1'
   }

   if request.method == 'POST':
      form = SearchVariantForm(request.POST, request.FILES)

      if form.is_valid():
         input_file = request.FILES['input_file'] if 'input_file' in request.FILES else None
         input_file_copy = save_input_file(input_file)
         context['current_file_name'] = path.basename(input_file.name)
         #db input hardcoded data
         db_input_data = [{'Chr': 'HARDCODEDchrX', 'Start': 107844640}]
         # run interpretation and display the result
         interpretations = run_intervar(input_file_copy.name, db_input_data)
         context['search_variant_form'] = SearchVariantForm()
         context['evidence_formset'] = PathogenicInterpretationFormSet(initial=interpretations)
      else:
         # display the search variant form with the errors
         context['search_variant_form'] = form
   else:
      context['search_variant_form'] = SearchVariantForm()
   return render(request, 'geneticvariants/index.html', context)

The specifications of my project have changed, and now apart from an xlsx file, it is also possible that the user through the html form send strings (in a text input, with basically the same content as the xlsx file).

The desired logic would be the following (semi pseudo-code):

def search_variant(request):
   gene = request.POST.get('gene')
   variant = request.POST.get('variant')
   specimen = request.POST.get('specimen')
   context = {
      'current_tab': '1'
   }

    if gene or variant or specimen:
        #same logic as per xlsx but with data from db_input_data (hardcoded)
          if request.method == 'POST':
              form = SearchVariantForm(request.POST, request.FILES)
              if form.is_valid():
                 input_file = request.FILES['input_file'] if 'input_file' in request.FILES else None
                 input_file_copy = save_input_file(input_file)
                 context['current_file_name'] = path.basename(input_file.name)
                 #db input hardcoded data
                 db_input_data = [{'Chr': 'HARDCODEDchrX', 'Start': 107844640}]
                 # run interpretation and display the result
                 interpretations = run_intervar(input_file_copy.name, db_input_data)
                 context['search_variant_form'] = SearchVariantForm()
                 context['evidence_formset'] = PathogenicInterpretationFormSet(initial=interpretations)
              else:
                 # display the search variant form with the errors
                 context['search_variant_form'] = form
          else:
            context['search_variant_form'] = SearchVariantForm()

    else:
        #if there is a xlsx file and no input text from gene, variant and specimen variables:
            #original search_variant logic (code above)
          if request.method == 'POST':
              form = SearchVariantForm(request.POST, request.FILES)
              if form.is_valid():
                 input_file = request.FILES['input_file'] if 'input_file' in request.FILES else None
                 input_file_copy = save_input_file(input_file)
                 context['current_file_name'] = path.basename(input_file.name)
                 # run interpretation and display the result
                 interpretations = run_intervar(input_file_copy.name, None)
                 context['search_variant_form'] = SearchVariantForm()
                 context['evidence_formset'] = PathogenicInterpretationFormSet(initial=interpretations)
              else:
                 # display the search variant form with the errors
                 context['search_variant_form'] = form
          else:
            context['search_variant_form'] = SearchVariantForm()

But the problem comes here:

form = SearchVariantForm(request.POST, request.FILES)

If we check the definitionn of SearchVariantForm:

class SearchVariantForm(forms.Form):
    input_file = forms.FileField(required=False, label='Select an input file...')

    def clean(self):
        cleaned_data = super().clean()
        input_file = cleaned_data.get('input_file')

        if not input_file:
            raise forms.ValidationError('Please provide an Excel or CSV file.')

        (root, ext) = splitext(input_file.name)

        if ext not in ('.xlsx', '.xls', '.csv',):
            raise forms.ValidationError('Please provide an Excel or CSV file.')

    def __init__(self, *args, **kwargs):
        super(SearchVariantForm, self).__init__(*args, **kwargs)
        for visible in self.visible_fields():
            visible.field.widget.attrs['class'] = 'form-control form-control-sm'

How could I adapt SearchVariantForm so that, in the case of not uploading an xlsx file but there are variables (gene or variant or specimen) in the request, I can create a form instead of getting an Please provide an Excel or CSV file. error?



from Django - How to check if the user has uploaded an excel file or input text and validate the form

No comments:

Post a Comment