Ejemplo n.º 1
0
    def setUp(self):
        self.emails = ["*****@*****.**", "*****@*****.**"]

        self.agency = Agency(name='Agency With Offices',
                             zip_code=20404,
                             emails=self.emails)
        self.agency.save()

        self.office = Office(agency=self.agency,
                             name='Office 1',
                             zip_code=20404,
                             emails=self.emails)
        self.office.save()

        self.office2 = Office(agency=self.agency,
                              name='Office 2',
                              zip_code=20404,
                              emails=self.emails)
        self.office2.save()

        self.agency2 = Agency(name='Agency Without Offices',
                              zip_code=20009,
                              emails=self.emails)
        self.agency2.save()

        self.requester = Requester.objects.create(first_name='Alice',
                                                  last_name='Bobson',
                                                  email='*****@*****.**')
        self.request = FOIARequest.objects.create(
            requester=self.requester,
            office=self.office,
            date_end=date.today(),
            request_body='All the cheese')
Ejemplo n.º 2
0
    def setUp(self):
        self.emails = ["*****@*****.**", "*****@*****.**"]

        self.agency = Agency(
            name='Agency With Offices', zip_code=20404, emails=self.emails)
        self.agency.save()

        self.office = Office(
            agency=self.agency, name='Office 1', zip_code=20404,
            emails=self.emails)
        self.office.save()

        self.office2 = Office(
            agency=self.agency, name='Office 2', zip_code=20404,
            emails=self.emails)
        self.office2.save()

        self.agency2 = Agency(
            name='Agency Without Offices', zip_code=20009, emails=self.emails)
        self.agency2.save()

        self.requester = Requester.objects.create(
            first_name='Alice', last_name='Bobson', email='*****@*****.**')
        self.request = FOIARequest.objects.create(
            requester=self.requester, office=self.office,
            date_end=date.today(), request_body='All the cheese')
Ejemplo n.º 3
0
    def test_agency_save(self):
        agency = Agency(name='Department of Transportation')
        agency.save()

        retrieved = Agency.objects.get(pk=agency.pk)
        self.assertEqual(
            'Agency: Department of Transportation', str(retrieved))
        self.assertEqual(retrieved.slug, 'department-of-transportation')
Ejemplo n.º 4
0
    def test_office_save(self):
        agency = Agency(name='Department of Commerce')
        agency.save()

        office = Office(name='Commerce Is FUNdamental', agency=agency)
        office.save()
        retrieved = Office.objects.get(pk=office.pk)

        self.assertEqual(retrieved.office_slug, 'commerce-is-fundamental')
        self.assertEqual(
            retrieved.slug, 'department-of-commerce--commerce-is-fundamental')
Ejemplo n.º 5
0
 def setUp(self):
     self.emails = ["*****@*****.**", "*****@*****.**"]
     self.agency = Agency(name='Agency With Offices',
                          zip_code=20404,
                          emails=self.emails)
     self.agency.save()
     self.office = Office(agency=self.agency,
                          name='Office 1',
                          zip_code=20404,
                          emails=self.emails)
     self.office.save()
Ejemplo n.º 6
0
    def test_no_email(self):
        """ For agencies without their own request form, and without an email
        address, do not display a FOIA request form. """

        a = Agency(name="Broadcasting Board of Governors", slug="brodcasting")
        a.save()

        response = self.client.get(
            reverse('contact_landing', args=['broadcasting']))
        self.assertTrue(200, response.status_code)
        content = response.content.decode('utf-8')
        self.assertTrue('Request online' not in content)
Ejemplo n.º 7
0
    def test_no_email(self):
        """ For agencies without their own request form, and without an email
        address, do not display a FOIA request form. """

        a = Agency(name="Broadcasting Board of Governors", slug="brodcasting")
        a.save()

        response = self.client.get(
            reverse('contact_landing', args=['broadcasting']))
        self.assertTrue(200, response.status_code)
        content = response.content.decode('utf-8')
        self.assertTrue('Request online' not in content)
Ejemplo n.º 8
0
def load_data(data):
    """
    Loads data from each yaml file into the database.
    """

    # Load the agency
    name = data['name']
    slug = Agency.slug_for(name)
    a, created = Agency.objects.get_or_create(slug=slug, name=name)

    # Load the agency-specific values
    load_agency_fields(a, data)

    #   If the agency only has a single department the contactable fields
    if len(data['departments']) == 1:
        dept_rec = data['departments'][0]
        contactable_fields(a, dept_rec)

    a.save()
    add_request_time_statistics(data, a)

    # Load agency offices
    if len(data['departments']) > 1:
        for dept_rec in data['departments']:

            # If top-level=True office is saved as agency
            if dept_rec.get('top_level'):
                sub_agency_name = dept_rec['name']
                sub_agency_slug = Agency.slug_for(sub_agency_name)
                sub_agency, created = Agency.objects.get_or_create(
                    slug=sub_agency_slug, name=sub_agency_name)
                sub_agency.parent = a
                load_agency_fields(sub_agency, dept_rec)
                contactable_fields(sub_agency, dept_rec)
                sub_agency.save()
                add_request_time_statistics(dept_rec, sub_agency)

            else:
                # Just an office
                office_name = dept_rec['name']
                office_slug = Office.slug_for(office_name)
                full_slug = slug + '--' + office_slug

                o, created = Office.objects.get_or_create(
                    agency=a, slug=full_slug)

                o.office_slug = office_slug
                o.name = office_name
                contactable_fields(o, dept_rec)
                o.save()
                add_request_time_statistics(dept_rec, a, o)
def load_data(data):
    """
    Loads data from each yaml file into the database.
    """

    # Load the agency
    name = data['name']
    slug = Agency.slug_for(name)
    a, created = Agency.objects.get_or_create(slug=slug, name=name)

    # Load the agency-specific values
    load_agency_fields(a, data)

    #   If the agency only has a single department the contactable fields
    if len(data['departments']) == 1:
        dept_rec = data['departments'][0]
        contactable_fields(a, dept_rec)

    a.save()
    add_request_time_statistics(data, a)

    # Load agency offices
    if len(data['departments']) > 1:
        for dept_rec in data['departments']:

            # If top-level=True office is saved as agency
            if dept_rec.get('top_level'):
                sub_agency_name = dept_rec['name']
                sub_agency_slug = Agency.slug_for(sub_agency_name)
                sub_agency, created = Agency.objects.get_or_create(
                    slug=sub_agency_slug, name=sub_agency_name)
                sub_agency.parent = a
                load_agency_fields(sub_agency, dept_rec)
                contactable_fields(sub_agency, dept_rec)
                sub_agency.save()
                add_request_time_statistics(dept_rec, sub_agency)

            else:
                # Just an office
                office_name = dept_rec['name']
                office_slug = Office.slug_for(office_name)
                full_slug = slug + '--' + office_slug

                o, created = Office.objects.get_or_create(agency=a,
                                                          slug=full_slug)

                o.office_slug = office_slug
                o.name = office_name
                contactable_fields(o, dept_rec)
                o.save()
                add_request_time_statistics(dept_rec, a, o)
Ejemplo n.º 10
0
    def setUp(self):
        self.emails = ["*****@*****.**", "*****@*****.**"]
        self.agency = Agency(name='Agency With Offices',
                             zip_code=20404,
                             emails=self.emails)
        self.agency.save()
        self.office = Office(agency=self.agency,
                             name='Office 1',
                             zip_code=20404,
                             emails=self.emails)
        self.office.save()

        # Turn off SHOW_WEBFORM
        settings.SHOW_WEBFORM = False
        self.reload_urls()
Ejemplo n.º 11
0
    def test_statstest_save(self):
        """ Confirm obj saves and validate number handling. """
        agency = Agency(name='Department of Homeland Security')
        agency.save()

        stats = Stats(
            agency=agency,
            year=2014,
            stat_type='S',
            median=1.26,
        )
        stats.save()

        retrieved = Stats.objects.get(pk=stats.pk)
        self.assertEqual(retrieved.median, 1.26)
        self.assertEqual(retrieved.less_than_one, False)
Ejemplo n.º 12
0
class RequestSwitchTests(TestCase):
    fixtures = ['agencies_test.json', 'offices_test.json']

    def reload_urls(self):
        """ The flag the turns of the requests API is in urls.py, so we have to
        reload the URLs in Django to make tests. This reloads those tests. """

        if settings.ROOT_URLCONF in sys.modules:
            reload(sys.modules[settings.ROOT_URLCONF])
            import_module(settings.ROOT_URLCONF)

    def setUp(self):
        self.emails = ["*****@*****.**", "*****@*****.**"]
        self.agency = Agency(name='Agency With Offices',
                             zip_code=20404,
                             emails=self.emails)
        self.agency.save()
        self.office = Office(agency=self.agency,
                             name='Office 1',
                             zip_code=20404,
                             emails=self.emails)
        self.office.save()

        # Turn off SHOW_WEBFORM
        settings.SHOW_WEBFORM = False
        self.reload_urls()

    def tearDown(self):
        # Turn on SHOW_WEBFORM
        settings.SHOW_WEBFORM = True
        self.reload_urls()

    def test_api_off(self):
        """ The request API should not the available. """

        requester_email = "*****@*****.**"
        response = self.client.post("/api/request/",
                                    content_type='application/json',
                                    data=json.dumps({
                                        'agency': self.agency.slug,
                                        'email': requester_email,
                                        'first_name': "FOIA",
                                        'last_name': "Requester",
                                        'body': "A new request"
                                    }))
        self.assertEqual(404, response.status_code)
Ejemplo n.º 13
0
    def test_create_request_no_email(self):
        """ Try to submit a request to an agency that has no online form, and
        no email address. This should fail with the appropriate HTTP status
        code. """

        a = Agency(name="Broadcasting Board of Governors", slug="broadcasting")
        a.save()

        c = Client()

        data = {
            'agency': 'broadcasting',
            'first_name': 'Joe',
            'last_name': 'Public',
            'email': '*****@*****.**',
            'body': 'I want documents about palm trees.'
        }
        data_string = json.dumps(data)
        response = c.post('/api/request/',
                          content_type="application.json",
                          data=data_string)
        self.assertEqual(400, response.status_code)
Ejemplo n.º 14
0
    def test_agency_preparer(self):
        """ Test the preparer that deals with just the unique Agency fields.
        """

        agency = Agency(name='agency-name',
                        description='agency-description',
                        abbreviation='AN',
                        slug='agency-slug')
        fields_preparer = agency_preparer()
        ap = fields_preparer.prepare(agency)
        self.assertEqual('agency-name', ap['name'])
        self.assertEqual('agency-description', ap['description'])
        self.assertEqual('AN', ap['abbreviation'])
        self.assertEqual('agency-slug', ap['slug'])
Ejemplo n.º 15
0
 def test_common_requests_field(self):
     """Verify that we can treat a JSONField as a list"""
     agency = Agency(name='Dr. Suess',
                     common_requests=['Thing one', 'Thing two', 'Red Fish'])
     agency.save()
     # Serialized to the DB and back again
     retrieved = Agency.objects.get(pk=agency.pk)
     self.assertEqual(retrieved.common_requests,
                      ['Thing one', 'Thing two', 'Red Fish'])
     agency.delete()
Ejemplo n.º 16
0
class RequestFormTests(SimpleTestCase):
    def setUp(self):
        self.emails = ["*****@*****.**", "*****@*****.**"]

        self.agency = Agency(name='Agency With Offices',
                             zip_code=20404,
                             emails=self.emails)
        self.agency.save()

        self.office = Office(agency=self.agency,
                             name='Office 1',
                             zip_code=20404,
                             emails=self.emails)
        self.office.save()

        self.office2 = Office(agency=self.agency,
                              name='Office 2',
                              zip_code=20404,
                              emails=self.emails)
        self.office2.save()

        self.agency2 = Agency(name='Agency Without Offices',
                              zip_code=20009,
                              emails=self.emails)
        self.agency2.save()

        self.requester = Requester.objects.create(first_name='Alice',
                                                  last_name='Bobson',
                                                  email='*****@*****.**')
        self.request = FOIARequest.objects.create(
            requester=self.requester,
            office=self.office,
            date_end=date.today(),
            request_body='All the cheese')

    # destroy them all
    def tearDown(self):
        for model in [FOIARequest, Requester, Office, Agency]:
            model.objects.all().delete()

    def test_request_form_successful(self):
        """The agency name should be present in the request form"""
        response = self.client.get(
            reverse('form', kwargs={'slug': self.agency.slug}))
        self.assertContains(response, self.agency.name)

    def test_request_form_404(self):
        """Should get a 404 if requesting an agency that doesn't exist"""
        response = self.client.get(
            reverse('form', kwargs={'slug': 'does-not-exist'}))
        self.assertEqual(404, response.status_code)

    def test_contact_landing_404(self):
        """Verify that non-existing agency/offices cause 404s"""
        response = self.client.get(
            reverse('contact_landing', kwargs={'slug': 'sssss'}))
        self.assertEqual(response.status_code, 404)
        response = self.client.get(
            reverse('contact_landing', kwargs={'slug': 'sss--ss'}))
        self.assertEqual(response.status_code, 404)

    def test_contact_landing_success(self):
        """If loading an agency or a top-level office, we should see agency
        name. If an office, we should not see peer offices."""

        list_fingerprint = "Make your FOIA request directly"
        list_fingerprint += " to the most relevant group or component"

        response = self.client.get(
            reverse('contact_landing', kwargs={'slug': self.agency.slug}))
        self.assertContains(response, self.agency.name)
        self.assertContains(response, self.office.name)
        self.assertContains(response, self.office2.name)
        self.assertContains(response, list_fingerprint)

        response = self.client.get(
            reverse('contact_landing', kwargs={'slug': self.office.slug}))
        self.assertContains(response, self.agency.name)
        self.assertContains(response, self.office.name)
        self.assertNotContains(response, self.office2.name)
        self.assertNotContains(response, list_fingerprint)

        response = self.client.get(
            reverse('contact_landing', kwargs={'slug': self.agency2.slug}))
        self.assertContains(response, self.agency2.name)
        self.assertNotContains(response, self.office.name)
        self.assertNotContains(response, self.office2.name)
        self.assertNotContains(response, list_fingerprint)

    def test_about(self):
        """The /about/ page should load without errors."""
        response = self.client.get(reverse('about'))
        self.assertEqual(response.status_code, 200)
Ejemplo n.º 17
0
class RequestFormTests(SimpleTestCase):
    def setUp(self):
        self.emails = ["*****@*****.**", "*****@*****.**"]

        self.agency = Agency(
            name='Agency With Offices', zip_code=20404, emails=self.emails)
        self.agency.save()

        self.office = Office(
            agency=self.agency, name='Office 1', zip_code=20404,
            emails=self.emails)
        self.office.save()

        self.office2 = Office(
            agency=self.agency, name='Office 2', zip_code=20404,
            emails=self.emails)
        self.office2.save()

        self.agency2 = Agency(
            name='Agency Without Offices', zip_code=20009, emails=self.emails)
        self.agency2.save()

        self.requester = Requester.objects.create(
            first_name='Alice', last_name='Bobson', email='*****@*****.**')
        self.request = FOIARequest.objects.create(
            requester=self.requester, office=self.office,
            date_end=date.today(), request_body='All the cheese')

    # destroy them all
    def tearDown(self):
        for model in [FOIARequest, Requester, Office, Agency]:
            model.objects.all().delete()

    def test_request_form_successful(self):
        """The agency name should be present in the request form"""
        response = self.client.get(reverse(
            'form', kwargs={'slug': self.agency.slug}))
        self.assertContains(response, self.agency.name)

    def test_request_form_404(self):
        """Should get a 404 if requesting an agency that doesn't exist"""
        response = self.client.get(reverse(
            'form', kwargs={'slug': 'does-not-exist'}))
        self.assertEqual(404, response.status_code)

    def test_request_success(self):
        """Request should be retrieved and displayed"""
        response = self.client.get(reverse(
            'success', kwargs={'id': self.request.id}))
        self.assertContains(response, self.requester.email)
        self.assertContains(response, self.agency.name)

    def test_request_success_404(self):
        """Should get a 404 if trying to get a success page for a request
        which doesn't exist"""
        response = self.client.get(reverse(
            'success', kwargs={'id': 9999999999}))
        self.assertEqual(404, response.status_code)

    def test_contact_landing_404(self):
        """Verify that non-existing agency/offices cause 404s"""
        response = self.client.get(reverse(
            'contact_landing', kwargs={'slug': 'sssss'}))
        self.assertEqual(response.status_code, 404)
        response = self.client.get(
            reverse('contact_landing', kwargs={'slug': 'sss--ss'}))
        self.assertEqual(response.status_code, 404)

    def test_contact_landing_success(self):
        """If loading an agency or a top-level office, we should see agency
        name. If an office, we should not see peer offices."""

        list_fingerprint = "Make your FOIA request directly"
        list_fingerprint += " to the most relevant group or component"

        response = self.client.get(reverse(
            'contact_landing', kwargs={'slug': self.agency.slug}))
        self.assertContains(response, self.agency.name)
        self.assertContains(response, self.office.name)
        self.assertContains(response, self.office2.name)
        self.assertContains(response, list_fingerprint)

        response = self.client.get(reverse(
            'contact_landing', kwargs={'slug': self.office.slug}))
        self.assertContains(response, self.agency.name)
        self.assertContains(response, self.office.name)
        self.assertNotContains(response, self.office2.name)
        self.assertNotContains(response, list_fingerprint)

        response = self.client.get(reverse(
            'contact_landing', kwargs={'slug': self.agency2.slug}))
        self.assertContains(response, self.agency2.name)
        self.assertNotContains(response, self.office.name)
        self.assertNotContains(response, self.office2.name)
        self.assertNotContains(response, list_fingerprint)

    def test_learn(self):
        """The /learn/ page should load without errors."""
        response = self.client.get(reverse('learn'))
        self.assertEqual(response.status_code, 200)

    def test_about(self):
        """The /about/ page should load without errors."""
        response = self.client.get(reverse('about'))
        self.assertEqual(response.status_code, 200)
Ejemplo n.º 18
0
def process_yamls(folder):

    # only load yaml files
    for item in iglob(folder + "/*.yaml"):
        data_file = os.path.join(folder, item)
        data = yaml.load(open(data_file))

        # Agencies
        name = data['name']
        slug = Agency.slug_for(name)

        a, created = Agency.objects.get_or_create(slug=slug, name=name)

        a.abbreviation = data['abbreviation']
        a.description = data.get('description')
        a.keywords = data.get('keywords')
        a.common_requests = data.get('common_requests', [])
        a.no_records_about = data.get('no_records_about', [])

        #   Only has a single, main branch/office
        if len(data['departments']) == 1:
            dept_rec = data['departments'][0]
            contactable_fields(a, dept_rec)

        a.save()
        add_request_time_statistics(data, a)

        # Offices
        if len(data['departments']) > 1:
            for dept_rec in data['departments']:
                if dept_rec.get('top_level'):
                    # This is actually an agency
                    sub_agency_name = dept_rec['name']
                    sub_agency_slug = Agency.slug_for(sub_agency_name)

                    sub_agency, created = Agency.objects.get_or_create(
                        slug=sub_agency_slug, name=sub_agency_name)
                    sub_agency.parent = a

                    abbreviation = build_abbreviation(sub_agency_name)
                    sub_agency.abbreviation = abbreviation
                    sub_agency.description = dept_rec.get('description')
                    sub_agency.keyword = dept_rec.get('keywords')
                    sub_agency.common_requests = dept_rec.get(
                        'common_requests', [])
                    sub_agency.no_records_about = dept_rec.get(
                        'no_records_about', [])
                    contactable_fields(sub_agency, dept_rec)
                    sub_agency.save()
                    add_request_time_statistics(dept_rec, sub_agency)
                else:
                    # Just an office
                    office_name = dept_rec['name']
                    office_slug = Office.slug_for(office_name)
                    full_slug = slug + '--' + office_slug

                    o, created = Office.objects.get_or_create(
                        agency=a, slug=full_slug)

                    o.office_slug = office_slug
                    o.name = office_name
                    contactable_fields(o, dept_rec)
                    o.save()
                    add_request_time_statistics(dept_rec, a, o)
Ejemplo n.º 19
0
class FOIARequestTests(TestCase):
    fixtures = ['agencies_test.json', 'offices_test.json']

    def setUp(self):
        self.emails = ["*****@*****.**", "*****@*****.**"]
        self.agency = Agency(name='Agency With Offices',
                             zip_code=20404,
                             emails=self.emails)
        self.agency.save()
        self.office = Office(agency=self.agency,
                             name='Office 1',
                             zip_code=20404,
                             emails=self.emails)
        self.office.save()

    def test_create_request(self):
        """ Submit a simple, minimal FOIA request to the API and expect it to
        succeed.  """

        c = Client()

        data = {
            'agency': 'department-of-commerce',
            'first_name': 'Joe',
            'last_name': 'Public',
            'email': '*****@*****.**',
            'body': 'I want documents about palm trees.'
        }

        data_string = json.dumps(data)
        response = c.post('/api/request/',
                          content_type="application/json",
                          data=data_string)
        self.assertEqual(201, response.status_code)

    def test_create_request_no_email(self):
        """ Try to submit a request to an agency that has no online form, and
        no email address. This should fail with the appropriate HTTP status
        code. """

        a = Agency(name="Broadcasting Board of Governors", slug="broadcasting")
        a.save()

        c = Client()

        data = {
            'agency': 'broadcasting',
            'first_name': 'Joe',
            'last_name': 'Public',
            'email': '*****@*****.**',
            'body': 'I want documents about palm trees.'
        }
        data_string = json.dumps(data)
        response = c.post('/api/request/',
                          content_type="application.json",
                          data=data_string)
        self.assertEqual(400, response.status_code)

    def test_submit_request_to_agency(self):
        requester_email = "*****@*****.**"
        self.assertEqual(0,
                         len(Requester.objects.filter(email=requester_email)))

        response = self.client.post("/api/request/",
                                    content_type='application/json',
                                    data=json.dumps({
                                        'agency': self.agency.slug,
                                        'email': requester_email,
                                        'first_name': "FOIA",
                                        'last_name': "Requester",
                                        'body': "A new request",
                                    }))

        self.assertEqual(201, response.status_code, response.content)
        data = helpers.json_from(response)
        self.assertTrue(data.get('tracking_id') is not None)
        self.assertEqual('O', data.get('status'))

        request_id = data['tracking_id']
        foia_request = FOIARequest.objects.get(pk=request_id)
        self.assertTrue(foia_request is not None)
        self.assertEqual(foia_request.agency, self.agency)
        self.assertEqual(1,
                         len(Requester.objects.filter(email=requester_email)))
        self.assertEqual(requester_email, foia_request.requester.email)

    def test_submit_request_to_office(self):
        requester_email = "*****@*****.**"
        self.assertEqual(0,
                         len(Requester.objects.filter(email=requester_email)))

        response = self.client.post("/api/request/",
                                    content_type='application/json',
                                    data=json.dumps({
                                        'agency':
                                        self.office.agency.slug,
                                        'office':
                                        self.office.office_slug,
                                        'email':
                                        requester_email,
                                        'first_name':
                                        "FOIA",
                                        'last_name':
                                        "Requester",
                                        'body':
                                        "A new request",
                                    }))

        self.assertEqual(201, response.status_code, response.content)
        data = helpers.json_from(response)
        self.assertTrue(data.get('tracking_id') is not None)
        self.assertEqual('O', data.get('status'))

        request_id = data['tracking_id']
        foia_request = FOIARequest.objects.get(pk=request_id)
        self.assertTrue(foia_request is not None)
        self.assertEqual(foia_request.office, self.office)
        self.assertEqual(1,
                         len(Requester.objects.filter(email=requester_email)))
        self.assertEqual(requester_email, foia_request.requester.email)

    def test_submit_invalid_request(self):
        requester_email = "*****@*****.**"
        self.assertEqual(0,
                         len(Requester.objects.filter(email=requester_email)))

        response = self.client.post("/api/request/",
                                    content_type='application/json',
                                    data=json.dumps({
                                        'agency': "not-a-valid-agency",
                                        'email': requester_email,
                                        'first_name': "FOIA",
                                        'last_name': "Requester",
                                        'body': "A new request",
                                    }))

        self.assertEqual(404, response.status_code)
        self.assertEqual(0,
                         len(Requester.objects.filter(email=requester_email)))
def load_data(data):
    """
    Loads data from each yaml file into the database.
    """

    # Agencies
    name = data['name']
    slug = Agency.slug_for(name)

    a, created = Agency.objects.get_or_create(slug=slug, name=name)

    a.abbreviation = data['abbreviation']
    a.description = data.get('description')
    a.keywords = data.get('keywords')
    a.common_requests = data.get('common_requests', [])
    a.no_records_about = data.get('no_records_about', [])

    #   Only has a single, main branch/office
    if len(data['departments']) == 1:
        dept_rec = data['departments'][0]
        contactable_fields(a, dept_rec)

    a.save()
    add_request_time_statistics(data, a)

    # Offices
    if len(data['departments']) > 1:
        for dept_rec in data['departments']:
            if dept_rec.get('top_level'):
                # This is actually an agency
                sub_agency_name = dept_rec['name']
                sub_agency_slug = Agency.slug_for(sub_agency_name)

                sub_agency, created = Agency.objects.get_or_create(
                    slug=sub_agency_slug, name=sub_agency_name)
                sub_agency.parent = a

                abbreviation = dept_rec.get('abbreviation')
                if not abbreviation:
                    abbreviation = build_abbreviation(sub_agency_name)
                sub_agency.abbreviation = abbreviation

                sub_agency.description = dept_rec.get('description')
                sub_agency.keywords = dept_rec.get('keywords')
                sub_agency.common_requests = dept_rec.get(
                    'common_requests', [])
                sub_agency.no_records_about = dept_rec.get(
                    'no_records_about', [])
                contactable_fields(sub_agency, dept_rec)
                sub_agency.save()
                add_request_time_statistics(dept_rec, sub_agency)
            else:
                # Just an office
                office_name = dept_rec['name']
                office_slug = Office.slug_for(office_name)
                full_slug = slug + '--' + office_slug

                o, created = Office.objects.get_or_create(agency=a,
                                                          slug=full_slug)

                o.office_slug = office_slug
                o.name = office_name
                contactable_fields(o, dept_rec)
                o.save()
                add_request_time_statistics(dept_rec, a, o)
Ejemplo n.º 21
0
def process_yamls(folder):

    # only load yaml files
    for item in iglob(folder + "/*.yaml"):
        data_file = os.path.join(folder, item)
        data = yaml.load(open(data_file))

        # Agencies
        name = data['name']
        slug = Agency.slug_for(name)

        a, created = Agency.objects.get_or_create(slug=slug, name=name)

        a.abbreviation = data['abbreviation']
        a.description = data.get('description')
        a.keywords = data.get('keywords')
        a.common_requests = data.get('common_requests', [])
        a.no_records_about = data.get('no_records_about', [])

        #   Only has a single, main branch/office
        if len(data['departments']) == 1:
            dept_rec = data['departments'][0]
            contactable_fields(a, dept_rec)

        a.save()
        add_request_time_statistics(data, a)

        # Offices
        if len(data['departments']) > 1:
            for dept_rec in data['departments']:
                if dept_rec.get('top_level'):
                    # This is actually an agency
                    sub_agency_name = dept_rec['name']
                    sub_agency_slug = Agency.slug_for(sub_agency_name)

                    sub_agency, created = Agency.objects.get_or_create(
                        slug=sub_agency_slug, name=sub_agency_name)
                    sub_agency.parent = a

                    abbreviation = build_abbreviation(sub_agency_name)
                    sub_agency.abbreviation = abbreviation
                    sub_agency.description = dept_rec.get('description')
                    sub_agency.keyword = dept_rec.get('keywords')
                    sub_agency.common_requests = dept_rec.get(
                        'common_requests', [])
                    sub_agency.no_records_about = dept_rec.get(
                        'no_records_about', [])
                    contactable_fields(sub_agency, dept_rec)
                    sub_agency.save()
                    add_request_time_statistics(dept_rec, sub_agency)
                else:
                    # Just an office
                    office_name = dept_rec['name']
                    office_slug = Office.slug_for(office_name)
                    full_slug = slug + '--' + office_slug

                    o, created = Office.objects.get_or_create(agency=a,
                                                              slug=full_slug)

                    o.office_slug = office_slug
                    o.name = office_name
                    contactable_fields(o, dept_rec)
                    o.save()
                    add_request_time_statistics(dept_rec, a, o)