pymongo works as stack in tornado?

50 views
Skip to first unread message

aliane abdelouahab

unread,
Sep 26, 2012, 4:43:35 AM9/26/12
to Tornado Web Server
hi, it seems that the bug i got when implementing a deletion from
GridFS is because of a king of stack: an item cant be removed from
anywhere, but only the latest one added! because when iterating in the
template, it cant get the one that is in the middle on in the last
after refreshing.

here is the code:

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")
renderer = self.fs
except (errors.AutoReconnect, errors.ConnectionFailure):
self.redirect("/error")
try:
self.render("ventes.html", produits=produits,
renderer=renderer)
except gridfs.errors.NoFile, e:
self.write(str(e))

class Supprimer(BaseHandler):
@tornado.web.authenticated
def post(self):
debut = time.clock()
prod = self.get_arguments("supprime")
produ = prod[0][:]
idd = self.db.users.find_one({"produit_up.spec.id":produ})
["produit_up"][0["avatar"]["photo"]
try:
self.db.users.update({"produit_up.spec.id":produ},
{"$pull":{"produit_up"{"spec.id":produ}}})
self.fs.delete(ObjectId('{0}'.format(idd)))
except (errors.AutoReconnect, errors.ConnectionFailure):
self.redirect("/error")
self.redirect("/ventes") # this will redirect to the first code.

and the template:

{% for produit in produits %}
<div class="produit">
<span class="nom">Le nom du produit: {{produit["spec"]["namep"]}}</
span>
{% from bson import ObjectId %}
{% if produit["avatar"]["orientation"]=="portrait" %}
<span><img src="/{{renderer.get(ObjectId(produit["avatar"]
["photo"])).filename}}"height="250px" class="imag">
{% else %}
<span><img src="/{{renderer.get(ObjectId(produit["avatar"]
["photo"])).filename}}"width="250px"class="imag">
{% end %}

<div class="supprimer" >
<form name="supprimer" class="supprimer" method="post" action="/
supprimer">
{% raw xsrf_form_html() %}
<input type="hidden" value="{{produit["spec"]["id"]}}"
name="supprime"/

<input type="submit" value="SUPPRIMER!"/>
</form>
</div>

so here is how it works:
i delete an element,
self.db.users.update({"produit_up.spec.id":produ}, {"$pull":
{"produit_up:{"spec.id":produ}}})
and then go to to gridFS to delete the picture of that element:
self.fs.delete(ObjectId('{0}'.format(idd)))
and then i'll redirect the user to the page where he can find the
product that are still there.
and if i've two products it's okey, since the result will be only one
product staying, but if i've more than 3, then, if i dont delete the
first one (the last one added) then i got an error from pymongo
complaining of missing the _id of the porject deleted, it's like the -
id is still rendered!
here is the link to mongodb groups;
https://groups.google.com/group/mongodb-user/browse_thread/thread/50bc50dd8e3b55ab?hl=fr#

aliane abdelouahab

unread,
Sep 30, 2012, 8:45:14 AM9/30/12
to Tornado Web Server
i still dont get the solution, i know that i dide something bad,
because the returned ObjectID dont match to the deleted element, but
why!
here the code formatted on stackoverflow
http://stackoverflow.com/questions/12653548/gridfs-elements-dont-follow-document-elements

aliane abdelouahab

unread,
Sep 30, 2012, 10:57:00 AM9/30/12
to Tornado Web Server
am sorry, i've solved it.
it seems that the problem came from using the list index, i've fixed
it to the first one, this is why i dident get the error when deleting
the first element.
http://stackoverflow.com/questions/12653548/gridfs-elements-dont-follow-document-elements
> here the code formatted on stackoverflowhttp://stackoverflow.com/questions/12653548/gridfs-elements-dont-foll...
Reply all
Reply to author
Forward
0 new messages