示例#1
0
 def get_context_data(self, **kwargs):
     # Call the base implementation first to get a context
     context = super(IndexView,self).get_context_data(**kwargs)
     start = datetime.today() - timedelta(days=14)
     # Add in a QuerySet of all the books
     interestManager = InterestManager()
     account = InterestAccount.objects.get(username='******')
     context['all'] = interestManager.getIdsByProject(account, 'GEN_630D1B86-31EE-494F-A718-48C8B6B4EA11')
     return context
示例#2
0
def fill_context(context, summary):
    """Fetch all data and add to given dict"""

    logger = logging.getLogger(__name__)

    current_site = Site.objects.get_current()
    context["domain"] = current_site.domain

    context["summary"] = summary
    """ Fetch project from summary """
    project = summary.project
    context["project"] = project

    """ dates for this mailing """
    currentstart = summary.dateStart
    currentend = summary.dateEnd

    previousstart = summary.dateStart - timedelta(days=15)
    previousend = summary.dateStart

    """ Fetch Analytics settings for this project """
    ga_settings = AnalyticsSettings.objects.get(project=project)
    logger.debug("fetched analytics settings: {}".format(ga_settings))
    ga_view = ga_settings.ga_view
    ga_goal = ga_settings.goal_to_track

    """ for testing purpose add content directly to context """
    ga_manager = AnalyticsManager()

    visits = ga_manager.get_weekly_visits(ga_view, currentstart.isoformat(), currentend.isoformat())
    context["traffic"] = visits["rows"]

    context["traffic_target_sessions"] = ga_settings.sessions_target
    context["traffic_target_pageviews"] = ga_settings.pageviews_target

    """ Get Google Analytics conversions for this and previous period """
    if not ga_goal == "0":
        conversions = ga_manager.get_conversion_count_for_goal(
            ga_view, ga_goal, currentstart.isoformat(), currentend.isoformat()
        )
        context["conversions"] = conversions
        previous_conversions = ga_manager.get_conversion_count_for_goal(
            ga_view, ga_goal, previousstart.isoformat(), previousend.isoformat()
        )
        context["previous_conversions"] = previous_conversions
        total_conversions = ga_manager.get_conversion_count_for_goal(
            ga_view, ga_goal, ga_manager.GA_NULL_DATE, currentend.isoformat()
        )
        context["total_conversions"] = total_conversions

        """ Get Google Analytics conversion rate for this and previous period """
        logger.debug("analytics period: {} - {}".format(currentstart.isoformat(), currentend.isoformat()))
        conversion_rate = ga_manager.get_conversion_rate_for_goal(
            ga_view, ga_goal, currentstart.isoformat(), currentend.isoformat()
        )
        context["conversionrate"] = conversion_rate

        previous_conversion_rate = ga_manager.get_conversion_rate_for_goal(
            ga_view, ga_goal, previousstart.isoformat(), previousend.isoformat()
        )
        context["previousconversionrate"] = previous_conversion_rate

        total_conversion_rate = ga_manager.get_conversion_rate_for_goal(
            ga_view, ga_goal, ga_manager.GA_NULL_DATE, currentend.isoformat()
        )
        context["total_conversion_rate"] = total_conversion_rate

    else:
        context["conversions"] = 0
        context["previous_conversions"] = 0
        context["total_conversions"] = 0
        context["conversionrate"] = 0
        context["previousconversionrate"] = 0
        context["total_conversion_rate"] = 0

    """ Get top pages """
    context["top_pages"] = ga_manager.get_top_pages(ga_view, currentstart, currentend)

    """ Get number of interested people from niki for this  and previous period """
    interestManager = InterestManager()
    nip = interestManager.getNikiInterestProjectByProject(project)
    idlist = []
    context["interest"] = 0
    context["previousinterest"] = 0
    context["interesttotal"] = 0
    if nip is not None:
        account = nip.interestAccount
        idlist = interestManager.getIdsByProjectBetween(
            account, nip.nikiProjectId, util.date_to_datetime(currentstart), util.date_to_datetime(currentend)
        )
        logger.debug("fetched idlist from nikiinterest: {}".format(idlist))
        context["interest"] = len(idlist)
        previousidlist = interestManager.getIdsByProjectBetween(
            account, nip.nikiProjectId, util.date_to_datetime(previousstart), util.date_to_datetime(previousend)
        )
        context["previousinterest"] = len(previousidlist)
        interest_total = len(interestManager.getIdsByProject(account, nip.nikiProjectId))
        context["interesttotal"] = interest_total

    if project.nikiProject != "0":
        """ Get project Niki sales stats """
        nikimanager = NikiConverter()
        context["availability"] = nikimanager.getAvailability(project.nikiProject)
        context["nikisalerent"] = nikimanager.getProjectSaleRentType(project.nikiProject)
        if context["availability"] is not None:
            sold_count = context["availability"][2]
    if project.fanpage_id != "0":
        """ Get the sex and age spread of likes on the fanpage """
        fbmanager = FacebookManager()
        agesexspread = fbmanager.get_likes_sex_age_spread_sorted(project)
        if not agesexspread == []:
            context["fbagesexspread"] = agesexspread

    if project.mailchimp_list_id != "0":
        """ Get Mailchimp list growth statistics """
        mcmanager = MailchimpManager(project.mailchimp_api_token)
        context["mailchimp"] = mcmanager.get_list_growth_data(project.mailchimp_list_id)
    try:
        context["project_score"] = util.project_score(sold_count, interest_total, total_conversion_rate)
    except:
        context["project_score"] = 0
    return context