Implemented object deletion on null values. Added ModelChoiceWidget to ContentReferences.
[philo.git] / contrib / penfield / models.py
index 0e5bce0..bf5178e 100644 (file)
@@ -6,7 +6,7 @@ from django.conf.urls.defaults import url, patterns
 from django.core.urlresolvers import reverse
 from django.http import Http404, HttpResponse
 from datetime import datetime
 from django.core.urlresolvers import reverse
 from django.http import Http404, HttpResponse
 from datetime import datetime
-from philo.contrib.penfield.utils import paginate
+from philo.utils import paginate
 from philo.contrib.penfield.validators import validate_pagination_count
 
 
 from philo.contrib.penfield.validators import validate_pagination_count
 
 
@@ -15,13 +15,18 @@ class Blog(Entity, Titled):
        def entry_tags(self):
                """ Returns a QuerySet of Tags that are used on any entries in this blog. """
                return Tag.objects.filter(blogentries__blog=self).distinct()
        def entry_tags(self):
                """ Returns a QuerySet of Tags that are used on any entries in this blog. """
                return Tag.objects.filter(blogentries__blog=self).distinct()
+       
+       @property
+       def entry_dates(self):
+               dates = {'year': self.entries.dates('date', 'year', order='DESC'), 'month': self.entries.dates('date', 'month', order='DESC'), 'day': self.entries.dates('date', 'day', order='DESC')}
+               return dates
 
 
 register_value_model(Blog)
 
 
 class BlogEntry(Entity, Titled):
 
 
 register_value_model(Blog)
 
 
 class BlogEntry(Entity, Titled):
-       blog = models.ForeignKey(Blog, related_name='entries')
+       blog = models.ForeignKey(Blog, related_name='entries', blank=True, null=True)
        author = models.ForeignKey(getattr(settings, 'PHILO_PERSON_MODULE', 'auth.User'), related_name='blogentries')
        date = models.DateTimeField(default=datetime.now)
        content = models.TextField()
        author = models.ForeignKey(getattr(settings, 'PHILO_PERSON_MODULE', 'auth.User'), related_name='blogentries')
        date = models.DateTimeField(default=datetime.now)
        content = models.TextField()
@@ -75,6 +80,17 @@ class BlogView(MultiView):
                elif isinstance(obj, Tag):
                        if obj in self.blog.entry_tags:
                                return reverse(self.tag_view, urlconf=self, kwargs={'tag_slugs': obj.slug})
                elif isinstance(obj, Tag):
                        if obj in self.blog.entry_tags:
                                return reverse(self.tag_view, urlconf=self, kwargs={'tag_slugs': obj.slug})
+               elif isinstance(obj, (str, unicode)):
+                       split_obj = obj.split(':')
+                       if len(split_obj) > 1:
+                               entry_archive_view_args = {}
+                               if split_obj[0].lower() == 'archives':
+                                       entry_archive_view_args.update({'year': str(int(split_obj[1])).zfill(4)})
+                                       if len(split_obj) > 2:
+                                               entry_archive_view_args.update({'month': str(int(split_obj[2])).zfill(2)})
+                                               if len(split_obj) > 3:
+                                                       entry_archive_view_args.update({'day': str(int(split_obj[3])).zfill(2)})
+                                       return reverse(self.entry_archive_view, urlconf=self, kwargs=entry_archive_view_args)
                raise ViewCanNotProvideSubpath
        
        @property
                raise ViewCanNotProvideSubpath
        
        @property
@@ -114,15 +130,10 @@ class BlogView(MultiView):
                return base_patterns + entry_patterns
        
        def index_view(self, request, node=None, extra_context=None):
                return base_patterns + entry_patterns
        
        def index_view(self, request, node=None, extra_context=None):
-               entries = self.blog.entries.all()
-               if self.entries_per_page:
-                       paginated_page = paginate(request, entries, self.entries_per_page)
-                       entries = paginated_page.object_list
-               else:
-                       paginated_page = None
+               paginator, page, entries = paginate(self.blog.entries.all(), self.entries_per_page, request.GET.get('page', 1))
                context = {}
                context.update(extra_context or {})
                context = {}
                context.update(extra_context or {})
-               context.update({'blog': self.blog, 'entries': entries, 'paginated_page': paginated_page})
+               context.update({'blog': self.blog, 'paginator': paginator, 'paginated_page': page, 'entries': entries})
                return self.index_page.render_to_response(node, request, extra_context=context)
        
        def entry_view(self, request, slug, year=None, month=None, day=None, node=None, extra_context=None):
                return self.index_page.render_to_response(node, request, extra_context=context)
        
        def entry_view(self, request, slug, year=None, month=None, day=None, node=None, extra_context=None):
@@ -152,14 +163,11 @@ class BlogView(MultiView):
                        entries = entries.filter(date__month=month)
                if day:
                        entries = entries.filter(date__day=day)
                        entries = entries.filter(date__month=month)
                if day:
                        entries = entries.filter(date__day=day)
-               if self.entries_per_page:
-                       paginated_page = paginate(request, entries, self.entries_per_page)
-                       entries = paginated_page.object_list
-               else:
-                       paginated_page = None
+               
+               paginator, page, entries = paginate(entries, self.entries_per_page, request.GET.get('page', 1))
                context = {}
                context.update(extra_context or {})
                context = {}
                context.update(extra_context or {})
-               context.update({'blog': self.blog, 'year': year, 'month': month, 'day': day, 'entries': entries, 'paginated_page': paginated_page})
+               context.update({'blog': self.blog, 'year': year, 'month': month, 'day': day, 'paginator': paginator, 'paginated_page': page, 'entries': entries})
                return self.entry_archive_page.render_to_response(node, request, extra_context=context)
        
        def tag_view(self, request, tag_slugs, node=None, extra_context=None):
                return self.entry_archive_page.render_to_response(node, request, extra_context=context)
        
        def tag_view(self, request, tag_slugs, node=None, extra_context=None):
@@ -180,14 +188,10 @@ class BlogView(MultiView):
                if entries.count() <= 0:
                        raise Http404
                
                if entries.count() <= 0:
                        raise Http404
                
-               if self.entries_per_page:
-                       paginated_page = paginate(request, entries, self.entries_per_page)
-                       entries = paginated_page.object_list
-               else:
-                       paginated_page = None
+               paginator, page, entries = paginate(entries, self.entries_per_page, request.GET.get('page', 1))
                context = {}
                context.update(extra_context or {})
                context = {}
                context.update(extra_context or {})
-               context.update({'blog': self.blog, 'tags': tags, 'entries': entries, 'paginated_page': paginated_page})
+               context.update({'blog': self.blog, 'tags': tags, 'paginator': paginator, 'paginated_page': page, 'entries': entries})
                return self.tag_page.render_to_response(node, request, extra_context=context)
        
        def tag_archive_view(self, request, node=None, extra_context=None):
                return self.tag_page.render_to_response(node, request, extra_context=context)
        
        def tag_archive_view(self, request, node=None, extra_context=None):
@@ -203,6 +207,9 @@ class Newsletter(Entity, Titled):
        pass
 
 
        pass
 
 
+register_value_model(Newsletter)
+
+
 class NewsletterArticle(Entity, Titled):
        newsletter = models.ForeignKey(Newsletter, related_name='articles')
        authors = models.ManyToManyField(getattr(settings, 'PHILO_PERSON_MODULE', 'auth.User'), related_name='newsletterarticles')
 class NewsletterArticle(Entity, Titled):
        newsletter = models.ForeignKey(Newsletter, related_name='articles')
        authors = models.ManyToManyField(getattr(settings, 'PHILO_PERSON_MODULE', 'auth.User'), related_name='newsletterarticles')
@@ -211,6 +218,7 @@ class NewsletterArticle(Entity, Titled):
        full_text = models.TextField()
        
        class Meta:
        full_text = models.TextField()
        
        class Meta:
+               get_latest_by = 'date'
                ordering = ['-date']
                unique_together = (('newsletter', 'slug'),)
 
                ordering = ['-date']
                unique_together = (('newsletter', 'slug'),)
 
@@ -221,13 +229,16 @@ register_value_model(NewsletterArticle)
 class NewsletterIssue(Entity, Titled):
        newsletter = models.ForeignKey(Newsletter, related_name='issues')
        number = models.PositiveIntegerField()
 class NewsletterIssue(Entity, Titled):
        newsletter = models.ForeignKey(Newsletter, related_name='issues')
        number = models.PositiveIntegerField()
-       articles = models.ManyToManyField(NewsletterArticle)
+       articles = models.ManyToManyField(NewsletterArticle, related_name='issues')
        
        class Meta:
                ordering = ['-number']
                unique_together = (('newsletter', 'number'),)
 
 
        
        class Meta:
                ordering = ['-number']
                unique_together = (('newsletter', 'number'),)
 
 
+register_value_model(NewsletterIssue)
+
+
 class NewsletterView(MultiView):
        ARTICLE_PERMALINK_STYLE_CHOICES = (
                ('D', 'Year, month, and day'),
 class NewsletterView(MultiView):
        ARTICLE_PERMALINK_STYLE_CHOICES = (
                ('D', 'Year, month, and day'),