# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2017-03-17 14:56
from __future__ import unicode_literals
import csv
from django.db import migrations



def load_ratings(apps, schema_editor):
    Rating = apps.get_model("movieratings_app", "Rating")
    Rater = apps.get_model("movieratings_app", "Rater")
    Movie = apps.get_model("movieratings_app", "Movie")
    datafile_PATH = 'u.data'

# this rating.movie_id must be a movie instance
    with open(datafile_PATH, 'r', encoding="latin-1") as f:
        cats = {'fieldnames': ('rater_id', 'movie_id', 'rating', 'timestamp'), 'delimiter': '\t'}
        reader = csv.DictReader(f, **cats)
        for row in reader:
            m = Movie.objects.get(id=row['movie_id'])
            rater = Rater.objects.get(id=row['rater_id'])
            r = Rating(rating=row['rating'], timestamp=row['timestamp'], movie=m, rater=rater)
            r.save()


class Migration(migrations.Migration):

    dependencies = [
        ('movieratings_app', '0006_auto_20170317_1456'),
    ]

    operations = [
        migrations.RunPython(load_ratings)
    ]
