Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[change/fix] Improved device export #850
- Added more device, config and geo fields to export - Fixes #850
- Loading branch information
1 parent
2c58297
commit 32c712a
Showing
7 changed files
with
174 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import json | ||
|
||
from django.core.exceptions import ObjectDoesNotExist | ||
from import_export import resources, widgets | ||
from import_export.fields import Field | ||
from swapper import load_model | ||
|
||
from . import settings as app_settings | ||
|
||
Device = load_model('config', 'Device') | ||
Config = load_model('config', 'Config') | ||
|
||
|
||
class DeviceResource(resources.ModelResource): | ||
organization = Field(attribute='organization__name', column_name='organization') | ||
group = Field(attribute='group__name', column_name='group') | ||
config_status = Field(attribute='config__status', column_name='config_status') | ||
config_backend = Field(attribute='config__backend', column_name='config_backend') | ||
config_data = Field(attribute='config__config', column_name='config_data') | ||
config_context = Field(attribute='config__context', column_name='config_context') | ||
config_templates = Field( | ||
attribute='config__templates', | ||
column_name='config_templates', | ||
widget=widgets.ManyToManyWidget(Config, field='pk', separator=','), | ||
) | ||
organization_id = Field(attribute='organization_id', column_name='organization_id') | ||
group_id = Field(attribute='group_id', column_name='group_id') | ||
|
||
def dehydrate_config_data(self, device): | ||
try: | ||
return json.dumps(device.config.config, sort_keys=True) | ||
except ObjectDoesNotExist: | ||
pass | ||
|
||
def dehydrate_config_context(self, device): | ||
try: | ||
return json.dumps(device.config.context, sort_keys=True) | ||
except ObjectDoesNotExist: | ||
pass | ||
|
||
class Meta: | ||
model = Device | ||
fields = [ | ||
'name', | ||
'mac_address', | ||
'organization', | ||
'group', | ||
'model', | ||
'os', | ||
'system', | ||
'notes', | ||
'last_ip', | ||
'management_ip', | ||
'config_status', | ||
'config_backend', | ||
'config_data', | ||
'config_context', | ||
'config_templates', | ||
'created', | ||
'modified', | ||
'id', | ||
'key', | ||
'organization_id', | ||
'group_id', | ||
] | ||
if app_settings.HARDWARE_ID_ENABLED: | ||
fields.insert(1, 'hardware_id') | ||
export_order = fields |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from django.core.exceptions import ObjectDoesNotExist | ||
from import_export.fields import Field | ||
|
||
from ..config.exportable import DeviceResource | ||
|
||
|
||
class GeoDeviceResource(DeviceResource): | ||
venue = Field(attribute='devicelocation__location__name', column_name='venue') | ||
address = Field( | ||
attribute='devicelocation__location__address', column_name='address' | ||
) | ||
coords = Field(attribute='devicelocation__location__geometry', column_name='coords') | ||
is_mobile = Field( | ||
attribute='devicelocation__location__is_mobile', column_name='is_mobile' | ||
) | ||
venue_type = Field( | ||
attribute='devicelocation__location__type', column_name='venue_type' | ||
) | ||
floor = Field(attribute='devicelocation__floorplan__floor', column_name='floor') | ||
floor_position = Field( | ||
attribute='devicelocation__indoor', column_name='floor_position' | ||
) | ||
location_id = Field( | ||
attribute='devicelocation__location_id', column_name='location_id' | ||
) | ||
floorplan_id = Field( | ||
attribute='devicelocation__floorplan_id', column_name='floorplan_id' | ||
) | ||
|
||
def dehydrate_coords(self, device): | ||
try: | ||
return device.devicelocation.location.geometry.wkt | ||
except ObjectDoesNotExist: | ||
pass | ||
|
||
class Meta(DeviceResource.Meta): | ||
fields = DeviceResource.Meta.fields[:] # copy | ||
# add geo fields after before last_ip | ||
# fmt: off | ||
fields[fields.index('last_ip'):fields.index('last_ip')] = [ | ||
'venue', | ||
'address', | ||
'coords', | ||
'is_mobile', | ||
'venue_type', | ||
'floor', | ||
'floor_position', | ||
] | ||
# fmt: on | ||
# add id fields at the end | ||
fields += ['location_id', 'floorplan_id'] | ||
export_order = fields |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters