Commits

Luke Plant committed db6363a

Added 'likes' field to Author test model, and a view using a FilterSet for Author

Comments (0)

Files changed (6)

django_easyfilters/tests/admin.py

     list_editable = ["binding", "genre", "price", "date_published", "edition"]
     list_filter = ["genre", "authors", "binding", "price"]
 
+
+class AuthorAdmin(admin.ModelAdmin):
+    list_display = ["id", "name", "likes"]
+    list_editable = ["name", "likes"]
+
 admin.site.register(Book, BookAdmin)
-admin.site.register(Author)
+admin.site.register(Author, AuthorAdmin)
 admin.site.register(Genre)

django_easyfilters/tests/fixtures/django_easyfilters_tests.json

     "pk": 4, 
     "model": "tests.author", 
     "fields": {
-      "name": "A. A. Milne"
+      "name": "A. A. Milne", 
+      "likes": 1234
     }
   }, 
   {
     "pk": 6, 
     "model": "tests.author", 
     "fields": {
-      "name": "Anne Bront\u00eb"
+      "name": "Anne Bront\u00eb", 
+      "likes": 678
     }
   }, 
   {
     "pk": 10, 
     "model": "tests.author", 
     "fields": {
-      "name": "Arthur C. Clarke"
+      "name": "Arthur C. Clarke", 
+      "likes": 890
     }
   }, 
   {
     "pk": 9, 
     "model": "tests.author", 
     "fields": {
-      "name": "Charles Dickens"
+      "name": "Charles Dickens", 
+      "likes": 777
     }
   }, 
   {
     "pk": 2, 
     "model": "tests.author", 
     "fields": {
-      "name": "Charlotte Bront\u00eb"
+      "name": "Charlotte Bront\u00eb", 
+      "likes": 1534
     }
   }, 
   {
     "pk": 5, 
     "model": "tests.author", 
     "fields": {
-      "name": "E. H. Shepard"
+      "name": "E. H. Shepard", 
+      "likes": 970
     }
   }, 
   {
     "pk": 7, 
     "model": "tests.author", 
     "fields": {
-      "name": "Emily Bront\u00eb"
+      "name": "Emily Bront\u00eb", 
+      "likes": 456
     }
   }, 
   {
     "pk": 8, 
     "model": "tests.author", 
     "fields": {
-      "name": "George Orwell"
+      "name": "George Orwell", 
+      "likes": 912
     }
   }, 
   {
     "pk": 1, 
     "model": "tests.author", 
     "fields": {
-      "name": "Jane Austen"
+      "name": "Jane Austen", 
+      "likes": 533
     }
   }, 
   {
     "pk": 3, 
     "model": "tests.author", 
     "fields": {
-      "name": "Lewis Carroll"
+      "name": "Lewis Carroll", 
+      "likes": 1101
     }
   }, 
   {

django_easyfilters/tests/models.py

 
 class Author(models.Model):
     name = models.CharField(max_length=50)
+    likes = models.IntegerField(default=0)
 
     def __unicode__(self):
         return self.name

django_easyfilters/tests/templates/authors.html

+<html>
+  <head>
+    <title>Author list</title>
+  </head>
+  <style type="text/css">
+body {
+    font-family: Verdana;
+}
+
+h1 {
+    font-size: 1.4em;
+}
+h2 {
+    font-size: 1.2em;
+    margin: 0.6em -0.4em;
+}
+
+div.book {
+    padding-left: 1em;
+    margin-bottom: 0.6em;
+    border-top: 1px solid #888;
+}
+
+/* Filters */
+.filterline {
+    margin-bottom: 0.5em;
+    margin-left: 1em;
+    text-indent: -1em;
+    line-height: 1.8em;
+}
+
+.filterline .filterlabel {
+    font-weight: bold;
+}
+
+
+.removefilter a:link,
+.removefilter a:visited,
+.addfilter a:link,
+.addfilter a:visited {
+    text-decoration: none;
+}
+
+.removefilter a:link,
+.removefilter a:visited {
+    border-radius: 4px;
+    padding: 0px 1px 2px 3px;
+    border: 1px solid #113399;
+    background-color: #113399;
+    color: white;
+}
+
+.addfilter a:link,
+.addfilter a:visited {
+    border-radius: 4px;
+    padding: 0px 1px 2px 3px;
+    border: 1px solid #f0f0f0;
+    background-color: #f0f0f0;
+    color: black;
+}
+
+/* :hover come after to override */
+.addfilter a:hover,
+.removefilter a:hover {
+    border-radius: 4px;
+    padding: 0px 1px 2px 3px;
+    border: 1px solid #3355bb;
+    background-color: #3355bb;
+    color: white;
+}
+
+.displayfilter {
+    border-radius: 4px;
+    padding: 0px 1px 2px 3px;
+    border: 1px solid #303030;
+    background-color: white;
+    color: #505050;
+}
+
+  </style>
+  <body>
+
+    <h1>Authors</h1>
+    {{ authorsfilter }}
+
+    <p>The following authors were found:</p>
+    {% for author in authors %}
+      <div class="author">
+        <h2>{{ author.name }}</h2>
+      </div>
+    {% endfor %}
+  </body>
+</html>

django_easyfilters/tests/urls.py

 
 urlpatterns = patterns('',
                        (r'^books/', 'django_easyfilters.tests.views.books'),
+                       (r'^authors/', 'django_easyfilters.tests.views.authors'),
                        (r'^admin/', include(admin.site.urls)),
 
 )

django_easyfilters/tests/views.py

 from django.shortcuts import render
 
-from django_easyfilters.tests.models import Book
+from django_easyfilters.tests.models import Book, Author
 from django_easyfilters import FilterSet
+from django_easyfilters.filters import NumericRangeFilter
 
 class BookFilterSet(FilterSet):
     fields = [
     booksfilter = BookFilterSet(books, request.GET)
     return render(request, "books.html", {'books': booksfilter.qs,
                                           'booksfilter': booksfilter})
+
+class AuthorFilterSet(FilterSet):
+    fields = [
+        ('likes', {}, NumericRangeFilter)
+        ]
+
+def authors(request):
+    authors = Author.objects.all()
+    authorsfilter = AuthorFilterSet(authors, request.GET)
+    return render(request, "authors.html", {'authors': authorsfilter.qs,
+                                            'authorsfilter': authorsfilter})