def __init__(self,
                 registry: LinzElectoralDistrictRegistry,
                 context: LinzRedistrictingContext,
                 parent=None):
        super().__init__(parent)
        self.existing_names = list(registry.district_titles().keys())
        self.existing_codes = [
            f['code'] for f in registry.source_layer.getFeatures()
        ]

        self.setWindowTitle(
            self.tr('Create New {} Electorate').format(
                context.get_name_for_current_task()))

        dialog_layout = QVBoxLayout()
        label = QLabel(
            self.tr('Enter name for new {} electorate:').format(
                context.get_name_for_current_task()))
        dialog_layout.addWidget(label)

        self.name_line_edit = QLineEdit()
        self.name_line_edit.setPlaceholderText('Electorate name')
        dialog_layout.addWidget(self.name_line_edit)

        label = QLabel(self.tr('Enter code for new electorate:'))
        dialog_layout.addWidget(label)

        self.code_line_edit = QLineEdit()
        self.code_line_edit.setPlaceholderText('Electorate code')
        dialog_layout.addWidget(self.code_line_edit)

        self.feedback_label = QLabel()
        dialog_layout.addWidget(self.feedback_label)

        self.button_box = QDialogButtonBox(QDialogButtonBox.Ok
                                           | QDialogButtonBox.Cancel)
        dialog_layout.addWidget(self.button_box)
        self.button_box.rejected.connect(self.reject)
        self.button_box.accepted.connect(self.accept)

        self.name_line_edit.textChanged.connect(self.name_changed)
        self.code_line_edit.textChanged.connect(self.code_changed)

        self.setLayout(dialog_layout)
        self.button_box.button(QDialogButtonBox.Ok).setEnabled(False)
    def testLinzDistrictRegistry(self):
        """
        Test a LinzDistrictRegistry
        """
        layer = QgsVectorLayer(
            "Point?crs=EPSG:4326&field=fld1:string&field=fld2:string&field=type:string&field=estimated_pop:int&field=deprecated:int&field=stats_nz_pop:int&field=stats_nz_var_20:int&field=stats_nz_var_23:int&field=scenario_id:int",
            "source", "memory")
        f = QgsFeature()
        f.setAttributes(["test4", "xtest1", 'GN', 1000])
        f2 = QgsFeature()
        f2.setAttributes(["test2", "xtest3", 'GS', 2000])
        f3 = QgsFeature()
        f3.setAttributes(["test3", "xtest3", 'M', 3000])
        f4 = QgsFeature()
        f4.setAttributes(["test1", NULL, 'GN', 4000])
        f5 = QgsFeature()
        f5.setAttributes(["test2", "xtest2", 'GS', 5000])
        f6 = QgsFeature()
        f6.setAttributes(["test5", "xtest5", 'GN', 5000, True])
        layer.dataProvider().addFeatures([f, f2, f3, f4, f5, f6])
        quota_layer = make_quota_layer()

        reg = LinzElectoralDistrictRegistry(source_layer=layer,
                                            quota_layer=quota_layer,
                                            electorate_type='',
                                            source_field='fld1',
                                            title_field='fld1')
        self.assertEqual(reg.district_list(),
                         ['test1', 'test2', 'test3', 'test4'])

        self.assertEqual(reg.get_district_type('test1'), 'GN')
        self.assertEqual(reg.get_district_type('test2'), 'GS')
        self.assertEqual(reg.get_district_type('test3'), 'M')
        self.assertEqual(reg.get_district_type('test4'), 'GN')

        reg = LinzElectoralDistrictRegistry(source_layer=layer,
                                            quota_layer=quota_layer,
                                            electorate_type='',
                                            source_field='fld2',
                                            title_field='fld2')
        self.assertEqual(reg.district_list(), ['xtest1', 'xtest2', 'xtest3'])

        reg = LinzElectoralDistrictRegistry(source_layer=layer,
                                            quota_layer=quota_layer,
                                            electorate_type='GN',
                                            source_field='fld1',
                                            title_field='fld2')
        self.assertEqual(reg.district_list(), ['test1', 'test4'])
        self.assertEqual(reg.district_titles(), {
            NULL: 'test1',
            'xtest1': 'test4'
        })
        reg = LinzElectoralDistrictRegistry(source_layer=layer,
                                            quota_layer=quota_layer,
                                            electorate_type='GS',
                                            source_field='fld1',
                                            title_field='fld2')
        self.assertEqual(reg.district_list(), ['test2'])
        reg = LinzElectoralDistrictRegistry(source_layer=layer,
                                            quota_layer=quota_layer,
                                            electorate_type='M',
                                            source_field='fld1',
                                            title_field='fld2')
        self.assertEqual(reg.district_list(), ['test3'])