Skip to content

Code snippets

Atul Varma edited this page Apr 14, 2020 · 12 revisions

This page contains some code snippets that might be useful for one-off use. Since they're not part of the primary codebase, of course, they may not work anymore. Please feel free to delete or update them if that's the case.

PRs with code snippets

The "code snippet" label can be used to identify and search for pull requests that have code snippets.

RapidPro snippets

  • Migrating a custom field from RapidPro to a Django model - See the comment in #925.
  • Finding users dropped by follow-up campaigns - See #919.
  • Determining when a RapidPro contact was added to a contact group - Work on this was done in #902 but the code was never merged because it wasn't needed more than once.

Deleting all DocuSign envelopes

This is especially useful when working on the DocuSign envelope generation code: because we actually cache envelopes, it one actually has to re-generate PDF forms to generate a new envelope, which can be a bit of a pain, so going into python manage.py shell and deleting all envelopes from your development database can be useful:

from hpaction.models import DocusignEnvelope; DocusignEnvelope.objects.all().delete()

Finding users in a WoW portfolio

To find users in a portfolio that's rooted in a BBL (using Who Owns What's algorithm), see #944.

Caching derived data for onboarding

#401 contained a snippet we can run when we add a new derived data field for onboarding:

import os

if __name__ == '__main__':
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')

    import django
    django.setup()

    from onboarding.models import OnboardingInfo

    for info in OnboardingInfo.objects.filter(pad_bbl=''):
        print(f"Updating address metadata for {info.user}.")
        info.save()

Listing users who reported as NYCHA but don't live in NYCHA housing

SELECT onb.user_id
FROM onboarding_onboardinginfo as onb
WHERE onb.lease_type = 'NYCHA' AND onb.pad_bbl <> '' AND onb.pad_bbl NOT IN (
    SELECT pad_bbl from nycha_nychaproperty
)

Conversely, here's a query for users who didn't self-report as NYCHA but are actually in a NYCHA BBL:

SELECT onb.user_id, onb.lease_type
FROM onboarding_onboardinginfo as onb
WHERE onb.lease_type <> 'NYCHA' AND onb.pad_bbl <> '' AND onb.pad_bbl IN (
    SELECT pad_bbl from nycha_nychaproperty
)

Listing users who have logged in since sending their letter of complaint

SELECT u.username, u.last_login, lr.created_at
FROM users_justfixuser AS u, loc_letterrequest AS lr
WHERE u.id = lr.user_id AND u.last_login > lr.created_at;
Clone this wiki locally