import time, requests, json
from pyspider.database.base.projectdb import ProjectDB as BaseProjectDB


class ProjectDB(BaseProjectDB):
    __collection_name__ = 'projectdb'

    def __init__(self, url, database='projectdb'):
        self.url = url
        self.database = database
        self.insert('', {})

    def _default_fields(self, each):
        if each is None:
            return each
        each.setdefault('group', None)
        each.setdefault('status', 'TODO')
        each.setdefault('script', '')
        each.setdefault('comments', None)
        each.setdefault('rate', 0)
        each.setdefault('burst', 0)
        each.setdefault('updatetime', 0)
        return each

    def insert(self, name, obj={}):
        url = self.url + self.__collection_name__ + "/" + name
        obj = dict(obj)
        obj['name'] = name
        obj['updatetime'] = time.time()
        res = requests.put(url, data = json.dumps(obj), headers = {"Content-Type": "application/json"}).json()
        print('[couchdb projectdb insert] - url: {} res: {}'.format(url,res))
        return res

    def update(self, name, obj={}, **kwargs):
        obj = dict(obj)
        obj.update(kwargs)
        self.insert(name, obj)

    def get_all(self, fields=None):
        payload = {
            "selector": {},
            "fields": fields
        }
        res = requests.post(self.url+"_find", data=json.dumps(payload)).json()
        print('[couchdb projectdb get_all] - url: {} res: {}'.format(self.url, res))
        return res


    def get(self, name, fields=None):
        payload = {
            "selector": {"name": name},
            "fields": fields,
            "limit": 1
        }
        res = requests.post(self.url + "_find", data=json.dumps(payload)).json()
        print('[couchdb projectdb get] - url: {} res: {}'.format(self.url, res))
        return res

    def check_update(self, timestamp, fields=None):
        for project in self.get_all(fields=('updatetime', 'name')):
            if project['updatetime'] > timestamp:
                project = self.get(project['name'], fields)
                yield self._default_fields(project)

    def drop(self, name):
        doc = self.get(name)
        res = requests.delete(self.url+name+"/"+doc["_rev"]).json()
        print('[couchdb projectdb drop] - url: {} res: {}'.format(self.url, res))
        return res


@@ -239,17 +239,34 @@
 rl = url
+ + database + %22/%22
 %0A
-
         
@@ -763,41 +763,8 @@
 rl +
- self.__collection_name__ + %22/%22 +
  nam
@@ -1309,32 +1309,65 @@
 ields%0A        %7D%0A
+        url = self.url + %22_find%22%0A
         res = re
@@ -1382,24 +1382,11 @@
 ost(
-self.url+%22_find%22
+url
 , da
@@ -1477,37 +1477,32 @@
 res: %7B%7D'.format(
-self.
 url, res))%0A     
@@ -1670,32 +1670,65 @@
 t%22: 1%0A        %7D%0A
+        url = self.url + %22_find%22%0A
         res = re
@@ -1739,34 +1739,19 @@
 ts.post(
-self.url + %22_find%22
+url
 , data=j
@@ -1834,37 +1834,32 @@
 res: %7B%7D'.format(
-self.
 url, res))%0A     
@@ -2223,38 +2223,22 @@
     
-res = requests.delete(
+url = 
 self.url
 +nam
@@ -2237,18 +2237,24 @@
 .url
-+
+ + 
 name
-+%22/%22+
+ + %22/%22 + 
 doc%5B
@@ -2260,16 +2260,50 @@
 %5B%22_rev%22%5D
+%0A        res = requests.delete(url
 ).json()
@@ -2369,21 +2369,16 @@
 .format(
-self.
 url, res

fix couchdb urls