नीचे सभी मॉडल प्रश्नों (संबंधों सहित) फ़िल्टर करने के लिए सरलीकृत परिभाषित क्वेरी कन्स्ट्रक्टर है। आप इसे query_cls
पैरामीटर sessionmaker
पर पास कर सकते हैं। उपयोगकर्ता आईडी पैरामीटर को वैश्विक होने की आवश्यकता नहीं है जब तक कि यह पहले से उपलब्ध होने पर सत्र का निर्माण हो।
class HackedQuery(Query):
def get(self, ident):
# Use default implementation when there is no condition
if not self._criterion:
return Query.get(self, ident)
# Copied from Query implementation with some changes.
if hasattr(ident, '__composite_values__'):
ident = ident.__composite_values__()
mapper = self._only_mapper_zero(
"get() can only be used against a single mapped class.")
key = mapper.identity_key_from_primary_key(ident)
if ident is None:
if key is not None:
ident = key[1]
else:
from sqlalchemy import util
ident = util.to_list(ident)
if ident is not None:
columns = list(mapper.primary_key)
if len(columns)!=len(ident):
raise TypeError("Number of values doen't match number "
'of columns in primary key')
params = {}
for column, value in zip(columns, ident):
params[column.key] = value
return self.filter_by(**params).first()
def QueryPublic(entities, session=None):
# It's not directly related to the problem, but is useful too.
query = HackedQuery(entities, session).with_polymorphic('*')
# Version for several entities needs thorough testing, so we
# don't use it yet.
assert len(entities)==1, entities
cls = _class_to_mapper(entities[0]).class_
public_condition = getattr(cls, 'public_condition', None)
if public_condition is not None:
query = query.filter(public_condition)
return query
यह केवल एक मॉडल प्रश्नों के लिए काम करता है, और वहाँ बहुत काम यह अन्य मामलों के लिए उपयुक्त बनाने के लिए है। मैं एक विस्तृत संस्करण देखना चाहता हूं क्योंकि इसमें अधिकांश वेब अनुप्रयोगों के लिए कार्यक्षमता होनी चाहिए। यह प्रत्येक मॉडल वर्ग में संग्रहीत निश्चित स्थिति का उपयोग करता है, इसलिए आपको इसे अपनी आवश्यकताओं में संशोधित करना होगा।
स्रोत
2010-05-22 05:40:38
क्या आपने इसे प्राप्त करने के लिए डेटाबेस सुरक्षा प्रणाली का उपयोग करने के बारे में सोचा है? उदाहरण के लिए, ओरेकल इस कार्यक्षमता को अपनी वीपीडी सुविधा (http://www.oracle.com/technology/deploy/security/database-security/virtual-private-database/index.html) के साथ प्रदान करता है। आप अन्य डेटाबेस में भी समान कार्यक्षमता प्राप्त कर सकते हैं (उदा। Http://ddj.com/database/215900773 और http://technet.microsoft.com/en-gb/library/cc966395.aspx)। बस "पंक्ति स्तर सुरक्षा RDBSName" के लिए Google। – stephan
या उन सभी तालिकाओं पर विचार या नियम सेट करें जो उपयुक्त सीमा जोड़ते हैं, और सीधे अंतर्निहित तालिका को पढ़ने की क्षमता के उपयोगकर्ताओं को पट्टी करें। http://www.postgresql.org/docs/8.4/interactive/rules.html –