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/50b...