Example #1
0
def currency_formatter(input,organization):
    if organization != None:
        for payment_info in organization.organizationpaymentinfo_set.all():
            return payment_info.currency.convert_display(input)
    else:
        currency_obj = Currency(order = 1, name= "USD")
        return currency_obj.convert_display(input)
Example #2
0
def currency_formatter(input, organization):
    if organization != None:
        for payment_info in organization.organizationpaymentinfo_set.all():
            return payment_info.currency.convert_display(input)
    else:
        currency_obj = Currency(order=1, name="USD")
        return currency_obj.convert_display(input)
Example #3
0
 def currency_formatter(self, input, organization):
     from atrinsic.base.models import Currency, Organization
     if organization != None and isinstance(organization, Organization):
         currency_obj = Currency(order=1, name=self.currency)
         if (self.currency == None) | (self.exchange_rate == None):
             self.currency = organization.organizationpaymentinfo_set.all(
             )[0].currency.name
             self.currency_obj = Currency(order=1, name=self.currency)
             self.exchange_rate = self.currency_obj.get_exchange_rate(
                 self.currency)
         return currency_obj.convert_display_v2(input,
                                                float(self.exchange_rate),
                                                self.currency)
     else:
         currency_obj = Currency(order=1, name="USD")
         if self.currency == None:
             self.currency = 'USD'
         if self.exchange_rate == None:
             self.exchange_rate = currency_obj.get_exchange_rate(
                 self.currency)
         return currency_obj.convert_display_v2(input,
                                                float(self.exchange_rate),
                                                self.currency)
Example #4
0
 def currency_formatter(self,input,organization):
     from atrinsic.base.models import Currency,Organization
     if organization != None and isinstance(organization,Organization):
         currency_obj = Currency(order = 1, name= self.currency)
         if (self.currency == None) | (self.exchange_rate == None):
             self.currency = organization.organizationpaymentinfo_set.all()[0].currency.name
             self.currency_obj = Currency(order = 1, name = self.currency)
             self.exchange_rate = self.currency_obj.get_exchange_rate(self.currency)
         return currency_obj.convert_display_v2(input,float(self.exchange_rate),self.currency)
     else:
         currency_obj = Currency(order = 1, name= "USD")
         if self.currency == None:
             self.currency = 'USD'
         if self.exchange_rate == None:
             self.exchange_rate = currency_obj.get_exchange_rate(self.currency)
         return currency_obj.convert_display_v2(input,float(self.exchange_rate),self.currency)
Example #5
0
class Report(object):
    def __init__(self,start_date,end_date,organization,group_by=0,spec=[],publisher_set='',advertiser_set='',post_process=None):
        # spec takes a list of tuples, each defining the column.
        # Tuple will be (Display Name,field,formatter,aggregator)
        #  formatter is a function prototype that takes an input and returns a string
        #  aggregator is for generating the footer row
        
        from atrinsic.base.models import Organization

        self.group_iterator = "report_date,"
        self.start_date = start_date
        self.end_date = end_date
        self.table_cache = {}
        self.raw_data = []
        self.results = []
        self.organization = organization
        self.publisher_set = ''
        self.advertiser_set = ''
        self.iterator_header = ""
        self.post_process = post_process
        self.currency = None
        self.exchange_rate = None
        self.cache = {}
        self.data = None
        self.data_set = None
        self.report_query = None
        if int(group_by) == 0:
            self.group_by = "report_date"
        elif int(group_by) == 1:
            self.group_by = "WEEKOFYEAR(report_date)"
        elif int(group_by) == 2:
            self.group_by = "MONTH(report_date)"
        elif int(group_by) == 3:
            self.group_by = "QUARTER(report_date)"
        elif int(group_by) == 4:
            self.group_by = "website_url"
            self.group_iterator = "website_url,"
        
        if isinstance(self.organization,Organization):
            if self.organization.is_publisher():
                self.org_type = "publisher"
                self.organization_id = "and publisher_id = %d" % self.organization.id
            else:
                self.org_type = "advertiser"
                self.organization_id = "and advertiser_id = %d" % self.organization.id
        else:
            self.org_type = self.organization
            self.organization_id = ""
        
        self.spec = specs[self.org_type][spec]
        
        if self.group_by == "website_url":
            if self.spec[0][0] != 'Website' and self.spec[0][0] == "Date":
                self.spec.pop(0)
                self.spec.insert(0,('Website','website_url','null_formatter',null_aggregator))
        
        if publisher_set != '':
            if isinstance(publisher_set,type([])):
                id_set = ''
                for id in publisher_set:
                    if isinstance(id,unicode):
                        id_set+="%s," % id[2:]
                    else:
                        id_set+="%s," % id
                if id_set != '':
                    self.publisher_set = "and publisher_id in (%s)" % id_set[:-1]
            else:
                self.publisher_set = "and publisher_id = %s" % publisher_set[2:]
        if advertiser_set != '':
            if isinstance(advertiser_set,type([])):
                id_set = ''
                for id in advertiser_set:
                    if isinstance(id,unicode):
                        id_set+="%s," % id[2:]
                    else:
                        id_set+="%s," % id
                if id_set != '':
                    self.advertiser_set = "and advertiser_id in (%s)" % id_set[:-1]
            elif isinstance(advertiser_set,QuerySet):
                id_set = ''
                for adv in advertiser_set:
                    id_set+="%s," % adv.id
                if id_set != '':
                    self.advertiser_set = "and advertiser_id in (%s)" % id_set[:-1]
            else:
                self.advertiser_set = "and advertiser_id = %s" % advertiser_set[2:]
                
        self.GetBaseQuerySet()
    def GetBaseQuerySet(self):
        return []
        
    def RenderHeader(self):
        result = []
        for col in self.spec:
            result.append((col[0],col[1]))

        return result

    def RenderContents(self,as_rows = True):
        from django.db import connection, transaction
        from django.utils.encoding import smart_str
        cursor = connection.cursor()
        cursor.execute(self.report_query)

        query_set = today_var1 = cursor.fetchall()

        self.results = query_set
        formatted_row = []
        formatted_set = []
        row_count = 0
        
        for row in query_set:
            row_count += 1
            for_index = 0
            for col in row:                
                try:
                    formatter = self.spec[for_index][2]
                    if as_rows:
                        formatted_row.append(smart_str(getattr(self, formatter)(col,self.organization)))
                    else:
                        formatted_set.append(smart_str(getattr(self, formatter)(col,self.organization)))
                    for_index += 1
                except:
                    pass
            if as_rows:
                formatted_set.append(formatted_row)
                formatted_row = []
        if as_rows:
            return formatted_set
        else:
            return formatted_set,row_count

    def RenderFooter(self):
        i = 1
        result = [("Grand Total","Grand Total")]
        for display,field,formatter,aggregator in self.spec[1:]:
            try:
                if field == 'clicks':
                    click_index = i
                if field == 'leads':
                    lead_index = i
                if field == 'orders':
                    order_index = i
                if aggregator == average_aggregator:
                    if field == 'conversion_click_to_lead':
                        agg=float(result[lead_index][0])/float(result[click_index][0])*100
                        result.append((agg,getattr(self,formatter)(agg,self.organization)))
                    elif field == 'conversion_click_to_order':
                        agg=float(result[order_index][0])/float(result[click_index][0])*100
                        result.append((agg,getattr(self,formatter)(agg,self.organization)))
                    else:
                        agg = aggregator([x[i] for x in self.results])
                        result.append((agg,getattr(self,formatter)(agg,self.organization)))
                else:
                    agg = aggregator([x[i] for x in self.results])
                    result.append((agg,getattr(self,formatter)(agg,self.organization)))
            except:
                result.append("-")
            i += 1
        return result
        
    def currency_formatter(self,input,organization):
        from atrinsic.base.models import Currency,Organization
        if organization != None and isinstance(organization,Organization):
            currency_obj = Currency(order = 1, name= self.currency)
            if (self.currency == None) | (self.exchange_rate == None):
                self.currency = organization.organizationpaymentinfo_set.all()[0].currency.name
                self.currency_obj = Currency(order = 1, name = self.currency)
                self.exchange_rate = self.currency_obj.get_exchange_rate(self.currency)
            return currency_obj.convert_display_v2(input,float(self.exchange_rate),self.currency)
        else:
            currency_obj = Currency(order = 1, name= "USD")
            if self.currency == None:
                self.currency = 'USD'
            if self.exchange_rate == None:
                self.exchange_rate = currency_obj.get_exchange_rate(self.currency)
            return currency_obj.convert_display_v2(input,float(self.exchange_rate),self.currency)
        
    def null_formatter(self,input,organization):
        from django.utils.encoding import smart_str
        return smart_str(input)
        
    def percent_formatter(self,input,organization):
        return "%0.2f%%" % (input)
    
    def whole_formatter(self,input,organization):
        return FormatWithCommas("%d",int(round(input)))
    
    def link_formatter(self,input,organization):
        return u"<a href=''>%s</a>" % input
        
    def date_formatter(self,input,organization):
        try:
            return input.strftime("%m/%d/%Y")
        except:
            return null_formatter(input,organization)
Example #6
0
class Report(object):
    def __init__(self,
                 start_date,
                 end_date,
                 organization,
                 group_by=0,
                 spec=[],
                 publisher_set='',
                 advertiser_set='',
                 post_process=None):
        # spec takes a list of tuples, each defining the column.
        # Tuple will be (Display Name,field,formatter,aggregator)
        #  formatter is a function prototype that takes an input and returns a string
        #  aggregator is for generating the footer row

        from atrinsic.base.models import Organization

        self.group_iterator = "report_date,"
        self.start_date = start_date
        self.end_date = end_date
        self.table_cache = {}
        self.raw_data = []
        self.results = []
        self.organization = organization
        self.publisher_set = ''
        self.advertiser_set = ''
        self.iterator_header = ""
        self.post_process = post_process
        self.currency = None
        self.exchange_rate = None
        self.cache = {}
        self.data = None
        self.data_set = None
        self.report_query = None
        if int(group_by) == 0:
            self.group_by = "report_date"
        elif int(group_by) == 1:
            self.group_by = "WEEKOFYEAR(report_date)"
        elif int(group_by) == 2:
            self.group_by = "MONTH(report_date)"
        elif int(group_by) == 3:
            self.group_by = "QUARTER(report_date)"
        elif int(group_by) == 4:
            self.group_by = "website_url"
            self.group_iterator = "website_url,"

        if isinstance(self.organization, Organization):
            if self.organization.is_publisher():
                self.org_type = "publisher"
                self.organization_id = "and publisher_id = %d" % self.organization.id
            else:
                self.org_type = "advertiser"
                self.organization_id = "and advertiser_id = %d" % self.organization.id
        else:
            self.org_type = self.organization
            self.organization_id = ""

        self.spec = specs[self.org_type][spec]

        if self.group_by == "website_url":
            if self.spec[0][0] != 'Website' and self.spec[0][0] == "Date":
                self.spec.pop(0)
                self.spec.insert(0, ('Website', 'website_url',
                                     'null_formatter', null_aggregator))

        if publisher_set != '':
            if isinstance(publisher_set, type([])):
                id_set = ''
                for id in publisher_set:
                    if isinstance(id, unicode):
                        id_set += "%s," % id[2:]
                    else:
                        id_set += "%s," % id
                if id_set != '':
                    self.publisher_set = "and publisher_id in (%s)" % id_set[:
                                                                             -1]
            else:
                self.publisher_set = "and publisher_id = %s" % publisher_set[2:]
        if advertiser_set != '':
            if isinstance(advertiser_set, type([])):
                id_set = ''
                for id in advertiser_set:
                    if isinstance(id, unicode):
                        id_set += "%s," % id[2:]
                    else:
                        id_set += "%s," % id
                if id_set != '':
                    self.advertiser_set = "and advertiser_id in (%s)" % id_set[:
                                                                               -1]
            elif isinstance(advertiser_set, QuerySet):
                id_set = ''
                for adv in advertiser_set:
                    id_set += "%s," % adv.id
                if id_set != '':
                    self.advertiser_set = "and advertiser_id in (%s)" % id_set[:
                                                                               -1]
            else:
                self.advertiser_set = "and advertiser_id = %s" % advertiser_set[
                    2:]

        self.GetBaseQuerySet()

    def GetBaseQuerySet(self):
        return []

    def RenderHeader(self):
        result = []
        for col in self.spec:
            result.append((col[0], col[1]))

        return result

    def RenderContents(self, as_rows=True):
        from django.db import connection, transaction
        from django.utils.encoding import smart_str
        cursor = connection.cursor()
        cursor.execute(self.report_query)

        query_set = today_var1 = cursor.fetchall()

        self.results = query_set
        formatted_row = []
        formatted_set = []
        row_count = 0

        for row in query_set:
            row_count += 1
            for_index = 0
            for col in row:
                try:
                    formatter = self.spec[for_index][2]
                    if as_rows:
                        formatted_row.append(
                            smart_str(
                                getattr(self, formatter)(col,
                                                         self.organization)))
                    else:
                        formatted_set.append(
                            smart_str(
                                getattr(self, formatter)(col,
                                                         self.organization)))
                    for_index += 1
                except:
                    pass
            if as_rows:
                formatted_set.append(formatted_row)
                formatted_row = []
        if as_rows:
            return formatted_set
        else:
            return formatted_set, row_count

    def RenderFooter(self):
        i = 1
        result = [("Grand Total", "Grand Total")]
        for display, field, formatter, aggregator in self.spec[1:]:
            try:
                if field == 'clicks':
                    click_index = i
                if field == 'leads':
                    lead_index = i
                if field == 'orders':
                    order_index = i
                if aggregator == average_aggregator:
                    if field == 'conversion_click_to_lead':
                        agg = float(result[lead_index][0]) / float(
                            result[click_index][0]) * 100
                        result.append(
                            (agg, getattr(self, formatter)(agg,
                                                           self.organization)))
                    elif field == 'conversion_click_to_order':
                        agg = float(result[order_index][0]) / float(
                            result[click_index][0]) * 100
                        result.append(
                            (agg, getattr(self, formatter)(agg,
                                                           self.organization)))
                    else:
                        agg = aggregator([x[i] for x in self.results])
                        result.append(
                            (agg, getattr(self, formatter)(agg,
                                                           self.organization)))
                else:
                    agg = aggregator([x[i] for x in self.results])
                    result.append((agg, getattr(self,
                                                formatter)(agg,
                                                           self.organization)))
            except:
                result.append("-")
            i += 1
        return result

    def currency_formatter(self, input, organization):
        from atrinsic.base.models import Currency, Organization
        if organization != None and isinstance(organization, Organization):
            currency_obj = Currency(order=1, name=self.currency)
            if (self.currency == None) | (self.exchange_rate == None):
                self.currency = organization.organizationpaymentinfo_set.all(
                )[0].currency.name
                self.currency_obj = Currency(order=1, name=self.currency)
                self.exchange_rate = self.currency_obj.get_exchange_rate(
                    self.currency)
            return currency_obj.convert_display_v2(input,
                                                   float(self.exchange_rate),
                                                   self.currency)
        else:
            currency_obj = Currency(order=1, name="USD")
            if self.currency == None:
                self.currency = 'USD'
            if self.exchange_rate == None:
                self.exchange_rate = currency_obj.get_exchange_rate(
                    self.currency)
            return currency_obj.convert_display_v2(input,
                                                   float(self.exchange_rate),
                                                   self.currency)

    def null_formatter(self, input, organization):
        from django.utils.encoding import smart_str
        return smart_str(input)

    def percent_formatter(self, input, organization):
        return "%0.2f%%" % (input)

    def whole_formatter(self, input, organization):
        return FormatWithCommas("%d", int(round(input)))

    def link_formatter(self, input, organization):
        return u"<a href=''>%s</a>" % input

    def date_formatter(self, input, organization):
        try:
            return input.strftime("%m/%d/%Y")
        except:
            return null_formatter(input, organization)