Opened 11 years ago

Closed 11 years ago

#5475 closed (fixed)

[patch] Luhn algorithm implementation should be added to Django

Reported by: Ben Slavin Owned by: Ben Slavin
Component: Internationalization Version: master
Severity: Keywords: sprintsept14
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


The Luhn algorithm is used for validating credit cards and numerous national ID numbers. Django should have a built-in implementation to enable localflavors to add functionality.

Factored out of #3961 to separate additions. Slight implementation details have been changed.

Re: discussion there, the Luhn algorithm works from right to left, so strings are used for processing. Quick (non-scientific) benchmarking shows this string-based method to be faster than other Python implementations available online.

Note: filed under 'internationalization', but arguments could be made for other categorizations.

Attachments (1)

ticket_5475__revision_6218.diff (2.4 KB) - added by Ben Slavin 11 years ago.
Added django.utils.checksums, which contains an implementation of the Luhn algorithm. Tests included.

Download all attachments as: .zip

Change History (8)

Changed 11 years ago by Ben Slavin

Added django.utils.checksums, which contains an implementation of the Luhn algorithm. Tests included.

comment:1 Changed 11 years ago by Ben Slavin

Status: newassigned
Triage Stage: UnreviewedAccepted

Accepting based on status of #3961.

comment:2 Changed 11 years ago by Adrian Holovaty

Resolution: wontfix
Status: assignedclosed

This belongs in something like Satchmo -- not in Django itself.

comment:3 Changed 11 years ago by russell@…

Adrian, how do you propose we implement validation for identity numbers (akin to social security numbers) in za localflavour?

comment:4 Changed 11 years ago by Ben Slavin

Resolution: wontfix
Status: closedreopened

Adrian, I appreciate your rejection of this based on the credit card aspect, but as Russell mentions, you neglect to consider the other applications.

Both Canada and South Africa use the Luhn algorithm for their national identification numbers. I'm fairly certain that other countries do as well. If the US localflavor gets a USSocialSecurityNumberField, I think it's fair to provide this functionality.

I'm reopening because:

  1. This implementation was suggested by Malcolm (not trying to drag him into a debate here).
  2. I don't think you understood the reason for the patch before closing it.
  3. #3961 (za localflavor) depends on this patch.

I won't reopen again if you still feel it should be closed, but I want to make sure appropriate attention is given.

comment:5 Changed 11 years ago by Adrian Holovaty

Ah, good point -- I was wrong in closing this. My apologies!

comment:6 Changed 11 years ago by Fredrik Lundh <fredrik@…>

Triage Stage: AcceptedReady for checkin

For the record, this is used for lots of stuff in Sweden, including "person numbers" (SSN), postgiro/bankgiro codes, OCR payment codes, etc.

(But I have to admit that I've never heard it called "Luhn" before -- the formal name here is "the modulus-10 method with weights 1 and 2". You'll learn something every day ;-)

Anyway, check it in already!

comment:7 Changed 11 years ago by Malcolm Tredinnick

Resolution: fixed
Status: reopenedclosed

(In [6569]) Fixed #5475 -- Added the Luhn check algorithm to django.utils.checksums so that
localflavors don't have to reimplement it each time. Thanks, hawkeye.

Note: See TracTickets for help on using tickets.
Back to Top