import csv

from money.models import Movement


def parse_csv(raw_csv, parser, header_lines=0):
	reader = csv.reader(raw_csv, delimiter=',', quotechar='"')
	rows = []

	for row in reader:
		if reader.line_num > header_lines and row:
			rows.append(parser.parse_row(row))
	return rows


def import_movements(data, bank_account):
	rejected = []
	accepted = 0
	for row in data:
		obj, created = Movement.objects.get_or_create(
			bank_account=bank_account,
			description=row["description"],
			amount=row["amount"],
			date=row["date"],
		)
		if created:
			accepted += 1
		else:
			rejected.append(row)
	return accepted, rejected
import csv

from money.models import Movement


def parse_csv(raw_csv, parser, header_lines=0, reverse_order=False):
    reader = csv.reader(raw_csv, delimiter=',', quotechar='"')
    rows = []

    for row in reader:
        if reader.line_num > header_lines and row:
            rows.append(parser.parse_row(row))
    if reverse_order:
        rows.reverse()
    return rows


def import_movements(data, bank_account):
    rejected = []
    accepted = 0
    for row in data:
        obj, created = Movement.objects.get_or_create(
            bank_account=bank_account,
            description=row["description"],
            amount=row["amount"],
            date=row["date"],
        )
        if created:
            accepted += 1
        else:
            rejected.append(row)
    return accepted, rejected

Allow to reverse the order of the CSV for a proper reading
Allow to reverse the order of the CSV for a proper reading

  import csv
  
  from money.models import Movement
  
  
- def parse_csv(raw_csv, parser, header_lines=0):
+ def parse_csv(raw_csv, parser, header_lines=0, reverse_order=False):
- 	reader = csv.reader(raw_csv, delimiter=',', quotechar='"')
+     reader = csv.reader(raw_csv, delimiter=',', quotechar='"')
- 	rows = []
+     rows = []
  
- 	for row in reader:
+     for row in reader:
- 		if reader.line_num > header_lines and row:
+         if reader.line_num > header_lines and row:
- 			rows.append(parser.parse_row(row))
+             rows.append(parser.parse_row(row))
+     if reverse_order:
+         rows.reverse()
- 	return rows
+     return rows
  
  
  def import_movements(data, bank_account):
- 	rejected = []
+     rejected = []
- 	accepted = 0
+     accepted = 0
- 	for row in data:
+     for row in data:
- 		obj, created = Movement.objects.get_or_create(
+         obj, created = Movement.objects.get_or_create(
- 			bank_account=bank_account,
+             bank_account=bank_account,
- 			description=row["description"],
+             description=row["description"],
- 			amount=row["amount"],
- 			date=row["date"],
- 		)
- 		if created:
- 			accepted += 1
- 		else:
- 			rejected.append(row)
+             amount=row["amount"],
+             date=row["date"],
+         )
+         if created:
+             accepted += 1
+         else:
+             rejected.append(row)
- 	return accepted, rejected
+     return accepted, rejected
+ 
Allow to reverse the order of the CSV for a proper reading
## Code Before:
import csv

from money.models import Movement


def parse_csv(raw_csv, parser, header_lines=0):
	reader = csv.reader(raw_csv, delimiter=',', quotechar='"')
	rows = []

	for row in reader:
		if reader.line_num > header_lines and row:
			rows.append(parser.parse_row(row))
	return rows


def import_movements(data, bank_account):
	rejected = []
	accepted = 0
	for row in data:
		obj, created = Movement.objects.get_or_create(
			bank_account=bank_account,
			description=row["description"],
			amount=row["amount"],
			date=row["date"],
		)
		if created:
			accepted += 1
		else:
			rejected.append(row)
	return accepted, rejected
## Instruction:
Allow to reverse the order of the CSV for a proper reading
## Code After:
import csv

from money.models import Movement


def parse_csv(raw_csv, parser, header_lines=0, reverse_order=False):
    reader = csv.reader(raw_csv, delimiter=',', quotechar='"')
    rows = []

    for row in reader:
        if reader.line_num > header_lines and row:
            rows.append(parser.parse_row(row))
    if reverse_order:
        rows.reverse()
    return rows


def import_movements(data, bank_account):
    rejected = []
    accepted = 0
    for row in data:
        obj, created = Movement.objects.get_or_create(
            bank_account=bank_account,
            description=row["description"],
            amount=row["amount"],
            date=row["date"],
        )
        if created:
            accepted += 1
        else:
            rejected.append(row)
    return accepted, rejected
