class ManyToManyThroughWidgetTestCase(TestCase):
    def setUp(self):
        self.licence = LicenceFactory()
        DataCenterAssetLicenceFactory.create_batch(3, licence=self.licence)
        self.base_objects_ids = [
            bo.pk for bo in self.licence.base_objects.all()
        ]
        self.widget = ManyToManyThroughWidget(model=BaseObjectLicence,
                                              related_model=BaseObject,
                                              through_field='base_object')

    def test_clean(self):
        result = self.widget.clean(','.join(map(str, self.base_objects_ids)))
        self.assertCountEqual(
            result, BaseObject.objects.filter(pk__in=self.base_objects_ids))

    def test_clean_empty_value(self):
        result = self.widget.clean('')
        self.assertCountEqual(result, BaseObject.objects.none())
        self.assertEqual(result.model, BaseObject)

    def test_render(self):
        result = self.widget.render(self.licence.baseobjectlicence_set.all())
        self.assertCountEqual(map(int, result.split(',')),
                              self.base_objects_ids)
 def setUp(self):
     self.licence = LicenceFactory()
     DataCenterAssetLicenceFactory.create_batch(3, licence=self.licence)
     self.base_objects_ids = [
         bo.pk for bo in self.licence.base_objects.all()
     ]
     self.widget = ManyToManyThroughWidget(model=BaseObjectLicence,
                                           related_model=BaseObject,
                                           through_field='base_object')
Exemple #3
0
class LicenceResource(RalphModelResource):
    manufacturer = fields.Field(
        column_name='manufacturer',
        attribute='manufacturer',
        widget=ImportedForeignKeyWidget(assets.Manufacturer),
    )
    licence_type = fields.Field(
        column_name='licence_type',
        attribute='licence_type',
        widget=ImportedForeignKeyWidget(LicenceType),
    )
    software = fields.Field(
        column_name='software',
        attribute='software',
        widget=ImportedForeignKeyWidget(Software),
    )
    region = fields.Field(
        column_name='region',
        attribute='region',
        widget=ImportedForeignKeyWidget(Region),
    )
    office_infrastructure = fields.Field(
        column_name='office_infrastructure',
        attribute='office_infrastructure',
        widget=ImportedForeignKeyWidget(OfficeInfrastructure),
    )
    users = ThroughField(column_name='users',
                         attribute='users',
                         widget=UserManyToManyWidget(model=LicenceUser),
                         through_model=LicenceUser,
                         through_from_field_name='licence',
                         through_to_field_name='user')
    base_objects = ThroughField(column_name='base_objects',
                                attribute='baseobjectlicence_set',
                                widget=ManyToManyThroughWidget(
                                    model=BaseObjectLicence,
                                    related_model=base.BaseObject,
                                    through_field='base_object'),
                                through_model=BaseObjectLicence,
                                through_from_field_name='licence',
                                through_to_field_name='base_object')

    class Meta:
        model = Licence
        prefetch_related = ('tags', )
        exclude = (
            'content_type',
            'baseobject_ptr',
        )

    def get_queryset(self):
        return Licence.objects_used_free_with_related.all()

    def dehydrate_price(self, licence):
        return str(licence.price)
Exemple #4
0
class SupportResource(ResourceWithPrice, RalphModelResource):
    support_type = fields.Field(
        column_name='support_type',
        attribute='support_type',
        widget=ImportedForeignKeyWidget(SupportType),
    )
    base_objects = ThroughField(
        column_name='base_objects',
        attribute='baseobjectssupport_set',
        widget=ManyToManyThroughWidget(
            model=BaseObjectsSupport,
            related_model=base.BaseObject,
            through_field='baseobject'
        ),
        through_model=BaseObjectsSupport,
        through_from_field_name='support',
        through_to_field_name='baseobject'
    )
    region = fields.Field(
        column_name='region',
        attribute='region',
        widget=ImportedForeignKeyWidget(Region),
    )
    budget_info = fields.Field(
        column_name='budget_info',
        attribute='budget_info',
        widget=ImportedForeignKeyWidget(assets.BudgetInfo),
    )
    property_of = fields.Field(
        column_name='property_of',
        attribute='property_of',
        widget=ImportedForeignKeyWidget(assets.AssetHolder),
    )
    assigned_objects_count = fields.Field(
        readonly=True,
        column_name='assigned_objects_count',
        attribute='assigned_objects_count',
    )

    class Meta:
        model = Support
        exclude = ('content_type', 'baseobject_ptr',)
        prefetch_related = ('tags',)

    def get_queryset(self):
        return Support.objects_with_related.all()

    def dehydrate_assigned_objects_count(self, support):
        # this somehow ignores readonly flag, throwing AttributeError during
        # import, but we need it only for export
        try:
            return support.assigned_objects_count
        except AttributeError:
            pass
Exemple #5
0
 def _get_base_objects_through_field(self):
     return ThroughField(column_name='base_objects',
                         attribute='base_objects',
                         widget=ManyToManyThroughWidget(
                             model=BaseObjectLicence,
                             related_model=BaseObject,
                             through_field='base_object',
                         ),
                         through_model=BaseObjectLicence,
                         through_from_field_name='licence',
                         through_to_field_name='base_object')