forked from ixc/mongodj
/
base.py
112 lines (90 loc) · 3.33 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import pymongo
from django.db.backends import BaseDatabaseFeatures, \
BaseDatabaseWrapper, \
BaseDatabaseClient, \
BaseDatabaseValidation, \
BaseDatabaseIntrospection
from django.core.exceptions import ImproperlyConfigured
from .creation import DatabaseCreation
from mongodj.operations import DatabaseOperations
class DatabaseFeatures(BaseDatabaseFeatures):
distinguishes_insert_from_update = False
supports_deleting_related_objects = False
supports_multi_table_inheritance = False
can_return_id_from_insert = True
class DatabaseClient(BaseDatabaseClient):
pass
class DatabaseValidation(BaseDatabaseValidation):
pass
class DatabaseIntrospection(BaseDatabaseIntrospection):
def table_names(self):
"""
Show defined models
"""
return self.connection.db_connection.collection_names()
def sequence_list(self):
# TODO: check if it's necessary to implement that
pass
class CursorWrapper():
"""
Connection is essentially a cursor in mongoDB.
Let's imitate the methods cursor has
"""
def __init__(self, conn, NAME):
from .mongodb_serializer import TransformDjango
self.conn = conn
self.db_name = NAME
self.db = conn[NAME]
self.db.add_son_manipulator(TransformDjango())
def execute(self, query, args=None):
pass
def commit(self, *args, **kw):
# TODO - what is the state of
# transaction support in mongo?
return True
def rollback(self):
pass
def fetchone(self):
return {"test":1}
def sql_flush(self):
pass
def close(self):
# TODO
# how to close the damn things?
# or do we need to?
pass
def __getattr__(self, attr):
if not attr in self.__dict__:
return getattr(self.db, attr)
self.__dict__[attr]
def get_connection_from_dict(settings_dict):
NAME = settings_dict["NAME"]
HOST = settings_dict["HOST"]
try:
PORT = int(settings_dict["PORT"])
except ValueError:
raise ImproperlyConfigured("PORT must be an integer, or a string \
which is easily convertable to an integer")
USER = settings_dict["USER"]
PASSWORD = settings_dict["PASSWORD"]
conn = pymongo.Connection(HOST, PORT)
if USER and PASSWORD:
auth = conn['admin'].authenticate(USER, PASSWORD)
if not auth:
raise ImproperlyConfigured("Username and/or password for \
the mongoDB are not correct")
return conn, NAME
class DatabaseWrapper(BaseDatabaseWrapper):
def __init__(self, *args, **kwds):
super(DatabaseWrapper, self).__init__(*args, **kwds)
settings_dict = self.settings_dict
conn, name = get_connection_from_dict(settings_dict)
self.db_connection = CursorWrapper(conn, name)
self.features = DatabaseFeatures()
self.ops = DatabaseOperations(self)
self.client = DatabaseClient(self)
self.creation = DatabaseCreation(self)
self.validation = DatabaseValidation(self)
self.introspection = DatabaseIntrospection(self)
def _cursor(self):
return self.db_connection