Skip to content
Browse files

fix: urls prefix was incorrectly constructed - didn't set matching fr…

…om start; test for disabled views added; example updated
  • Loading branch information...
1 parent eb2d8c9 commit 5e45ee24b3b46b7b9a990732ca893c91f5329f56 @rudyryk rudyryk committed Mar 23, 2013
Showing with 55 additions and 11 deletions.
  1. +2 −2 example/example/settings.py
  2. +13 −1 example/example/urls.py
  3. +11 −0 example/pages/models.py
  4. +12 −1 example/pages/views.py
  5. +2 −2 smarter/__init__.py
  6. +15 −5 smarter/tests.py
View
4 example/example/settings.py
@@ -51,12 +51,12 @@
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
-MEDIA_ROOT = ''
+MEDIA_ROOT = path.join(PROJECT_ROOT, 'media')
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
-MEDIA_URL = ''
+MEDIA_URL = '/media/'
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
View
14 example/example/urls.py
@@ -7,8 +7,10 @@
import smarter
site = smarter.Site()
-from pages.views import PageViews
+# from pages.models import Page, PageFile
+from pages.views import PageViews, PageFileViews
site.register(PageViews)
+site.register(PageFileViews)
urlpatterns = patterns('',
# Examples:
@@ -24,3 +26,13 @@
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
+
+
+from django.conf import settings
+
+if settings.DEBUG:
+ urlpatterns += patterns('',
+ url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
+ 'document_root': settings.MEDIA_ROOT,
+ }),
+ )
View
11 example/pages/models.py
@@ -11,3 +11,14 @@ def __unicode__(self):
def get_absolute_url(self):
return reverse('page-details', kwargs={'pk': self.pk})
+
+
+class PageFile(models.Model):
+ page = models.ForeignKey(Page)
+ attachment = models.FileField(upload_to='files')
+
+ def __unicode__(self):
+ return unicode(self.attachment)
+
+ def get_absolute_url(self):
+ return self.attachment.url
View
13 example/pages/views.py
@@ -1,5 +1,5 @@
import smarter
-from .models import Page
+from .models import Page, PageFile
class PageViews(smarter.GenericViews):
model = Page
@@ -12,4 +12,15 @@ class PageViews(smarter.GenericViews):
'exclude': ('owner',),
'redirect': lambda view, request, **kwargs: kwargs['obj'].get_absolute_url()
}
+ }
+
+class PageFileViews(smarter.GenericViews):
+ model = PageFile
+
+ options = {
+ 'edit': None,
+ 'details': None,
+ 'add': {
+ 'redirect': lambda view, request, **kwargs: view.get_url('index')
+ }
}
View
4 smarter/__init__.py
@@ -110,9 +110,9 @@ def register(self, views, model=None, base_url=None, prefix=None):
prefix_bits.append(model_name)
if base_url:
- base_url = '%s/' % (base_url.strip(' /') or model_name)
+ base_url = '^%s/' % (base_url.strip(' /') or model_name)
else:
- base_url = '%s/' % model_name
+ base_url = '^%s/' % model_name
self._registered.append({
'base_url': base_url,
View
20 smarter/tests.py
@@ -31,10 +31,7 @@ def get_absolute_url(self):
class AnotherTestModel(models.Model):
"""Well, another model for tests."""
- text = models.TextField()
-
- def get_absolute_url(self):
- return ('/test/testmodel/%s/' % self.pk)
+ another_text = models.TextField()
class TestViews(smarter.GenericViews):
@@ -65,7 +62,14 @@ class TestViews(smarter.GenericViews):
'url': r'(?P<pk>\d+)/protected/',
'form': None,
'permissions': ('smarter.view_testmodel',)
- }
+ },
+ }
+
+
+class AnotherTestViews(smarter.GenericViews):
+ options = {
+ # 'index': None, # Won't be enabled
+ 'add': None, # Won't be enabled
}
@@ -76,6 +80,7 @@ def setUp(self):
self.client = Client()
self.site = smarter.Site()
self.site.register(TestViews, TestModel)
+ self.site.register(AnotherTestViews, AnotherTestModel, base_url='another')
TestModel.objects.create(id=1, text='The first object.')
global urlpatterns
@@ -121,6 +126,7 @@ def test_generic_views_read(self):
self._test_url('/test/testmodel/100/')
self._test_url('/test/testmodel/100/edit/')
self._test_url('/test/testmodel/100/remove/')
+ self._test_url('/test/fakeprefix-testmodel/', 404)
def test_initial_option(self):
r = self.client.get('/test/testmodel/add/?text=Hohoho!')
@@ -166,5 +172,9 @@ def test_decorated_view(self):
r = self.client.get('/test/testmodel/1/decorated/')
self.assertRedirects(r, '/test/testmodel/?next=/test/testmodel/1/decorated/')
+ def test_disabled_view(self):
+ self._test_url('/test/another/')
+ self._test_url('/test/another/add/', 404)
+
def test_permissions(self):
self._test_url('/test/testmodel/1/protected/', 403)

0 comments on commit 5e45ee2

Please sign in to comment.
Something went wrong with that request. Please try again.