def published(self): """ Return only published entries """ return self \ .filter(status=self.model.PUBLISHED) \ .filter( Q(publication_date__isnull=True) | Q(publication_date__lte=now()) ).filter( Q(publication_end_date__isnull=True) | Q(publication_end_date__gte=now()) )
def validate_unique_slug(self, cleaned_data): """ Test whether the slug is unique within a given time period. """ kwargs = {} error_msg = _("The slug is not unique") # The /year/month/slug/ URL determines when a slug can be unique. pubdate = cleaned_data['publication_date'] or now() if '{year}' in appsettings.FLUENT_BLOGS_ENTRY_LINK_STYLE: kwargs['year'] = pubdate.year error_msg = _("The slug is not unique within it's publication year.") if '{month}' in appsettings.FLUENT_BLOGS_ENTRY_LINK_STYLE: kwargs['month'] = pubdate.month error_msg = _("The slug is not unique within it's publication month.") if '{day}' in appsettings.FLUENT_BLOGS_ENTRY_LINK_STYLE: kwargs['day'] = pubdate.day error_msg = _("The slug is not unique within it's publication day.") date_range = get_date_range(**kwargs) if date_range: dup_qs = EntryModel.objects.filter(slug=cleaned_data['slug'], publication_date__range=date_range) else: dup_qs = EntryModel.objects.filter(slug=cleaned_data['slug']) if self.instance and self.instance.pk: dup_qs = dup_qs.exclude(pk=self.instance.pk) # Test whether the slug is unique in the current month # Note: doesn't take changes to FLUENT_BLOGS_ENTRY_LINK_STYLE into account. if dup_qs.exists(): raise ValidationError(error_msg)
def validate_unique_slug(self, cleaned_data): """ Test whether the slug is unique within a given time period. """ date_kwargs = {} error_msg = _("The slug is not unique") # The /year/month/slug/ URL determines when a slug can be unique. pubdate = cleaned_data['publication_date'] or now() if '{year}' in appsettings.FLUENT_BLOGS_ENTRY_LINK_STYLE: date_kwargs['year'] = pubdate.year error_msg = _("The slug is not unique within it's publication year.") if '{month}' in appsettings.FLUENT_BLOGS_ENTRY_LINK_STYLE: date_kwargs['month'] = pubdate.month error_msg = _("The slug is not unique within it's publication month.") if '{day}' in appsettings.FLUENT_BLOGS_ENTRY_LINK_STYLE: date_kwargs['day'] = pubdate.day error_msg = _("The slug is not unique within it's publication day.") date_range = get_date_range(**date_kwargs) # Base filters are configurable for translation support. dup_filters = self.get_unique_slug_filters(cleaned_data) if date_range: dup_filters['publication_date__range'] = date_range dup_qs = EntryModel.objects.filter(**dup_filters) if self.instance and self.instance.pk: dup_qs = dup_qs.exclude(pk=self.instance.pk) # Test whether the slug is unique in the current month # Note: doesn't take changes to FLUENT_BLOGS_ENTRY_LINK_STYLE into account. if dup_qs.exists(): raise ValidationError(error_msg)
def published(self): """ Return only published entries for the current site. """ if appsettings.FLUENT_BLOGS_FILTER_SITE_ID: qs = self.parent_site(settings.SITE_ID) else: qs = self return qs \ .filter(status=self.model.PUBLISHED) \ .filter( Q(publication_date__isnull=True) | Q(publication_date__lte=now()) ).filter( Q(publication_end_date__isnull=True) | Q(publication_end_date__gte=now()) )
def save_model(self, request, obj, form, change): # Automatically store the user in the author field. if not change: obj.author = request.user if not obj.publication_date: # auto_now_add makes the field uneditable. # default fills the field before the post is written (too early) obj.publication_date = now() obj.save()