diff --git a/cmsplugin_randomquote/cms_plugins.py b/cmsplugin_randomquote/cms_plugins.py index e932e49..f7b69b8 100644 --- a/cmsplugin_randomquote/cms_plugins.py +++ b/cmsplugin_randomquote/cms_plugins.py @@ -2,20 +2,18 @@ from django.utils.translation import ugettext_lazy as _ from cms.plugin_base import CMSPluginBase from cms.plugin_pool import plugin_pool -from .models import Quote +from .models import Quote, RandomQuotePlugin class QuotePlugin(CMSPluginBase): """This plugin randomly renders a quote from the database.""" name = _('Quote') - render_template = 'cmsplugin_randomquote/quote.html' + render_template = 'cmsplugin_randomquote/randomquotes.html' + model = RandomQuotePlugin def render(self, context, instance, placeholder): try: - t_obj = Quote.objects.order_by('?')[0] - context['quote'] = t_obj.quote_text - context['author'] = t_obj.author - context['author_url'] = t_obj.author_url + context['quotes'] = Quote.objects.order_by('?')[:instance.amount] except IndexError: pass diff --git a/cmsplugin_randomquote/migrations/0002_randomquoteplugin.py b/cmsplugin_randomquote/migrations/0002_randomquoteplugin.py new file mode 100644 index 0000000..6b4c6e9 --- /dev/null +++ b/cmsplugin_randomquote/migrations/0002_randomquoteplugin.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('cms', '0011_auto_20150419_1006'), + ('cmsplugin_randomquote', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='RandomQuotePlugin', + fields=[ + ('cmsplugin_ptr', models.OneToOneField(primary_key=True, parent_link=True, to='cms.CMSPlugin', auto_created=True, serialize=False)), + ('amount', models.IntegerField(default=1, help_text='The number of random quotes to be displayed.')), + ], + options={ + 'abstract': False, + }, + bases=('cms.cmsplugin',), + ), + ] diff --git a/cmsplugin_randomquote/models.py b/cmsplugin_randomquote/models.py index bf02b40..5a1be2a 100644 --- a/cmsplugin_randomquote/models.py +++ b/cmsplugin_randomquote/models.py @@ -2,6 +2,7 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ from django.utils.encoding import python_2_unicode_compatible +from cms.models.pluginmodel import CMSPlugin @python_2_unicode_compatible class Quote(models.Model): @@ -11,3 +12,13 @@ class Quote(models.Model): def __str__(self): return '[%s] %s...' % (self.author, self.quote_text[:20]) + +@python_2_unicode_compatible +class RandomQuotePlugin(CMSPlugin): + amount = models.IntegerField( + default=1, + help_text=_('The number of random quotes to be displayed.') + ) + + def __str__(self): + return 'displaying %s' % (self.amount) \ No newline at end of file diff --git a/cmsplugin_randomquote/templates/cmsplugin_randomquote/quote.html b/cmsplugin_randomquote/templates/cmsplugin_randomquote/quote.html deleted file mode 100644 index 3c4ceee..0000000 --- a/cmsplugin_randomquote/templates/cmsplugin_randomquote/quote.html +++ /dev/null @@ -1,6 +0,0 @@ -
-
{{ quote }}
-

{% if author %}– - {% if author_url %}{{ author }}{% else %}{{ author }}{% endif %} - {% endif %}

-
diff --git a/cmsplugin_randomquote/templates/cmsplugin_randomquote/randomquotes.html b/cmsplugin_randomquote/templates/cmsplugin_randomquote/randomquotes.html new file mode 100644 index 0000000..ee04ea6 --- /dev/null +++ b/cmsplugin_randomquote/templates/cmsplugin_randomquote/randomquotes.html @@ -0,0 +1,8 @@ +{% for quote in quotes %} +
+
{{ quote.quote_text }}
+

{% if quote.author %}– + {% if quote.author_url %}{{ quote.author }}{% else %}{{ quote.author }}{% endif %} + {% endif %}

+
+{% endfor %} \ No newline at end of file