예제 #1
0
    def register(self, obj, slug=None, verbose_name=None):
        """
		Register an object with the registry.
		
		:param obj: The object to register.
		:param slug: The slug which will be used to register the object. If ``slug`` is ``None``, it will be generated from ``verbose_name`` or looked for at ``obj.slug``.
		:param verbose_name: The verbose name for the object. If ``verbose_name`` is ``None``, it will be looked for at ``obj.verbose_name``.
		:raises: :class:`RegistrationError` if a different object is already registered with ``slug``, or if ``slug`` is not a valid slug.
		
		"""
        verbose_name = verbose_name if verbose_name is not None else obj.verbose_name

        if slug is None:
            slug = getattr(obj, 'slug', slugify(verbose_name))
        slug = smart_str(slug)

        if not slug_re.search(slug):
            raise RegistrationError(u"%s is not a valid slug." % slug)

        if slug in self._registry:
            reg = self._registry[slug]
            if reg['obj'] != obj:
                raise RegistrationError(
                    u"A different object is already registered as `%s`" % slug)
        else:
            self._registry[slug] = {'obj': obj, 'verbose_name': verbose_name}
예제 #2
0
	def register(self, obj, slug=None, verbose_name=None):
		"""
		Register an object with the registry.
		
		:param obj: The object to register.
		:param slug: The slug which will be used to register the object. If ``slug`` is ``None``, it will be generated from ``verbose_name`` or looked for at ``obj.slug``.
		:param verbose_name: The verbose name for the object. If ``verbose_name`` is ``None``, it will be looked for at ``obj.verbose_name``.
		:raises: :class:`RegistrationError` if a different object is already registered with ``slug``, or if ``slug`` is not a valid slug.
		
		"""
		verbose_name = verbose_name if verbose_name is not None else obj.verbose_name
		
		if slug is None:
			slug = getattr(obj, 'slug', slugify(verbose_name))
		slug = smart_str(slug)
		
		if not slug_re.search(slug):
			raise RegistrationError(u"%s is not a valid slug." % slug)
		
		
		if slug in self._registry:
			reg = self._registry[slug]
			if reg['obj'] != obj:
				raise RegistrationError(u"A different object is already registered as `%s`" % slug)
		else:
			self._registry[slug] = {
				'obj': obj,
				'verbose_name': verbose_name
			}
예제 #3
0
    def get_menuitem(self, context, path_or_menuslug, site_instance):
        # allow for failing early.
        if isinstance(path_or_menuslug, MenuItem):
            return ItemWithMeta(obj=path_or_menuslug, query=None)

        path_or_menuslug = force_text(path_or_menuslug)
        if path_or_menuslug.isdigit():
            lookup = {'pk': int(path_or_menuslug)}
        elif slug_re.search(path_or_menuslug):
            lookup = {'menu_slug': path_or_menuslug}
        elif len(path_or_menuslug) > 0:
            lookup = {'uri__iexact': path_or_menuslug}
        elif 'request' in context:
            lookup = {'uri__iexact': context['request'].path}
        else:
            msg = ("Couldn't figure out a lookup method for argument "
                   "{0!r}".format(path_or_menuslug))
            logger.warning(msg, extra={
                'request': context.get('request')
            })
            return ItemWithMeta(obj=None, query=None)

        lookup.update(site=site_instance, is_published=True)

        try:
            obj = (MenuItem.objects.select_related('site')
                   .defer('_original_content_type', '_original_content_id')
                   .get(**lookup))
            return ItemWithMeta(obj=obj, query=lookup)
        except MenuItem.DoesNotExist:
            msg = "Unable to find menu item using {0!r}".format(lookup)
            logger.warning(msg, exc_info=1, extra={
                'request': context.get('request')
            })
        return ItemWithMeta(obj=None, query=lookup)