def test_A_query(self): from django_mongodb_engine.query import A obj1 = RawModel.objects.create(raw=[{'a': 1, 'b': 2}]) obj2 = RawModel.objects.create(raw=[{'a': 1, 'b': 3}]) self.assertEqualLists(RawModel.objects.filter(raw=A('a', 1)), [obj1, obj2]) self.assertEqual(RawModel.objects.get(raw=A('b', 2)), obj1) self.assertEqual(RawModel.objects.get(raw=A('b', 3)), obj2)
def test_query_embedded(self): Model(x=3, em=EmbeddedModel(charfield='foo')).save() obj = Model(x=3, em=EmbeddedModel(charfield='blurg')) obj.save() Model(x=3, em=EmbeddedModel(charfield='bar')).save() obj_from_db = Model.objects.get(em=A('charfield', 'blurg')) self.assertEqual(obj, obj_from_db)
def test_fields(self): t1 = TestFieldModel(title="p1", mlist=["ab", { 'a': 23, "b": True }], slist=["bc", "ab"], mdict={ 'a': 23, "b": True }, mset=["a", 'b', "b"]) t1.save() t = TestFieldModel.objects.get(id=t1.id) self.assertEqual(t.mlist, ["ab", {'a': 23, "b": True}]) self.assertEqual(t.mlist_default, ["a", "b"]) self.assertEqual(t.slist, ["ab", "bc"]) self.assertEqual(t.slist_default, ["a", "b"]) self.assertEqual(t.mdict, {'a': 23, "b": True}) self.assertEqual(t.mdict_default, {"a": "a", 'b': 1}) self.assertEqual(sorted(t.mset), ["a", 'b']) self.assertEqual(sorted(t.mset_default), ["a", 'b']) from django_mongodb_engine.query import A t2 = TestFieldModel.objects.get(mlist=A("a", 23)) self.assertEqual(t1.pk, t2.pk)
def get_queryset(self): queryset = super(CollectionListView, self).get_queryset() # If the 'contains' parameter is used, the filter_backends have been cleared to prevent # reset of the queryset. Therefore, add a public_access filter to the queryset. # TODO correct the behavior of filter_backends, and remove this hack to get around it if self.contains_uri != None: from django_mongodb_engine.query import A queryset = queryset.filter( references=A('expression', self.contains_uri), public_access__in=[ACCESS_TYPE_EDIT, ACCESS_TYPE_VIEW]) return queryset
def no_other_record_has_same_name(self, name, self_id): if not self.repo: return True from concepts.models import Concept from django_mongodb_engine.query import A conceptsQuery = Concept.objects.filter( parent_id=self.repo.id, is_active=True, retired=False, names=A('name', name.name)).filter( names=A('locale', name.locale)).exclude(id=self_id) concepts = conceptsQuery.exclude(names=A('type', 'SHORT')) if concepts: #Could not get it to work with one query with 2 'A' excludes for names thus querying twice concepts = conceptsQuery.exclude(names=A('type', 'Short')) isEmpty = not concepts return isEmpty
def get_queryset(self): queryset = super(CollectionListView, self).get_queryset() # If the 'contains' parameter is used, the filter_backends have been cleared to prevent # reset of the queryset. Therefore, add a public_access filter to the queryset. # TODO correct the behavior of filter_backends, and remove this hack to get around it if self.contains_uri != None: from django_mongodb_engine.query import A queryset = queryset.filter(references=A('expression', self.contains_uri), public_access__in=[ACCESS_TYPE_EDIT, ACCESS_TYPE_VIEW]) if self.user: if self.user != 'root': from users.models import UserProfile user_profile = UserProfile.objects.filter(mnemonic=self.user) if user_profile: queryset = queryset.filter(parent_id__in=[user_profile[0].id] + user_profile[0].organizations, public_access__in=[ACCESS_TYPE_EDIT, ACCESS_TYPE_VIEW]) return queryset
def combine_A(field, value): # The pk is actually stored as "id", so change it, we also need extract the # pk from and models and wrap any IDs in an ObjectId, if field in ('pk', 'id'): field = "id" if isinstance(value, models.Model): # Specifically getattr field because we don't know if it's 'pk' # or 'id' and they might not be the same thing. value = getattr(value, field) # If value is None, we want to leave it as None, otherwise wrap it if value is not None and not isinstance(value, ObjectId): value = ObjectId(value) # If 'value' is already an A(), we need to extract the field part out if isinstance(value, A): field = "%s.%s" % (field, value.op) value = value.val return A(field, value)