
from datetime import datetime
from dateutil.relativedelta import relativedelta
from django.conf.urls.defaults import *
from django.core.urlresolvers import reverse_lazy
from django.views.generic.list_detail import object_detail, object_list
from django.views.generic.create_update import update_object
from django.contrib.auth.decorators import login_required, user_passes_test
from haystack.query import SearchQuerySet

from portal import views, forms, models, permissions
from portal.haystack_util import QueryFacet, FacetClass, QueryFacetClass

FACETS = [
    QueryFacetClass(
        "years",
        "Date",
        paramname="date",
        sort=FacetClass.FACET_SORT_NAME,
        queries=[
            QueryFacet(query=("*", 1933), desc="Before 1933"),
            QueryFacet(query=(1933, 1939), desc="1933-1939"),
            QueryFacet(query=1940),
            QueryFacet(query=1941),
            QueryFacet(query=1942),
            QueryFacet(query=1943),
            QueryFacet(query=1944),
            QueryFacet(query=1945),
            QueryFacet(query=(1946, "*"), desc="After 1945"),
        ]
    ),
    FacetClass(
        "languages_of_description",
        "Language of Description",
        paramname="langdesc"
    ),
    FacetClass(
        "languages",
        "Language",
        paramname="lang"
    ),
    FacetClass(
        "location_of_materials",
        "Location of Materials",
        paramname="country",
    ),
    FacetClass(
        "tags",
        "Keyword",
        paramname="tag"
    ),
]

infolist = dict(
        queryset=models.Collection.objects.all(),
        paginate_by=20,
        template_name="collection_list.html",
)

viewdict = dict(
        queryset=models.Collection.objects.all()
)


urlpatterns = patterns('',
    url(r'^search/?$', views.PortalSearchListView.as_view(
        model=models.Collection,
        template_name="collection_search.html",
        facetclasses = FACETS),
            name='collection_search'),
    url(r'^search/(?P<facet>[^\/]+)/?$', views.PaginatedFacetView.as_view(
        redirect='collection_search',
        form_class=forms.FacetListSearchForm,
        model=models.Collection,
        facetclasses=FACETS),
            name='collection_facets'),
    url(r'^/?$', object_list, infolist, name='collection_list'),
    url(r'^(?P<slug>[-\w]+)/?$', object_detail, dict(
            queryset=models.Collection.objects.all(),
            template_name="collection_detail.html"
        ), name='collection_detail'),

    # Crud Actions
    url(r'^edit/(?P<slug>[-\w]+)/?$',
            user_passes_test(permissions.is_staff)(
                views.CollectionEditView.as_view()), name='collection_edit'),
    url(r'^delete/(?P<slug>[-\w]+)/?$', 
            user_passes_test(permissions.is_staff)(
                views.CollectionDeleteView.as_view()), name='collection_delete'),
    url(r'^create/?$', 
            user_passes_test(permissions.is_staff)(
                views.CollectionEditView.as_view()), name='collection_create'),
)


@@ -2305,224 +2305,183 @@
 '),%0A
-    url(r'%5E(?P%3Cslug%3E%5B-%5Cw%5D+)/?$', object_detail, dict(%0A            queryset=models.Collection.objects.all(),%0A            template_name=%22collection_detail.html%22%0A        ), name='collection_detail'),%0A%0A    # Crud Actions
+%0A    # Crud Actions%0A    url(r'%5Ecreate/?$', %0A            user_passes_test(permissions.is_staff)(%0A                views.CollectionEditView.as_view()), name='collection_create'),
 %0A   
@@ -2824,134 +2824,239 @@
 '),%0A
-    url(r'%5Ecreate/?$', %0A            user_passes_test(permissions.is_staff)(%0A                views.CollectionEditView.as_view()
+%0A    # This URL has to go last because it matches everything...%0A    url(r'%5E(?P%3Cslug%3E%5B-%5Cw%5D+)/?$', object_detail, dict(%0A            queryset=models.Collection.objects.all(),%0A            template_name=%22collection_detail.html%22%0A        
 ), n
@@ -3067,25 +3067,25 @@
 'collection_
-create
+detail
 '),%0A)%0A%0A

Fix URL ordering that was causing the slug pattern to grab everything and cause 404s. Added appropriate comment.