using GridFS with templates?

40 views
Skip to first unread message

aliane abdelouahab

unread,
Sep 23, 2012, 5:41:31 PM9/23/12
to Tornado Web Server
hi, i've a problem when making a loop using a GridFS pictures:

class VentesHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
user = self.get_secure_cookie("mechtari")
info = tornado.escape.json_decode(user)
email = info["personnel"]["email"]
produits = self.db.users.find({"personnel.email":email},
{"produit_up":1, "_id":0}).distinct("produit_up")
produit_pic_id =
self.db.users.find({"personnel.email":email}).distinct("produit_up.avatar.photo")
orientation =
self.db.users.find({"personnel.email":email}).distinct("produit_up.avatar.orientation")
renderer = self.fs
self.render("ventes.html", produits=produits,
produit_pic_id=produit_pic_id, orientation=orientation,
renderer=renderer)


{% for produit in produits %}
{% for id in produit_pic_id %}
<div class="produit">
{% import pymongo %}
{% if orientation=="portrait" %} <!-- dumb technic to avoid image
stretching ^_^ -->
<span><img src="/
{{renderer.get(pymongo.son_manipulator.ObjectId([id for id in
produit_pic_id])).filename}}" height="200px" class="imag">
{% else %}
<span><img src="/
{{renderer.get(pymongo.son_manipulator.ObjectId(id)).filename}}"
width="200px"class="imag">
{% end %}
</div>
</div>
{% end %}
{% end %}
{% end %}


i got the pictures repeated the time of uploaded products! so if i
uploaded 2 products (product_up), then i'll get 4 products with all
possibles switching of the product pictures!

by the way, note the hack to avoid (self not defined...)
and what about the import? do i make another variable x =
pymongo.son_manipulator to avoid the template to load the whole module
and uses a lot of memory?

A. Jesse Jiryu Davis

unread,
Sep 23, 2012, 7:20:56 PM9/23/12
to python-...@googlegroups.com
Your template is behaving the way I would expect it to, based on your code. You have nested for-loops. The outer for-loop repeats once per produit, and the inner for-loop repeats once per id in produit_pic_id. So you'll render len(produits) * len(produit_pic_id) images. Perhaps you want to delete {% for produit in produits %} ?

Also, note that you can do {% from bson import ObjectId %}, this is better style than importing it from pymongo.son_manipulator.

aliane abdelouahab

unread,
Sep 24, 2012, 5:12:23 AM9/24/12
to Tornado Web Server
thank you for the reply
but if i delete {% for produit in produits %} then i`ll not get
product information?

On 24 sep, 00:20, "A. Jesse Jiryu Davis" <ajesseda...@gmail.com>
wrote:
> Your template is behaving the way I would expect it to, based on your code.
> You have nested for-loops. The outer for-loop repeats once per produit, and
> the inner for-loop repeats once per id in produit_pic_id. So you'll render len(produits)
> * len(produit_pic_id) images. Perhaps you want to delete {% for produit in
> produits %} ?
>
> Also, note that you can do {% from bson import ObjectId %}, this is better
> style than importing it from pymongo.son_manipulator.
>
>
>
>
>
>
>
> On Sunday, September 23, 2012 5:41:34 PM UTC-4, aliane abdelouahab wrote:
>
> > hi, i've a problem when making a loop using a GridFS pictures:
>
> > class VentesHandler(BaseHandler):
> >     @tornado.web.authenticated
> >     def get(self):
> >         user = self.get_secure_cookie("mechtari")
> >         info = tornado.escape.json_decode(user)
> >         email = info["personnel"]["email"]
> >         produits = self.db.users.find({"personnel.email":email},
> > {"produit_up":1, "_id":0}).distinct("produit_up")
> >         produit_pic_id =
> > self.db.users.find({"personnel.email":email}).distinct("produit_up.avatar.p hoto")
>
> >         orientation =
> > self.db.users.find({"personnel.email":email}).distinct("produit_up.avatar.o rientation")

aliane abdelouahab

unread,
Sep 24, 2012, 5:19:57 AM9/24/12
to Tornado Web Server
SOLVED:

{% from bson import ObjectId %}
{% if orientation=="portrait" %} <!-- cette technique pour eviter les
images a y etre deformees -->
<span><img src="/{{renderer.get(ObjectId(produit["avatar"]
["photo"])).filename}}" height="200px" class="imag">
{% else %}
<span><img src="/{{renderer.get(ObjectId(produit["avatar"]
["photo"])).filename}}" width="200px"class="imag">
{% end %}

since i got the produit variable, then why not using it again ;)
thank you :D

aliane abdelouahab

unread,
Sep 24, 2012, 5:30:33 AM9/24/12
to Tornado Web Server
update:
even orientation is gone, now the code is:

class VentesHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
user = self.get_secure_cookie("mechtari")
info = tornado.escape.json_decode(user)
email = info["personnel"]["email"]
try:
produits = self.db.users.find({"personnel.email":email},
{"produit_up":1, "_id":0}).distinct("produit_up")
print produits
renderer = self.fs
except (errors.AutoReconnect, errors.ConnectionFailure):
self.redirect("/error")
self.render("ventes.html", produits=produits,
renderer=renderer)

the template:

{% for produit in produits %}
<div class="produit">
{% from bson import ObjectId %}
{% if produit["avatar"]["orientation"]=="portrait" %}
<span><img src="/{{renderer.get(ObjectId(produit["avatar"]
["photo"])).filename}}" height="300px" class="imag">
{% else %}
<span><img src="/{{renderer.get(ObjectId(produit["avatar"]
["photo"])).filename}}" width="300px"class="imag">
{% end %}
</div>
{% end %}
Reply all
Reply to author
Forward
0 new messages