Permalink
Browse files

published and in_lists, in models, admin and managers

  • Loading branch information...
1 parent 08ca28b commit f5981c4a7bb0d4d3bd81efa69814e18a79eaca7b @evildmp committed Feb 16, 2012
@@ -1,7 +1,8 @@
+from django.utils.translation import ugettext_lazy as _
from django.db import models
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
-
+from datetime import datetime
from cms.models.fields import PlaceholderField
from filer.fields.image import FilerImageField
@@ -25,7 +26,11 @@ class Meta:
help_text= u"e.g. Man bites dog (if left blank, will be copied from Title)")
summary = models.TextField(verbose_name="Summary",
null=False, blank=False,
- help_text="e.g. Cardiff man arrested in latest wave of man-on-dog violence (maximum two lines)",)
+ help_text="e.g. Cardiff man arrested in latest wave of man-on-dog violence (maximum two lines)")
+ published = models.BooleanField(default=False, verbose_name=_(u"Is published"), db_index=True,
+ help_text=_(u"Select when ready to be published"))
+ in_lists = models.BooleanField(_(u"Display in lists"), default=True, db_index=True,
+ help_text=_(u"If deselected, this item will not appear in lists"))
body = PlaceholderField('body', help_text="Not used or required for external items")
image = FilerImageField(null=True, blank=True)
@@ -36,7 +41,7 @@ class Meta:
publish_to = models.ManyToManyField(Entity, null=True, blank=True, related_name="%(class)s_publish_to",
help_text=u"Use these sensibly - don't send minor items to the home page, for example")
please_contact = models.ManyToManyField(Person, related_name='%(class)s_person',
- help_text=u'The person to whom enquiries about this should be directed ',
+ help_text=u"The person to whom enquiries about this should be directed",
null=True, blank=True)
IMPORTANCES = (
(0, u"Normal"),
@@ -1,5 +1,101 @@
+import operator
+
+from datetime import datetime
+
from django.db import models
+from django.db.models import Q
+from django.conf import settings
+
+MULTIPLE_ENTITY_MODE = settings.MULTIPLE_ENTITY_MODE
class ArkestraGenericModelManager(models.Manager):
def get_by_natural_key(self, slug):
return self.get(slug=slug)
+
+ def get_items(self, instance):
+ publishable_items = self.get_publishable_items(instance)
+ if instance.order_by == "importance/date":
+ top_items, ordinary_items = self.get_items_ordered_by_importance_and_date(instance, publishable_items)
+ return top_items + ordinary_items
+ else:
+ return self.get_publishable_items(instance)
+
+ def get_publishable_items(self, instance):
+ # returns items that can be published, latest items first
+ publishable_items = self.get_items_for_entity(instance) \
+ .filter(date__lte = datetime.today(), published=True, in_lists=True) \
+ .order_by('-date')
+ return publishable_items
+
+ def get_items_for_entity(self, instance):
+ # returns every items item associated with this entity,
+ # or all items items if MULTIPLE_ENTITY_MODE is False, or instance.entity is unspecified
+ if MULTIPLE_ENTITY_MODE and instance.entity:
+ items_for_entity = self.model.objects.filter(
+ Q(hosted_by=instance.entity) | Q(publish_to=instance.entity)
+ ).distinct()
+ else:
+ items_for_entity = self.model.objects.all()
+ # print "All items", all_items.count()
+ return items_for_entity
+
+ def get_items_ordered_by_importance_and_date(self, instance, publishable_items):
+ ordinary_items = []
+
+ # split the within-date items for this entity into two sets
+ publishable_items = self.get_publishable_items(instance)
+ sticky_items = publishable_items.order_by('-importance').filter(
+ Q(hosted_by=instance.entity) | Q(is_sticky_everywhere = True),
+ sticky_until__gte=datetime.today(),
+ )
+ non_sticky_items = publishable_items.exclude(
+ Q(hosted_by=instance.entity) | Q(is_sticky_everywhere = True),
+ sticky_until__gte=datetime.today(),
+ )
+ # print "Sticky items", sticky_items.count()
+ # print "Non-sticky items", non_sticky_items.count()
+ top_items = list(sticky_items)
+
+ # now we have to go through the non-top items, and find any that can be promoted
+ # get the set of dates where possible promotable items can be found
+ dates = non_sticky_items.dates('date', 'day').reverse()
+ # print "Going through the date set"
+ for date in dates:
+ # print " examining possibles from", date
+ # get all non-top items from this date
+ possible_top_items = non_sticky_items.filter(date__year=date.year, date__month= date.month, date__day= date.day)
+ # promotable items have importance > 0
+ # print " found", possible_top_items.count(), "of which I will promote", possible_top_items.filter(Q(hosted_by=instance.entity) | Q(is_sticky_everywhere = True),importance__gte = 1).count()
+ # add the good ones to the top items list
+ top_items.extend(possible_top_items.filter(
+ Q(hosted_by=instance.entity) | Q(is_sticky_everywhere = True),
+ importance__gte = 1)
+ )
+ # if this date set contains any unimportant items, then there are no more to promote
+ demotable_items = possible_top_items.exclude(
+ Q(hosted_by=instance.entity) | Q(is_sticky_everywhere = True),
+ importance__gte = 1
+ )
+ if demotable_items.count() > 0:
+ # put those unimportant items into ordinary items
+ ordinary_items.extend(demotable_items)
+ # print " demoting", demotable_items.count()
+ # and stop looking for any more
+ break
+ # and add everything left in non-sticky items before this date
+ if dates:
+ remaining_items = non_sticky_items.filter(date__lte=date)
+ # print " demoting the remaining", remaining_items.count()
+ ordinary_items.extend(remaining_items)
+ for item in top_items:
+ item.sticky = True
+ if instance.format == "title":
+ item.importance = None
+ # print "Top items", len(top_items)
+ # print "Ordinary items", len(ordinary_items)
+ ordinary_items.sort(key=operator.attrgetter('date'), reverse = True)
+ return top_items, ordinary_items
+
+
+
+
@@ -86,6 +86,7 @@ class Meta:
}),
'where_to_publish': ('', {'fields': ('publish_to',)}),
'people': ('People to contact about this item', {'fields': ('please_contact',)}),
+ 'publishing_control': ('Publishing control', {'fields': ('published', 'in_lists')}),
'date': ('', {'fields': ('date',)}),
'closing_date': ('', {'fields': ('closing_date',)}),
'importance': ('', {'fields': ('importance',)}),
@@ -513,7 +513,8 @@ class PersonLite(models.Model):
surname = models.CharField(max_length=50)
def __unicode__(self):
- return unicode(self.given_name + " " + self.middle_names + " " + self.surname)
+ # TODO: make it smarter, i.e. don't include empty/None strings
+ return u"%s %s %s" % (self.given_name, self.middle_names, self.surname)
def __getInitials(self):
if self.given_name <> '' and self.middle_names <> '':
@@ -696,7 +697,7 @@ def __unicode__(self):
else:
return unicode(self.role)
- def save(self):
+ def save(self, *args, **kwargs):
"""
The rules:
order importance_to_entity
@@ -722,7 +723,7 @@ def save(self):
self.importance_to_person = 2
if self.importance_to_entity < 2:
self.importance_to_entity = 2 # and importance_to_entity must be 2
- super(Membership, self).save()
+ super(Membership, self).save(*args, **kwargs)
class EntityAutoPageLinkPluginEditor(CMSPlugin):
View
@@ -101,7 +101,7 @@ class NewsArticleAdmin(NewsAndEventsAdmin):
fieldset_stickiness = ('How this item should behave in lists', {'fields': ('sticky_until', 'is_sticky_everywhere',)})
tabs = (
- ('Basic', {'fieldsets': (fieldsets["basic"], fieldsets["host"], fieldsets["image"]),}),
+ ('Basic', {'fieldsets': (fieldsets["basic"], fieldsets["host"], fieldsets["image"], fieldsets["publishing_control"],),}),
('Date & significance', {'fieldsets': (fieldsets["date"], fieldsets["importance"], fieldset_stickiness)}),
('Body', {'fieldsets': (fieldsets["body"],)}),
('Where to Publish', {'fieldsets': (fieldsets["where_to_publish"],)}),
@@ -230,7 +230,7 @@ class EventAdmin(NewsAndEventsAdmin):
fieldset_featuring = ('Featured people', {'fields': ('featuring',)})
fieldset_jumpiness = ('How this item should behave in lists', {'fields': ('jumps_queue_on', 'jumps_queue_everywhere')})
tabs = (
- ('Basic', {'fieldsets': (fieldsets["basic"], fieldset_type, fieldsets["host"], fieldsets["image"])}),
+ ('Basic', {'fieldsets': (fieldsets["basic"], fieldset_type, fieldsets["host"], fieldsets["image"], fieldsets["publishing_control"],)}),
('Date & significance', {'fieldsets':
(
fieldset_when,
@@ -10,93 +10,6 @@
COLLECT_TOP_ALL_FORTHCOMING_EVENTS = settings.COLLECT_TOP_ALL_FORTHCOMING_EVENTS
-class NewsArticleManager(ArkestraGenericModelManager):
- def get_news_ordered_by_importance_and_date(self, instance):
- ordinary_news = []
-
- # split the within-date items for this entity into two sets
- publishable_news = self.get_publishable_news(instance)
- sticky_news = publishable_news.order_by('-importance').filter(
- Q(hosted_by=instance.entity) | Q(is_sticky_everywhere = True),
- sticky_until__gte=datetime.today(),
- )
- non_sticky_news = publishable_news.exclude(
- Q(hosted_by=instance.entity) | Q(is_sticky_everywhere = True),
- sticky_until__gte=datetime.today(),
- )
- # print "Sticky news", sticky_news.count()
- # print "Non-sticky news", non_sticky_news.count()
- top_news = list(sticky_news)
-
- # now we have to go through the non-top items, and find any that can be promoted
- # get the set of dates where possible promotable items can be found
- dates = non_sticky_news.dates('date', 'day').reverse()
- # print "Going through the date set"
- for date in dates:
- # print " examining possibles from", date
- # get all non-top items from this date
- possible_top_news = non_sticky_news.filter(date__year=date.year, date__month= date.month, date__day= date.day)
- # promotable items have importance > 0
- # print " found", possible_top_news.count(), "of which I will promote", possible_top_news.filter(Q(hosted_by=instance.entity) | Q(is_sticky_everywhere = True),importance__gte = 1).count()
- # add the good ones to the top news list
- top_news.extend(possible_top_news.filter(
- Q(hosted_by=instance.entity) | Q(is_sticky_everywhere = True),
- importance__gte = 1)
- )
- # if this date set contains any unimportant items, then there are no more to promote
- demotable_items = possible_top_news.exclude(
- Q(hosted_by=instance.entity) | Q(is_sticky_everywhere = True),
- importance__gte = 1
- )
- if demotable_items.count() > 0:
- # put those unimportant items into ordinary news
- ordinary_news.extend(demotable_items)
- # print " demoting", demotable_items.count()
- # and stop looking for any more
- break
- # and add everything left in non-sticky news before this date
- if dates:
- remaining_items = non_sticky_news.filter(date__lte=date)
- # print " demoting the remaining", remaining_items.count()
- ordinary_news.extend(remaining_items)
- for item in top_news:
- item.sticky = True
- if instance.format == "title":
- item.importance = None
- # print "Top news", len(top_news)
- # print "Ordinary news", len(ordinary_news)
- ordinary_news.sort(key=operator.attrgetter('date'), reverse = True)
- return top_news, ordinary_news
-
- def get_publishable_news(self, instance):
- # returns news items that can be published, latest news first
- publishable_news = self.get_all_news(instance) \
- .filter(date__lte = datetime.today()) \
- .order_by('-date')
- return publishable_news
-
-
- def get_all_news(self, instance):
- # returns every news item associated with this entity,
- # or all news items if MULTIPLE_ENTITY_MODE is False, or instance.entity is unspecified
- if MULTIPLE_ENTITY_MODE and instance.entity:
- all_news = self.model.objects.filter(
- Q(hosted_by=instance.entity) | Q(publish_to=instance.entity)
- ).distinct()
- else:
- all_news = self.model.objects.all()
- # print "All news", all_news.count()
- return all_news
-
- def get_items(self, instance):
- if instance.order_by == "importance/date":
- top_news, ordinary_news = self.get_news_ordered_by_importance_and_date(instance)
- instance.news = top_news + ordinary_news
- else:
- instance.news = self.get_publishable_news(instance)
- return instance.news
-
-
class EventManager(ArkestraGenericModelManager):
def get_items(self, instance):
self.get_events(instance) # gets previous_events, forthcoming_events, top_events, ordinary_events
@@ -125,6 +38,8 @@ def get_events(self, instance):
else:
all_events = self.model.objects.all().order_by('start_date', 'start_time')
+ all_events = all_events.filter(published=True, in_lists=True)
+
actual_events = all_events.filter(
# if it's (not a series and not a child) - series events are excluded, children too unless:
# the child's parent is a series and its children can be advertised
Oops, something went wrong.

0 comments on commit f5981c4

Please sign in to comment.