def _statistics(request, store): form = DateRangeForm(request.GET) if not form.is_valid(): form = DateRangeForm() start_date = form.fields['start_date'].initial end_date = form.fields['end_date'].initial end_string = '#' else: start_date = form.cleaned_data['start_date'] end_date = form.cleaned_data['end_date'] if end_date > date.today(): end_date = date.today() if start_date >= end_date: url = reverse('solonotebooks.cotizador.views_store.statistics') return HttpResponseRedirect(url) # Normal clicks raw_data = ExternalVisit.objects.filter( shn__store=store, date__gte=start_date, date__lte=end_date).values('date').annotate( Count('id')).order_by('date') chart_data = [(entry['date'], entry['id__count']) for entry in raw_data] click_count = sum([e[1] for e in chart_data]) generate_timelapse_chart([chart_data], start_date, end_date, [u'Número de visitas'], 'store_' + str(store.id) + '_01.png', u'Número de clicks normales a ' + str(store)) # Sponsored clicks raw_data = SponsoredVisit.objects.filter( shp__shpe__store=store, date__gte=start_date, date__lte=end_date).values('date').annotate( Count('id')).order_by('date') chart_data = [(entry['date'], entry['id__count']) for entry in raw_data] sponsored_click_count = sum([e[1] for e in chart_data]) generate_timelapse_chart([chart_data], start_date, end_date, [u'Número de visitas'], 'store_' + str(store.id) + '_02.png', u'Número de clicks patrocinados a ' + str(store)) return { 'store': store, 'form': form, 'click_count': click_count, 'sponsored_click_count': sponsored_click_count, 'tag': random.randint(1, 1000000) }
def entity_details(request, shpe_id): store = request.user.get_profile().assigned_store shpe = StoreHasProductEntity.objects.get(pk=shpe_id) if shpe.store != store and not request.user.is_superuser: url = reverse('solonotebooks.cotizador.views_store.index') return HttpResponseRedirect(url) if shpe.is_hidden or not shpe.shp: return append_store_metadata_to_response( request, 'store/entity_details_no_data.html', { 'store': store, 'shpe': shpe, }) shp = StoreHasProduct.objects.get(pk=shpe.shp.id) other_shpes = shp.storehasproductentity_set.filter( is_available=True).order_by('latest_price') if other_shpes.count() == 1: other_shpes = None product = shp.product form = DateRangeForm(request.GET) if not form.is_valid(): form = DateRangeForm() start_date = form.fields['start_date'].initial end_date = form.fields['end_date'].initial end_string = '#' else: start_date = form.cleaned_data['start_date'] end_date = form.cleaned_data['end_date'] if end_date > date.today(): end_date = date.today() if start_date >= end_date: request.flash[ 'error'] = 'Por favor seleccione un rango de fechas valido' url = reverse('solonotebooks.cotizador.views_store.entity_details', args=[shpe.id]) return HttpResponseRedirect(url) if store.sponsor_cap: # First chart raw_data = ProductVisit.objects.filter( notebook=product, date__gte=start_date, date__lt=end_date + timedelta(days=1)).extra(select={ 'd': 'CAST(date AS DATE)' }).values('d').annotate(Count('id')).order_by('d') chart_data = [(entry['d'], entry['id__count']) for entry in raw_data] product_visit_count = sum([e[1] for e in chart_data]) generate_timelapse_chart([chart_data], start_date, end_date, [u'Número de visitas'], 'unit_' + str(shp.id) + '_01.png', u'Número de visitas al producto en SoloTodo') # Second chart raw_data = ExternalVisit.objects.filter( shn__shp__product=product, date__gte=start_date, date__lt=end_date + timedelta(days=1)).values('date').annotate( Count('id')).order_by('date') chart_data = [(entry['date'], entry['id__count']) for entry in raw_data] all_external_visit_count = sum([e[1] for e in chart_data]) schart_data = [chart_data] raw_data = ExternalVisit.objects.filter( shn__shp__product=product, shn__store=store, date__gte=start_date, date__lt=end_date + timedelta(days=1)).values('date').annotate( Count('id')).order_by('date') chart_data = [(entry['date'], entry['id__count']) for entry in raw_data] store_external_visit_count = sum([e[1] for e in chart_data]) schart_data.append(chart_data) generate_timelapse_chart( schart_data, start_date, end_date, ['Clicks totales', 'Clicks a ' + unicode(store)], 'unit_' + str(shp.id) + '_02.png', u'Número de clicks a tiendas') # Third chart raw_data = ExternalVisit.objects.filter( shn__shp__product=product, date__gte=start_date, date__lt=end_date + timedelta(days=1)).values('shn__store').annotate(Count('id')) chart_data = [(unicode(Store.objects.get(pk=pair['shn__store'])), pair['id__count']) for pair in raw_data] generated_pie_chart = generate_pie_chart( chart_data, 'unit_' + str(shp.id) + '_03.png', u'Distribución de clicks entre tiendas') # Fourth chart raw_data = SponsoredVisit.objects.filter( shp=shp, date__gte=start_date, date__lt=end_date + timedelta(days=1)).values('date').annotate( Count('id')).order_by('date') chart_data = [(entry['date'], entry['id__count']) for entry in raw_data] sponsored_visit_count = sum([e[1] for e in chart_data]) generate_timelapse_chart([chart_data], start_date, end_date, [u'Número de visitas patrocinadas'], 'unit_' + str(shp.id) + '_04.png', u'Número de visitas patrocinadas') else: product_visit_count = 0 store_external_visit_count = 0 all_external_visit_count = 0 sponsored_visit_count = 0 generated_pie_chart = False product = shp.product return append_store_metadata_to_response( request, 'store/entity_details.html', { 'store': store, 'shpe': shpe, 'shp': shp, 'other_shpes': other_shpes, 'product': product, 'form': form, 'tag': random.randint(1, 1000000), 'product_prices': product.storehasproduct_set.filter( shpe__isnull=False).order_by('shpe__latest_price'), 'product_visit_count': product_visit_count, 'store_external_visit_count': store_external_visit_count, 'all_external_visit_count': all_external_visit_count, 'sponsored_visit_count': sponsored_visit_count, 'generated_pie_chart': generated_pie_chart, })