Fix #228 - Add name field to `DataSet`

This commit is contained in:
Rob Hudson 2017-03-29 14:23:22 -04:00
Родитель e6ab7873fd
Коммит e9c588eda0
4 изменённых файлов: 46 добавлений и 7 удалений

Просмотреть файл

@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2017-03-29 18:24
from __future__ import unicode_literals
from django.db import migrations, models
def update_name_from_date(apps, schema_editor):
DataSet = apps.get_model('api', 'DataSet')
for ds in DataSet.objects.all():
ds.name = ds.date.isoformat()
ds.save()
class Migration(migrations.Migration):
dependencies = [
('api', '0006_on_delete_cascade_dataset'),
]
operations = [
migrations.AddField(
model_name='dataset',
name='name',
field=models.CharField(default='update-me', max_length=50),
preserve_default=False,
),
migrations.RunPython(update_name_from_date, migrations.RunPython.noop),
migrations.AlterField(
model_name='dataset',
name='name',
field=models.CharField(max_length=50, unique=True),
preserve_default=False,
),
]

Просмотреть файл

@ -3,6 +3,7 @@ from django.db import models
class DataSet(models.Model):
name = models.CharField(max_length=50, unique=True)
date = models.DateField()
display = models.BooleanField(default=False)
import_start = models.DateTimeField(null=True)

Просмотреть файл

@ -33,7 +33,8 @@ class TestMetric(TestCase):
id=2, name='Searches Per Active Day', description='Searches descr',
type='N')[0]
dataset, _ = DataSet.objects.get_or_create(date=date, display=True)
dataset, _ = DataSet.objects.get_or_create(name=date.isoformat(),
date=date, display=True)
cat_data = [
('x86', 0.95, 1),
@ -125,7 +126,9 @@ class TestMetric(TestCase):
# Test that a newer dataset with display=False isn't returned in
# the API.
self.create_data()
DataSet.objects.create(date=datetime.date(2016, 2, 2), display=False)
newdate = datetime.date(2016, 2, 2)
DataSet.objects.create(name=newdate.isoformat(), date=newdate,
display=False)
response = self.client.get(reverse('metric', args=['1']),
data={'date': '2016-02-05'})
self.assertEqual(response.status_code, 200)

Просмотреть файл

@ -176,8 +176,8 @@ conn, cursor = get_database_connection()
process_date = datetime.datetime.strptime(environ.get('date'), '%Y%m%d').date()
# Ensure we haven't already imported a dataset for this process_date.
sql = "SELECT 1 FROM api_dataset WHERE date=%s"
params = [process_date]
sql = "SELECT 1 FROM api_dataset WHERE name=%s"
params = [process_date.isoformat()]
if DEBUG_SQL:
print sql, params
else:
@ -200,9 +200,9 @@ df = df.filter("application_name_mode='Firefox'")
columns = df.columns
# Create a new dataset for this import.
sql = ("INSERT INTO api_dataset (date, display, import_start) "
"VALUES (%s, %s, %s) RETURNING id")
params = [process_date, False, datetime.datetime.now()]
sql = ("INSERT INTO api_dataset (name, date, display, import_start) "
"VALUES (%s, %s, %s, %s) RETURNING id")
params = [process_date.isoformat(), process_date, False, datetime.datetime.now()]
if DEBUG_SQL:
dataset_id = 0
print sql, params