Browse Source

Misc fixes/improvements in CSV import from bank and matching

Alexandre Aubin 7 years ago
parent
commit
1192b2c094
2 changed files with 25 additions and 8 deletions
  1. 24 8
      coin/billing/management/commands/import_payments_from_csv.py
  2. 1 0
      requirements.txt

+ 24 - 8
coin/billing/management/commands/import_payments_from_csv.py

@@ -27,6 +27,8 @@ import logging
 import os
 import re
 
+import unidecode
+
 # Django specific imports
 from argparse import RawTextHelpFormatter
 from django.core.management.base import BaseCommand, CommandError
@@ -189,13 +191,14 @@ class Command(BaseCommand):
 
     def try_to_match_payment_with_members(self, payments):
 
-        members = Member.objects.filter(status="member")
+        #members = Member.objects.filter(status="member")
+        members = Member.objects.all()
 
         idregex = re.compile(ID_REGEX)
 
         for payment in payments:
 
-            payment_label = payment["label"]
+            payment_label = payment["label"].upper()
 
             # First, attempt to match the member ID
             idmatches = idregex.findall(payment_label)
@@ -211,11 +214,14 @@ class Command(BaseCommand):
             # Second, attempt to find the username
             usernamematch = None
             for member in members:
-                matches = re.compile(r"(?i)(\b|_)"+re.escape(member.username)+r"(\b|_)") \
+                username = self.flatten(member.username)
+                matches = re.compile(r"(?i)(\b|_)"+re.escape(username)+r"(\b|_)") \
                             .findall(payment_label)
+
                 # If not found, try next
                 if len(matches) == 0:
                     continue
+
                 # If we already had a match, abort the whole search because we
                 # have multiple usernames matched !
                 if usernamematch != None:
@@ -236,22 +242,30 @@ class Command(BaseCommand):
                 if member.last_name == "":
                     continue
 
-                matches = re.compile(r"(?i)(\b|_)"+re.escape(str(member.last_name))+r"(\b|_)") \
+                # "Flatten" accents in the last name... (probably the CSV
+                # don't contain 'special' chars like accents
+                member_last_name = self.flatten(member.last_name)
+
+                matches = re.compile(r"(?i)(\b|_)"+re.escape(member_last_name)+r"(\b|_)") \
                             .findall(payment_label)
+
                 # If not found, try next
                 if len(matches) == 0:
                     continue
+
                 # If this familyname was matched several time, abort the whole search
-                if len(matches) > 1:
-                    familynamematch = None
-                    break
+                #if len(matches) > 1:
+                #    print("Several matches ! Aborting !")
+                #    familynamematch = None
+                #    break
+
                 # If we already had a match, abort the whole search because we
                 # have multiple familynames matched !
                 if familynamematch != None:
                     familynamematch = None
                     break
 
-                familynamematch = str(member.last_name)
+                familynamematch = member_last_name
                 usernamematch = str(member.username)
 
             if familynamematch != None:
@@ -336,3 +350,5 @@ class Command(BaseCommand):
                                              date=new_payment["date"],
                                              member=member)
 
+    def flatten(self, some_string):
+        return unidecode.unidecode(some_string).upper()

+ 1 - 0
requirements.txt

@@ -16,3 +16,4 @@ feedparser
 six==1.10.0
 WeasyPrint==0.31
 freezegun==0.3.8
+unidecode