Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add baseobject_ptr to Support Resource exclude #2271

Merged
merged 1 commit into from
Mar 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/ralph/data_importer/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
)
from ralph.data_importer.widgets import (
AssetServiceEnvWidget,
BaseObjectLicenceWidget,
BaseObjectManyToManyWidget,
BaseObjectThroughWidget,
BaseObjectWidget,
ImportedForeignKeyWidget,
LicenceUserWidget,
NullStringWidget,
UserManyToManyWidget,
UserWidget
Expand Down Expand Up @@ -311,15 +309,15 @@ class LicenceResource(RalphModelResource):
users = ThroughField(
column_name='users',
attribute='users',
widget=LicenceUserWidget(model=LicenceUser),
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='base_objects',
widget=BaseObjectLicenceWidget(model=base.BaseObject),
widget=BaseObjectThroughWidget(model=base.BaseObject),
through_model=BaseObjectLicence,
through_from_field_name='licence',
through_to_field_name='base_object'
Expand Down Expand Up @@ -349,10 +347,13 @@ class SupportResource(RalphModelResource):
attribute='support_type',
widget=ImportedForeignKeyWidget(SupportType),
)
base_objects = fields.Field(
base_objects = ThroughField(
column_name='base_objects',
attribute='base_objects',
widget=BaseObjectManyToManyWidget(base.BaseObject),
widget=BaseObjectThroughWidget(model=base.BaseObject),
through_model=BaseObjectsSupport,
through_from_field_name='support',
through_to_field_name='baseobject'
)
region = fields.Field(
column_name='region',
Expand All @@ -372,6 +373,7 @@ class SupportResource(RalphModelResource):

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

def dehydrate_price(self, support):
return str(support.price)
Expand Down
4 changes: 2 additions & 2 deletions src/ralph/data_importer/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ralph.accounts.tests.factories import UserFactory
from ralph.data_importer.fields import ThroughField
from ralph.data_importer.resources import LicenceResource
from ralph.data_importer.widgets import LicenceUserWidget
from ralph.data_importer.widgets import UserManyToManyWidget
from ralph.licences.models import Licence, LicenceUser
from ralph.licences.tests.factories import LicenceFactory

Expand All @@ -28,7 +28,7 @@ def test_through_field(self):
through_to_field_name='user',
attribute='users',
column_name='users',
widget=LicenceUserWidget(model=get_user_model())
widget=UserManyToManyWidget(model=get_user_model())
)

self.assertEqual(self.licence.users.all().count(), 3)
Expand Down
16 changes: 1 addition & 15 deletions src/ralph/data_importer/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,7 @@ def render(self, value):
return self.separator.join([obj.username for obj in value.all()])


class LicenceUserWidget(widgets.ManyToManyWidget):

def clean(self, value):
if not value:
return get_user_model().objects.none()
usernames = value.split(self.separator)
return get_user_model().objects.filter(username__in=usernames)

def render(self, value):
return self.separator.join(
[obj.username for obj in value.all()]
)


class BaseObjectLicenceWidget(widgets.ManyToManyWidget):
class BaseObjectThroughWidget(widgets.ManyToManyWidget):

def clean(self, value):
if not value:
Expand Down