The choice of logic implementation code in the formation of instances from a csv file?

There is a task to parse a csv file and based on the obtained data to create instances in the database. Buck DRF at the front React.
The peculiarity is that the processing of the file is not completely hidden. The logic is this:
There is a button download file. The file is loaded, validinputs but once in the database, nothing was created. A window appears with a list of sparsely data (like a table) and in this window a new button confirm by pressing on which the request to the database goes.
What I did:
1. Created a class for the file upload (the Upload button)

class FileUploadView(APIView):
 parser_classes = ( MultiPartParser, FormParser)

 def put(self, request, format=None):
 if 'file' not in request.data:
 raise ParseError("Empty content")
 f = request.data['file']
 filename = f.name
 file_ext = filename[-4:]
 if file_ext == '.csv':
 file = default_storage.save(filename, f)
 r = csv_file_parser(file)
 status = 204
else:
 status = 406
 r = "File format error"
 return Response({'response': r}, status=status)


In the class function is called csv_file_parser the result of which is a json containing all the data Spasenie.

def csv_file_parser(file):

 with open(file) as csvfile:
 reader = csv.DictReader(csvfile)
 line_count = 1
 result_dict= {}
 for row in reader:
 for key, value in row.items():
 if not value:
 raise ParseError('Missing value in file. Check the {} line'.format(line_count))
 new = {line_count: row}
result_dict.update(new)
 line_count += 1
 return json.dumps(result_dict)


Working code (if you have ideas to optimize it - Wellcome :) ). What I can't figure out is how do I push the button to Confirm code. What to use for this?
April 7th 20 at 15:38
1 answer
April 7th 20 at 15:40
Solution
You now send Spasenie data from the Beck to the front. Well, build at the front of the form confirming this information. After clicking on Confirm send these data directly as a json again on the back. But on the other endpoint, which will take this data and make something in the database.

PS Instead of filename[-4:] file extension it is better to get it os.path.splitext(filename). Well, or check to do so if filename.endswith('.csv').
Seems logical, thank you. - Mose.Breitenberg commented on April 7th 20 at 15:43
Perhaps it would be logical at the front to parse csv, display for confirmation and after confirmation, send to back. Well or drive there - here the data, as described above. - earnestine.Smitham72 commented on April 7th 20 at 15:46

Find more questions by tags DjangoDjango Rest Framework