Skip to content
This repository was archived by the owner on Oct 4, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions vcweb/core/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,22 +239,38 @@ class Meta:
class ExperimentParameterValueForm(forms.ModelForm):
required_css_class = 'required'

def __init__(self, *args, **kwargs):
super(ExperimentParameterValueForm, self).__init__(*args, **kwargs)
self.fields['parameter'].queryset = self.fields[
'parameter'].queryset.filter(scope='experiment')
def __init__(self, post_dict=None, instance=None, pk=None, **kwargs):
if instance is None and pk is not None and pk != '-1':
instance = ExperimentParameterValue.objects.get(pk=pk)
super(ExperimentParameterValueForm, self).__init__(post_dict, instance=instance, **kwargs)

self.fields['parameter'].queryset = self.fields['parameter'].queryset.filter(scope='experiment')

for name, field in self.fields.items():
if field.widget.__class__ == CheckboxInput:
if isinstance(field.widget, CheckboxInput):
field.widget.attrs['data-bind'] = 'checked: %s' % name
else:
field.widget.attrs['data-bind'] = 'value: %s' % name

if post_dict:
self.request_type = post_dict.get('request_type')

def save(self, commit=True):
epv = super(ExperimentParameterValueForm, self).save(commit=False)
if self.request_type == 'delete':
logger.warn("Deleting round parameter value %s", epv)
epv.delete()
elif commit:
epv.save()
return epv


class Meta:
model = ExperimentParameterValue
exclude = ('experiment_configuration', 'last_modified', 'date_created')
exclude = ('last_modified', 'date_created')
widgets = {
'string_value': forms.Textarea(attrs={'cols': 40, 'rows': 3}),
'experiment_configuration': forms.HiddenInput
}


Expand Down
50 changes: 24 additions & 26 deletions vcweb/core/templates/experimenter/edit-configuration.html
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ <h4 class="modal-title">Edit Details and Click Save</h4>

<script type='text/html' id='round-param-template'>
<form method="post" class="form-horizontal" data-bind="submit: $root.saveRoundParam">
<input type="hidden" data-bind="value: pk">
<input type="hidden" data-bind="value: pk" class="hidden-param">
{% bootstrap_form round_param_form layout="horizontal" label_class="col-md-3" field_class="col-md-6"%}
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Save</button>
Expand All @@ -214,7 +214,7 @@ <h4 class="modal-title">Edit Details and Click Save</h4>
<script type='text/html' id='exp-param-template'>
<form method="post" class="form-horizontal" data-bind="submit: $root.saveExpParam">
{% bootstrap_form exp_param_form layout="horizontal" label_class="col-md-3" field_class="col-md-6"%}
<input type="hidden" data-bind="value: pk">
<input type="hidden" data-bind="value: pk" class="hidden-param">
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
Expand All @@ -223,8 +223,8 @@ <h4 class="modal-title">Edit Details and Click Save</h4>

<script type="text/html" id="round-config-template">
<form class="form-horizontal" data-bind="submit: $root.saveRoundConfig">
{% bootstrap_form round_config_form layout="horizontal" label_class="col-md-4" field_class="col-md-6"%}
<input type="hidden" data-bind="value: pk">
{% bootstrap_form round_config_form layout="horizontal" label_class="col-md-4" field_class="col-md-6"%}
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
Expand All @@ -234,9 +234,9 @@ <h4 class="modal-title">Edit Details and Click Save</h4>
<script type="text/javascript">

var JSONdata = $.parseJSON("{{ json_data | escapejs }}");
var experiment_configuration_pk = {{ experiment_config.pk }}

$(function() {

//wrapper to an observable that requires accept/cancel
ko.protectedObservable = function(initialValue) {
//private variables
Expand Down Expand Up @@ -284,6 +284,7 @@ <h4 class="modal-title">Edit Details and Click Save</h4>
self.display_name = ko.observable(data.display_name);
self.pk = ko.observable(data.pk);
self.round_configuration = ko.observable(data.round_configuration || data.round_configuration_pk);
self.experiment_configuration = ko.observable(data.experiment_configuration || data.experiment_configuration_pk);
self.parameter = ko.observable(data.parameter_pk);
self.string_value = ko.observable(data.string_value);
self.int_value = ko.observable(data.int_value);
Expand Down Expand Up @@ -348,6 +349,7 @@ <h4 class="modal-title">Edit Details and Click Save</h4>
},
'expParamValuesList': {
create: function (options) {
options.data.experiment_configuration_pk = experiment_configuration_pk
return new ParamViewModel(options.data);
}
}
Expand All @@ -365,20 +367,24 @@ <h4 class="modal-title">Edit Details and Click Save</h4>
}

model.addExpParam = function (data, event) {
var temp = new ParamViewModel({'is_active': true});
var temp = new ParamViewModel({'experiment_configuration': experiment_configuration_pk ,'is_active': true});
model.modalData(temp);
model.template("exp-param-template");
model.activateModal(true);
}

model.editExpParam = function (data){
model.modalData(data);
model.editExpParam = function (expParamModel){
model.modalData(expParamModel);
model.template("exp-param-template");
model.activateModal(true);
}

model.removeExpParam = function (expParam){
$.post("/api/configuration/experiment/param/" + expParam.pk(), 'request_type=delete')
var data = 'experiment_configuration='+ experiment_configuration_pk +
'&parameter='+ expParam.parameter()+
'&request_type=delete';

$.post("/api/configuration/experiment/param/" + expParam.pk(), data)
.done(function(result) {
if (result.success){
model.expParamValuesList.remove(expParam);
Expand Down Expand Up @@ -443,7 +449,7 @@ <h4 class="modal-title">Edit Details and Click Save</h4>
}
model.saveRoundParam = function(form) {
var form = $(form),
paramPk = parseInt(form.find("input[type='hidden']").val()),
paramPk = parseInt(form.find("input[type='hidden'].hidden-param").val()),
formData = form.serialize();

if (paramPk) {
Expand Down Expand Up @@ -483,7 +489,7 @@ <h4 class="modal-title">Edit Details and Click Save</h4>

model.saveRoundConfig = function(form) {
var form = $(form);
var roundPk = form.find("input[type='hidden']").val();
var roundPk = form.find("input[type='hidden'].hidden-param").val();
var formData = form.serialize();

if (roundPk) {
Expand Down Expand Up @@ -559,45 +565,37 @@ <h4 class="modal-title">Edit Details and Click Save</h4>

model.saveExpParam = function(form) {
var form = $(form);
var paramPk = parseInt(form.find("input[type='hidden']").val());
var paramPk = parseInt(form.find("input[type='hidden'].hidden-param").val());
var formData = form.serialize();

if (paramPk) {
//Update Request

var match = ko.utils.arrayFirst(model.expParamValuesList(), function (expParam) {
return parseInt(paramPk) === expParam.pk();
});
$.ajax("/api/configuration/experiment/param/"+ paramPk, {
data: formData + '&request_type=update',
type: "POST",
success: function(result){
$.post("/api/configuration/experiment/param/"+ paramPk, formData + '&request_type=update')
.done(function(result){
if(result.success){
match.display_name(result.experiment_param.display_name);
model.hideModal();
} else {
// Error Message
}
}
});
});
} else {
// Create Request

formData += '&experiment_configuration=' + {{ experiment_config.pk }};
$.ajax("/api/configuration/experiment/param/-1", {
data: formData + '&request_type=create',
type: "POST",
success: function(result) {
$.post("/api/configuration/experiment/param/-1", formData + '&request_type=create')
.done(function(result) {
if(result.success){
model.expParamValuesList.push(new ParamViewModel(result.experiment_param));
model.hideModal();
} else {
// Error Message
}
}
});
});
}
}

model.saveExperimentConfig = function(form) {
var form = $(form);
var formData = form.serialize();
Expand Down
45 changes: 5 additions & 40 deletions vcweb/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,50 +1124,15 @@ def reset_password(email, from_email='vcweb@asu.edu', template='registration/pas

@group_required(PermissionGroup.experimenter)
def update_experiment_param_value(request, pk):
# extract request type
request_type = request.POST.get('request_type')

# delete Request
if request_type == 'delete' and pk:
try:
ExperimentParameterValue.objects.get(pk=pk).delete()
return JsonResponse(SUCCESS_DICT)
except:
return JsonResponse(FAILURE_DICT)
# create Request
elif request_type == 'create':
form = ExperimentParameterValueForm(request.POST or None)
if form.is_valid():
epv = form.save(commit=False)
exp_config_pk = request.POST.get('experiment_configuration')
epv.experiment_configuration = ExperimentConfiguration.objects.get(
pk=exp_config_pk)
epv.save()
return JsonResponse({'success': True, 'experiment_param': epv.to_dict()})
# update Request
elif request_type == 'update' and pk:
epv = ExperimentParameterValue.objects.get(pk=pk)
form = ExperimentParameterValueForm(request.POST or None, instance=epv)
if form.is_valid():
form.save()
return JsonResponse({'success': True, 'experiment_param': epv.to_dict()})

return JsonResponse({
'success': False,
'message': form.errors
})
form = ExperimentParameterValueForm(request.POST or None, pk=pk)
if form.is_valid():
epv = form.save()
return JsonResponse({'success': True, 'experiment_param': epv.to_dict() })
return JsonResponse({'success': False, 'errors': form.errors })


@group_required(PermissionGroup.experimenter)
def update_round_param_value(request, pk):
""" FIXME: I'd like to see this method structured more like this, see also changes to RoundParameterValueForm
form = RoundParameterValueForm(request.POST or None, pk=pk)
if form.is_valid():
rpv = form.save()
return JsonResponse({'success': True, 'round_param': rpv.to_dict() })
return JsonResponse({'success': False, 'errors': form.errors })
"""

form = RoundParameterValueForm(request.POST or None, pk=pk)
if form.is_valid():
rpv = form.save()
Expand Down