from django.conf import settings
from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from django.contrib.auth.decorators import login_required
admin.autodiscover()

from utils import decorated_includes
import request.urls
import delegation.urls

urlpatterns = patterns('',
    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    url(r'^grappelli/', include('grappelli.urls')),
    url(r'^admin/', include(admin.site.urls)),

    url(r'^request/',
        decorated_includes(login_required, include(request.urls.urlpatterns))),
    url(r'^delegation/',
        decorated_includes(login_required, include(delegation.urls.urlpatterns))),
    url(r'^', include('polynum.pages.urls')),
    url(r'^', include('polynum.entity.urls')),
    url(r'^oai/', include('polynum.oai.urls')),
    url(r'^editor/', include('polynum.editor.urls')),
    url(r'^su/(?P<username>.*)/$', 'polynum.base.views.su', {'redirect_url': '/'}),
    )

if 'polynum.base.backends.CASBackend' in settings.AUTHENTICATION_BACKENDS:
    urlpatterns += patterns('',
            url(r'^accounts/login/$', 'django_cas.views.login', name='login'),
            url(r'^accounts/logout/$', 'django_cas.views.logout', name='logout'),
            )
else:
    urlpatterns += patterns('',
            url(r'^accounts/login/$', 'django.contrib.auth.views.login', 
                {'template_name': 'login.html'} if not settings.CAS_SERVER_URL else {}, name='login'),
            url(r'^accounts/logout/$', 'django.contrib.auth.views.logout', 
                {'template_name': 'logout.html'} if not settings.CAS_SERVER_URL else {}, name='logout'),
            )


