Exemplo n.º 1
0
def importView(request, kind):
  """
  Renders import.html, which offers user the option to upload an XML file.
  Uploading an XML file requires the correct password, and the file itself
  will be validated to ensure not only that it is an XML file, but also that
  it matches the pre-defined schema for the website. Type of import (clear or
  merge) is determined based on value of kind.

  @type request: string
  @param request: An HTML request
  @type kind: string
  @param kind: Type of merge that was selected

  @rtype: HTML page
  @return: Rendered version of import.html
  """

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

    if form.is_valid() and passwordValidate(form.cleaned_data['password'], kind):
      # process data
      upload = request.FILES['xmlfile']

      # validate XML file
      e_tree = validate(upload)
      if type(e_tree) == str:
        # XML file not valid
        return render(request, 'wcdb/import.html', {'form': form,
          'success': False, 'password': "", 'output': e_tree})

      # Valid XML file
      if e_tree :
        # Clear Database if Clear Import was selected and XML file was valid
        if kind == 'clear' :
          Crisis.objects.all().delete()
          Person.objects.all().delete()
          Org.objects.all().delete()
          Li.objects.all().delete()
          Relations.objects.all().delete()

        # Populate database
        populate_models(e_tree)

        return render(request, 'wcdb/import.html', {'form': form, 'success': "Uploaded successfully!", 'password': False})
  return render(request, 'wcdb/import.html', {'form': form, 'success': False, 'password': "******"})
	def test_validate1(self):
		f = open('wcdb/xml1.xml')
		self.assertEqual(type(f), file)
		self.assert_(type(validate(f)) == str)
	def test_validate2(self):
		f = open('wcdb/xml2.xm')
		self.assertEqual(type(f), file)
		self.assertEqual(validate(f), False)
	def test_validate0(self):
		f = open('wcdb/xml0.xml')
		self.assertEqual(type(f), file)
		self.assert_(validate(f) != False)