Beispiel #1
0
def _add_experiment_free(request, exp_type_name):
    'The add feature view'
    exp_type_name = 'NS_000'
    context = RequestContext(request)
    context.update(csrf(request))

    exp_type = Cvterm.objects.get(cv__name=EXPERIMENT_TYPES,
                                  name=exp_type_name)

    request_data = request.POST if request.method == 'POST' else None
    FeatFormset = formset_factory(ExperimentFeatureForm)
    TextFormset = modelformset_factory(ExperimentPropText,
                                       exclude=('experiment',))
    ImageFormset = modelformset_factory(ExperimentPropImage,
                                        exclude=('experiment',))
    GeneriFileFormset = modelformset_factory(ExperimentPropGenericFile,
                                             exclude=('experiment',))
    ExcelFormset = formset_factory(ExperimentExcelForm)
    KeywordFormset = formset_factory(ExperimentKeywordForm)

    if request_data:
        form = ExperimentForm(request_data, instance=Experiment())
        feat_formset = FeatFormset(request_data, prefix='feature')
        subfeat_form = ExperimentSubFeatureForm(request_data)
        text_formset = TextFormset(request_data, prefix='text')
        image_formset = ImageFormset(request_data, request.FILES,
                                     prefix='image')
        generic_file_formset = GeneriFileFormset(request_data, request.FILES,
                                                 prefix='generic_file')
        excel_formset = ExcelFormset(request_data, request.FILES,
                                     prefix='excel')
        keyword_formset = KeywordFormset(request_data, prefix='keyword')
        if (form.is_valid() and text_formset.is_valid() and
            feat_formset.is_valid() and subfeat_form.is_valid() and
                excel_formset.is_valid() and generic_file_formset.is_valid()):
            try:
                experiment = _add_experiment(form=form,
                                             text_formset=text_formset,
                                             image_formset=image_formset,
                                             excel_formset=excel_formset,
                                             feat_formset=feat_formset,
                                             subfeat_form=subfeat_form,
                                             generic_file_formset=generic_file_formset,
                                             keyword_formset=keyword_formset,
                                             user=request.user)
            except IntegrityError as error:
                print error
                raise
            except RuntimeError as error:
                msg = 'This user is not entitled to add this experiment'
                msg += ' as it is'
                return HttpResponseForbidden(msg)
            return redirect(experiment.url)
        else:
            print "no valid"
    else:
        form = ExperimentForm(instance=Experiment())
        get_request_data = request.GET if request.method == 'GET' else None
        if get_request_data:
            initial = [{'feature': get_request_data.get('feature')}]
        else:
            initial = None

        feat_formset = FeatFormset(initial=initial, prefix='feature')
#         feat_formset = FeatFormset(prefix='feature')
        subfeat_form = ExperimentSubFeatureForm()
        text_formset = TextFormset(prefix='text',
                                   queryset=ExperimentPropText.objects.none())
        none_image_query = ExperimentPropImage.objects.none()
        image_formset = ImageFormset(prefix='image', queryset=none_image_query)
        none_genericf_query = ExperimentPropGenericFile.objects.none()
        generic_file_formset = GeneriFileFormset(prefix='generic_file',
                                                 queryset=none_genericf_query)
        excel_formset = ExcelFormset(prefix='excel')
        keyword_formset = KeywordFormset(prefix='keyword')
    context['form'] = form
    context['feature_formset'] = feat_formset
    context['subfeat_form'] = subfeat_form
    context['text_formset'] = text_formset
    context['image_formset'] = image_formset
    context['excel_formset'] = excel_formset
    context['generic_file_formset'] = generic_file_formset
    context['keyword_formset'] = keyword_formset
    context['exp_cv_type'] = exp_type
    template = 'experiment_add_free.html'
    return render_to_response(template, context)
Beispiel #2
0
def _add_experiment_SE(request, exp_type_name):
    settings = EXP_SETTINGS[exp_type_name]
    exp_type = Cvterm.objects.get(cv__name=EXPERIMENT_TYPES,
                                  name=exp_type_name)
    context = RequestContext(request)
    context.update(csrf(request))
    quantitative_exp_def = settings.get('quantitative_outputs_def', exp_type)
    quantitative = Cvterm.objects.filter(cv__name=NUMERIC_TYPES,
                                         definition=quantitative_exp_def)
    request_data = request.POST if request.method == 'POST' else None
    FeatFormset = formset_factory(ExperimentFeatureForm)
    NumericFormset = formset_factory(ExperimentNumForm,
                                     extra=len(quantitative),
                                     formset=BaseExperimentNumFormset)
    ImageFormset = modelformset_factory(ExperimentPropImage,
                                        exclude=('experiment',))
    GenericFileFormset = formset_factory(ExperimentGenericFileForm)
    TextFormset = modelformset_factory(ExperimentPropText,
                                       exclude=('experiment',))
    ExcelFormset = formset_factory(ExperimentExcelForm)
    KeywordFormset = formset_factory(ExperimentKeywordForm)

    if request_data:
        form = ExperimentForm(request_data, instance=Experiment())
        feat_formset = FeatFormset(request_data, prefix='feature')
        subfeat_form = ExperimentSubFeatureForm(request_data)
        numeric_formset = NumericFormset(request_data, prefix='numeric')
        generic_file_formset = GenericFileFormset(request_data, request.FILES,
                                                  prefix='generic_file')
        image_formset = ImageFormset(request_data, request.FILES,
                                     prefix='image')
        text_formset = TextFormset(request_data, prefix='text')
        excel_formset = ExcelFormset(request_data, request.FILES,
                                     prefix='excel')
        keyword_formset = KeywordFormset(request_data, prefix='keyword')
        protocol_form = ExperimentProtocolForm(request_data, request.FILES)

        if (form.is_valid() and feat_formset.is_valid() and
            subfeat_form.is_valid() and numeric_formset.is_valid() and
            image_formset.is_valid() and text_formset.is_valid() and
                generic_file_formset.is_valid() and excel_formset.is_valid()
                and keyword_formset.is_valid() and protocol_form.is_valid()):
            try:
                experiment = _add_experiment(form=form,
                                             feat_formset=feat_formset,
                                             subfeat_form=subfeat_form,
                                             user=request.user,
                                             numeric_formset=numeric_formset,
                                             image_formset=image_formset,
                                             text_formset=text_formset,
                                             generic_file_formset=generic_file_formset,
                                             excel_formset=excel_formset,
                                             keyword_formset=keyword_formset,
                                             protocol_form=protocol_form)
                print "valid"
            except IntegrityError as error:
                print error
                raise
            except RuntimeError as error:
                msg = 'This user is not entitled to add this experiment'
                msg += ' as it is'
                return HttpResponseForbidden(msg)
            return redirect(experiment.url)
        else:
            print "no valid"

    else:
        form = ExperimentForm(instance=Experiment())
        get_request_data = request.GET if request.method == 'GET' else None
        if get_request_data:
            initial = [{'feature': get_request_data.get('feature')}]
        else:
            initial = None
        feat_formset = FeatFormset(initial=initial, prefix='feature')
        subfeat_form = ExperimentSubFeatureForm()
        numeric_formset = NumericFormset(prefix='numeric')
        generic_file_formset = GenericFileFormset(prefix='generic_file')
        none_image_query = ExperimentPropImage.objects.none()
        image_formset = ImageFormset(prefix='image', queryset=none_image_query)
        text_formset = TextFormset(prefix='text',
                                   queryset=ExperimentPropText.objects.none())
        excel_formset = ExcelFormset(prefix='excel')
        keyword_formset = KeywordFormset(prefix='keyword')
        protocol_form = ExperimentProtocolForm()

    context['form'] = form
    context['feature_formset'] = feat_formset
    context['subfeat_form'] = subfeat_form
    context['numeric_formset'] = numeric_formset
    context['generic_file_formset'] = generic_file_formset
    context['image_formset'] = image_formset
    context['text_formset'] = text_formset
    context['excel_formset'] = excel_formset
    context['keyword_formset'] = keyword_formset
    context['protocol_form'] = protocol_form
    context['exp_cv_type'] = exp_type
    context['plant_species'] = settings.get('plant_species', None)
    context['chassis'] = settings.get('chassis', None)

    context['quantitative_outputs'] = quantitative.order_by('name')
    context['excel_mandatory'] = settings['excel_mandatory']
    return render_to_response('experiment_add_standard.html', context)