Commits

Josh VanderLinden  committed 9ca3532

Fixed a problem with the template tag returning already completed books when asking for books in progress.

  • Participants
  • Parent commits 67c7e21

Comments (0)

Files changed (3)

File bibliophile/admin.py

 class ProgressInline(admin.TabularInline):
     model = Progress
 
+class ProgressAdmin(admin.ModelAdmin):
+    list_display = ('book', 'milestone', 'date_achieved')
+
 class BookAdmin(admin.ModelAdmin):
     list_display = ('title', 'authors', 'progress', 'user')
     list_filter = ('show_progress', 'date_added', 'author')
     search_fields = ('title', 'description', 'isbn_10')
     prepopulated_fields = {'slug': ('title',)}
-    
+
     inlines = (ProgressInline,)
 
 admin.site.register(Author, AuthorAdmin)
 admin.site.register(Book, BookAdmin)
-admin.site.register(Progress)
+admin.site.register(Progress, ProgressAdmin)

File bibliophile/models.py

     ecs.setLicenseKey(CAN_PERFORM_AMAZON_LOOKUP)
 
 PROGRESS_CHOICES = (
-    ('p', 'Pages'),
-    ('c', 'Chapters'),
+    ('p', 'Page'),
+    ('c', 'Chapter'),
 )
 
 class Author(models.Model):
     last_name = models.CharField(max_length=50)
     slug = models.SlugField()
     website = models.URLField(blank=True)
-    
+
     def __unicode__(self):
         return u'%s, %s' % (self.last_name, self.first_name)
-    
+
     def get_absolute_url(self):
         return reverse('bibliophile-author', args=[self.slug])
-    
+
     def url_name(self):
         return mark_safe(u'<a href="%s">%s %s</a>' % (self.get_absolute_url(),
                                                       self.first_name,
     def _books_on_file(self):
         return self.book_set.count()
     books_on_file = property(_books_on_file)
-    
+
     class Meta:
         ordering = ('last_name', 'first_name')
 
     maximum_progress = models.PositiveIntegerField(help_text="The number of pages or chapters in the book, depending on your chosen progress mode.", default=0)
     date_added = models.DateField(auto_now_add=True)
     date_updated = models.DateField(auto_now=True)
-    
+
     def __unicode__(self):
-        return u'%s by %s' % (self.title, self.author_string)
+        return self.title #u'%s by %s' % (self.title, self.author_string)
 
     def _progress(self):
         try:
 
         return (float(value) / self.maximum_progress) * 100
     progress = property(_progress)
-    
+
     def date_finished(self):
         if self.progress >= 100:
             return self.progress_set.order_by('-milestone')[0].date_achieved
         return None
-    
+
     def _clean_author_string(self):
         return u', '.join([unicode(a) for a in self.author.all()])
     authors = property(_clean_author_string)
-    
+
     def _author_string(self):
         return mark_safe(u', '.join([a.url_name() for a in self.author.all()]))
     author_string = property(_author_string)
-    
+
     def get_amazon_data(self):
         if not hasattr(self, 'amazon_data'):
             self.amazon_data = ecs.ItemLookup(self.isbn_10)[0]
-            
+
     def amazon_url(self):
         self.get_amazon_data()
         return self.amazon_data.DetailPageURL
-    
+
     def price(self):
         self.get_amazon_data()
         cart = ecs.CartCreate([self.amazon_data], (1,))
         return cart.CartItems[0].Price.FormattedPrice
-    
+
     class Meta:
         ordering = ['-date_added', 'title']
 
     book = models.ForeignKey(Book)
     milestone = models.PositiveIntegerField()
     date_achieved = models.DateField(default=date.today)
-    
+
+    def __unicode__(self):
+        return 'Progressed to %s %i of %i in %s' % (
+                    self.book.get_progress_mode_display().lower(),
+                    self.milestone,
+                    self.book.maximum_progress,
+                    self.book.title
+                )
+
     class Meta:
         ordering = ['-date_achieved', 'book', '-milestone']
         verbose_name_plural = 'progress'

File bibliophile/templatetags/bibliophile_tags.py

 
 class BooksNode(template.Node):
     """
-    Injects a list of finished/unfinished/planned books into the context for 
+    Injects a list of finished/unfinished/planned books into the context for
     the user to render.
-    
+
     TODO: Make this work on a site with multiple users.
     """
     def __init__(self, count, varname, finished=False, planned=False):
         self.varname = varname
         self.finished = finished
         self.planned = planned
-    
+
     def render(self, context):
         """
         Determines which books should be returned.
         else:
             # books that have been started but not finished
             op = '<'
-        
+
         params = ("bibliophile_book.id = book_id",
                   "milestone %s bibliophile_book.maximum_progress" % op)
-        
-        qs = Progress.objects.extra(where=params)
+
+        qs = Progress.objects.extra(where=params,
+                                    tables=['bibliophile_book'],
+                                    order_by=['-date_achieved', '-milestone'])
         books = []
         for progress in qs:
             if progress.book not in books:
-                books.append(progress.book)
-                
+                if (not self.finished and progress.book.progress < 100) or \
+                    self.finished:
+                    books.append(progress.book)
+
                 if len(books) >= self.count: break
 
         context[self.varname] = books
         tag, count, a, varname = token.split_contents()
     except ValueError:
         raise template.TemplateSyntaxError('finished_books syntax: {% finished_books 5 as finished %}')
-    
+
     return BooksNode(count, varname, finished=True)
 
 def unfinished_books(parser, token):
         tag, count, a, varname = token.split_contents()
     except ValueError:
         raise template.TemplateSyntaxError('unfinished_books syntax: {% unfinished_books 5 as unfinished %}')
-    
+
     return BooksNode(count, varname, finished=False)
 
 def planning_to_read(parser, token):
         tag, count, a, varname = token.split_contents()
     except ValueError:
         raise template.TemplateSyntaxError('planning_to_read syntax: {% planning_to_read 5 as planned %}')
-    
+
     return BooksNode(count, varname, planned=True)
 
 register.tag(finished_books)