def get_context_data(self, **kwargs): ctx = super(IndexView, self).get_context_data(**kwargs) nb_element = 3 TODAY = timezone.now() months = 3 if months > 2: FROM = TODAY+relativedelta(months=-abs(months - 1)) else: FROM = TODAY+relativedelta(months=-1) # Let's create the list of the months x_data = [] month_days = [] for d in list(rrule(MONTHLY,dtstart=FROM, count=months)): month_days.append(get_month_day_range(d)) x_data.append(d.strftime('%B')) # We must obtain the lsit of sources of the whole period sources_ls = Lead.objects.filter( modified__range=(month_days[0][0], month_days[-1][-1]) ).order_by('source').distinct('source').values('source') chart_data = {'x': []} extra_serie = {"tooltip": {"y_start": "Total ", "y_end": " leads"}} # let's populate the actuat data i = 1 for source in sources_ls: serie = [] for period in month_days: serie.append( Lead.objects.filter(modified__range=period).filter(source__icontains=source['source']).count() ) chart_data['y'+unicode(i)] = serie chart_data['name'+unicode(i)] = source['source'] chart_data['extra'+unicode(i)] = extra_serie i += 1 chart_data['x'] = [period[0].strftime('%B') for period in month_days] charttype = "multiBarChart" chartcontainer = 'multibarchart_container' # container name data = { 'charttype': charttype, 'chartdata': chart_data, 'chartcontainer': chartcontainer, 'extra': { 'x_is_date': False, 'x_axis_format': '', 'tag_script_js': True, 'jquery_on_ready': False, }, } n_ctx = ctx.copy() n_ctx.update(data) return n_ctx
def get_context_data(self, **kwargs): TARGET = settings.AGENCY_MONTH_TARGET TODAY = datetime.now().date() ctx = super(DashboardView, self).get_context_data(**kwargs) month_modified_leads = Lead.objects.filter(modified__range=get_month_day_range(TODAY)).exclude(long_term=True) month_created_leads = Lead.objects.filter(created__range=get_month_day_range(TODAY)).exclude(long_term=True) month_booked_leads = Lead.objects.filter(booked=True, booked_date__range=get_month_day_range(TODAY)).exclude( long_term=True ) init_val = dict( leads_in=month_created_leads.count(), follow_ups=month_modified_leads.filter( first_response=False, second_response=False, offer=False, hot=False, booked=False ).count(), hot=Lead.objects.filter(hot=True).exclude(long_term=True).count(), booked=month_booked_leads.count(), goal=(month_booked_leads.count() / TARGET), target=TARGET, agent=self.request.user.get_full_name(), room=get_room_chan_name(self.request.user), ) ctx["init_val"] = json.dumps(init_val, cls=DjangoJSONEncoder) return ctx