Example #1
0
    def get_context_data(self, **kwargs):
        """
        Loads common information that is needed throughout the website.

        First it gets blog information, that is, getting articles by year and
        month, and articles by tag. Then adds email and google recaptcha keys
        to compose the context data that will be passed to all the views.

        Arguments:
            **kwargs: Django's default kwargs for this overridden function.
        """
        # Populate entries by time line info.
        entries_by_time_line = []
        for active_year in Article.active_years():
            articles_per_year = Article.articles_per_year(active_year)
            month_list = []
            for active_month in Article.active_months_per_year(active_year):
                articles = Article.articles_per_month_and_year(
                    active_year,
                    active_month
                )
                articles_list = []
                for article in articles:
                    articles_list.append((article.id, article.title))
                month_list.append((
                    active_month,
                    MONTHS_OF_THE_YEAR[active_month],
                    articles.count(),
                    articles_list
                ))
            entries_by_time_line.append((active_year,
                                         articles_per_year.count(),
                                         month_list))
        # Populate entries by tag info.
        entries_by_tag = []
        for tag in Tag.objects.all():
            articles = tag.related_articles.all()
            articles_list = []
            for article in articles:
                articles_list.append((article.id, article.title))
            entries_by_tag.append((tag.name, tag.__str__(),
                                   articles.count(), articles_list))
        # Update context dictionary with the previous information and email
        # and google recaptcha settings.
        context = super(ContextMixin, self).get_context_data(**kwargs)
        app_context = {
            'email': settings.DEFAULT_FROM_EMAIL,
            'google_recaptcha_client_key':
                settings.GOOGLE_RECAPTCHA_CLIENT_KEY,
            'entries_by_time_line': entries_by_time_line,
            'entries_by_tag': entries_by_tag
        }
        context.update(app_context)
        return context
 def test_articles_per_month_and_year(self):
     """
     Tests the articles_per_month_and_year function.
     """
     self.assertEqual(Article.articles_per_month_and_year(2012, 10).count(),
                      0)
     Article.objects.create(published=datetime.datetime(
         2012, 11, 12, 0, 0, 0, 0, get_current_timezone()))
     self.assertEqual(Article.articles_per_month_and_year(2012, 10).count(),
                      0)
     article1 = Article.objects.create(published=datetime.datetime(
         2012, 10, 17, 0, 0, 0, 0, get_current_timezone()))
     self.assertEqual(Article.articles_per_month_and_year(2012, 10).count(),
                      1)
     self.assertTrue(article1 in Article.articles_per_month_and_year(2012,
                                                                     10))
     article2 = Article.objects.create(published=datetime.datetime(
         2012, 10, 3, 0, 0, 0, 0, get_current_timezone()))
     self.assertEqual(Article.articles_per_month_and_year(2012, 10).count(),
                      2)
     self.assertTrue(article2 in Article.articles_per_month_and_year(2012,
                                                                     10))