Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
"""
API resources
"""
# Python imports
import logging
# Django imports
from django import forms
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.db.models import Count
from django.utils.datastructures import SortedDict
# 3rd party imports
from tastypie import fields
from tastypie.authentication import ApiKeyAuthentication
from tastypie.constants import ALL, ALL_WITH_RELATIONS
from tastypie.validation import FormValidation, Validation
# Project imports
from ..authorization.line_item_bids import Line_Item_BidsAuthorization
# Core app imports
from vbenergyzone.core.models.line_item_bids import Line_Item_Bid
from vbenergyzone.core.models.permissions import get_permissions
from ..paginator import BackbonePaginator
from . import ResourceBase
# ==============================================================================
class Line_Item_BidValidationForm(forms.ModelForm): #pylint: disable=R0924
"""
Define a form for validation.
"""
class Meta: # pylint: disable=C1001, W0232, C0111, R0903
fields = (
'id',
)
model = Line_Item_Bid
class Line_Item_BidsResource(ResourceBase):
awarded_by_price_request = fields.ToOneField(
attribute='price_request',
blank=True,
full=True,
null=True,
to='vbenergyzone.api.resources.price_requests.Price_RequestsResource',
)
bid = fields.ToOneField(
attribute='bid',
blank=True,
full=True,
null=True,
to='vbenergyzone.api.resources.bids.BidsResource',
)
line_item = fields.ToOneField(
attribute='line_item',
blank=True,
full=True,
null=True,
to='vbenergyzone.api.resources.line_items.Line_ItemsResource',
)
price_request = fields.ToOneField(
attribute='bid__price_request_supplier__price_request',
blank=True,
full=True,
null=True,
to='vbenergyzone.api.resources.price_requests.Price_RequestsResource',
)
supplier = fields.ToOneField(
attribute='bid__price_request_supplier__supplier',
blank=True,
full=True,
null=True,
to='vbenergyzone.api.resources.suppliers.SuppliersResource',
)
class Meta:
always_return_data = True
authentication = ApiKeyAuthentication()
authorization = Line_Item_BidsAuthorization()
collection_name = 'objects'
detail_allowed_methods = ['delete', 'get', 'patch', 'put',]
fields = (
'deletable',
'updatable',
'viewable',
'bid_entry',
'heat_rate_multiplier',
'id',
'no_bid',
'price_request',
'supplier',
)
filtering = {
'awarded_by_price_request': ALL,
'bid' : ALL,
'bid_entry' : ALL,
'heat_rate_multiplier' : ALL,
'id' : ALL,
'line_item' : ALL,
'no_bid' : ALL,
'price_request' : ALL,
'supplier' : ALL,
}
include_absolute_url = False
limit = 10
list_allowed_methods = ['get', 'post',]
max_limit = None
ordering = [
'bid_entry',
'heat_rate_multiplier',
'id',
'no_bid',
'price_request',
'supplier',
]
paginator_class = BackbonePaginator
queryset = Line_Item_Bid.objects.all()
resource_name = 'line_item_bids'
validation = FormValidation(form_class=Line_Item_BidValidationForm)
def alter_detail_data_to_serialize(self, request, bundle):
super(Line_Item_BidsResource, self).alter_detail_data_to_serialize(request, bundle)
# All requests return full detail.
bundle.data['meta']['detail'] = 'full'
return bundle
def alter_list_data_to_serialize(self, request, data):
super(Line_Item_BidsResource, self).alter_list_data_to_serialize(request, data)
# All requests return full detail.
data['meta']['detail'] = 'full'
return data
def apply_sorting(self, obj_list, options=None):
options = options.copy()
if 'sort_by' in options:
if 'order' in options:
if options['order'] == 'desc':
options['sort_by'] = '-' + options['sort_by']
if options['sort_by'] == 'supplier':
obj_list = obj_list.order_by(
'bid__price_request_supplier__supplier__name'
)
elif options['sort_by'] == '-supplier':
obj_list = obj_list.order_by(
'-bid__price_request_supplier__supplier__name'
)
elif options['sort_by'] == 'price_request':
obj_list = obj_list.order_by(
'bid__price_request_supplier__price_request__name'
)
elif options['sort_by'] == '-price_request':
obj_list = obj_list.order_by(
'-bid__price_request_supplier__price_request__name'
)
else:
obj_list = obj_list.order_by(options['sort_by'])
return obj_list
def build_filters(self, filters=None):
if filters is None:
filters = {}
orm_filters = super(Line_Item_BidsResource, self).build_filters(filters)
if "q" in filters:
search_string = filters['q']
search_terms = search_string.split()
sqs = Line_Item_Bid.objects.filter(pk=-1)
for search_term in search_terms:
sqs |= Line_Item_Bid.objects.filter(bid__price_request_supplier__supplier__name__icontains=search_term)
sqs |= Line_Item_Bid.objects.filter(bid__price_request_supplier__price_request__name__icontains=search_term)
orm_filters["pk__in"] = [i.pk for i in sqs]
if 'bid' in filters:
bid_pk = filters['bid']
orm_filters['bid'] = bid_pk
if 'customer' in filters:
customer_pk = filters['customer']
orm_filters['bid__price_request_supplier__price_request__meters__customer'] = customer_pk
if 'line_item' in filters:
line_item_pk = filters['line_item']
orm_filters['line_item'] = line_item_pk
if 'price_request' in filters:
price_request_pk = filters['price_request']
orm_filters['bid__price_request_supplier__price_request'] = price_request_pk
if 'supplier' in filters:
supplier_pk = filters['supplier']
orm_filters['bid__price_request_supplier__supplier'] = supplier_pk
return orm_filters
def dehydrate_awarded_by_price_request(self, bundle):
retval = None
line_item_bid = bundle.obj
awarded_by_price_request = line_item_bid.awarded_by_price_request
if awarded_by_price_request:
retval = {
"id": awarded_by_price_request.id,
"name": awarded_by_price_request.name,
"resource_uri": bundle.data['awarded_by_price_request'].data['resource_uri'],
}
return retval
def dehydrate_bid(self, bundle):
retval = None
line_item_bid = bundle.obj
bid = line_item_bid.bid
if bid:
retval = {
"id": bid.id,
"resource_uri": bundle.data['bid'].data['resource_uri'],
}
return retval
def dehydrate_line_item(self, bundle):
retval = None
line_item_bid = bundle.obj
line_item = line_item_bid.line_item
if line_item:
retval = {
"id": line_item.id,
"resource_uri": bundle.data['line_item'].data['resource_uri'],
}
return retval
def dehydrate_price_request(self, bundle):
retval = None
line_item_bid = bundle.obj
price_request = line_item_bid.price_request
if price_request:
retval = {
"id": price_request.id,
"name": price_request.name,
"resource_uri": bundle.data['price_request'].data['resource_uri'],
}
return retval
def dehydrate_supplier(self, bundle):
retval = None
line_item_bid = bundle.obj
bid = line_item_bid.bid
if bid:
supplier = bid.supplier
if supplier:
retval = {
"id": supplier.id,
"name": supplier.name,
"resource_uri": bundle.data['supplier'].data['resource_uri'],
}
return retval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.