def get(self, **kwargs): self._repopulate_if_necessary() if "pk" in kwargs: kwargs["id"] = kwargs["pk"] del kwargs["pk"] if "id" in kwargs: dic = self._get_from_store(int(kwargs["id"])) else: for ct in self._store.content_types.values(): for k, v in kwargs.items(): if k not in ct: raise ContentType.DoesNotExist() if ct[k] != v: break else: dic = ct break else: raise ContentType.DoesNotExist() def disable_save(*args, **kwargs): raise NotImplementedError("You can't save simulated content types") # We do this because some tests to comparisons with 'is' so we store # constructed ContentTypes in the thread local and return them if possible if dic["id"] in self._store.constructed_instances: return self._store.constructed_instances[dic["id"]] else: result = ContentType(**dic) result.save = new.instancemethod(disable_save, ContentType, result) self._store.constructed_instances[dic["id"]] = result return result
def get(self, *args, **kwargs): # Special case for Django >= 1.11 which sometimes generates Q() objects # with a single filter when querying content types. This handles that one # case.. if we find there are others we'll have to do something less hacky! if args and not kwargs: if len(args) == 1 and len( args[0].children) == 1 and not args[0].negated: kwargs = dict(args[0].children) else: raise ValueError("Unsupported Q operation") ContentType = self._get_model() self._repopulate_if_necessary() if "pk" in kwargs: kwargs["id"] = kwargs["pk"] del kwargs["pk"] if "id" in kwargs: dic = self._get_from_store(int(kwargs["id"])) else: for ct in self._store.content_types.values(): for k, v in kwargs.items(): if k not in ct: raise ContentType.DoesNotExist() if ct[k] != v: break else: dic = ct break else: raise ContentType.DoesNotExist() def disable_save(*args, **kwargs): raise NotImplementedError("You can't save simulated content types") # We do this because some tests to comparisons with 'is' so we store # constructed ContentTypes in the thread local and return them if possible if dic["id"] in self._store.constructed_instances: return self._store.constructed_instances[dic["id"]] else: ContentType = self._get_model() result = ContentType(**dic) result.save = new.instancemethod(disable_save, ContentType, result) self._store.constructed_instances[dic["id"]] = result return result
def test_parse_content_type_id_with_nonexistent_content_type_raises_error( self): with patch('translation.field_ids.ContentType.objects.get' ) as content_type_get: content_type_get.side_effect = ContentType.DoesNotExist() with self.assertRaises(ContentTypeDoesNotExistError): _parse_content_type_id( 'contenttranslationtools.not_a_content_type')
def get_serializer_class_by_name(self, type_name): try: # try to convert type name into a serializer class app_label, model_name = type_name.split("_") operation_type = ContentType.objects.get_by_natural_key( app_label, model_name) operation_model = operation_type.model_class() return self.get_serializer_class(operation_model) except (ValueError, ContentType.DoesNotExist): # invalid type name raise ContentType.DoesNotExist("Provided type_name is invalid.")
def _create_content_type_queries(self) -> Q: queries = self._create_queries_using_model_name( self.pair_user.user_model_name) content_type_query = Q() content_types = ContentType.objects.filter(queries, app_label='accounts') if not content_types.exists(): raise ContentType.DoesNotExist('not found ContentType object') for content in content_types: content_type_query |= Q(content_type=content) return content_type_query
def add_permissions_for_models(app_label, model__in): content_types = ContentType.objects.filter(app_label=app_label, model__in=model__in) if not content_types.count() == len(model__in): raise ContentType.DoesNotExist("Found %s, expected 4" % content_types.count()) for content_type in content_types: codename = "view_%s" % content_type.model if not Permission.objects.filter(content_type=content_type, codename=codename): Permission.objects.create(content_type=content_type, codename=codename, name="Can view %s" % content_type.name) if settings.DEBUG: print "Added view permission for %s" % content_type.name else: if settings.DEBUG: print "Permission already exists for %s" % content_type.name create_location_related_permissions(ContentType.objects.get(app_label="birth_registration", model="f101"), True) print "Added location-related permissions for f101"
def get_form(self, form_class): kwargs = self.get_form_kwargs() try: data = kwargs["data"] ctype_pk, object_pk = data.get("content_type"), data.get( "object_id") if ctype_pk is None or object_pk is None: raise AttributeError( "Missing content_type or object_id field.") model = ContentType.objects.get_for_id(ctype_pk) target = model.get_object_for_this_type(pk=object_pk) except AttributeError: raise AttributeError( "The given content-type id %d does not resolve to a model." % ctype_pk) except ContentType.DoesNotExist: raise ContentType.DoesNotExist( "No matching content-type id and object id exists." % (ctype_pk, object_pk)) except (ValueError, ValidationError), e: raise e( "Attempting to get content-type %d and object %d raised %s", (ctype_pk, object_pk, e.__class__.__name__))
def _get_from_store(self, id): try: return self._store.content_types[id] except KeyError: raise ContentType.DoesNotExist()
def _get_from_store(self, id): try: return self._store.content_types[id] except KeyError: from django.contrib.contenttypes.models import ContentType raise ContentType.DoesNotExist()