Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_prepare(self):
# Without fields.
data = {
'title': 'Cosmos',
'author': 'Carl Sagan',
'short_desc': 'A journey through the stars by an emminent astrophysist.',
'pub_date': '1980'
}
# Should be unmodified.
self.assertIsInstance(self.res.preparer, Preparer)
self.assertEqual(self.res.prepare(data), data)
self.res.preparer = FieldsPreparer(fields={
'title': 'title',
'author': 'author',
'synopsis': 'short_desc',
})
self.assertEqual(self.res.prepare(data), {
'author': 'Carl Sagan',
'synopsis': 'A journey through the stars by an emminent astrophysist.',
'title': 'Cosmos'
})
}
http_methods = {
'list': {
'GET': 'list',
'POST': 'create',
'PUT': 'update_list',
'DELETE': 'delete_list',
},
'detail': {
'GET': 'detail',
'POST': 'create_detail',
'PUT': 'update',
'DELETE': 'delete',
}
}
preparer = Preparer()
serializer = JSONSerializer()
def __init__(self, *args, **kwargs):
self.init_args = args
self.init_kwargs = kwargs
self.request = None
self.data = None
self.endpoint = None
self.status = 200
@classmethod
def as_list(cls, *init_args, **init_kwargs):
"""
Used for hooking up the actual list-style endpoints, this returns a
wrapper function that creates a new instance of the resource class &
calls the correct view method for it.
It also is relevant as the protocol subclasses should implement to work with
Restless.
"""
def __init__(self):
super(Preparer, self).__init__()
def prepare(self, data):
"""
Handles actually transforming the data.
By default, this does nothing & simply returns the data passed to it.
"""
return data
class FieldsPreparer(Preparer):
"""
A more complex preparation object, this will return a given set of fields.
This takes a ``fields`` parameter, which should be a dictionary of
keys (fieldnames to expose to the user) & values (a dotted lookup path to
the desired attribute/key on the object).
Example::
preparer = FieldsPreparer(fields={
# ``user`` is the key the client will see.
# ``author.pk`` is the dotted path lookup ``FieldsPreparer``
# will traverse on the data to return a value.
'user': 'author.pk',
})
self.data = [self.data]
deleted = None
else:
deleted = EC2Instance.objects.exclude(
ec2id__in=[i['InstanceId'] for i in self.data]).delete()
for instc in self.data:
instance, created = EC2Instance.objects.get_or_create(ec2id=instc['InstanceId'])
instance.name = [x['Value'] for x in instc['Tags'] if x['Key'] == 'Name'][0]
instance.launch_time = instc['LaunchTime']
instance.running = instc['State']['Name'] == 'running'
instance.extra_data = instc
instance.save()
return {'saved': len(self.data), 'deleted': deleted}
class FDTicketPreparer(Preparer):
"""Custom FieldsPreparer class for FreskdeskTicketResource.
"""
def prepare(self, data):
result = {
'ticket_id': data.ticket_id,
'type': data.type,
'subject': data.subject,
'description': data.description_text,
'status': data.get_status_display(),
'source': data.get_source_display(),
'priority': data.get_priority_display(),
'support_category': data.custom_fields['support_category'],
'support_subcategory': data.custom_fields['support_subcategory'],
'requester__email': data.freshdesk_requester.email if data.freshdesk_requester else '',
'created_at': data.created_at.isoformat() if data.created_at else '',
'updated_at': data.updated_at.isoformat() if data.updated_at else '',
def __init__(self):
super(Preparer, self).__init__()
from __future__ import unicode_literals, absolute_import
from oim_cms.utils import CSVDjangoResource
from restless.preparers import Preparer
from .models import HardwareAsset
class HardwareAssetPreparer(Preparer):
"""Custom field preparer class for HardwareAssetResource.
"""
def prepare(self, data):
result = {
'vendor': data.vendor.name,
'date_purchased': data.date_purchased,
'purchased_value': data.purchased_value,
'notes': data.notes,
'asset_tag': data.asset_tag,
'finance_asset_tag': data.finance_asset_tag,
'hardware_model': data.hardware_model.model_type,
'status': data.get_status_display(),
'serial': data.serial,
'location': str(data.location) if data.location else '',
'assigned_user': data.assigned_user.email if data.assigned_user else ''
}