Browse Source

Fix crash on invoice validation with PostgreSQL db

Invoice numbers were working great with SQLite; but PostrgeSQL does not accept
every PCRE regexp, so that's a patch to use postgresql compatible regexp for
invoice number validation.
Jocelyn Delalande 8 years ago
parent
commit
4273911f8c
2 changed files with 19 additions and 2 deletions
  1. 3 2
      coin/billing/models.py
  2. 16 0
      coin/utils.py

+ 3 - 2
coin/billing/models.py

@@ -17,7 +17,7 @@ from coin.offers.models import OfferSubscription
 from coin.members.models import Member
 from coin.html2pdf import render_as_pdf
 from coin.utils import private_files_storage, start_of_month, end_of_month, \
-                       disable_for_loaddata
+                       disable_for_loaddata, postgresql_regexp
 from coin.isp_database.context_processors import branding
 
 
@@ -87,7 +87,8 @@ class InvoiceQuerySet(models.QuerySet):
     def with_valid_number(self):
         """ Excludes previous numbering schemes or draft invoices
         """
-        return self.filter(number__regex=InvoiceNumber.RE_INVOICE_NUMBER.pattern)
+        return self.filter(number__regex=postgresql_regexp(
+            InvoiceNumber.RE_INVOICE_NUMBER))
 
 class Invoice(models.Model):
 

+ 16 - 0
coin/utils.py

@@ -171,6 +171,22 @@ def disable_for_loaddata(signal_handler):
     return wrapper
 
 
+def postgresql_regexp(regexp):
+    """ Make a PCRE regexp PostgreSQL compatible (kinda)
+
+    PostgreSQL forbids using capture-group names, this function removes them.
+    :param regexp: a PCRE regexp or pattern
+    :return a PostgreSQL regexp
+    """
+    try:
+        original_pattern = regexp.pattern
+    except AttributeError:
+        original_pattern = regexp
+
+    return re.sub(
+        r'\?P<.*?>', '', original_pattern)
+
+
 if __name__ == '__main__':
     # ldap_hash expects an unicode string
     print(ldap_hash(sys.argv[1].decode("utf-8")))