Package: django-charitychecker
django-charitycheckerA django app for verifying U.S. nonprofits by their EIN number. For a
general python module with similar functionality, check outcharitycheck.Visit thegithub.Installation:From pip:pip install django-charitycheckerIn your settings.py:INSTALLED_APPS=(...charitychecker...)Note that if you want django-charitychecker to work properly, you’ll
need to periodically fetch data from the IRS to update your local
database.Useage:django-charitychecker only checks U.S. based nonprofits, this means that
it will not verify foreign nonprofits registered with the IRS (there are
a few, but the data entry format for these organizations is inconsistent
and so can’t be machine parsed easily).django-charitychecker makes the following functions and models public:Public Objects and MethodsModelscharitychecker.IRSNonprofitDataa model representing a nonprofit with the following fields:AttributesIRSNonprofitData.ein: a string representing the nonprofit’s EIN
number.IRSNonprofitData.name: a string representing the name of the
Nonprofit as published in IRS Publication 78.IRSNonprofitData.city: a string representing the city in which
the nonprofit is incorporated.IRSNonprofitData.state: a string representing the state in which
the nonprofit is incorporated.IRSNonprofitData.country: a string representing the country in
which the nonprofit is incorporated.IRSNonprofitData.deductability_code: the tax deductability code
of the nonprofit organization. See thecharitycheckREADME
for more information on deductability codes.Of course, you can retrieve a nonprofit’s info using their EIN byIRSNonprofitData.objects.get(ein="someein string").MethodsIRSNonprofitData.verify_nonprofit(ein, name=None, city=None, state=None, country=None, deductability_code=None):
return true if there is a nonprofit in the charitychecker database
with information matching the information provided to the function as
arguments, return false otherwise.IRSNonprofitData.get_deductability_code(ein, name=None, city=None, state=None, country=None):
if a nonprofit is found in the charitychecker database with
information matching the information provided as arguments to the
function, then return that nonprofit’s deductability code, otherwise
return the empty string.Utilitiesignore_blank_spaceA function which takes an iterator (it’s meant for file like objects but
should take any iterator returning strings), and returns that iterator
skipping all blank lines and stripping all white space from the end of
any line.open_zip_from_urlA context manager taking two arguments,zip_urlandfile_name.
The context manager downloads the file fromzip_url, attempts to
unzip and then return the file at the pathfile_namefrom the
downloaded zip archive. It does all of this in memory without writing to
disk.irs_nonprofit_data_context_managerA context manager for dealing with the IRS Publication 78 data. It
automatically downloads, unzips, and opens the file from the IRS
website, closing the resources when finished. It skips anyFORGNregistered nonprofits, and returns all of the nonprofit data strings as
a generator. Use is as you would any context manager:with IRSNonprofitDataContextManager() as irs_data:
    # code using irs_data as a file object
    ....The Publication 78 file is a series of lines of the formein|name|city|state|country|deductability_code.update_database_from_fileA function that takesfile_manager,convert_line,pk_field,
andmodelarguments, then uses a file to update in bulk your
application’s database. Specifically, it takes:file_manager: a context manager which returns a generator
yielding strings. Conceptually, we think of this iterator as a file
like object, but any generator should work.convert_line: a function whose input is a line yielded byfile_managerand that outputs a dictionary mapping keys which are
field names ofmodeland values which will be used to instantiate
those field names.pk_field: The name field which is defined to be the primary key
onmodel. This field should not be anAutoFieldfor the
following reasons: 1.) bulk updating commands in Django do not call
the save method on the model, and thus do not setAutoFieldprimary keys, 2.) if you are updating your database from some
third-party source data, you want to be able to identify each line in
the third-party data uniquely, thus some primary-key like
value/unique identifier should already exist in your source data.
Using anAutoFieldinstead would mean that the function would
have no way of distinguishing new data and old data that’s been
updated.model: the model you want to update.Note! This function will delete any data that is in your database and
not present in the source file provided byfile_manager.update_charitychecker_dataA function that, when called, downloads a fresh copy of the IRS
Publication 78 data, unzips it, and uses it to update the charitychecker
database. It accepts no arguments.Management Commandsupdate_charitychecker_dataA command that downloads a new copy of the IRS Publication 78 data,
unzips it, and uses the data to update the charitychecker database.This command takes no arguments, and only the default django command
options. Run it by typing into the command prompt:python manage.py update_charitychecker_dataOf course, you can only run the command after charitychecker is
installed into your project’ssettings.pyfile’sINSTALLED_APPS,
and you’ve runpython manage.py syncdb. This command could take a
long time to finish, because it checks that your entire nonprofit
database (800,000+ rows) is up to date.TestingTest the app as you would any other Django app. In a project with
charitychecker installed run:python manage.py test charitycheckerNote that since the test suite tests downloading and unzipping the data
from the IRS, the test suite can take a minute or so, if you want to
speed up the tests simply skip the tests usingirs_nonprofit_data_context_managerand the test suite should run
much faster.ContributingPull requests are welcome. Also check outcharitycheckfor a
general python equivalent of this package if you are interested in
contributing.Suggestions for contributing:python 3 supportincorporate this module into a larger family of modules/packages
dealing with nonprofit data (but of course, following the UNIX
philosophy is really important here. Do one thing and do it well!)writing tests for theopen_zip_from_urlandupdate_database_from_filefunctions which currently are only
tested indirectly by the tests onirs_nonprofit_data_context_managerandupdate_charitychecker_data, which are both wrappers for these
more general functions.add tests for the newly added string methods on the IRSNonprofitData
model.AuthorsAuthored by Nicholas A. Lourie. You can contact him atdeveloper.nick@kozbox.com.