from django.core.management.base import BaseCommand, CommandError
from django.core.files.base import ContentFile

import csv
import os
import re
from carnatic import models
from django.conf import settings
import data
import requests

class Command(BaseCommand):
    help = "Load ajay's updates to instruments"

    def handle(self, *args, **options):
        fname = args[0]

        #TODO: utf-8 in csv?
        reader = csv.reader(open(fname, "rb"))
        reader.next() # header
        for row in reader:
            name = row[0]
            percussion = row[2]
            wikipedia = row[4]
            picture = row[5]
            altpicture = row[6]
            altsource = row[7]
            source = row[8]
            description = row[9]

            i = models.Instrument.objects.get(name=name)
            i.is_percussion = not not percussion
            if not picture and altpicture:
                picture = altpicture
            if picture:
                req = requests.get(picture)
                piccontents = req.contents
                im = data.models.image()
                im.image.save("%s.jpg" % i.name, contentfile(piccontents))
                # TODO: Clear images
                i.images.remove()
                i.images.add(im)
                if source:
                    sn = data.models.SourceName.objects.get(name="kutcheris.com")
                    sourceob, created = data.models.Source.objects.get_or_create(source_name=sn, uri=link, defaults={"title": a.name})
                else:
                    sourceob = None
                descriptionob = data.models.Description.objects.create(description=description, source=sourceob)
                i.description = descriptionob
                i.save()




@@ -228,16 +228,509 @@
 quests%0A%0A
+def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs):%0A    # csv.py doesn't do Unicode; encode temporarily as UTF-8:%0A    csv_reader = csv.reader(utf_8_encoder(unicode_csv_data),%0A                            dialect=dialect, **kwargs)%0A    for row in csv_reader:%0A        # decode UTF-8 back to Unicode, cell by cell:%0A        yield %5Bunicode(cell, 'utf-8') for cell in row%5D%0A%0Adef utf_8_encoder(unicode_csv_data):%0A    for line in unicode_csv_data:%0A        yield line.encode('utf-8')%0A%0A
 class Co
@@ -867,37 +867,8 @@
 0%5D%0A%0A
-        #TODO: utf-8 in csv?%0A
     
@@ -1209,16 +1209,56 @@
 = row%5B9%5D
+.decode(%22utf-8%22)%0A%0A            print name
 %0A%0A      
@@ -1453,32 +1453,90 @@
     if picture:%0A
+                print %22downloading picture from%22, picture%0A
                 
@@ -1604,17 +1604,16 @@
 .content
-s
 %0A       
@@ -1638,20 +1638,54 @@
 .models.
-imag
+Image()%0A                i.images.remov
 e()%0A    
@@ -1733,16 +1733,16 @@
 me, 
-c
+C
 ontent
-f
+F
 ile(
@@ -1759,45 +1759,8 @@
 s))%0A
-                # TODO: Clear images%0A
     
@@ -1772,39 +1772,38 @@
        i.images.
-remove(
+add(im
 )%0A              
@@ -1800,36 +1800,31 @@
         
-    i.images.add(im)
+if description:
 %0A       
@@ -1913,21 +1913,17 @@
 me=%22
-kutcheris.com
+Wikipedia
 %22)%0A 
@@ -2026,12 +2026,14 @@
 uri=
-link
+source
 , de
@@ -2049,18 +2049,16 @@
 title%22: 
-a.
 name%7D)%0A 
@@ -2299,10 +2299,8 @@
 save()%0A%0A
-%0A%0A

fix and run instrument matcher