Ejemplo n.º 1
0
    def test_column_choices(self):
        """Choices computed for the exported model

        When using the ColumnFormSet and the ColumnForm, all the accessible
        items (fields, relations, methods, attributes...) from the exported
        model are present in the "column" form field choices

        """
        ColumnInlineFormSet = inlineformset_factory(Export,
                                                    Column,
                                                    form=ColumnForm,
                                                    formset=ColumnFormSet)
        formset = ColumnInlineFormSet(instance=self.empty_export)
        # the column field has choices
        form = formset.forms[0]
        self.assertTrue(hasattr(form.fields['column'], 'choices'))
        # all the table items are in the column field choices
        self.choices = form.fields['column'].choices
        self.assertTrue(all([(i, i) in self.choices for i in self.im.items]))
        # and all the related tables items are in the fields choices
        # export has a FK to Format named 'export_format'
        im_format = [
            'export_format.%s' % i for i in InspectModel(Format).items
        ]
        self.assertTrue(all([(i, i) in self.choices for i in im_format]))
        # export has a FK to ContentType named 'model'
        im_ct = ['model.%s' % i for i in InspectModel(ContentType).items]
        self.assertTrue(all([(i, i) in self.choices for i in im_ct]))
Ejemplo n.º 2
0
    def test_column_choices(self):
        """Choices computed for the exported model

        When using the ColumnFormSet and the ColumnForm, all the accessible
        items (fields, relations, methods, attributes...) from the exported
        model are present in the "column" form field choices

        """
        class OneToOneToExport(models.Model):
            """Fake model.

            Make sure that get_choices works with
            SingleRelatedObjectDescriptor, as explained in ticket #4.

            """
            name = models.CharField(max_length=50)
            o2o = models.OneToOneField(Export)

        # reload the model's relations, to have the OneToOneToExport's relation
        # taken into account
        self.empty_export._meta._fill_related_objects_cache()
        self.empty_export._meta.init_name_map()

        ColumnInlineFormSet = inlineformset_factory(Export,
                                                    Column,
                                                    form=ColumnForm,
                                                    formset=ColumnFormSet)
        formset = ColumnInlineFormSet(instance=self.empty_export)
        # the column field has choices
        form = formset.forms[0]
        self.assertTrue(hasattr(form.fields['column'], 'choices'))
        choices = form.fields['column'].choices
        # all the model items are in the column field choices
        self.assertTrue(all([(i, i) in choices for i in self.im.items]))
        # and all the related model items are in the fields choices
        # export has a FK to Format named 'export_format'
        im_format = [
            'export_format.%s' % i for i in InspectModel(Format).items
        ]
        self.assertTrue(all([(i, i) in choices for i in im_format]))
        # export has a FK to ContentType named 'model'
        im_ct = ['model.%s' % i for i in InspectModel(ContentType).items]
        self.assertTrue(all([(i, i) in choices for i in im_ct]))
        # OneToOneToExport has a OneToOneField to ContentType named
        # 'onetoonetoexport'
        im_o2o = [
            'onetoonetoexport.%s' % i
            for i in InspectModel(OneToOneToExport).items
        ]
        self.assertTrue(all([(i, i) in choices for i in im_o2o]))
        # revert changes to name_map:  'unload' the OneToOneToExport relation
        del self.empty_export._meta._name_map['onetoonetoexport']
Ejemplo n.º 3
0
    def setUp(self):
        self.om = OtherModel.objects.create()
        ctype = ContentType.objects.get_for_model(OtherModel)
        self.mti = ModelToInspect.objects.create(
            foreign=self.om, one=self.om, content_type=ctype)
        self.mti.many.add(self.om)
        self.lm = LinkedModel.objects.create(toinspect=self.mti)

        self.im = InspectModel(self.mti)
Ejemplo n.º 4
0
def get_choices(model, prefixes=[]):
    choices = []
    prefix = '.'.join(prefixes)
    if prefix:
        prefix = '%s.' % prefix
    im = InspectModel(model)
    items = ['%s%s' % (prefix, i) for i in im.items]
    choices += zip(items, items)
    for f in im.relation_fields:
        related_model = getattr(model, f).field.rel.to
        if f in prefixes:  # we already went through this model
            return []  # end of recursion
        new_prefixes = prefixes + [f]
        choices += get_choices(related_model, prefixes=new_prefixes)
    return choices
Ejemplo n.º 5
0
    def setUp(self):
        # create an export of the Export model (inception !)
        ct = ContentType.objects.get(app_label='data_exports', model='export')
        self.empty_export = Export.objects.create(name='test empty export',
                                                  slug='test-empty-export',
                                                  model=ct)
        self.im = InspectModel(Export)

        # create an export of the Export model with columns
        self.export = Export.objects.create(name='test export',
                                            slug='test-export',
                                            model=ct)
        for f in self.im.items:
            Column.objects.create(export=self.export, column=f, order=0)

        user = User.objects.create_user('admin', '*****@*****.**', 'admin')
        user.is_superuser = True
        user.save()