-
Notifications
You must be signed in to change notification settings - Fork 1
/
manager.py
201 lines (148 loc) · 6.17 KB
/
manager.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -*- coding: utf-8 -*-
# Copyright 2005,2006,2007,2008 Spike^ekipS <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import sys, new, types, traceback
from django.conf import settings
from django.db import models
from django.db.models.query import QuerySet as queryset_django
from django.db.models.manager import Manager as manager_django
import constant, queryset, signals, utils
class Manager (models.Manager) :
def __init__ (self, **kwargs) :
super(Manager, self).__init__()
self.manager_id = constant.METHOD_NAME_SEARCH
self._objects_search_index_model_name = kwargs.get("index_model", None)
def contribute_to_class_index_model (self, index_model, name):
self.index_model = index_model
def contribute_to_class (self, model, name):
sys.MODELS_OBJECTS_SEARCHER.append(
(model, self._objects_search_index_model_name, name)
)
def get_query_set (self):
import core
core.initialize()
return queryset.QuerySet(self.index_model)
def raw_query (self, *args, **kwargs) :
return self.get_query_set().raw_query(*args, **kwargs)
METHODS_FOR_CREATE_INDEX = (
"_filter_or_exclude",
"create",
"get",
"latest",
"order_by",
"distinct",
"extra",
"reverse",
"get_or_create",
)
class MethodCreateIndex (object) :
def method_create_index (self, cls) :
objs = None
if hasattr(cls, "to_create_index") and cls.to_create_index is None :
return
elif hasattr(cls, "data_create_index") :
objs = iter(cls.data_create_index.values())
elif objs is None :
if type(cls) is types.GeneratorType :
objs = cls
elif isinstance(cls, queryset_django) :
objs = cls
elif type(cls) in (list, tuple, ) :
objs = iter(cls)
else :
objs = iter([cls, ])
try :
sys.INDEX_MANAGER.index(iter(objs))
except Exception, e :
if settings.DEBUG :
traceback.print_exc()
return cls
def attach_create_index (self, obj) :
# add create_index
try :
obj.create_index = new.instancemethod(
self.method_create_index, obj, obj.__class__, )
except :
raise
######################################################################
# Re-Write
for i in METHODS_FOR_CREATE_INDEX :
if hasattr(self, "_query_%s" % i) :
func = getattr(self, "_query_%s" % i)
else :
func = self.__get_query_method(i)
setattr(obj, i, new.instancemethod(func, obj, obj.__class__, ), )
return obj
def manager_method_get_empty_query_set (self, cls, ) :
_queryset = manager_django.get_empty_query_set(cls, )
return self.attach_create_index(_queryset)
def manager_method_get_query_set (self, cls) :
_queryset = manager_django.get_query_set(cls)
return self.attach_create_index(_queryset)
def analyze_model_manager (self, model) :
for f in dir(model) :
try :
[getattr(model, f).__class__, ]
except Exception, e :
continue
else :
ff = getattr(model, f)
if isinstance(ff, manager_django) and f != constant.METHOD_NAME_SEARCH and not hasattr(ff, "manager_id"):
ff.get_query_set = new.instancemethod(
self.manager_method_get_query_set, ff, ff.__class__
)
ff.get_empty_query_set = new.instancemethod(
self.manager_method_get_empty_query_set, ff, ff.__class__
)
######################################################################
# QuerySet method
def __get_query_method (self, name, ) :
def func (cls, *args, **kwargs) :
_queryset = getattr(queryset_django, name)(cls, *args, **kwargs)
_queryset = MethodCreateIndex.attach_create_index(_queryset)
return _queryset
return func
def _query_get_or_create (self, cls, *args, **kwargs) :
(_obj, created, ) = queryset_django.get_or_create(cls, *args, **kwargs)
_obj = MethodCreateIndex.attach_create_index(_obj)
"""
If object was created, the indexing job will be performed by
Signal Handlers(signals.post_save).
"""
_obj.to_create_index = created and None or ""
return (_obj, created, )
def _query_create (self, cls, **kwargs) :
_obj = queryset_django.create(cls, **kwargs)
_obj = MethodCreateIndex.attach_create_index(_obj)
_obj.to_create_index = None
return _obj
method_create_index = classmethod(method_create_index)
attach_create_index = classmethod(attach_create_index)
manager_method_get_empty_query_set = classmethod(manager_method_get_empty_query_set)
manager_method_get_query_set = classmethod(manager_method_get_query_set)
analyze_model_manager = classmethod(analyze_model_manager)
__get_query_method = classmethod(__get_query_method)
_query_get_or_create = classmethod(_query_get_or_create)
_query_create = classmethod(_query_create)
"""
Description
-----------
ChangeLog
---------
Usage
-----
"""
__author__ = "Spike^ekipS <[email protected]>"