"""A translation adapter for making queries between storage types.

Types are either:
1. PostgreSQL json fields
2. MongoDB collections.
"""

import json
from datetime import datetime as dt

from pymongo import MongoClient

import settings

DB_NAME = settings.ACTIVE_DB


if DB_NAME == 'mongo':
    client = MongoClient(host=settings.DB_URI, port=settings.DB_PORT)
    conn = client[settings.DB_NAME]
    coll = conn[settings.DB_TABLE]
else:
    raise NotImplemented('PostgreSQL is not yet supported.')


def reformat_data(data, c_id):
    """Format/clean existing config data to be re-inserted into database."""
    data.update(dict(id=c_id, date=dt.now()))
    return data


def _format_modules(data):
    """Form module data for JSON."""
    modules = []
    # Format modules data for json usage
    for item in data:
        if item.startswith('module_'):
            val_json = json.loads(data[item])
            modules.append(val_json)
    return modules


def read(c_id=None, filters={}):
    """Read a record."""
    if DB_NAME == 'mongo':
        if c_id is None:
            return coll.find(filters)
        else:
            return coll.find_one(dict(id=c_id))
    else:
        raise NotImplemented('PostgreSQL is not yet supported.')


def update(c_id, data=None, fmt_modules=True):
    """Update a record."""
    if data is None:
        return
    if DB_NAME == 'mongo':
        modules = _format_modules(data) if fmt_modules else data.get('modules')
        save_conf = {
            '$set': {
                'name': data.get('name', 'NONAME'),
                'modules': modules,
                'date': dt.now()
            }
        }
        save_conf['$set'].update(**data)
        coll.update(dict(id=c_id), save_conf)
    else:
        raise NotImplemented('PostgreSQL is not yet supported.')


def create(data=None):
    """Add a new record."""
    if data is None:
        return
    if DB_NAME == 'mongo':
        coll.insert(data)
    else:
        raise NotImplemented('PostgreSQL is not yet supported.')


def delete(c_id):
    """Delete a record."""
    if DB_NAME == 'mongo':
        coll.delete_one(dict(id=c_id))
    else:
        raise NotImplemented('PostgreSQL is not yet supported.')

@@ -964,34 +964,215 @@
 def 
-read(c_id=None, filters=%7B%7D
+count(**kwargs):%0A    %22%22%22Standard db count.%22%22%22%0A    if DB_NAME == 'mongo':%0A        return coll.count(**kwargs)%0A    else:%0A        raise NotImplemented('%7B%7D is not supported.'.format(DB_NAME))%0A%0A%0Adef read(**kwargs
 ):%0A 
@@ -1233,20 +1233,40 @@
      if 
-c_id
+kwargs.get('c_id', None)
  is None
@@ -1300,14 +1300,15 @@
 ind(
-filter
+**kwarg
 s)%0A 
@@ -1349,37 +1349,32 @@
 ll.find_one(
-dict(id=c_id)
+**kwargs
 )%0A    else:%0A
@@ -1403,34 +1403,26 @@
 mented('
-PostgreSQL
+%7B%7D
  is not 
 yet supp
@@ -1405,36 +1405,32 @@
 nted('%7B%7D is not 
-yet 
 supported.')%0A%0A%0Ad
@@ -1416,32 +1416,48 @@
  not supported.'
+.format(DB_NAME)
 )%0A%0A%0Adef update(c
@@ -1977,34 +1977,26 @@
 mented('
-PostgreSQL
+%7B%7D
  is not 
 yet supp
@@ -1979,36 +1979,32 @@
 nted('%7B%7D is not 
-yet 
 supported.')%0A%0A%0Ad
@@ -1990,32 +1990,48 @@
  not supported.'
+.format(DB_NAME)
 )%0A%0A%0Adef create(d
@@ -2198,34 +2198,26 @@
 mented('
-PostgreSQL
+%7B%7D
  is not 
 yet supp
@@ -2200,36 +2200,32 @@
 nted('%7B%7D is not 
-yet 
 supported.')%0A%0A%0Ad
@@ -2211,32 +2211,48 @@
  not supported.'
+.format(DB_NAME)
 )%0A%0A%0Adef delete(c
@@ -2390,34 +2390,26 @@
 mented('
-PostgreSQL
+%7B%7D
  is not 
 yet supp
@@ -2392,33 +2392,29 @@
 nted('%7B%7D is not 
-yet 
 supported.')%0A
@@ -2399,22 +2399,38 @@
 %7D is not supported.'
+.format(DB_NAME)
 )%0A

Add count function in adapters.