0

I made a model that displays articles and when you create an article you have the possibility to choose if this article will be a featured one.

So this is basically what I have in my Article model :

class Article(ModelMeta, TranslatableModel):
    taints_cache = True

    """
    Press article element,
    """
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)
    date_realization = models.DateField(_('Realised in'),
                                     default=timezone.now)
    image = FilerImageField(verbose_name=_('Featured image'), blank=True,
                             null=True,
                             on_delete=models.SET_NULL,
                             related_name='image_press_article',
                             help_text=_('Set if the article will be featured'))

    sources = models.ManyToManyField(ArticleSource, verbose_name=_('Source'),
                                    blank=False, null=True, related_name='sources_press_article')

    regions = models.ManyToManyField(Country, verbose_name=_('Country of the article'),
                                 blank=True, null=True,
                                 related_name='regions_press_article')

    global_regions = models.BooleanField('Global', default=True)

    featureArticle = models.BooleanField(_('Feature'), help_text=_('Feature this article'), default=False)

Then, I created a plugin that displays the featured articles. But the thing is, in the django plugin admin I let the user the possibility to choose which article he wants to display (with a maximum of 3). But in this choosing list, all my articles are listed.

What I want to, is to list only the articles that are checked as "featured", in my plugin admin. Instead of having all the articles.

Here what I have with my cms_plugin's model :

class FeaturedArticlePlugin(CMSPlugin):
    selected_article = SortedManyToManyField(Article, blank=True, verbose_name=_('Selected articles'),
                                       help_text=_('Select the featured articles to display'))

    def __str__(self):
        return u'%s Selected articles' % self.selected_article.all()

    def copy_relations(self, oldinstance):
        self.selected_article = oldinstance.selected_article.all()

And in my cms_plugins.py :

class PressPlugin(CMSPluginBase):
    module = 'Press'

class PressFeaturedArticlePlugin(PressPlugin):
    module = _('Press')
    name = _('Press feature')
    model = FeaturedArticlePlugin
    render_template = 'djangocms_press/plugins/feature_article.html'
    number_article = 3

    def render(self, context, instance, placeholder):
        """
        Get a list of selected_articles
        """
        selected_article = instance.selected_article.all()
        number_selected_article = selected_article.count()

        feature_article_list = list(selected_article[:self.number_article])

        context['instance'] = instance
        context['feature_article_list'] = feature_article_list
        return context


plugin_pool.register_plugin(PressFeaturedArticlePlugin)

So, I am sure it's nothing complicated but I can't point this out.

Anyone has a clue ?

EDIT From what I understand, the only thing that concern the display of all articles is this line :

selected_article = SortedManyToManyField(Article, blank=True, verbose_name=_('Selected articles'),
                                       help_text=_('Select the featured articles to display'))

So what I am suppose to do is to filter this selected_article with the featureArticle=True. But how to do it ?

Jay Cee
  • 1,855
  • 5
  • 28
  • 48

1 Answers1

0

I am not quite sure if I am missing something, but, couldn't you just apply a filter here?

selected_article = instance.selected_article.all().filter(featureArticle=true)
number_selected_article = selected_article.count()

Or is the problem with the lines after?

feature_article_list = list(selected_article[:self.number_article])

If your problem is selecting the extra articles, maybe you need to order them by date and select only the necessary?

feature_article_list = list(Articles.all().order_by('-created')[:self.number_article - number_selected_article]

Which will only select the extra necessaries?

Edit: Your situation kind of reminds me of a problem I once had. So I'll refer you to the same page that helped me in the past just in case you'd manage to figure it out.

Restrict django admin change permissions

Edit 2 : "I created a plugin that displays the featured articles. But the thing is, in the django plugin admin I let the user the possibility to choose which article he wants to display (with a maximum of 3). But in this choosing list, all my articles are listed."

Isn't it ok if all the articles are displayed there? How can you choose among them if they are not all displayed?

Community
  • 1
  • 1
phenxd
  • 689
  • 4
  • 17
  • Hmm I still have all the article displayed. I tried : `instance.selected_article.all().filter(featureArticle=True)` and `instance.selected_article.filter(featureArticle=True)` – Jay Cee Feb 09 '16 at 15:24
  • Could you verify that only the featured articles are in the field selected_articles? – phenxd Feb 09 '16 at 15:31
  • Well, feature_article_list is the context that is use for the template, I am not sure it impacts the admin ! Correct me if I'm wrong. – Jay Cee Feb 09 '16 at 15:33
  • And yes, the featured ones are in the list – Jay Cee Feb 09 '16 at 15:34
  • Wait a sec, is your problem in the FeaturedArticlePlugin or in the PressFeaturedArticlePlugin? – phenxd Feb 09 '16 at 15:42
  • This is all my question about.. I am not use to work with django-cms ! I was quite surprised that I found no one with kind of the same problem – Jay Cee Feb 09 '16 at 15:44
  • To answer your edit, you might have 10 featured article, but a maximum of 3 will be display in the templates, that's why ! And to answer your other question, I think the problem come from the model (FeaturedArticlePlugin) – Jay Cee Feb 09 '16 at 15:48
  • So it should be impossible to choose more than 3? Or possible yet we only show 3 in the template? – phenxd Feb 09 '16 at 15:49
  • Well, the person that will select it knows that he can't select more than 3. But yeah if 3 or more are selected only 3 will be displayed. – Jay Cee Feb 09 '16 at 15:50
  • How can FeatureArticlePlugin be the problem if you said this line selected_article = instance.selected_article.all() only returned the selected articles? Which is good – phenxd Feb 09 '16 at 15:53