def source_new(request): """ Page with the form to create a new Source. """ # We can get here one of two ways: either we just got to the form # page, or we just submitted the form. If POST, we submitted; if # GET, we just got here. if request.method == 'POST': # A form bound to the POST data sourceForm = ImageSourceForm(request.POST) pointGenForm = PointGenForm(request.POST) annotationAreaForm = AnnotationAreaPercentsForm(request.POST) # is_valid() calls our ModelForm's clean() and checks validity source_form_is_valid = sourceForm.is_valid() point_gen_form_is_valid = pointGenForm.is_valid() annotation_area_form_is_valid = annotationAreaForm.is_valid() if source_form_is_valid and point_gen_form_is_valid and annotation_area_form_is_valid: # After calling a ModelForm's is_valid(), an instance is created. # We can get this instance and add a bit more to it before saving to the DB. newSource = sourceForm.instance newSource.default_point_generation_method = PointGen.args_to_db_format(**pointGenForm.cleaned_data) newSource.image_annotation_area = AnnotationAreaUtils.percentages_to_db_format(**annotationAreaForm.cleaned_data) newSource.labelset = LabelSet.getEmptyLabelset() newSource.save() # Make the user a source admin newSource.assign_role(request.user, Source.PermTypes.ADMIN.code) # Add a success message messages.success(request, 'Source successfully created.') # Redirect to the source's main page return HttpResponseRedirect(reverse('source_main', args=[newSource.id])) else: # Show the form again, with error message messages.error(request, 'Please correct the errors below.') else: # An unbound form (empty form) sourceForm = ImageSourceForm() pointGenForm = PointGenForm() annotationAreaForm = AnnotationAreaPercentsForm() # RequestContext needed for CSRF verification of POST form, # and to correctly get the path of the CSS file being used. return render_to_response('images/source_new.html', { 'sourceForm': sourceForm, 'pointGenForm': pointGenForm, 'annotationAreaForm': annotationAreaForm, }, context_instance=RequestContext(request) )
def source_edit(request, source_id): """ Edit a source: name, visibility, location keys, etc. """ source = get_object_or_404(Source, id=source_id) if request.method == 'POST': # Cancel cancel = request.POST.get('cancel', None) if cancel: messages.success(request, 'Edit cancelled.') return HttpResponseRedirect(reverse('source_main', args=[source_id])) # Submit sourceForm = ImageSourceForm(request.POST, instance=source) pointGenForm = PointGenForm(request.POST) annotationAreaForm = AnnotationAreaPercentsForm(request.POST) # Make sure is_valid() is called for all forms, so all forms are checked and # all relevant error messages appear. source_form_is_valid = sourceForm.is_valid() point_gen_form_is_valid = pointGenForm.is_valid() annotation_area_form_is_valid = annotationAreaForm.is_valid() if source_form_is_valid and point_gen_form_is_valid and annotation_area_form_is_valid: editedSource = sourceForm.instance editedSource.default_point_generation_method = PointGen.args_to_db_format(**pointGenForm.cleaned_data) editedSource.image_annotation_area = AnnotationAreaUtils.percentages_to_db_format(**annotationAreaForm.cleaned_data) editedSource.save() messages.success(request, 'Source successfully edited.') return HttpResponseRedirect(reverse('source_main', args=[source_id])) else: messages.error(request, 'Please correct the errors below.') else: # Just reached this form page sourceForm = ImageSourceForm(instance=source) pointGenForm = PointGenForm(source=source) annotationAreaForm = AnnotationAreaPercentsForm(source=source) return render_to_response('images/source_edit.html', { 'source': source, 'editSourceForm': sourceForm, 'pointGenForm': pointGenForm, 'annotationAreaForm': annotationAreaForm, }, context_instance=RequestContext(request) )