Browse Source

Misc fixes in script to import payment from CSV

Alexandre Aubin 7 years ago
parent
commit
4b84cc9126
1 changed files with 20 additions and 19 deletions
  1. 20 19
      coin/billing/management/commands/import_payments_from_csv.py

+ 20 - 19
coin/billing/management/commands/import_payments_from_csv.py

@@ -25,7 +25,7 @@ DELIMITER=str(';')
 # The date format in the CSV
 DATE_FORMAT="%d/%m/%Y"
 # The default regex used to match the label of a payment with a member ID
-ID_REGEX=r"(?i)\bID[\s\-\_\/]*(\d+)\b"
+ID_REGEX=r"(?i)(\b|_)ID[\s\-\_\/]*(\d+)(\b|_)"
 
 
 
@@ -80,10 +80,10 @@ should run this command with --commit if you agree with the dry-run."""
         payments = self.convertCSVToDicts(self.cleanCSV(self.loadCSV(options["filename"])))
 
         # Dummy payments for test
-        payments = []
-        payments.append({ "date": "2017-03-02", "label":"foo ID 43 zob", "amount":30.0})
-        payments.append({ "date": "2017-03-14", "label":"foo JohnDoe zob", "amount":30.0})
-        payments.append({ "date": "2017-04-03", "label":"foo John Doe zob", "amount":30.0})
+        #payments = []
+        #payments.append({ "date": "2017-03-02", "label":"foo ID 43 zob", "amount":30.0})
+        #payments.append({ "date": "2017-03-14", "label":"foo JohnDoe zob", "amount":30.0})
+        #payments.append({ "date": "2017-04-03", "label":"foo John Doe zob", "amount":30.0})
 
         payments = self.tryToMatchPaymentWithMembers(payments)
         newPayments = self.filterAlreadyKnownPayments(payments)
@@ -107,7 +107,8 @@ should run this command with --commit if you agree with the dry-run."""
             return
 
         if not options["commit"]:
-            print "Use --commit to register these new payments"
+            print "Please carefully review the matches, then if everything \n" \
+                  "looks alright, use --commit to register these new payments."
         else:
             self.addNewPayments(newPayments)
 
@@ -139,9 +140,10 @@ should run this command with --commit if you agree with the dry-run."""
 
         for i, row in enumerate(data):
 
+            for j in range(len(row)):
+                row[j] = row[j].decode('utf-8')
+
             if len(row) < 4:
-                #logging.warning("Ignoring the following row (bad number of elements) :")
-                #logging.warning(str(row))
                 continue
 
             if not self.isDate(row[0]):
@@ -150,13 +152,12 @@ should run this command with --commit if you agree with the dry-run."""
                 continue
 
             if self.isMoneyAmount(row[2]):
-                #logging.warning("Ignoring the following row (not a payment from a member) :")
-                #logging.warning(str(row))
-                logging.warning("Ignoring row %s (not a payment from a member)" % str(i))
+                logging.warning("Ignoring row %s (not a payment)" % str(i))
+                logging.warning(str(row))
                 continue
 
             if not self.isMoneyAmount(row[3]):
-                logging.warning("Ignoring the following row (bad format for money amount in third colum) :")
+                logging.warning("Ignoring the following row (bad format for money amount in colun three) :")
                 logging.warning(str(row))
                 continue
 
@@ -164,8 +165,8 @@ should run this command with --commit if you agree with the dry-run."""
             row[0] = datetime.datetime.strptime(row[0], DATE_FORMAT).strftime("%Y-%m-%d")
 
             # Clean the label ...
-            row[1] = row[1].replace('\r', ' ')
-            row[1] = row[1].replace('\n', ' ')
+            row[4] = row[4].replace('\r', ' ')
+            row[4] = row[4].replace('\n', ' ')
 
             output.append(row)
 
@@ -180,7 +181,7 @@ should run this command with --commit if you agree with the dry-run."""
             payment = {}
 
             payment["date"] = row[0]
-            payment["label"] = row[1]
+            payment["label"] = row[4]
             payment["amount"] = float(row[3].replace(",","."))
 
             output.append(payment)
@@ -196,12 +197,12 @@ should run this command with --commit if you agree with the dry-run."""
 
         for payment in payments:
 
-            paymentLabel = str(payment["label"])
+            paymentLabel = payment["label"]
 
             # First, attempt to match the member ID
             idmatches = idregex.findall(paymentLabel)
             if len(idmatches) == 1:
-                i = int(idmatches[0])
+                i = int(idmatches[0][1])
                 memberMatches = [ member.username for member in members if member.pk==i ]
                 if len(memberMatches) == 1:
                     payment["memberMatched"] = memberMatches[0]
@@ -212,7 +213,7 @@ should run this command with --commit if you agree with the dry-run."""
             # Second, attempt to find the username
             usernamematch = None
             for member in members:
-                matches = re.compile(r"(?i)\b"+re.escape(member.username)+r"\b") \
+                matches = re.compile(r"(?i)(\b|_)"+re.escape(member.username)+r"(\b|_)") \
                             .findall(paymentLabel)
                 # If not found, try next
                 if len(matches) == 0:
@@ -234,7 +235,7 @@ should run this command with --commit if you agree with the dry-run."""
             # Third, attempt to match by family name
             familynamematch = None
             for member in members:
-                matches = re.compile(r"(?i)\b"+re.escape(str(member.last_name))+r"\b") \
+                matches = re.compile(r"(?i)(\b|_)"+re.escape(str(member.last_name))+r"(\b|_)") \
                             .findall(paymentLabel)
                 # If not found, try next
                 if len(matches) == 0: