Created
January 17, 2013 20:45
-
-
Save Ryochan7/4559615 to your computer and use it in GitHub Desktop.
Mezzyblocks patch to fix problems with empty queries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/blocktypes/random_image.py b/blocktypes/random_image.py | |
index 8234f50..9a8be3a 100644 | |
--- a/blocktypes/random_image.py | |
+++ b/blocktypes/random_image.py | |
@@ -2,6 +2,7 @@ from random import choice | |
from django.template import Context, Template | |
from django.template.loader import get_template, render_to_string | |
+from django.core.exceptions import ObjectDoesNotExist | |
from mezzanine.conf import settings | |
from mezzanine.galleries.models import Gallery | |
@@ -21,28 +22,38 @@ block_settings = ( | |
def getSetting( block, setting, default=None ): | |
"""Get a settings value configured for a block, or it's default.""" | |
- value = BlockConfig.objects.get( | |
- block_id = block.id, | |
- setting = BlockTypeSetting.objects.get( | |
- blocktype = block.blocktype_id, | |
- setting_name = setting ) | |
- ).value | |
- | |
- if not value: value = default | |
+ value = default | |
+ try: | |
+ value = BlockConfig.objects.get( | |
+ block_id = block.id, | |
+ setting = BlockTypeSetting.objects.get( | |
+ blocktype = block.blocktype_id, | |
+ setting_name = setting ) | |
+ ).value | |
+ except ObjectDoesNotExist: | |
+ pass | |
+ | |
return value | |
def block_context_processor(block): | |
- | |
gallery = getSetting( block, "gallery", block_settings[0][2] ) | |
- folder = Gallery.objects.get(title=gallery) | |
- files = folder.images.all() | |
- rand_file = choice(files) | |
+ try: | |
+ folder = Gallery.objects.get(title=gallery) | |
+ except Gallery.DoesNotExist: | |
+ folder = None | |
+ | |
+ title = "" | |
+ image = None | |
+ if folder: | |
+ files = folder.images.all() | |
+ rand_file = choice(files) | |
- title = rand_file.description | |
- image = settings.MEDIA_URL+str(rand_file.file) | |
- image = image[1:] | |
+ if rand_file: | |
+ title = rand_file.description | |
+ image = settings.MEDIA_URL+str(rand_file.file) | |
+ image = image[1:] | |
#Fill this context with any KEY, VALUE pairs | |
#to pass them to the template | |
diff --git a/templates/random_image.html b/templates/random_image.html | |
index 0fcf609..45efee8 100644 | |
--- a/templates/random_image.html | |
+++ b/templates/random_image.html | |
@@ -1,5 +1,5 @@ | |
<h1>{{title}}</h1> | |
{% if image %} | |
- <img href="/{{image}}" style="width:228px; height:228px;"/> | |
+ <img src="/{{image}}" style="width:229px; height:228px;" /> | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment