Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. regions_communes_upload(request):
  2.         template = 'regions_communes_upload.html'
  3.  
  4.         prompt = {
  5.             'order': 'Orden del Excel debe ser cprovin, region, cpoblac,commune'
  6.         }
  7.  
  8.         if request.method == "GET":
  9.             return render(request,template,prompt)
  10.  
  11.         rc_file = request.FILES['file']
  12.  
  13.         if not rc_file.name.endswith('.csv'):
  14.             messages.error(request,'Este no es un archivo CSV')
  15.  
  16.         data_set = rc_file.read().decode('UTF-8')
  17.         io_string = io.StringIO(data_set)
  18.         next(io_string)
  19.         for column in csv.reader(io_string, delimiter=',', quotechar="|"):
  20.             _, created = Region.objects.update_or_create(
  21.                 cprovin = column[0],
  22.                 name = column[1]
  23.                 ), temp = Commune.objects.update_or_create(
  24.                     cpoblac = column[2],
  25.                     name = column[3],
  26.                     region = column[0]
  27.                 )
  28.         context = {}
  29.  
  30.         return render(request,template, context)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement