Re: Template tag testing
On Jun 23, 12:00 pm, Michael <newmani...@gmail.com> wrote: > On Tue, Jun 23, 2009 at 11:53 AM, brianmac44 <brian@gmail.com> wrote: > I don't know if this is best practice, but here is the way I test my > template tags essentially by rendering the templates: > > http://bitbucket.org/newmaniese/newmanutils/src/tip/newmanutils/tests.py Thanks Michael. That was a good start for me. My tag needed some context from the testing client. http://dpaste.com/58833/ -Brian --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Template tag testing
I tried to test template tags with doctests and unittests but I'm not sure what token and/or context to use. What's the best practice for testing template tags? Thanks, Brian --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: S3Storage.py and Thumbnails using PIL (IOError / cannot identify image file)
With my code, what are you using as "content"? I'm using: form.cleaned_data['source'].read() So my code looks something like this: PHOTO_MEDIUM_SIZE = 400,400 source_image = form.cleaned_data['source'].read() resized_image = resize_photo(source_image,PHOTO_MEDIUM_SIZE)) photos.save(image_name,resized_image) You may want to also want to check the image file you are using for testing, interlaced PNG's are not supported by PIL. http://www.pythonware.com/library/pil/handbook/format-png.htm -Brian On Dec 22, 2:46 pm, Merrick <merr...@gmail.com> wrote: > I got the same result with brianmac44's code. I also verified that the > my resize_image() works when opening the file from the local > filesystem - so I am somehow not passing the file in a manner that > Image.open likes. > > On Dec 22, 4:45 am, brianmac44 <anon1...@gmail.com> wrote: > > > I had the same problem two weeks ago. This is what I wrote: > > > def resize_photo(self,content,size): > > img = Image.open(ContentFile(content)) > > img.thumbnail(size, Image.ANTIALIAS) > > io = StringIO.StringIO() > > img.save(io, 'PNG') > > return ContentFile(io.getvalue()) > > > Hope this helps. > > > -Brian > > > On Dec 22, 4:41 am, Merrick <merr...@gmail.com> wrote: > > > > Thank you I tried that and I still get the same error. > > > > I spent a little more time looking at PIL / Image.py and cleaning up > > > the code. From what I can tell the Image.open method is having trouble > > > with what I am passing to it. > > > > def resize_image(file, size=(50, 50)): > > > from PIL import Image > > > from cStringIO import StringIO > > > from django.core.files.base import ContentFile > > > > image_data = StringIO(file.read()) > > > > ### this line below is where the issue is ### > > > image = Image.open(image_data) > > > > if image.mode not in ('L', 'RGB'): > > > image = image.convert('RGB') > > > image.thumbnail(size, Image.ANTIALIAS) > > > o = StringIO() > > > image.save(o, "JPEG") > > > return ContentFile(o.getvalue()) > > > > This is how I call it: > > > picture = pform.cleaned_data['picture'] > > > thumbnail_content = resize_image(picture) > > > > Thank you for looking at this. > > > > On Dec 21, 3:08 pm, "j...@zigzap.com" <jlb2...@gmail.com> wrote: > > > > > From what I can tell your not wrapping the thumbnailfilein > > > > ContentFile your just returning the rawfilefrom the string IO. To > > > > use the the ImageField django provides you must provide it with afile > > > > that is in a special wrapper called ContentFile. I would suggest > > > > trying this: > > > > > from django.core.files.base import ContentFile (import this function > > > > at the top of your view or where ever you put def resize_image) > > > > > change the last line of def resize_image from "return o.getvalue()" to > > > > "return ContentFile(o.getvalue())" > > > > > I would also recommend changing "new_profile.picture.save(filename, > > > > thumbnail_content)" to "new_profile.picture.save(filename, > > > > thumbnail_content, save=False)" so that you are not hitting the > > > > database twice. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: S3Storage.py and Thumbnails using PIL (IOError / cannot identify image file)
I had the same problem two weeks ago. This is what I wrote: def resize_photo(self,content,size): img = Image.open(ContentFile(content)) img.thumbnail(size, Image.ANTIALIAS) io = StringIO.StringIO() img.save(io, 'PNG') return ContentFile(io.getvalue()) Hope this helps. -Brian On Dec 22, 4:41 am, Merrickwrote: > Thank you I tried that and I still get the same error. > > I spent a little more time looking at PIL / Image.py and cleaning up > the code. From what I can tell the Image.open method is having trouble > with what I am passing to it. > > def resize_image(file, size=(50, 50)): > from PIL import Image > from cStringIO import StringIO > from django.core.files.base import ContentFile > > image_data = StringIO(file.read()) > > ### this line below is where the issue is ### > image = Image.open(image_data) > > if image.mode not in ('L', 'RGB'): > image = image.convert('RGB') > image.thumbnail(size, Image.ANTIALIAS) > o = StringIO() > image.save(o, "JPEG") > return ContentFile(o.getvalue()) > > This is how I call it: > picture = pform.cleaned_data['picture'] > thumbnail_content = resize_image(picture) > > Thank you for looking at this. > > On Dec 21, 3:08 pm, "j...@zigzap.com" wrote: > > > From what I can tell your not wrapping the thumbnailfilein > > ContentFile your just returning the rawfilefrom the string IO. To > > use the the ImageField django provides you must provide it with afile > > that is in a special wrapper called ContentFile. I would suggest > > trying this: > > > from django.core.files.base import ContentFile (import this function > > at the top of your view or where ever you put def resize_image) > > > change the last line of def resize_image from "return o.getvalue()" to > > "return ContentFile(o.getvalue())" > > > I would also recommend changing "new_profile.picture.save(filename, > > thumbnail_content)" to "new_profile.picture.save(filename, > > thumbnail_content, save=False)" so that you are not hitting the > > database twice. > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Nested for loops in template
That worked. I just updated my entries model to include comments = generic.GenericRelation(FreeThreadedComment) Thanks. -Brian On Jul 18, 12:30 pm, "Scott Moonen" <[EMAIL PROTECTED]> wrote: > Brian, I think the solution is to add a GenericRelation to your entries > model; > seehttp://www.djangoproject.com/documentation/contenttypes/#reverse-gene... > an explanation. I think that after doing this you'll be able to > access > p.comments.count (assuming that the generic relation is named "comments"). > I haven't used the contenttypes framework yet, but from the looks of it I > believe that will work. > > -- Scott > > > > On Fri, Jul 18, 2008 at 12:21 PM, brianmac44 <[EMAIL PROTECTED]> wrote: > > > Hi Scott, > > Thanks for your help, but I'm still having trouble. > > I using django-threadedcomments > >http://code.google.com/p/django-threadedcomments/ > > so its a little confusing. > > > Here are my models > >http://dpaste.com/65848/ > > > -Brian > > > On Jul 18, 12:01 pm, "Scott Moonen" <[EMAIL PROTECTED]> wrote: > > > Brian, without knowing exactly how your models are configured, you can do > > > something like this: > > > > {{ p.comment_set.count }} > > > > If you use it in more than one place (e.g., as {% if p.comment_set.count > > %} > > > ... {{ p.comment_set.count }} ...), you will want to optimize things so > > that > > > the SQL "COUNT()" query is executed only once: > > > > {% with p.comment_set.count as comment_count %} > > > {% if comment_count %} . . . {{ comment_count }} . . . {% endif %} > > > {% endwith %} > > > > If you have any trouble getting this working, please let us have a look > > at > > > your entry and comment models (use dpaste.com) and we can help you > > figure > > > out exactly what to write. > > > > -- Scott > > > > On Fri, Jul 18, 2008 at 11:47 AM, brianmac44 <[EMAIL PROTECTED]> wrote: > > > > > I have two tables, one is entries and the other is comments. > > > > > In the template I would like display how many comments for each entry > > > > and some other entry data. > > > > So far I have... > > > > > view: > > > > entries_list = entries.objects.filter( ...) > > > > > template: > > > > {% for p in object_list %} > > > > > > > > > > > > {{ p.name }} > > > > > > > > > > > > ... > > > > > > > > > > > > {% endfor %} > > > > > How is this done? Should I use select_related in the view? > > > > Thanks in advance. > > > > --http://scott.andstuff.org/|http://truthadorned.org/<http://scott.andstuff.org/%7Chttp://truthadorned.org/> > > --http://scott.andstuff.org/|http://truthadorned.org/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Nested for loops in template
Hi Scott, Thanks for your help, but I'm still having trouble. I using django-threadedcomments http://code.google.com/p/django-threadedcomments/ so its a little confusing. Here are my models http://dpaste.com/65848/ -Brian On Jul 18, 12:01 pm, "Scott Moonen" <[EMAIL PROTECTED]> wrote: > Brian, without knowing exactly how your models are configured, you can do > something like this: > > {{ p.comment_set.count }} > > If you use it in more than one place (e.g., as {% if p.comment_set.count %} > ... {{ p.comment_set.count }} ...), you will want to optimize things so that > the SQL "COUNT()" query is executed only once: > > {% with p.comment_set.count as comment_count %} > {% if comment_count %} . . . {{ comment_count }} . . . {% endif %} > {% endwith %} > > If you have any trouble getting this working, please let us have a look at > your entry and comment models (use dpaste.com) and we can help you figure > out exactly what to write. > > -- Scott > > > > On Fri, Jul 18, 2008 at 11:47 AM, brianmac44 <[EMAIL PROTECTED]> wrote: > > > I have two tables, one is entries and the other is comments. > > > In the template I would like display how many comments for each entry > > and some other entry data. > > So far I have... > > > view: > > entries_list = entries.objects.filter( ...) > > > template: > > {% for p in object_list %} > > > > > > {{ p.name }} > > > > > > ... > > > > > > {% endfor %} > > > How is this done? Should I use select_related in the view? > > Thanks in advance. > > --http://scott.andstuff.org/|http://truthadorned.org/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Nested for loops in template
I have two tables, one is entries and the other is comments. In the template I would like display how many comments for each entry and some other entry data. So far I have... view: entries_list = entries.objects.filter( ...) template: {% for p in object_list %} {{ p.name }} ... {% endfor %} How is this done? Should I use select_related in the view? Thanks in advance. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---