import json
import urllib2

import cherrypy
from pandas import read_csv

from db import db
from utils import df_to_mongo, mongo_to_json, SOURCE

class Collection(object):

    def __init__(self):
        pass

    exposed = True

    def DELETE(self, id=None):
        '''
        Delete id 'id' from mongo
        '''
        if id:
            return 'deleted id: %s' % id

    def GET(self, id=None, format='json', query=None):
        '''
        Return data set for id 'id' in format 'format'.
        Execute query 'query' in mongo if passed.
        '''
        if id:
            json_str = mongo_to_json(db().collections.find({SOURCE: id}))
            raise Exception(json_str)
            return json.dumps(json_str)

    def POST(self, url=None, data=None):
        '''
        Read data from URL 'url'.
        If URL is not provided and data is provided, read posted data 'data'.
        '''
        if url and 'http://' in url or 'https://' in url:
            f = urllib2.urlopen(url)
            df = read_csv(f, na_values=['n/a'])
            j = df_to_mongo(df)
            # add metadata to file
            for e in j:
                e[SOURCE] = url
            # insert data into collections
            db().collections.insert(j)
            return json.dumps({'id': url})

@@ -606,16 +606,45 @@
 to_json(
+%5B%0A                r for r in 
 db().col
@@ -670,17 +670,16 @@
 CE: id%7D)
-)
 %0A       
@@ -687,32 +687,9 @@
     
-raise Exception(json_str
+%5D
 )%0A  

encode mongo to json