Advertisement
31ph4n70m

Untitled

Jul 24th, 2019
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. from django.views.decorators.csrf import csrf_exempt
  2. from rest_framework.views import APIView
  3. from rest_framework.exceptions import ParseError
  4. import io
  5. import csv
  6. from rest_framework.response import Response
  7.  
  8.  
  9. #header: es un diccionario con los mismos items que row
  10.  
  11. @method_decorator(csrf_exempt, name='dispatch')
  12. class MassiveUploadView(APIView):
  13.     parser_class = (FileUploadParser,)
  14.  
  15.     def post(self, request):
  16.         if 'file' not in request.data:
  17.             raise ParseError("Empty content")
  18.  
  19.         file = request.data['file']
  20.         file = file.read().decode('utf-8')
  21.         file = io.StringIO(file)
  22.         file_reader = csv.DictReader(file)
  23.         for row in file_reader:
  24.             new_row = row.copy()
  25.             for k, v in row.items():
  26.                 for k2, v2 in header.items():
  27.                     if k == k2:
  28.                         new_row[v2] = new_row.pop(k)
  29.             im = Inmueble(**row)
  30.             im.save()
  31.  
  32.         return Response(status=204)
  33.  
  34. -----------------------------------------------------------------------------
  35. <body>
  36. <div id="massive">
  37.     <h3>Massive upload</h3>
  38.     <form action="{% url 'muv' %}" enctype="multipart/form-data" id="mass_up_form" method="post">
  39.         Select a file: <input type="file" name="file"><br><br>
  40.         <input type="submit">
  41.     </form>
  42. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement