Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grafico historico de precio en sucursales online #211

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions preciosa/precios/graficos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
from preciosa.precios.models import Precio, Sucursal, Producto
from django.shortcuts import get_object_or_404, render, render_to_response
from django.template import RequestContext
from django.utils import simplejson as json
from preciosa.precios.tests.factories import (SucursalFactory,
ProductoFactory,
PrecioFactory)
from datetime import time, date, timedelta

"""
Genera objetos json con datos formateados para ser
graficados por la libraría de gráficos .js
Flot
"""

IDS_SUCURSALES_ONLINE = []

IDS_SUCURSALES = IDS_SUCURSALES_ONLINE

def to_flottimestamp(dt):
"""Converts a datetime object to UTC timestamp"""
return int(dt.strftime("%s"))*1000


# genera el json con los datos para graficar en flot
def graphdata_sucursal(sucursal, producto, dias=30):
data = list(Precio.objects.historico(sucursal=sucursal, producto=producto,
dias=dias, distintos=False))

points = []
for item in data:
fecha, precio = item['created'], item['precio']
points.append([to_flottimestamp(fecha), float(precio)])

graph_data = {"data": points, "label": sucursal.nombre}

return graph_data


# hacer un grafico comparando todas las sucursales online
def graphdata_comparando_sucursales(producto, dias=30):
sucursales = Sucursal.objects.filter(online=True)
graph_data = [graphdata_sucursal(s, producto, dias) for s in sucursales]

return graph_data


# hacer un grafico del promedio
def graphdata_promedio_sucursales():
pass
128 changes: 128 additions & 0 deletions preciosa/precios/tests/test_graficos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
from django.test import TestCase
from datetime import timedelta, date
from django.utils import timezone
from decimal import Decimal
from preciosa.precios.models import Precio
from preciosa.precios.tests.factories import (SucursalFactory,
ProductoFactory,
PrecioFactory)

from preciosa.precios.graficos import graphdata_sucursal, to_flottimestamp,\
graphdata_comparando_sucursales


class TestPrecioHistorico(TestCase):

def setUp(self):
self.sucursal1 = SucursalFactory()
self.sucursal2 = SucursalFactory()
self.sucursal1.online = True
self.sucursal2.online = True
self.sucursal1.save()
self.sucursal2.save()

self.producto = ProductoFactory()

self.hoy = hoy = date.today()
self.ayer = ayer = hoy - timedelta(days=1)
self.anteayer = anteayer = ayer - timedelta(days=1)
self.hace10dias = hace10dias = hoy - timedelta(days=10)
self.hace20dias = hace20dias = hoy - timedelta(days=20)

sucursal = self.sucursal1
self.add('10.56', hoy, sucursal)
self.add('11.20', ayer, sucursal)
self.add('11.20', ayer, sucursal)
self.add('11.30', anteayer, sucursal)
self.add('9.12', hace10dias, sucursal)
self.add('8.12', hace20dias, sucursal)

sucursal = self.sucursal2
self.add('9.56', hoy, sucursal)
self.add('10.20', ayer, sucursal)
self.add('11.20', ayer, sucursal)
self.add('10.30', anteayer, sucursal)
self.add('8.12', hace10dias, sucursal)
self.add('9.12', hace20dias, sucursal)

def add(self, precio, created, sucursal, **kwargs):
return PrecioFactory(sucursal=sucursal,
producto=self.producto,
precio=precio,
created=created,
**kwargs)

def test_graficos_una_sucursal(self):
sucursal = self.sucursal1

graph_data = graphdata_sucursal(sucursal, self.producto, dias=15)
self.assertEqual(graph_data,
{
"data":
[[to_flottimestamp(self.hoy), 10.56],
[to_flottimestamp(self.ayer), 11.20],
[to_flottimestamp(self.ayer), 11.20],
[to_flottimestamp(self.anteayer), 11.30],
[to_flottimestamp(self.hace10dias), 9.12]
],
"label": sucursal.nombre
}
)

graph_data = graphdata_sucursal(sucursal, self.producto, dias=5)
self.assertEqual(graph_data,
{
"data":
[[to_flottimestamp(self.hoy), 10.56],
[to_flottimestamp(self.ayer), 11.20],
[to_flottimestamp(self.ayer), 11.20],
[to_flottimestamp(self.anteayer), 11.30],
],
"label": sucursal.nombre
}
)

graph_data = graphdata_sucursal(sucursal, self.producto, dias=30)
self.assertEqual(graph_data,
{
"data":
[[to_flottimestamp(self.hoy), 10.56],
[to_flottimestamp(self.ayer), 11.20],
[to_flottimestamp(self.ayer), 11.20],
[to_flottimestamp(self.anteayer), 11.30],
[to_flottimestamp(self.hace10dias), 9.12],
[to_flottimestamp(self.hace20dias), 8.12]
],
"label": sucursal.nombre
}
)

def test_grafico_sucursales_comparadas(self):
graph_data = graphdata_comparando_sucursales(self.producto, dias=15)
self.assertEqual(graph_data,
[{
"data":
[[to_flottimestamp(self.hoy), 10.56],
[to_flottimestamp(self.ayer), 11.20],
[to_flottimestamp(self.ayer), 11.20],
[to_flottimestamp(self.anteayer), 11.30],
[to_flottimestamp(self.hace10dias), 9.12]
],
"label": self.sucursal1.nombre
},
{
"data":
[[to_flottimestamp(self.hoy), 9.56],
[to_flottimestamp(self.ayer), 10.20],
[to_flottimestamp(self.ayer), 11.20],
[to_flottimestamp(self.anteayer), 10.30],
[to_flottimestamp(self.hace10dias), 8.12]
],
"label": self.sucursal2.nombre
}
]
)
pass

def test_grafico_promedio_sucursales(self):
pass
7 changes: 6 additions & 1 deletion preciosa/precios/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

from django.shortcuts import get_object_or_404, render
from django.views.generic import ListView, DetailView
from django.db.models import Q
from django.http import HttpResponse
from preciosa.precios.models import Producto, Categoria
from preciosa.precios.graficos import graphdata_comparando_sucursales
import operator
import json

# Vistas web

Expand Down Expand Up @@ -41,6 +42,10 @@ def get_context_data(self, **kwargs):
context['active'] = ','.join(['li.cat-%d a:first' % p.id
for p in self.object.categoria.get_ancestors()])
context['prods_similares'] = self.object.similares(5)

graph_data = graphdata_comparando_sucursales(self.object, dias=60)
context['graph_data'] = json.dumps(graph_data)

return context


Expand Down
Loading