<commit_before>import six, sys, os

from django.conf import settings
from django.http import HttpResponse
from django import http
from django.apps import apps
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.requests import RequestSite
from django.core.exceptions import ObjectDoesNotExist
from django.utils.translation import ugettext as _

from core.services import PDFGeneratorView
from v1.db_router import cfgov_apps
from .forms import HousingCounselorForm

if six.PY2:
    try:
        sys.path.append(os.environ.get('PDFREACTOR_LIB'))
        from PDFreactor import *
    except:
        PDFreactor = None


class HousingCounselorPDFView(PDFGeneratorView):
    def get_render_url(self):
        request = self.request

        form = HousingCounselorForm(request.GET)
        if form.is_valid():
            zip = form.cleaned_data['zip']
            api_url = 'hud-api-replace/%s.html/' % zip
            url = '%s://%s/%s' % (request.scheme, 'localhost', api_url)
            return url
        else:
            raise Exception(form.errors)

    def get(self, request):

        self.request = request
        if settings.DEBUG and PDFreactor is None:
            return HttpResponse("PDF Reactor is not configured, can not render %s" % self.get_render_url())

        return self.generate_pdf()

    def get_filename(self):
        return '%s.pdf' % self.request.GET['zip']

    def generate_pdf(self):
        url = self.get_render_url()

        if self.license is None:
            raise Exception("PDFGeneratorView requires a license")

        try:
            pdf_reactor = PDFreactor()
        except:
            raise PDFReactorNotConfigured('PDFreactor python library path needs to be configured.')

        pdf_reactor.setLogLevel(PDFreactor.LOG_LEVEL_WARN)
        pdf_reactor.setLicenseKey(str(self.license))
        pdf_reactor.setAuthor('CFPB')
        pdf_reactor.setAddTags(True)
        pdf_reactor.setAddBookmarks(True)

        result = pdf_reactor.renderDocumentFromURL(url)

        # Check if successful
        if result is None:
            # Not successful, return 500
            raise Exception('Error while rendering PDF: {}'.format(<commit_msg>when bad zip codes are encountered in production, *intentionally*<commit_after>import six, sys, os

from django.conf import settings
from django.http import HttpResponse
from django import http
from django.apps import apps
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.requests import RequestSite
from django.core.exceptions import ObjectDoesNotExist
from django.utils.translation import ugettext as _

from core.services import PDFGeneratorView
from v1.db_router import cfgov_apps
from .forms import HousingCounselorForm

if six.PY2:
    try:
        sys.path.append(os.environ.get('PDFREACTOR_LIB'))
        from PDFreactor import *
    except:
        PDFreactor = None

class InvalidZipException(Exception):
    pass

class HousingCounselorPDFView(PDFGeneratorView):
    def get_render_url(self):
        request = self.request

        form = HousingCounselorForm(request.GET)
        if form.is_valid():
            zip = form.cleaned_data['zip']
            api_url = 'hud-api-replace/%s.html/' % zip
            url = '%s://%s/%s' % (request.scheme, 'localhost', api_url)
            return url
        else:
            raise InvalidZipException(form.errors)

    def get(self, request):

        self.request = request
        if settings.DEBUG and PDFreactor is None:
            return HttpResponse("PDF Reactor is not configured, can not render %s" % self.get_render_url())

        try:
            return self.generate_pdf()
        except InvalidZipException:
            if not settings.DEBUG:
                return HttpResponse(status=500)
            raise

    def get_filename(self):
        return '%s.pdf' % self.request.GET['zip']

    def generate_pdf(self):
        url = self.get_render_url()

        if self.license is None:
            raise Exception("PDFGeneratorView requires a license")

        try:
            pdf_reactor = PDFreactor()
        except:
            raise PDFReactorNotConfigured('PDFreactor python library path needs to be configured.')

        pdf_reactor.setLogLevel(PDFreactor.LOG_LEVEL_WARN)
        pdf_reactor.setLicenseKey(str(self.license))
        pdf_reactor.setAuthor('CFPB')
        pdf_reactor.setAddTags(True)
        pdf_reactor.setAddBookmarks(True)

        result = pdf_reactor.renderDocumentFromURL(url)

        # Check if successful
        if result is None:
            # Not successful, return 500
            raise Exception('Error while rendering PDF: {}'.format(
{'fraction_non_alphanumeric': 0.08186264086753137, 'fraction_numerical': 0.0027641930682543057, 'mean_word_length': 2.7156398104265405, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1418943', 'n_tokens_mistral': 1335, 'n_tokens_neox': 1288, 'n_words': 345}