Esempio n. 1
0
class BillboardsView(ListView):
    template_name = 'core/billboards_list.html'
    model = Facebook_Feed

    stats = StatsEngine()

    def get_queryset(self, **kwargs):
        return Facebook_Feed.current_feeds.first()

    def get_context_data(self, **kwargs):
        context = super(BillboardsView, self).get_context_data(**kwargs)

        billboards = Billboards()

        context['list_of_billboards'] = []
        context['list_of_billboards'].append(
            billboards.number_of_followers_board)
        # context['list_of_billboards'].append(billboards.popularity_relative_growth_board)
        context['list_of_billboards'].append(
            billboards.popularity_growth_board)
        context['list_of_billboards'].append(billboards.number_of_status_board)
        context['list_of_billboards'].append(
            billboards.median_status_likes_board)
        context['list_of_billboards'].append(billboards.top_likes_board)

        context['list_of_billboards'] = sorted(
            context['list_of_billboards'], key=lambda x: x['order_of_board'])

        return context
Esempio n. 2
0
    def __init__(self):
        self.stats = StatsEngine()
        self.number_of_followers_board = self.create_billboard_dict(title=_('Number of Followers'),
                                                                    second_title=_('Total'),
                                                                    header_name=_('MK'),
                                                                    header_value_formatted='current_fan_count',
                                                                    link_uri_name='member',
                                                                    value_format="{:,}",
                                                                    top_num_of_values=TOP_NUM_OF_VALUES,
                                                                    is_sorted_reversed=True,
                                                                    data_set=Facebook_Feed.current_feeds.all(),
                                                                    data_name_attr='persona.owner.name',
                                                                    data_value_float_attr='current_fan_count',
                                                                    link_value_attr='persona.owner_id',
                                                                    calc_type='attribute',
                                                                    arguments_for_function=None,
                                                                    order_of_board=6,
                                                                    )

        self.popularity_growth_board = self.create_billboard_dict(title=_('Growth in Number of Followers'),
                                                                  second_title=DEFAULT_SECOND_TITLE,
                                                                  header_name='Name',
                                                                  header_value_formatted='growth in popularity (likes)',
                                                                  link_uri_name='member',
                                                                  value_format="{:,.0f}",
                                                                  top_num_of_values=TOP_NUM_OF_VALUES,
                                                                  is_sorted_reversed=True,
                                                                  data_set=Facebook_Feed.current_feeds.all(),
                                                                  data_name_attr='persona.owner.name',
                                                                  data_value_float_attr='popularity_dif',
                                                                  link_value_attr='persona.owner_id',
                                                                  calc_type='function',
                                                                  arguments_for_function={'days_back': DAYS_BACK,
                                                                                          'return_value': 'fan_count_dif_nominal'},
                                                                  order_of_board=2,
                                                                  )

        self.number_of_status_board = self.create_billboard_dict(title=_('Number of Statuses Published'),
                                                                 second_title=DEFAULT_SECOND_TITLE,
                                                                 header_name='Name',
                                                                 header_value_formatted='number of statuses last week',
                                                                 link_uri_name='member',
                                                                 value_format="{:,.0f}",
                                                                 top_num_of_values=TOP_NUM_OF_VALUES,
                                                                 is_sorted_reversed=True,
                                                                 data_set=Facebook_Feed.current_feeds.all(),
                                                                 data_name_attr='persona.owner.name',
                                                                 data_value_float_attr='n_statuses_last_week',
                                                                 link_value_attr='persona.owner_id',
                                                                 calc_type='stats',
                                                                 arguments_for_function=None,
                                                                 order_of_board=3,
                                                                 )

        self.median_status_likes_board = self.create_billboard_dict(title=_('Median #like Count per Status'),
                                                                    second_title=DEFAULT_SECOND_TITLE,
                                                                    header_name='Name',
                                                                    header_value_formatted='median_status_likes_last_week',
                                                                    link_uri_name='member',
                                                                    value_format="{:,.0f}",
                                                                    top_num_of_values=TOP_NUM_OF_VALUES,
                                                                    is_sorted_reversed=True,
                                                                    data_set=Facebook_Feed.current_feeds.all(),
                                                                    data_name_attr='persona.owner.name',
                                                                    data_value_float_attr='median_status_likes_last_week',
                                                                    link_value_attr='persona.owner_id',
                                                                    calc_type='stats',
                                                                    arguments_for_function=None,
                                                                    order_of_board=4,
                                                                    )

        self.top_likes_board = {
            'order_of_board': 5,
            'title': _('Top Statuses by Number of  #like '),
            'second_title': DEFAULT_SECOND_TITLE,
            'headers': {
                'name': 'Name',
                'value_formatted': 'likes for most popular status'
            },
            'link_uri_name': 'status-detail',
            'data': [
                {'name': Facebook_Status.objects.get(id=result_array[0]).feed.persona.owner.name,
                 'value_int': float(result_array[1]),
                 'value_formatted': "{:,.0f}".format(result_array[1]),
                 'value_reference_link': Facebook_Status.objects.get(id=result_array[0]).status_id,
                 }
                for result_array in self.stats.popular_statuses_last_week(
                        [feed.id for feed in Facebook_Feed.current_feeds.all()], TOP_NUM_OF_VALUES)
                ]
        }
Esempio n. 3
0
    def __init__(self):
        self.stats = StatsEngine()
        self.number_of_followers_board = self.create_billboard_dict(
            title='מספר עוקבים',
            second_title='סך הכל',
            header_name='חבר כנסת',
            header_value_formatted='current_fan_count',
            link_uri_name='member',
            value_format="{:,}",
            top_num_of_values=TOP_NUM_OF_VALUES,
            is_sorted_reversed=True,
            data_set=Facebook_Feed.current_feeds.all(),
            data_name_attr='persona.content_object.name',
            data_value_float_attr='current_fan_count',
            link_value_attr='persona.object_id',
            calc_type='attribute',
            arguments_for_function=None,
            order_of_board=6,
        )

        # self.popularity_relative_growth_board = self.create_billboard_dict(title='עלייה במספר העוקבים',
        # header_name='Name',
        #                                               header_value_formatted='growth popularity',
        #                                               link_uri_name='member',
        #                                               value_format="{:.2%}",
        #                                               top_num_of_values=TOP_NUM_OF_VALUES,
        #                                               is_sorted_reversed=True,
        #                                               data_set=Facebook_Feed.current_feeds.filter(feed_type='PP'),
        #                                               data_name_attr='persona.content_object.name',
        #                                               data_value_float_attr='popularity_dif',
        #                                               link_value_attr='persona.object_id',
        #                                               calc_type='function',
        #                                               arguments_for_function={'days_back': DAYS_BACK,
        #                                                                       'return_value': 'fan_count_dif_growth_rate'},
        #                                               order_of_board=1,
        # )
        #
        self.popularity_growth_board = self.create_billboard_dict(
            title='עלייה במספר העוקבים',
            second_title=DEFAULT_SECOND_TITLE,
            header_name='Name',
            header_value_formatted='growth in popularity (likes)',
            link_uri_name='member',
            value_format="{:,.0f}",
            top_num_of_values=TOP_NUM_OF_VALUES,
            is_sorted_reversed=True,
            data_set=Facebook_Feed.current_feeds.all(),
            data_name_attr='persona.content_object.name',
            data_value_float_attr='popularity_dif',
            link_value_attr='persona.object_id',
            calc_type='function',
            arguments_for_function={
                'days_back': DAYS_BACK,
                'return_value': 'fan_count_dif_nominal'
            },
            order_of_board=2,
        )

        self.number_of_status_board = self.create_billboard_dict(
            title='כמות הסטאטוסים',
            second_title=DEFAULT_SECOND_TITLE,
            header_name='Name',
            header_value_formatted='number of statuses last week',
            link_uri_name='member',
            value_format="{:,.0f}",
            top_num_of_values=TOP_NUM_OF_VALUES,
            is_sorted_reversed=True,
            data_set=Facebook_Feed.current_feeds.all(),
            data_name_attr='persona.content_object.name',
            data_value_float_attr='n_statuses_last_week',
            link_value_attr='persona.object_id',
            calc_type='stats',
            arguments_for_function=None,
            order_of_board=3,
        )

        self.median_status_likes_board = self.create_billboard_dict(
            title='כמות חציונית של #like לסטאטוס',
            second_title=DEFAULT_SECOND_TITLE,
            header_name='Name',
            header_value_formatted='median_status_likes_last_week',
            link_uri_name='member',
            value_format="{:,.0f}",
            top_num_of_values=TOP_NUM_OF_VALUES,
            is_sorted_reversed=True,
            data_set=Facebook_Feed.current_feeds.all(),
            data_name_attr='persona.content_object.name',
            data_value_float_attr='median_status_likes_last_week',
            link_value_attr='persona.object_id',
            calc_type='stats',
            arguments_for_function=None,
            order_of_board=4,
        )

        self.top_likes_board = {
            'order_of_board':
            5,
            'title':
            'הסטטוס עם כמות ה-#like הגבוהה ביותר',
            'second_title':
            DEFAULT_SECOND_TITLE,
            'headers': {
                'name': 'Name',
                'value_formatted': 'likes for most popular status'
            },
            'link_uri_name':
            'status-detail',
            'data': [{
                'name':
                Facebook_Status.objects.get(
                    id=result_array[0]).feed.persona.content_object.name,
                'value_int':
                float(result_array[1]),
                'value_formatted':
                "{:,.0f}".format(result_array[1]),
                'value_reference_link':
                Facebook_Status.objects.get(id=result_array[0]).status_id,
            } for result_array in self.stats.popular_statuses_last_week(
                [feed.id for feed in Facebook_Feed.current_feeds.all()],
                TOP_NUM_OF_VALUES)]
        }
Esempio n. 4
0
class Billboards(object):
    def __init__(self):
        self.stats = StatsEngine()
        self.number_of_followers_board = self.create_billboard_dict(title=_('Number of Followers'),
                                                                    second_title=_('Total'),
                                                                    header_name=_('MK'),
                                                                    header_value_formatted='current_fan_count',
                                                                    link_uri_name='member',
                                                                    value_format="{:,}",
                                                                    top_num_of_values=TOP_NUM_OF_VALUES,
                                                                    is_sorted_reversed=True,
                                                                    data_set=Facebook_Feed.current_feeds.all(),
                                                                    data_name_attr='persona.owner.name',
                                                                    data_value_float_attr='current_fan_count',
                                                                    link_value_attr='persona.owner_id',
                                                                    calc_type='attribute',
                                                                    arguments_for_function=None,
                                                                    order_of_board=6,
                                                                    )

        self.popularity_growth_board = self.create_billboard_dict(title=_('Growth in Number of Followers'),
                                                                  second_title=DEFAULT_SECOND_TITLE,
                                                                  header_name='Name',
                                                                  header_value_formatted='growth in popularity (likes)',
                                                                  link_uri_name='member',
                                                                  value_format="{:,.0f}",
                                                                  top_num_of_values=TOP_NUM_OF_VALUES,
                                                                  is_sorted_reversed=True,
                                                                  data_set=Facebook_Feed.current_feeds.all(),
                                                                  data_name_attr='persona.owner.name',
                                                                  data_value_float_attr='popularity_dif',
                                                                  link_value_attr='persona.owner_id',
                                                                  calc_type='function',
                                                                  arguments_for_function={'days_back': DAYS_BACK,
                                                                                          'return_value': 'fan_count_dif_nominal'},
                                                                  order_of_board=2,
                                                                  )

        self.number_of_status_board = self.create_billboard_dict(title=_('Number of Statuses Published'),
                                                                 second_title=DEFAULT_SECOND_TITLE,
                                                                 header_name='Name',
                                                                 header_value_formatted='number of statuses last week',
                                                                 link_uri_name='member',
                                                                 value_format="{:,.0f}",
                                                                 top_num_of_values=TOP_NUM_OF_VALUES,
                                                                 is_sorted_reversed=True,
                                                                 data_set=Facebook_Feed.current_feeds.all(),
                                                                 data_name_attr='persona.owner.name',
                                                                 data_value_float_attr='n_statuses_last_week',
                                                                 link_value_attr='persona.owner_id',
                                                                 calc_type='stats',
                                                                 arguments_for_function=None,
                                                                 order_of_board=3,
                                                                 )

        self.median_status_likes_board = self.create_billboard_dict(title=_('Median #like Count per Status'),
                                                                    second_title=DEFAULT_SECOND_TITLE,
                                                                    header_name='Name',
                                                                    header_value_formatted='median_status_likes_last_week',
                                                                    link_uri_name='member',
                                                                    value_format="{:,.0f}",
                                                                    top_num_of_values=TOP_NUM_OF_VALUES,
                                                                    is_sorted_reversed=True,
                                                                    data_set=Facebook_Feed.current_feeds.all(),
                                                                    data_name_attr='persona.owner.name',
                                                                    data_value_float_attr='median_status_likes_last_week',
                                                                    link_value_attr='persona.owner_id',
                                                                    calc_type='stats',
                                                                    arguments_for_function=None,
                                                                    order_of_board=4,
                                                                    )

        self.top_likes_board = {
            'order_of_board': 5,
            'title': _('Top Statuses by Number of  #like '),
            'second_title': DEFAULT_SECOND_TITLE,
            'headers': {
                'name': 'Name',
                'value_formatted': 'likes for most popular status'
            },
            'link_uri_name': 'status-detail',
            'data': [
                {'name': Facebook_Status.objects.get(id=result_array[0]).feed.persona.owner.name,
                 'value_int': float(result_array[1]),
                 'value_formatted': "{:,.0f}".format(result_array[1]),
                 'value_reference_link': Facebook_Status.objects.get(id=result_array[0]).status_id,
                 }
                for result_array in self.stats.popular_statuses_last_week(
                        [feed.id for feed in Facebook_Feed.current_feeds.all()], TOP_NUM_OF_VALUES)
                ]
        }

    def create_billboard_dict(self,
                              order_of_board,
                              title,
                              second_title,
                              header_name,
                              header_value_formatted,
                              link_uri_name,
                              value_format,
                              data_set,
                              data_name_attr,
                              data_value_float_attr,
                              top_num_of_values,
                              calc_type,
                              link_value_attr,
                              arguments_for_function=None,
                              is_sorted_reversed=True):

        billboard_dict = {
            'order_of_board': order_of_board,
            'title': title,
            'second_title': second_title,
            'headers': {
                'name': header_name,
                'value_formatted': header_value_formatted,
            },
            'link_uri_name': link_uri_name,
            'data': self.create_billboard_data_dict_list(value_format,
                                                         data_set,
                                                         data_name_attr,
                                                         data_value_float_attr,
                                                         calc_type,
                                                         arguments_for_function,
                                                         link_value_attr),
        }

        billboard_dict['data'] = sorted(billboard_dict['data'], key=lambda x: x['value_int'],
                                        reverse=is_sorted_reversed)[
                                 :top_num_of_values]

        return billboard_dict

    def create_billboard_data_dict_list(self,
                                        value_format,
                                        data_set,
                                        data_name_attr,
                                        data_value_int_attr,
                                        calc_type,
                                        arguments_for_function,
                                        link_value_attr):

        if calc_type == 'function':
            data_dict_list = [
                {'name': getattrd(object_instance, data_name_attr),
                 'value_int': float(getattrd(object_instance, data_value_int_attr)(**arguments_for_function) or 0),
                 'value_formatted': value_format.format(
                         getattrd(object_instance, data_value_int_attr)(**arguments_for_function) or 0),
                 'value_reference_link': getattrd(object_instance, link_value_attr),
                 }
                for object_instance in data_set
                ]

        elif calc_type == 'stats':

            stats_method = getattrd(self.stats, data_value_int_attr)

            data_dict_list = [
                {'name': getattrd(object_instance, data_name_attr),
                 'value_int': float(stats_method([object_instance.id]) or 0),
                 'value_formatted': value_format.format(stats_method([object_instance.id]) or 0),
                 'value_reference_link': getattrd(object_instance, link_value_attr),
                 }
                for object_instance in data_set
                ]
        else:
            # calc_type == 'attribute':
            data_dict_list = [
                {'name': getattrd(object_instance, data_name_attr),
                 'value_int': float(getattrd(object_instance, data_value_int_attr)),
                 'value_formatted': value_format.format(getattrd(object_instance, data_value_int_attr)),
                 'value_reference_link': getattrd(object_instance, link_value_attr),
                 }
                for object_instance in data_set
                ]

        return data_dict_list
Esempio n. 5
0
    def __init__(self):
        self.stats = StatsEngine()
        self.number_of_followers_board = self.create_billboard_dict(title='מספר עוקבים',
                                                                    second_title='סך הכל',
                                                                    header_name='חבר כנסת',
                                                                    header_value_formatted='current_fan_count',
                                                                    link_uri_name='member',
                                                                    value_format="{:,}",
                                                                    top_num_of_values=TOP_NUM_OF_VALUES,
                                                                    is_sorted_reversed=True,
                                                                    data_set=Facebook_Feed.current_feeds.all(),
                                                                    data_name_attr='persona.content_object.name',
                                                                    data_value_float_attr='current_fan_count',
                                                                    link_value_attr='persona.object_id',
                                                                    calc_type='attribute',
                                                                    arguments_for_function=None,
                                                                    order_of_board=6,
        )

        # self.popularity_relative_growth_board = self.create_billboard_dict(title='עלייה במספר העוקבים',
        # header_name='Name',
        #                                               header_value_formatted='growth popularity',
        #                                               link_uri_name='member',
        #                                               value_format="{:.2%}",
        #                                               top_num_of_values=TOP_NUM_OF_VALUES,
        #                                               is_sorted_reversed=True,
        #                                               data_set=Facebook_Feed.current_feeds.filter(feed_type='PP'),
        #                                               data_name_attr='persona.content_object.name',
        #                                               data_value_float_attr='popularity_dif',
        #                                               link_value_attr='persona.object_id',
        #                                               calc_type='function',
        #                                               arguments_for_function={'days_back': DAYS_BACK,
        #                                                                       'return_value': 'fan_count_dif_growth_rate'},
        #                                               order_of_board=1,
        # )
        #
        self.popularity_growth_board = self.create_billboard_dict(title='עלייה במספר העוקבים',
                                                                  second_title=DEFAULT_SECOND_TITLE,
                                                                  header_name='Name',
                                                                  header_value_formatted='growth in popularity (likes)',
                                                                  link_uri_name='member',
                                                                  value_format="{:,.0f}",
                                                                  top_num_of_values=TOP_NUM_OF_VALUES,
                                                                  is_sorted_reversed=True,
                                                                  data_set=Facebook_Feed.current_feeds.all(),
                                                                  data_name_attr='persona.content_object.name',
                                                                  data_value_float_attr='popularity_dif',
                                                                  link_value_attr='persona.object_id',
                                                                  calc_type='function',
                                                                  arguments_for_function={'days_back': DAYS_BACK,
                                                                                          'return_value': 'fan_count_dif_nominal'},
                                                                  order_of_board=2,
        )

        self.number_of_status_board = self.create_billboard_dict(title='כמות הסטאטוסים',
                                                                 second_title=DEFAULT_SECOND_TITLE,
                                                                 header_name='Name',
                                                                 header_value_formatted='number of statuses last week',
                                                                 link_uri_name='member',
                                                                 value_format="{:,.0f}",
                                                                 top_num_of_values=TOP_NUM_OF_VALUES,
                                                                 is_sorted_reversed=True,
                                                                 data_set=Facebook_Feed.current_feeds.all(),
                                                                 data_name_attr='persona.content_object.name',
                                                                 data_value_float_attr='n_statuses_last_week',
                                                                 link_value_attr='persona.object_id',
                                                                 calc_type='stats',
                                                                 arguments_for_function=None,
                                                                 order_of_board=3,
        )

        self.median_status_likes_board = self.create_billboard_dict(title='כמות חציונית של #like לסטאטוס',
                                                                    second_title=DEFAULT_SECOND_TITLE,
                                                                    header_name='Name',
                                                                    header_value_formatted='median_status_likes_last_week',
                                                                    link_uri_name='member',
                                                                    value_format="{:,.0f}",
                                                                    top_num_of_values=TOP_NUM_OF_VALUES,
                                                                    is_sorted_reversed=True,
                                                                    data_set=Facebook_Feed.current_feeds.all(),
                                                                    data_name_attr='persona.content_object.name',
                                                                    data_value_float_attr='median_status_likes_last_week',
                                                                    link_value_attr='persona.object_id',
                                                                    calc_type='stats',
                                                                    arguments_for_function=None,
                                                                    order_of_board=4,
        )

        self.top_likes_board = {
            'order_of_board': 5,
            'title': 'הסטטוס עם כמות ה-#like הגבוהה ביותר',
            'second_title': DEFAULT_SECOND_TITLE,
            'headers': {
                'name': 'Name',
                'value_formatted': 'likes for most popular status'
            },
            'link_uri_name': 'status-detail',
            'data': [
                {'name': Facebook_Status.objects.get(id=result_array[0]).feed.persona.content_object.name,
                 'value_int': float(result_array[1]),
                 'value_formatted': "{:,.0f}".format(result_array[1]),
                 'value_reference_link': Facebook_Status.objects.get(id=result_array[0]).status_id,
                }
                for result_array in self.stats.popular_statuses_last_week(
                    [feed.id for feed in Facebook_Feed.current_feeds.all()], TOP_NUM_OF_VALUES)
            ]
        }
Esempio n. 6
0
class BillboardsView(ListView):
    template_name = 'core/billboards_list.html'
    model = Facebook_Feed

    stats = StatsEngine()

    def get_queryset(self, **kwargs):
        return Facebook_Feed.current_feeds.first()

    def create_billboard_data_dict_list(self, value_format, data_set,
                                        data_name_attr, data_value_int_attr,
                                        calc_type, arguments_for_function,
                                        link_value_attr):

        if calc_type == 'function':
            data_dict_list = [{
                'name':
                getattrd(object_instance, data_name_attr),
                'value_int':
                float(
                    getattrd(object_instance,
                             data_value_int_attr)(**arguments_for_function)
                    or 0),
                'value_formatted':
                value_format.format(
                    getattrd(object_instance,
                             data_value_int_attr)(**arguments_for_function)
                    or 0),
                'value_reference_link':
                getattrd(object_instance, link_value_attr),
            } for object_instance in data_set]

        elif calc_type == 'stats':

            stats_method = getattrd(self.stats, data_value_int_attr)

            data_dict_list = [{
                'name':
                getattrd(object_instance, data_name_attr),
                'value_int':
                float(stats_method([object_instance.id]) or 0),
                'value_formatted':
                value_format.format(stats_method([object_instance.id]) or 0),
                'value_reference_link':
                getattrd(object_instance, link_value_attr),
            } for object_instance in data_set]
        else:
            # calc_type == 'attribute':
            data_dict_list = [{
                'name':
                getattrd(object_instance, data_name_attr),
                'value_int':
                float(getattrd(object_instance, data_value_int_attr)),
                'value_formatted':
                value_format.format(
                    getattrd(object_instance, data_value_int_attr)),
                'value_reference_link':
                getattrd(object_instance, link_value_attr),
            } for object_instance in data_set]

        return data_dict_list

    def create_billboard_dict(self,
                              title,
                              header_name,
                              header_value_formatted,
                              link_uri_name,
                              value_format,
                              data_set,
                              data_name_attr,
                              data_value_float_attr,
                              top_num_of_values,
                              calc_type,
                              link_value_attr,
                              arguments_for_function=None,
                              is_sorted_reversed=True):
        billboard_dict = {
            'title':
            title,
            'headers': {
                'name': header_name,
                'value_formatted': header_value_formatted,
            },
            'link_uri_name':
            link_uri_name,
            'data':
            self.create_billboard_data_dict_list(
                value_format, data_set, data_name_attr, data_value_float_attr,
                calc_type, arguments_for_function, link_value_attr),
        }

        billboard_dict['data'] = sorted(
            billboard_dict['data'],
            key=lambda x: x['value_int'],
            reverse=is_sorted_reversed)[:top_num_of_values]

        return billboard_dict

    def get_context_data(self, **kwargs):
        context = super(BillboardsView, self).get_context_data(**kwargs)

        billboard_1 = self.create_billboard_dict(
            title='popularity',
            header_name='Name',
            header_value_formatted='current_fan_count',
            link_uri_name='member',
            value_format="{:,}",
            top_num_of_values=10,
            is_sorted_reversed=True,
            data_set=Facebook_Feed.current_feeds.all(),
            data_name_attr='persona.content_object.name',
            data_value_float_attr='current_fan_count',
            link_value_attr='persona.object_id',
            calc_type='attribute',
            arguments_for_function=None,
        )

        billboard_2 = self.create_billboard_dict(
            title='popularity_growth',
            header_name='Name',
            header_value_formatted='growth popularity',
            link_uri_name='member',
            value_format="{:.2%}",
            top_num_of_values=10,
            is_sorted_reversed=True,
            data_set=Facebook_Feed.current_feeds.filter(feed_type='PP'),
            data_name_attr='persona.content_object.name',
            data_value_float_attr='popularity_dif',
            link_value_attr='persona.object_id',
            calc_type='function',
            arguments_for_function={
                'days_back': 7,
                'return_value': 'fan_count_dif_growth_rate'
            },
        )

        billboard_3 = self.create_billboard_dict(
            title='popularity_growth_nominal',
            header_name='Name',
            header_value_formatted='growth in popularity (likes)',
            link_uri_name='member',
            value_format="{:,.0f}",
            top_num_of_values=10,
            is_sorted_reversed=True,
            data_set=Facebook_Feed.current_feeds.all(),
            data_name_attr='persona.content_object.name',
            data_value_float_attr='popularity_dif',
            link_value_attr='persona.object_id',
            calc_type='function',
            arguments_for_function={
                'days_back': 7,
                'return_value': 'fan_count_dif_nominal'
            },
        )

        billboard_4 = self.create_billboard_dict(
            title='num_of_statuses',
            header_name='Name',
            header_value_formatted='number of statuses last month',
            link_uri_name='member',
            value_format="{:,.0f}",
            top_num_of_values=10,
            is_sorted_reversed=True,
            data_set=Facebook_Feed.current_feeds.all(),
            data_name_attr='persona.content_object.name',
            data_value_float_attr='n_statuses_last_month',
            link_value_attr='persona.object_id',
            calc_type='stats',
            arguments_for_function=None,
        )

        billboard_5 = self.create_billboard_dict(
            title='mean_status_likes_last_month',
            header_name='Name',
            header_value_formatted='mean_status_likes_last_month',
            link_uri_name='member',
            value_format="{:,.0f}",
            top_num_of_values=10,
            is_sorted_reversed=True,
            data_set=Facebook_Feed.current_feeds.all(),
            data_name_attr='persona.content_object.name',
            data_value_float_attr='mean_status_likes_last_month',
            link_value_attr='persona.object_id',
            calc_type='stats',
            arguments_for_function=None,
        )

        # billboard_6 = self.create_billboard_dict(title='Most popular status',
        # header_name='Name',
        # header_value_formatted='likes for most popular status',
        # value_format="{:,.0f}",
        # top_num_of_values=10,
        # is_sorted_reversed=True,
        # data_set=self.stats.popular_statuses_last_month(
        # [feed.id for feed in Facebook_Feed.current_feeds.all()], 10),
        # data_name_attr='persona.content_object.name',
        # data_value_float_attr='mean_status_likes_last_month',
        # calc_type='stats',
        # arguments_for_function=None,
        # )

        billboard_6 = {
            'title':
            'Most popular status this Month',
            'headers': {
                'name': 'Name',
                'value_formatted': 'likes for most popular status'
            },
            'link_uri_name':
            'status-detail',
            'data': [{
                'name':
                Facebook_Status.objects.get(
                    id=result_array[0]).feed.persona.content_object.name,
                'value_int':
                float(result_array[1]),
                'value_formatted':
                "{:,.0f}".format(result_array[1]),
                'value_reference_link':
                Facebook_Status.objects.get(id=result_array[0]).status_id,
            } for result_array in self.stats.popular_statuses_last_month(
                [feed.id for feed in Facebook_Feed.current_feeds.all()], 10)]
        }

        context['list_of_billboards'] = []
        context['list_of_billboards'].append(billboard_1)
        context['list_of_billboards'].append(billboard_2)
        context['list_of_billboards'].append(billboard_3)
        context['list_of_billboards'].append(billboard_4)
        context['list_of_billboards'].append(billboard_5)
        context['list_of_billboards'].append(billboard_6)

        return context
Esempio n. 7
0
    def __init__(self):
        self.stats = StatsEngine()
        self.number_of_followers_board = self.create_billboard_dict(
            title=_('Number of Followers'),
            second_title=_('Total'),
            header_name=_('MK'),
            header_value_formatted='current_fan_count',
            link_uri_name='member',
            value_format="{:,}",
            top_num_of_values=TOP_NUM_OF_VALUES,
            is_sorted_reversed=True,
            data_set=Facebook_Feed.current_feeds.all(),
            data_name_attr='persona.owner.name',
            data_value_float_attr='current_fan_count',
            link_value_attr='persona.owner_id',
            calc_type='attribute',
            arguments_for_function=None,
            order_of_board=6,
        )

        self.popularity_growth_board = self.create_billboard_dict(
            title=_('Growth in Number of Followers'),
            second_title=DEFAULT_SECOND_TITLE,
            header_name='Name',
            header_value_formatted='growth in popularity (likes)',
            link_uri_name='member',
            value_format="{:,.0f}",
            top_num_of_values=TOP_NUM_OF_VALUES,
            is_sorted_reversed=True,
            data_set=Facebook_Feed.current_feeds.all(),
            data_name_attr='persona.owner.name',
            data_value_float_attr='popularity_dif',
            link_value_attr='persona.owner_id',
            calc_type='function',
            arguments_for_function={
                'days_back': DAYS_BACK,
                'return_value': 'fan_count_dif_nominal'
            },
            order_of_board=2,
        )

        self.number_of_status_board = self.create_billboard_dict(
            title=_('Number of Statuses Published'),
            second_title=DEFAULT_SECOND_TITLE,
            header_name='Name',
            header_value_formatted='number of statuses last week',
            link_uri_name='member',
            value_format="{:,.0f}",
            top_num_of_values=TOP_NUM_OF_VALUES,
            is_sorted_reversed=True,
            data_set=Facebook_Feed.current_feeds.all(),
            data_name_attr='persona.owner.name',
            data_value_float_attr='n_statuses_last_week',
            link_value_attr='persona.owner_id',
            calc_type='stats',
            arguments_for_function=None,
            order_of_board=3,
        )

        self.median_status_likes_board = self.create_billboard_dict(
            title=_('Median #like Count per Status'),
            second_title=DEFAULT_SECOND_TITLE,
            header_name='Name',
            header_value_formatted='median_status_likes_last_week',
            link_uri_name='member',
            value_format="{:,.0f}",
            top_num_of_values=TOP_NUM_OF_VALUES,
            is_sorted_reversed=True,
            data_set=Facebook_Feed.current_feeds.all(),
            data_name_attr='persona.owner.name',
            data_value_float_attr='median_status_likes_last_week',
            link_value_attr='persona.owner_id',
            calc_type='stats',
            arguments_for_function=None,
            order_of_board=4,
        )

        self.top_likes_board = {
            'order_of_board':
            5,
            'title':
            _('Top Statuses by Number of  #like '),
            'second_title':
            _('Last Week'),
            'headers': {
                'name': 'Name',
                'value_formatted': 'likes for most popular status'
            },
            'link_uri_name':
            'status-detail',
            'data': [{
                'name':
                Facebook_Status.objects.get(
                    id=result_array[0]).feed.persona.owner.name,
                'value_int':
                float(result_array[1]),
                'value_formatted':
                "{:,.0f}".format(result_array[1]),
                'value_reference_link':
                Facebook_Status.objects.get(id=result_array[0]).status_id,
            } for result_array in self.stats.popular_statuses_last_week(
                [feed.id for feed in Facebook_Feed.current_feeds.all()],
                TOP_NUM_OF_VALUES)]
        }
Esempio n. 8
0
class Billboards(object):
    def __init__(self):
        self.stats = StatsEngine()
        self.number_of_followers_board = self.create_billboard_dict(
            title=_('Number of Followers'),
            second_title=_('Total'),
            header_name=_('MK'),
            header_value_formatted='current_fan_count',
            link_uri_name='member',
            value_format="{:,}",
            top_num_of_values=TOP_NUM_OF_VALUES,
            is_sorted_reversed=True,
            data_set=Facebook_Feed.current_feeds.all(),
            data_name_attr='persona.owner.name',
            data_value_float_attr='current_fan_count',
            link_value_attr='persona.owner_id',
            calc_type='attribute',
            arguments_for_function=None,
            order_of_board=6,
        )

        self.popularity_growth_board = self.create_billboard_dict(
            title=_('Growth in Number of Followers'),
            second_title=DEFAULT_SECOND_TITLE,
            header_name='Name',
            header_value_formatted='growth in popularity (likes)',
            link_uri_name='member',
            value_format="{:,.0f}",
            top_num_of_values=TOP_NUM_OF_VALUES,
            is_sorted_reversed=True,
            data_set=Facebook_Feed.current_feeds.all(),
            data_name_attr='persona.owner.name',
            data_value_float_attr='popularity_dif',
            link_value_attr='persona.owner_id',
            calc_type='function',
            arguments_for_function={
                'days_back': DAYS_BACK,
                'return_value': 'fan_count_dif_nominal'
            },
            order_of_board=2,
        )

        self.number_of_status_board = self.create_billboard_dict(
            title=_('Number of Statuses Published'),
            second_title=DEFAULT_SECOND_TITLE,
            header_name='Name',
            header_value_formatted='number of statuses last week',
            link_uri_name='member',
            value_format="{:,.0f}",
            top_num_of_values=TOP_NUM_OF_VALUES,
            is_sorted_reversed=True,
            data_set=Facebook_Feed.current_feeds.all(),
            data_name_attr='persona.owner.name',
            data_value_float_attr='n_statuses_last_week',
            link_value_attr='persona.owner_id',
            calc_type='stats',
            arguments_for_function=None,
            order_of_board=3,
        )

        self.median_status_likes_board = self.create_billboard_dict(
            title=_('Median #like Count per Status'),
            second_title=DEFAULT_SECOND_TITLE,
            header_name='Name',
            header_value_formatted='median_status_likes_last_week',
            link_uri_name='member',
            value_format="{:,.0f}",
            top_num_of_values=TOP_NUM_OF_VALUES,
            is_sorted_reversed=True,
            data_set=Facebook_Feed.current_feeds.all(),
            data_name_attr='persona.owner.name',
            data_value_float_attr='median_status_likes_last_week',
            link_value_attr='persona.owner_id',
            calc_type='stats',
            arguments_for_function=None,
            order_of_board=4,
        )

        self.top_likes_board = {
            'order_of_board':
            5,
            'title':
            _('Top Statuses by Number of  #like '),
            'second_title':
            _('Last Week'),
            'headers': {
                'name': 'Name',
                'value_formatted': 'likes for most popular status'
            },
            'link_uri_name':
            'status-detail',
            'data': [{
                'name':
                Facebook_Status.objects.get(
                    id=result_array[0]).feed.persona.owner.name,
                'value_int':
                float(result_array[1]),
                'value_formatted':
                "{:,.0f}".format(result_array[1]),
                'value_reference_link':
                Facebook_Status.objects.get(id=result_array[0]).status_id,
            } for result_array in self.stats.popular_statuses_last_week(
                [feed.id for feed in Facebook_Feed.current_feeds.all()],
                TOP_NUM_OF_VALUES)]
        }

    def create_billboard_dict(self,
                              order_of_board,
                              title,
                              second_title,
                              header_name,
                              header_value_formatted,
                              link_uri_name,
                              value_format,
                              data_set,
                              data_name_attr,
                              data_value_float_attr,
                              top_num_of_values,
                              calc_type,
                              link_value_attr,
                              arguments_for_function=None,
                              is_sorted_reversed=True):

        billboard_dict = {
            'order_of_board':
            order_of_board,
            'title':
            title,
            'second_title':
            second_title,
            'headers': {
                'name': header_name,
                'value_formatted': header_value_formatted,
            },
            'link_uri_name':
            link_uri_name,
            'data':
            self.create_billboard_data_dict_list(
                value_format, data_set, data_name_attr, data_value_float_attr,
                calc_type, arguments_for_function, link_value_attr),
        }

        billboard_dict['data'] = sorted(
            billboard_dict['data'],
            key=lambda x: x['value_int'],
            reverse=is_sorted_reversed)[:top_num_of_values]

        return billboard_dict

    def create_billboard_data_dict_list(self, value_format, data_set,
                                        data_name_attr, data_value_int_attr,
                                        calc_type, arguments_for_function,
                                        link_value_attr):

        if calc_type == 'function':
            data_dict_list = [{
                'name':
                getattrd(object_instance, data_name_attr),
                'value_int':
                float(
                    getattrd(object_instance,
                             data_value_int_attr)(**arguments_for_function)
                    or 0),
                'value_formatted':
                value_format.format(
                    getattrd(object_instance,
                             data_value_int_attr)(**arguments_for_function)
                    or 0),
                'value_reference_link':
                getattrd(object_instance, link_value_attr),
            } for object_instance in data_set]

        elif calc_type == 'stats':

            stats_method = getattrd(self.stats, data_value_int_attr)

            data_dict_list = [{
                'name':
                getattrd(object_instance, data_name_attr),
                'value_int':
                float(stats_method([object_instance.id]) or 0),
                'value_formatted':
                value_format.format(stats_method([object_instance.id]) or 0),
                'value_reference_link':
                getattrd(object_instance, link_value_attr),
            } for object_instance in data_set]
        else:
            # calc_type == 'attribute':
            data_dict_list = [{
                'name':
                getattrd(object_instance, data_name_attr),
                'value_int':
                float(getattrd(object_instance, data_value_int_attr)),
                'value_formatted':
                value_format.format(
                    getattrd(object_instance, data_value_int_attr)),
                'value_reference_link':
                getattrd(object_instance, link_value_attr),
            } for object_instance in data_set]

        return data_dict_list