Ejemplo n.º 1
0
def try_to_import(value, exception_text):
    """
    If ``value`` is not None and is not DoesNotDefined
    then try to import specified by ``value`` path.
    """
    if value is not DoesNotDefined and value is not None:
        return importpath(value, exception_text)
    return value
Ejemplo n.º 2
0
 def title(self, object):
     u"""Возвращает заголовок элемента"""
     if self.get_title is None:
         return self.title_text
     from importpath import importpath
     title = importpath(self.get_title)
     if callable(title):
         return title()
     else:
         return title
Ejemplo n.º 3
0
 def url(self, object):
     u"""Возвращает url элемента"""
     if self.get_url is None:
         return self.url_text
     from importpath import importpath
     url = importpath(self.get_url)
     if callable(url):
         return url()
     else:
         return url
Ejemplo n.º 4
0
def MACRO_REPL(match):
    """
    Replace function for macro.
    
    >>> bool(MACRO_RE.match('{@ example.models.Page 1 @}'))
    True
    
    >>> bool(MACRO_RE.match('{@ example.models.DoesNotExists 1 @}'))
    True
    
    >>> bool(MACRO_RE.match('{@ 1 @}'))
    False
    
    >>> MACRO_RE.sub(MACRO_REPL, '<a href="{@ example.models.Page 1 @}">Page</a>')
    '<a href="/page_by_id/1">Page</a>'
    
    >>> MACRO_RE.sub(MACRO_REPL, '<a href="{@ example.models.DoesNotExists 1 @}">Page</a>') 
    '<a href="">Page</a>'
    
    >>> MACRO_RE.sub(MACRO_REPL, '<a href="{@ 1 @}">Page</a>') 
    '<a href="{@ 1 @}">Page</a>'
    
    >>> MACRO_RE.sub(MACRO_REPL, '<a href="{@ example-models-Page 1 @}">Page</a>')
    '<a href="{@ example-models-Page 1 @}">Page</a>'
    
    >>> (MACRO_RE.sub(MACRO_REPL, u'<a href="{@ example.models.Page 1 @}">\u00a0</a>') ==
    ... u'<a href="/page_by_id/1">\u00a0</a>')
    True
    """
    model, pk = match.groups()
    for setting in getattr(settings, 'MODELURL_MODELS', []):
        if model == setting.get('model', ''):
            function = setting.get('function', 'get_absolute_url')
            break
    else:
        return mark_safe('')
    model = importpath(model)
    try:
        obj = model.objects.get(pk=pk)
    except model.DoesNotExist:
        return mark_safe('')
    if not function:
        function = 'get_absolute_url'
    url = getattr(obj, function)()
    return mark_safe(url.encode('utf-8'))
Ejemplo n.º 5
0
 def __init__(self, check_sites=[], check_schemes=['http', ],
     check_unregistered=True, *args, **kwargs):
     """
     ``check_sites`` is list of site-names that are served by this server.
     Urls with such site name will be checked.
     
     ``check_schemes`` is list of schemes that are served by this server.
     Only urls with such scheme will be checked.
     
     ``check_unregistered`` indicates that unregistered views must be checked.
     If ``check_unregistered`` is True and view will not response
     function will return ''
     """
     super(ReplaceByView, self).__init__(*args, **kwargs)
     self.views = {}
     for setting in getattr(settings, 'MODELURL_VIEWS', []):
         self.views[importpath(setting['view'])] = setting
     self.check_sites = [value.lower() for value in check_sites]
     self.check_schemes = [value.lower() for value in check_schemes]
     self.check_unregistered = check_unregistered
Ejemplo n.º 6
0
            '/static/modeltranslation/js/tabbed_translation_fields.js',
        )
        css = {
            'screen': ('/static/modeltranslation/css/tabbed_translation_fields.css',),
        }


class UrlAdmin(admin.ModelAdmin):
    model = Url
    inlines = [SeoInlines]

admin.site.register(Url, UrlAdmin)


if not hasattr(settings, 'SEO_FOR_MODELS'):
    raise ImproperlyConfigured('Please add ``SEO_FOR_MODELS = ["<app>.admin.<ModelAdmin>",]`` to your settings.py')

for model_name in settings.SEO_FOR_MODELS:
    model = importpath(model_name, 'SEO_FOR_MODELS')
    try:
        model_admin = admin.site._registry[model].__class__
    except KeyError:
        raise ImproperlyConfigured('Please set ``seo`` in your settings.py only as last INSTALLED_APPS')
    admin.site.unregister(model)

    setattr(model_admin, 'inlines', getattr(model_admin, 'inlines', []))
    if not SeoInlines in model_admin.inlines:
        model_admin.inlines = list(model_admin.inlines)[:] + [SeoInlines]

    admin.site.register(model, model_admin)