<s> from django . core . urlresolvers import reverse <EOL> from django . utils . dateparse import parse_datetime <EOL> from rest_framework . views import APIView <EOL> from rest_framework . generics import GenericAPIView <EOL> from rest_framework . response import Response <EOL> from . . mixins import ProviderMixin <EOL> from . . models import Transaction , Organization , Plan <EOL> from . . utils import datetime_or_now <EOL> from . . managers . metrics import monthly_balances <EOL> from . serializers import OrganizationSerializer <EOL> from . . managers . metrics import ( active_subscribers , aggregate_monthly , <EOL> aggregate_monthly_transactions , churn_subscribers ) <EOL> class BalancesAPIView ( ProviderMixin , APIView ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> start_at = request . GET . get ( '<STR_LIT>' , None ) <EOL> if start_at : <EOL> start_at = parse_datetime ( start_at ) <EOL> start_at = datetime_or_now ( start_at ) <EOL> ends_at = request . GET . get ( '<STR_LIT>' , None ) <EOL> if ends_at : <EOL> ends_at = parse_datetime ( ends_at ) <EOL> ends_at = datetime_or_now ( ends_at ) <EOL> result = [ ] <EOL> for key in [ Transaction . INCOME , Transaction . BACKLOG , <EOL> Transaction . RECEIVABLE ] : <EOL> result += [ { <EOL> '<STR_LIT:key>' : key , <EOL> '<STR_LIT>' : monthly_balances ( <EOL> organization = self . provider , account = key , until = ends_at ) <EOL> } ] <EOL> return Response ( { '<STR_LIT:title>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT:$>" , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : result } ) <EOL> class RevenueMetricAPIView ( ProviderMixin , APIView ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> ends_at = request . GET . get ( '<STR_LIT>' , None ) <EOL> if ends_at : <EOL> ends_at = parse_datetime ( ends_at ) <EOL> ends_at = datetime_or_now ( ends_at ) <EOL> account_table , _ , _ = aggregate_monthly_transactions ( self . provider , <EOL> Transaction . RECEIVABLE , account_title = '<STR_LIT>' , <EOL> from_date = ends_at , orig = '<STR_LIT>' , dest = '<STR_LIT>' ) <EOL> _ , payment_amounts = aggregate_monthly ( <EOL> self . provider , Transaction . RECEIVABLE , <EOL> from_date = ends_at , orig = '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> orig_account = Transaction . BACKLOG , <EOL> orig_organization = self . provider ) <EOL> _ , refund_amounts = aggregate_monthly ( <EOL> self . provider , Transaction . REFUND , <EOL> from_date = ends_at , orig = '<STR_LIT>' , dest = '<STR_LIT>' ) <EOL> account_table += [ <EOL> { "<STR_LIT:key>" : "<STR_LIT>" , "<STR_LIT>" : payment_amounts } , <EOL> { "<STR_LIT:key>" : "<STR_LIT>" , "<STR_LIT>" : refund_amounts } ] <EOL> return Response ( <EOL> { "<STR_LIT:title>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:$>" , "<STR_LIT>" : <NUM_LIT> , "<STR_LIT>" : account_table } ) <EOL> class CustomerMetricAPIView ( ProviderMixin , APIView ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> ends_at = request . GET . get ( '<STR_LIT>' , None ) <EOL> if ends_at : <EOL> ends_at = parse_datetime ( ends_at ) <EOL> ends_at = datetime_or_now ( ends_at ) <EOL> account_title = '<STR_LIT>' <EOL> account = Transaction . RECEIVABLE <EOL> _ , customer_table , customer_extra = aggregate_monthly_transactions ( self . provider , account , <EOL> account_title = account_title , <EOL> from_date = ends_at ) <EOL> return Response ( <EOL> { "<STR_LIT:title>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : customer_table , "<STR_LIT>" : customer_extra } ) <EOL> class PlanMetricAPIView ( ProviderMixin , APIView ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> ends_at = request . GET . get ( '<STR_LIT>' , None ) <EOL> if ends_at : <EOL> ends_at = parse_datetime ( ends_at ) <EOL> ends_at = datetime_or_now ( ends_at ) <EOL> table = [ ] <EOL> for plan in Plan . objects . filter ( organization = self . provider ) : <EOL> values = active_subscribers ( <EOL> plan , from_date = self . kwargs . get ( '<STR_LIT>' ) ) <EOL> table . append ( { <EOL> "<STR_LIT:key>" : plan . slug , <EOL> "<STR_LIT>" : values , <EOL> "<STR_LIT:location>" : reverse ( <EOL> '<STR_LIT>' , args = ( self . provider , plan ) ) , <EOL> "<STR_LIT>" : plan . is_active } ) <EOL> extra = [ { "<STR_LIT:key>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : churn_subscribers ( <EOL> from_date = self . kwargs . get ( '<STR_LIT>' ) ) } ] <EOL> return Response ( <EOL> { "<STR_LIT:title>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : table , "<STR_LIT>" : extra } ) <EOL> class OrganizationListAPIView ( ProviderMixin , GenericAPIView ) : <EOL> model = Organization <EOL> serializer_class = OrganizationSerializer <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> start_at = request . GET . get ( '<STR_LIT>' , None ) <EOL> if start_at : <EOL> start_at = parse_datetime ( start_at ) <EOL> start_at = datetime_or_now ( start_at ) <EOL> ends_at = request . GET . get ( '<STR_LIT>' , None ) <EOL> if ends_at : <EOL> ends_at = parse_datetime ( ends_at ) <EOL> ends_at = datetime_or_now ( ends_at ) <EOL> queryset = self . get_range_queryset ( start_at , ends_at ) <EOL> page_object_list = self . paginate_queryset ( queryset ) <EOL> serializer = self . serializer_class ( ) <EOL> return Response ( { <EOL> '<STR_LIT>' : start_at , <EOL> '<STR_LIT>' : ends_at , <EOL> '<STR_LIT:count>' : queryset . count ( ) , <EOL> self . queryset_name : [ serializer . to_representation ( organization ) <EOL> for organization in page_object_list ] , <EOL> } ) </s>
