from pymongo import MongoClient
from gridfs import GridFS
from bson.objectid import ObjectId
from datetime import datetime
from os import environ
from const import RequestType

def GetAdminUsers(db=None):
    if not db:
        client = MongoClient('data', 27017)
        db = client.aaf_db
    admins = []
    for admin in db.admin_users.find():
        admins.append(dict(admin))
    return admins

def set_admin_list(new_admin_list, db=None):
    if not db:
        client = MongoClient('data', 27017)
        db = client.aaf_db

    existing_admin_list = db.admin_users.find()
    for admin in existing_admin_list:
        user_found = False
        for index in range(0, len(new_admin_list)):
            if admin['userId'] == new_admin_list[index]['userId']:
                new_admin_list.pop(index)
                user_found = True
                break
        if not user_found:
            db.admin_users.remove(admin.get('_id'))

    for new_admin in new_admin_list:
        print(new_admin)
        db.admin_users.insert(new_admin)


class MongoConnection(object):
    def __init__(self, db=None):
        if db:
            self.db = db
        else:
            self.db = self.GetDb()
    #instance of Mongo DB Connection config connection and db values
    def GetDb(self):
        client = MongoClient('data', 27017)
        return client.aaf_db

    #get the collection for the request type
    def GetCollection(self, request_type):
        if request_type == RequestType.ASSISTANCE:
            return self.db.assistance_requests
        elif request_type == RequestType.DONATION:
            return self.db.donation_requests
        else:
            raise Exception('Invalid Request Collection')

    def GetGridFS(self):
        return GridFS(self.db, collection='request_documents')

class MongoInterface(object):
    def _getObjectId(self, obj):
        return str(obj)

    def findDocuments(self, collection, query, sort=None):
        return_value = { }
        results = [ ]

        #simple pagination. Can be costly with later pages in larger result sets
        search_results = collection.find(query)

        return search_results

    def getDocument(self, collection, id):
        doc = collection.find_one({'_id':ObjectId(id)})
        if doc:
            doc['_id'] = self._getObjectId(doc['_id'])
            return doc
        else:
            return None

    def insertDocument(self, collection, data):
        result = collection.insert_one(data).inserted_id
        return self._getObjectId(result)

    def updateDocument(self, collection, data, id, **kwargs):
        update_data = {'$set' : data}
        if 'push_data' in kwargs:
            update_data['$push'] = kwargs['push_data']
        if 'pull_data' in kwargs:
            update_data['$pull'] = kwargs['pull_data']
        doc = collection.update({'_id':ObjectId(id)}, update_data)
        return self._getObjectId(doc)

    def getFile(self, collection, id):
        file = collection.get(ObjectId(id))
        return file.read()

    def insertFile(self, collection, data):
        file = collection.put(data.encode("UTF-8"))
        return self._getObjectId(file)

    def deleteFile(self, collection, id):
        return collection.remove(ObjectId(id))

if __name__ == '__main__':
    db = MongoClient('172.18.0.2', 27017).aaf_db
    print(GetAdminUsers(db))

    #GetAdminUsers

    #db = MongoClient('172.18.0.2', 27017).aaf_db
    #db.admin_users.insert({'userId' : 10705332, 'userName' : 'Trevor Robinson'})
    #print(GetAdminUsers(db))

@@ -398,655 +398,8 @@
 ns%0A%0A
-def set_admin_list(new_admin_list, db=None):%0A    if not db:%0A        client = MongoClient('data', 27017)%0A        db = client.aaf_db%0A%0A    existing_admin_list = db.admin_users.find()%0A    for admin in existing_admin_list:%0A        user_found = False%0A        for index in range(0, len(new_admin_list)):%0A            if admin%5B'userId'%5D == new_admin_list%5Bindex%5D%5B'userId'%5D:%0A                new_admin_list.pop(index)%0A                user_found = True%0A                break%0A        if not user_found:%0A            db.admin_users.remove(admin.get('_id'))%0A%0A    for new_admin in new_admin_list:%0A        print(new_admin)%0A        db.admin_users.insert(new_admin)%0A%0A%0A
 clas

removing duplication