Example #1
0
    def test_validates_uri(self):
        country = Country.objects.first()

        work = Work(frbr_uri='bad', country=country)
        self.assertRaises(ValidationError, work.full_clean)

        work = Work(frbr_uri='', country=country)
        self.assertRaises(ValidationError, work.full_clean)
Example #2
0
    def get_form_kwargs(self):
        kwargs = super(AddWorkView, self).get_form_kwargs()

        work = Work()
        work.country = self.country
        work.locality = self.locality
        kwargs['instance'] = work

        return kwargs
Example #3
0
    def test_dont_link_constitution_in_constitution(self):
        constitution = Work(frbr_uri='/akn/za/act/1996/constitution')
        doc = Document(work=constitution, content=document_fixture(xml="""
        <section id="section-1">
          <num>1.</num>
          <heading>Tester</heading>
          <paragraph id="section-1.paragraph-0">
            <content>
              <p>the Constitution</p>
              <p>the Constitution, 1996</p>
              <p>the Constitution of South Africa</p>
              <p>the Constitution of the Republic of South Africa</p>
              <p>the Constitution of the Republic of South Africa, 1996</p>
              <p>the Constitution of the Republic of South Africa 1996</p>
              <p>the Constitution of the Republic of South Africa Act, 1996</p>
              <p>the Constitution of the Republic of South Africa, 1996 ( Act 108 of 1996 )</p>
              <p>the Constitution of the Republic of South Africa, 1996 ( Act No 108 of 1996 )</p>
              <p>the Constitution of the Republic of South Africa, 1996 ( Act No. 108 of 1996 )</p>
              <p>the Constitution of the Republic of South Africa Act, 1996 ( Act No. 108 of 1996 )</p>
              <p>the Constitution of the Republic of South Africa  Act 108 of 1996</p>
              <p>the Constitution of the Republic of South Africa (Act 108 of 1996)</p>
            </content>
          </paragraph>
        </section>"""))

        unchanged = doc.document_xml
        doc.doc.frbr_uri = FrbrUri.parse(constitution.frbr_uri)
        self.finder.find_references_in_document(doc)
        self.assertMultiLineEqual(unchanged, doc.document_xml)
Example #4
0
 def setUp(self):
     self.work = Work(frbr_uri='/za/act/1991/1')
     self.italics_terms_finder = BaseItalicsFinder()
     self.italics_terms = [
         'ad', 'ad hoc', 'ad idem', 'Gazette', 'Government Gazette',
         'habeus corpus', 'per'
     ]
     self.maxDiff = None
Example #5
0
    def test_find_simple(self):
        za = Country.objects.get(pk=1)
        user1 = User.objects.get(pk=1)
        settings.INDIGO['WORK_PROPERTIES'] = {
            'za': {
                'cap': 'Chapter (cap)',
            }
        }

        work = Work(
            frbr_uri='/akn/za/act/2002/5',
            title='Act 5 of 2002',
            country=za,
            created_by_user=user1,
        )
        work.properties['cap'] = '12'
        work.updated_by_user = user1
        work.save()

        document = Document(
            document_xml=document_fixture(
                xml="""
        <section eId="sec_1">
          <num>1.</num>
          <heading>Tester</heading>
          <paragraph eId="sec_1.paragraph-0">
            <content>
              <p>Something to do with Cap. 12.</p>
            </content>
          </paragraph>
        </section>"""
            ),
            language=self.eng,
            work=work)

        expected = Document(
            document_xml=document_fixture(
                xml="""
        <section eId="sec_1">
          <num>1.</num>
          <heading>Tester</heading>
          <paragraph eId="sec_1.paragraph-0">
            <content>
              <p>Something to do with <ref href="/akn/za/act/2002/5">Cap. 12</ref>.</p>
            </content>
          </paragraph>
        </section>"""
            ),
            language=self.eng,
            work=work)

        self.finder.find_references_in_document(document)
        root = etree.fromstring(expected.content)
        expected.content = etree.tostring(root, encoding='utf-8').decode('utf-8')
        self.assertEqual(expected.content, document.content)
Example #6
0
    def create_work(self, view, row, idx, dry_run):
        # copy all row details
        info = row
        info['row'] = idx + 2

        row = self.validate_row(view, row)

        if row.get('errors'):
            info['status'] = 'error'
            info['error_message'] = row['errors']
            return info

        frbr_uri = self.get_frbr_uri(row)

        try:
            work = Work.objects.get(frbr_uri=frbr_uri)
            info['work'] = work
            info['status'] = 'duplicate'
            info['amends'] = row.get('amends') or None
            info['commencement_date'] = row.get('commencement_date') or None

        except Work.DoesNotExist:
            work = Work()

            work.frbr_uri = frbr_uri
            work.country = view.country
            work.locality = view.locality
            work.title = row.get('title')
            work.publication_name = row.get('publication_name')
            work.publication_number = row.get('publication_number')
            work.publication_date = row.get('publication_date')
            work.commenced = bool(
                row.get('commencement_date') or row.get('commenced_by'))
            work.assent_date = row.get('assent_date')
            work.stub = row.get('stub')
            # handle spreadsheet that still uses 'principal'
            if 'stub' not in info:
                work.stub = not row.get('principal')
            work.created_by_user = view.request.user
            work.updated_by_user = view.request.user
            self.add_extra_properties(work, info)

            try:
                work.full_clean()
                if not dry_run:
                    work.save_with_revision(view.request.user)

                    # signals
                    work_changed.send(sender=work.__class__,
                                      work=work,
                                      request=view.request)

                    # info for links
                    pub_doc_params = {
                        'date': row.get('publication_date'),
                        'number': work.publication_number,
                        'publication': work.publication_name,
                        'country': view.country.place_code,
                        'locality':
                        view.locality.code if view.locality else None,
                    }
                    info['params'] = pub_doc_params

                    self.link_publication_document(work, info)

                    if not work.stub:
                        self.create_task(work, info, task_type='import')

                info['work'] = work
                info['status'] = 'success'

            except ValidationError as e:
                info['status'] = 'error'
                if hasattr(e, 'message_dict'):
                    info['error_message'] = ' '.join([
                        '%s: %s' % (f, '; '.join(errs))
                        for f, errs in e.message_dict.items()
                    ])
                else:
                    info['error_message'] = str(e)

        return info
Example #7
0
    def test_full_migration(self):
        work = Work(
            country=self.za,
            locality=self.cpt,
            frbr_uri="/akn/za-cpt/act/by-law/2014/liquor-trading-days-and-hours"
        )

        doc = Document(
            title="Liquor Trading Hours",
            frbr_uri=
            "/akn/za-cpt/act/by-law/2014/liquor-trading-days-and-hours",
            work=work,
            language=self.eng,
            expression_date=date(2016, 8, 17),
            created_by_user_id=1,
            document_xml="""
<akomaNtoso xmlns="http://docs.oasis-open.org/legaldocml/ns/akn/3.0">
  <act contains="singleVersion">
    <meta>
      <identification source="#slaw">
        <FRBRWork>
          <FRBRthis value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/!main"/>
          <FRBRuri value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours"/>
          <FRBRalias value="Liquor Trading Days and Hours"/>
          <FRBRdate date="2010-09-10" name="Generation"/>
          <FRBRauthor href="#council"/>
          <FRBRcountry value="za"/>
          <FRBRsubtype value="by-law"/>
        </FRBRWork>
        <FRBRExpression>
          <FRBRthis value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/eng@2012-04-26/!main"/>
          <FRBRuri value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/eng@2012-04-26"/>
          <FRBRdate date="2012-04-26" name="Generation"/>
          <FRBRauthor href="#council"/>
          <FRBRlanguage language="eng"/>
        </FRBRExpression>
        <FRBRManifestation>
          <FRBRthis value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/eng@2012-04-26/!main"/>
          <FRBRuri value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/eng@2012-04-26"/>
          <FRBRdate date="2020-05-27" name="Generation"/>
          <FRBRauthor href="#slaw"/>
        </FRBRManifestation>
      </identification>
      <publication number="6788" name="Province of Western Cape: Provincial Gazette" showAs="Province of Western Cape: Provincial Gazette" date="2010-09-10"/>
      <lifecycle source="#Laws-Africa">
        <eventRef date="2012-04-26" type="amendment" source="#amendment-0-source" id="amendment-2012-04-26"/>
        <eventRef date="2014-01-17" type="repeal" source="#repeal-source" id="repeal-2014-01-17"/>
      </lifecycle>
      <references>
        <TLCOrganization href="https://edit.laws.africa" showAs="Laws.Africa" eId="Laws-Africa"/>
        <TLCOrganization href="https://github.com/longhotsummer/slaw" showAs="Slaw" eId="slaw"/>
        <TLCOrganization href="/ontology/organization/za/council" showAs="Council" eId="council"/>
        <TLCTerm showAs="agricultural area" href="/ontology/term/this.eng.agricultural_area" eId="term-agricultural_area"/>
        <passiveRef href="/akn/za-cpt/act/by-law/2012/liquor-trading-days-and-hours" showAs="Liquor Trading Days and Hours: Amendment" id="amendment-0-source"/>
        <passiveRef href="/akn/za-cpt/act/by-law/2014/control-undertakings-liquor" showAs="Control of Undertakings that Sell Liquor to the Public" id="repeal-source"/>
      </references>
    </meta>
    <preamble>
      <p>WHEREAS a municipality may, in terms of section 156 of the Constitution, make and administer by-laws for the effective administration of matters which it has the right to administer;</p>
      <p>WHEREAS it is the intention of the City to set trading days and hours for all licensed premises, businesses or outlet situated within the City of Cape Town that sell liquor to the public;</p>
      <p>AND NOW THEREFORE, BE IT ENACTED by the Council of the City of Cape Town, as follows:-</p>
    </preamble>
    <body>
      <section eId="sec_1">
        <num>1.</num>
        <heading>Definitions</heading>
        <subsection eId="sec_1__subsec_1">
          <num>(1)</num>
          <content>
            <p>In this By–Law, unless the context indicates otherwise-</p>
            <p refersTo="#term-agricultural_area">"<def refersTo="#term-agricultural_area">agricultural area</def>" means an area predominantly <term refersTo="#term-zoned" eId="sec_1__subsec_1__term_1">zoned</term> agriculture or any other equivalent zoning, with the purpose to promote and protect agricultural activity on a farm as an important economic, environmental and cultural resource, where limited provision is made for non-agricultural uses to provide owners with an opportunity to increase the economic potential of their properties, without causing a significant negative impact on the primary agricultural resource;</p>
          </content>
        </subsection>
      </section>
    </body>
    <attachments>
      <attachment eId="att_1">
        <heading>Schedule</heading>
        <subheading>Trading hours for selling of liquor on licensed premises</subheading>
        <doc name="schedule">
          <meta>
            <identification source="#slaw">
              <FRBRWork>
                <FRBRthis value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/!schedule"/>
                <FRBRuri value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours"/>
                <FRBRalias value="Schedule"/>
                <FRBRdate date="1980-01-01" name="Generation"/>
                <FRBRauthor href="#council"/>
                <FRBRcountry value="za"/>
                <FRBRsubtype value="by-law"/>
              </FRBRWork>
              <FRBRExpression>
                <FRBRthis value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/eng@2012-04-26/!schedule"/>
                <FRBRuri value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/eng@2012-04-26"/>
                <FRBRdate date="1980-01-01" name="Generation"/>
                <FRBRauthor href="#council"/>
                <FRBRlanguage language="eng"/>
              </FRBRExpression>
              <FRBRManifestation>
                <FRBRthis value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/eng@2012-04-26/!schedule"/>
                <FRBRuri value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/eng@2012-04-26"/>
                <FRBRdate date="2020-05-27" name="Generation"/>
                <FRBRauthor href="#slaw"/>
              </FRBRManifestation>
            </identification>
          </meta>
          <mainBody>
            <hcontainer eId="hcontainer_1" name="hcontainer">
              <content>
                <table eId="hcontainer_1__table_1">
                  <tr>
                    <th>
                      <p>Location category &amp; licensed premises type</p>
                    </th>
                    <th>
                      <p>Maximum permitted trading hours</p>
                    </th>
                  </tr>
                </table>
              </content>
            </hcontainer>
          </mainBody>
        </doc>
      </attachment>
    </attachments>
  </act>
</akomaNtoso>
""")
        AKN3Laggards().migrate_act(doc.doc)
        output = doc.doc.to_xml(pretty_print=True, encoding='unicode')

        # check XML
        self.assertMultiLineEqual(
            """<akomaNtoso xmlns="http://docs.oasis-open.org/legaldocml/ns/akn/3.0">
  <act contains="singleVersion" name="act">
    <meta>
      <identification source="#slaw">
        <FRBRWork>
          <FRBRthis value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/!main"/>
          <FRBRuri value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours"/>
          <FRBRalias value="Liquor Trading Days and Hours" name="title"/>
          <FRBRdate date="2010-09-10" name="Generation"/>
          <FRBRauthor href="#council"/>
          <FRBRcountry value="za-cpt"/>
          <FRBRsubtype value="by-law"/><FRBRnumber value="liquor-trading-days-and-hours"/>
        </FRBRWork>
        <FRBRExpression>
          <FRBRthis value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/eng@2012-04-26/!main"/>
          <FRBRuri value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/eng@2012-04-26"/>
          <FRBRdate date="2012-04-26" name="Generation"/>
          <FRBRauthor href="#council"/>
          <FRBRlanguage language="eng"/>
        </FRBRExpression>
        <FRBRManifestation>
          <FRBRthis value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/eng@2012-04-26/!main"/>
          <FRBRuri value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/eng@2012-04-26"/>
          <FRBRdate date="2020-05-27" name="Generation"/>
          <FRBRauthor href="#slaw"/>
        </FRBRManifestation>
      </identification>
      <publication number="6788" name="Province of Western Cape: Provincial Gazette" showAs="Province of Western Cape: Provincial Gazette" date="2010-09-10"/>
      <lifecycle source="#Laws-Africa">
        <eventRef date="2012-04-26" type="amendment" source="#amendment-0-source" eId="amendment-2012-04-26"/>
        <eventRef date="2014-01-17" type="repeal" source="#repeal-source" eId="repeal-2014-01-17"/>
      </lifecycle>
      <references source="#cobalt">
        <TLCOrganization href="https://edit.laws.africa" showAs="Laws.Africa" eId="Laws-Africa"/>
        <TLCOrganization href="https://github.com/longhotsummer/slaw" showAs="Slaw" eId="slaw"/>
        <TLCOrganization href="/ontology/organization/za/council" showAs="Council" eId="council"/>
        <TLCTerm showAs="agricultural area" href="/ontology/term/this.eng.agricultural_area" eId="term-agricultural_area"/>
        <passiveRef href="/akn/za-cpt/act/by-law/2012/liquor-trading-days-and-hours" showAs="Liquor Trading Days and Hours: Amendment" eId="amendment-0-source"/>
        <passiveRef href="/akn/za-cpt/act/by-law/2014/control-undertakings-liquor" showAs="Control of Undertakings that Sell Liquor to the Public" eId="repeal-source"/>
      </references>
    </meta>
    <preamble>
      <p>WHEREAS a municipality may, in terms of section 156 of the Constitution, make and administer by-laws for the effective administration of matters which it has the right to administer;</p>
      <p>WHEREAS it is the intention of the City to set trading days and hours for all licensed premises, businesses or outlet situated within the City of Cape Town that sell liquor to the public;</p>
      <p>AND NOW THEREFORE, BE IT ENACTED by the Council of the City of Cape Town, as follows:-</p>
    </preamble>
    <body>
      <section eId="sec_1">
        <num>1.</num>
        <heading>Definitions</heading>
        <subsection eId="sec_1__subsec_1">
          <num>(1)</num>
          <content>
            <p>In this By–Law, unless the context indicates otherwise-</p>
            <p refersTo="#term-agricultural_area">"<def refersTo="#term-agricultural_area">agricultural area</def>" means an area predominantly <term refersTo="#term-zoned" eId="sec_1__subsec_1__term_1">zoned</term> agriculture or any other equivalent zoning, with the purpose to promote and protect agricultural activity on a farm as an important economic, environmental and cultural resource, where limited provision is made for non-agricultural uses to provide owners with an opportunity to increase the economic potential of their properties, without causing a significant negative impact on the primary agricultural resource;</p>
          </content>
        </subsection>
      </section>
    </body>
    <attachments>
      <attachment eId="att_1">
        <heading>Schedule</heading>
        <subheading>Trading hours for selling of liquor on licensed premises</subheading>
        <doc name="schedule">
          <meta>
            <identification source="#slaw">
              <FRBRWork>
                <FRBRthis value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/!schedule"/>
                <FRBRuri value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours"/>
                <FRBRalias value="Schedule" name="title"/>
                <FRBRdate date="1980-01-01" name="Generation"/>
                <FRBRauthor href="#council"/>
                <FRBRcountry value="za-cpt"/>
                <FRBRsubtype value="by-law"/><FRBRnumber value="liquor-trading-days-and-hours"/>
              </FRBRWork>
              <FRBRExpression>
                <FRBRthis value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/eng@2012-04-26/!schedule"/>
                <FRBRuri value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/eng@2012-04-26"/>
                <FRBRdate date="1980-01-01" name="Generation"/>
                <FRBRauthor href="#council"/>
                <FRBRlanguage language="eng"/>
              </FRBRExpression>
              <FRBRManifestation>
                <FRBRthis value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/eng@2012-04-26/!schedule"/>
                <FRBRuri value="/akn/za-cpt/act/by-law/2010/liquor-trading-days-and-hours/eng@2012-04-26"/>
                <FRBRdate date="2020-05-27" name="Generation"/>
                <FRBRauthor href="#slaw"/>
              </FRBRManifestation>
            </identification>
          </meta>
          <mainBody>
            <hcontainer eId="hcontainer_1" name="hcontainer">
              <content>
                <table eId="hcontainer_1__table_1">
                  <tr>
                    <th>
                      <p>Location category &amp; licensed premises type</p>
                    </th>
                    <th>
                      <p>Maximum permitted trading hours</p>
                    </th>
                  </tr>
                </table>
              </content>
            </hcontainer>
          </mainBody>
        </doc>
      </attachment>
    </attachments>
  </act>
</akomaNtoso>
""", output)

        assert_validates(doc.doc)
Example #8
0
    def test_full_migration2(self):
        work = Work(country=self.za,
                    locality=self.cpt,
                    frbr_uri="/akn/za/act/gn/2020/650/")

        doc = Document(title="A work",
                       frbr_uri="/akn/za/act/gn/2020/650/",
                       work=work,
                       language=self.eng,
                       expression_date=date(2016, 8, 17),
                       created_by_user_id=1,
                       document_xml="""
<akomaNtoso xmlns="http://docs.oasis-open.org/legaldocml/ns/akn/3.0">
  <act name="act" contains="originalVersion">
    <meta>
      <identification source="#cobalt">
        <FRBRWork>
          <FRBRuri value="/akn/za/act/gn/2020/650"/>
          <FRBRalias value="A work" name="title"/>
          <FRBRthis value="/akn/za/act/gn/2020/650/!main"/>
          <FRBRdate date="1990-01-01" name="Generation"/>
          <FRBRauthor href=""/>
          <FRBRcountry value="za"/>
          <FRBRnumber value="650"/>
          <FRBRsubtype value="gn"/>
        </FRBRWork>
        <FRBRExpression>
          <FRBRuri value="/akn/za/act/gn/2020/650/eng@2020-06-05"/>
          <FRBRthis value="/akn/za/act/gn/2020/650/eng@2020-06-05/!main"/>
          <FRBRdate date="2020-06-05" name="Generation"/>
          <FRBRauthor href=""/>
          <FRBRlanguage language="eng"/>
        </FRBRExpression>
        <FRBRManifestation>
          <FRBRuri value="/akn/za/act/gn/2020/650/eng@2020-06-05"/>
          <FRBRthis value="/akn/za/act/gn/2020/650/eng@2020-06-05/!main"/>
          <FRBRdate date="2020-06-18" name="Generation"/>
          <FRBRauthor href=""/>
        </FRBRManifestation>
      </identification>
      <publication number="43412" name="Government Gazette" showAs="Government Gazette" date="2020-06-05"/>
      <references>
        <TLCOrganization id="cobalt" href="https://github.com/laws-africa/cobalt" showAs="cobalt"/>
      </references>
    </meta>
    <body>
      <section eId="section_1">
        <content>
          <p/>
        </content>
      </section>
    </body>
  </act>
</akomaNtoso>
""")
        AKN3Laggards().migrate_act(doc.doc)
        output = doc.doc.to_xml(pretty_print=True, encoding='unicode')

        # check XML
        self.assertMultiLineEqual(
            """<akomaNtoso xmlns="http://docs.oasis-open.org/legaldocml/ns/akn/3.0">
  <act name="act" contains="originalVersion">
    <meta>
      <identification source="#cobalt">
        <FRBRWork>
          <FRBRthis value="/akn/za/act/gn/2020/650/!main"/>
          <FRBRuri value="/akn/za/act/gn/2020/650"/>
          <FRBRalias value="A work" name="title"/>
          <FRBRdate date="1990-01-01" name="Generation"/>
          <FRBRauthor href=""/>
          <FRBRcountry value="za"/><FRBRsubtype value="gn"/>
        
          <FRBRnumber value="650"/>
          </FRBRWork>
        <FRBRExpression>
          <FRBRthis value="/akn/za/act/gn/2020/650/eng@2020-06-05/!main"/>
          <FRBRuri value="/akn/za/act/gn/2020/650/eng@2020-06-05"/>
          <FRBRdate date="2020-06-05" name="Generation"/>
          <FRBRauthor href=""/>
          <FRBRlanguage language="eng"/>
        </FRBRExpression>
        <FRBRManifestation>
          <FRBRthis value="/akn/za/act/gn/2020/650/eng@2020-06-05/!main"/>
          <FRBRuri value="/akn/za/act/gn/2020/650/eng@2020-06-05"/>
          <FRBRdate date="2020-06-18" name="Generation"/>
          <FRBRauthor href=""/>
        </FRBRManifestation>
      </identification>
      <publication number="43412" name="Government Gazette" showAs="Government Gazette" date="2020-06-05"/>
      <references source="#cobalt">
        <TLCOrganization href="https://github.com/laws-africa/cobalt" showAs="cobalt" eId="cobalt"/>
      </references>
    </meta>
    <body>
      <section eId="section_1">
        <content>
          <p/>
        </content>
      </section>
    </body>
  </act>
</akomaNtoso>
""", output)

        assert_validates(doc.doc)
Example #9
0
 def test_numbered_title_none(self):
     work = Work(frbr_uri='/za/act/1999/constitution')
     self.assertIsNone(self.plugin.work_numbered_title(work))
Example #10
0
 def test_numbered_title_ignore_subtype(self):
     plugin = BaseWorkDetail()
     plugin.no_numbered_title_subtypes = ['gn']
     work = Work(frbr_uri='/za/act/gn/1999/32')
     self.assertIsNone(plugin.work_numbered_title(work))
Example #11
0
 def test_numbered_title_none(self):
     work = Work(frbr_uri='/za/act/1999/constitution')
     self.assertIsNone(self.plugin.work_numbered_title(work))
     work = Work(
         frbr_uri='/akn/za/act/2012/constitution-seventeenth-amendment')
     self.assertIsNone(self.plugin.work_numbered_title(work))
Example #12
0
 def test_numbered_title_caps(self):
     work = Work(frbr_uri='/za/act/gn/1999/r32')
     self.assertEqual('Government Notice R32 of 1999', self.plugin.work_numbered_title(work))
Example #13
0
 def get_object(self, queryset=None):
     work = Work()
     work.country = self.request.user.editor.country
     return work
Example #14
0
 def get_object(self, *args, **kwargs):
     work = Work()
     work.country = self.request.user.editor.country_code
     return work
Example #15
0
 def setUp(self):
     self.work = Work(frbr_uri='/akn/za/act/1998/1')
     self.finder = TermsFinderENG()
Example #16
0
 def setUp(self):
     self.work = Work(frbr_uri='/akn/za/act/1991/1')
     self.section_refs_finder = SectionRefsFinderENG()
     self.eng = Language.for_code('eng')
     self.maxDiff = None
Example #17
0
    def get_works(self, table):
        works = []

        # clean up headers
        headers = [h.split(' ')[0].lower() for h in table[0]]

        # transform rows into list of dicts for easy access
        rows = [{header: row[i]
                 for i, header in enumerate(headers) if header}
                for row in table[1:]]

        for idx, row in enumerate(rows):
            # ignore if it's blank or explicitly marked 'ignore' in the 'ignore' column
            if not row.get('ignore') and [
                    val for val in row.itervalues() if val
            ]:
                info = {
                    'row': idx + 2,
                }
                works.append(info)

                try:
                    frbr_uri = self.get_frbr_uri(self.country, self.locality,
                                                 row)
                except ValueError as e:
                    info['status'] = 'error'
                    info['error_message'] = e.message
                    continue

                try:
                    work = Work.objects.get(frbr_uri=frbr_uri)
                    info['work'] = work
                    info['status'] = 'duplicate'

                # TODO one day: also mark first work as duplicate if user is trying to import two of the same (currently only the second one will be)

                except Work.DoesNotExist:
                    work = Work()

                    work.frbr_uri = frbr_uri
                    work.title = row.get('title')
                    work.country = self.country
                    work.locality = self.locality
                    work.publication_name = row.get('publication_name')
                    work.publication_number = row.get('publication_number')
                    work.created_by_user = self.request.user
                    work.updated_by_user = self.request.user

                    try:
                        work.publication_date = self.make_date(
                            row.get('publication_date'), 'publication_date')
                        work.commencement_date = self.make_date(
                            row.get('commencement_date'), 'commencement_date')
                        work.assent_date = self.make_date(
                            row.get('assent_date'), 'assent_date')
                        work.full_clean()
                        work.save()

                        # signals
                        work_changed.send(sender=work.__class__,
                                          work=work,
                                          request=self.request)

                        info['status'] = 'success'
                        info['work'] = work

                    except ValidationError as e:
                        info['status'] = 'error'
                        if hasattr(e, 'message_dict'):
                            info['error_message'] = ' '.join([
                                '%s: %s' % (f, '; '.join(errs))
                                for f, errs in e.message_dict.items()
                            ])
                        else:
                            info['error_message'] = e.message

        return works
Example #18
0
    def create_work(self, view, row, idx):
        # handle spreadsheet that still uses 'principal'
        row['stub'] = row.get(
            'stub') if 'stub' in row else not row.get('principal')
        row = self.validate_row(view, row)
        row.status = None
        row.row_number = idx + 2

        if row.errors:
            return row

        frbr_uri = self.get_frbr_uri(row)

        try:
            row.work = Work.objects.get(frbr_uri=frbr_uri)
            row.status = 'duplicate'

        except Work.DoesNotExist:
            work = Work()

            work.frbr_uri = frbr_uri
            work.country = view.country
            work.locality = view.locality
            for attribute in [
                    'title', 'publication_name', 'publication_number',
                    'assent_date', 'publication_date', 'commenced', 'stub'
            ]:
                setattr(work, attribute, getattr(row, attribute, None))
            work.created_by_user = view.request.user
            work.updated_by_user = view.request.user
            self.add_extra_properties(work, row)

            try:
                work.full_clean()
                if not self.dry_run:
                    work.save_with_revision(view.request.user)

                    # signals
                    work_changed.send(sender=work.__class__,
                                      work=work,
                                      request=view.request)

                # info for linking publication document
                row.params = {
                    'date': work.publication_date,
                    'number': work.publication_number,
                    'publication': work.publication_name,
                    'country': view.country.place_code,
                    'locality': view.locality.code if view.locality else None,
                }

                self.link_publication_document(work, row)

                if not work.stub:
                    self.create_task(work, row, task_type='import-content')

                row.work = work
                row.status = 'success'

            except ValidationError as e:
                if hasattr(e, 'message_dict'):
                    row.errors = ' '.join([
                        '%s: %s' % (f, '; '.join(errs))
                        for f, errs in e.message_dict.items()
                    ])
                else:
                    row.errors = str(e)

        return row
Example #19
0
 def setUp(self):
     self.work = Work(frbr_uri='/za/act/1998/1')
     self.builder = TOCBuilderBase()
     self.eng = Language.for_code('eng')
Example #20
0
    def get_works(self, view, table):
        works = []

        # clean up headers
        headers = [h.split(' ')[0].lower() for h in table[0]]

        # transform rows into list of dicts for easy access
        rows = [{header: row[i]
                 for i, header in enumerate(headers) if header}
                for row in table[1:]]

        for idx, row in enumerate(rows):
            # ignore if it's blank or explicitly marked 'ignore' in the 'ignore' column
            if row.get('ignore') or not [val for val in row.values() if val]:
                continue

            info = {
                'row': idx + 2,
            }
            works.append(info)

            row = self.validate_row(view, row)

            if row.get('errors'):
                info['status'] = 'error'
                info['error_message'] = row['errors']
                continue

            frbr_uri = self.get_frbr_uri(row)

            try:
                work = Work.objects.get(frbr_uri=frbr_uri)
                info['work'] = work
                info['status'] = 'duplicate'
                info['amends'] = row.get('amends') or None
                info['commencement_date'] = row.get(
                    'commencement_date') or None

            except Work.DoesNotExist:
                work = Work()

                work.frbr_uri = frbr_uri
                work.country = view.country
                work.locality = view.locality
                work.title = row.get('title')
                work.publication_name = row.get('publication_name')
                work.publication_number = row.get('publication_number')
                work.publication_date = row.get('publication_date')
                work.commencement_date = row.get('commencement_date')
                work.assent_date = row.get('assent_date')
                work.stub = not row.get('principal')
                work.created_by_user = view.request.user
                work.updated_by_user = view.request.user

                try:
                    work.full_clean()
                    work.save_with_revision(view.request.user)

                    # signals
                    work_changed.send(sender=work.__class__,
                                      work=work,
                                      request=view.request)

                    # info for links, extra properties
                    pub_doc_params = {
                        'date': row.get('publication_date'),
                        'number': work.publication_number,
                        'publication': work.publication_name,
                        'country': view.country.place_code,
                        'locality':
                        view.locality.code if view.locality else None,
                    }
                    info['params'] = pub_doc_params

                    for header in headers:
                        info[header] = row.get(header)

                    info['status'] = 'success'
                    info['work'] = work

                except ValidationError as e:
                    info['status'] = 'error'
                    if hasattr(e, 'message_dict'):
                        info['error_message'] = ' '.join([
                            '%s: %s' % (f, '; '.join(errs))
                            for f, errs in e.message_dict.items()
                        ])
                    else:
                        info['error_message'] = e.message

        return works
Example #21
0
 def test_numbered_title_simple(self):
     work = Work(frbr_uri='/za/act/1999/32')
     self.assertEqual('Act 32 of 1999', self.plugin.work_numbered_title(work))
Example #22
0
 def setUp(self):
     self.maxDiff = None
     self.work = Work(frbr_uri='/akn/za/act/1998/1')
     self.finder = RefsFinderAFRza()
Example #23
0
    def get_works(self, table, form):
        works = []

        # clean up headers
        headers = [h.split(' ')[0].lower() for h in table[0]]

        # transform rows into list of dicts for easy access
        rows = [
            {header: row[i] for i, header in enumerate(headers) if header}
            for row in table[1:]
        ]

        for idx, row in enumerate(rows):
            # ignore if it's blank or explicitly marked 'ignore' in the 'ignore' column
            if not row.get('ignore') and [val for val in row.itervalues() if val]:
                info = {
                    'row': idx + 2,
                }
                works.append(info)

                try:
                    frbr_uri = self.get_frbr_uri(self.country, self.locality, row)
                except ValueError as e:
                    info['status'] = 'error'
                    info['error_message'] = e.message
                    continue

                try:
                    work = Work.objects.get(frbr_uri=frbr_uri)
                    info['work'] = work
                    info['status'] = 'duplicate'
                    if row.get('amends'):
                        info['amends'] = row.get('amends')
                    if row.get('commencement_date'):
                        info['commencement_date'] = row.get('commencement_date')

                except Work.DoesNotExist:
                    work = Work()

                    work.frbr_uri = frbr_uri
                    work.title = self.strip_title_string(row.get('title'))
                    work.country = self.country
                    work.locality = self.locality
                    work.publication_name = row.get('publication_name')
                    work.publication_number = row.get('publication_number')
                    work.created_by_user = self.request.user
                    work.updated_by_user = self.request.user
                    work.stub = not row.get('primary')

                    try:
                        work.publication_date = self.make_date(row.get('publication_date'), 'publication_date')
                        work.commencement_date = self.make_date(row.get('commencement_date'), 'commencement_date')
                        work.assent_date = self.make_date(row.get('assent_date'), 'assent_date')
                        work.full_clean()
                        work.save()

                        # link publication document
                        params = {
                            'date': row.get('publication_date'),
                            'number': work.publication_number,
                            'publication': work.publication_name,
                            'country': self.country.place_code,
                            'locality': self.locality.code if self.locality else None,
                        }
                        self.get_publication_document(params, work, form)

                        # signals
                        work_changed.send(sender=work.__class__, work=work, request=self.request)

                        info['status'] = 'success'
                        info['work'] = work

                        # TODO: neaten this up
                        if row.get('commenced_by'):
                            info['commenced_by'] = row.get('commenced_by')
                        if row.get('amends'):
                            info['amends'] = row.get('amends')
                        if row.get('repealed_by'):
                            info['repealed_by'] = row.get('repealed_by')
                        if row.get('with_effect_from'):
                            info['with_effect_from'] = row.get('with_effect_from')
                        if row.get('parent_work'):
                            info['parent_work'] = row.get('parent_work')

                    except ValidationError as e:
                        info['status'] = 'error'
                        if hasattr(e, 'message_dict'):
                            info['error_message'] = ' '.join(
                                ['%s: %s' % (f, '; '.join(errs)) for f, errs in e.message_dict.items()]
                            )
                        else:
                            info['error_message'] = e.message

        return works
Example #24
0
 def setUp(self):
     self.work = Work(frbr_uri='/akn/za/act/1991/1')
     self.finder = RefsFinderSubtypesENG()
     self.eng = Language.for_code('eng')
     self.maxDiff = None
Example #25
0
 def create(self, validated_data):
     work = Work()
     validated_data['created_by_user'] = self.context['request'].user
     return self.update(work, validated_data)