2

I have "document_delete" in my view, which calls "is_draft owner":

This is the "is_draft_owner":

def is_draft_owner(id = None, user = None):
    if id and user:
        return user.pk is Draft.objects.get(id = id).user_id
    else:
        return False

def document_delete(request):
    if is_draft_owner(request.POST['id'], request.user):
        draft = Draft.objects.get(id = request.POST['id'])
        draft.delete()
        return HttpResponse("done")

This is the view for the page:

@login_required
def posting_draft(request):
    user = request.user
    user_drafts = Draft.objects.filter(user = user) # Order by date last oppened
    drafts = dict()
    for d in user_drafts:
        drafts[d.title] = d.id
    alertnum = get_alertnum(user)
    return render_to_response('Posting/Pages/posting_draft.html', {'STATIC_URL':STATIC_URL, 'draft_l' : drafts, 'selected':"dr", 'alertnum': alertnum})

Somehow, when I don't call "is_draft_owner", everything works. When I do call it, I get this:

    [12/Aug/2012 00:53:45] "GET /posting/drafts HTTP/1.1" 200 2783
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 86, in run
    self.finish_response()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 127, in finish_response
    self.write(data)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 210, in write
    self.send_headers()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 268, in send_headers
    self.send_preamble()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 192, in send_preamble
    'Date: %s\r\n' % format_date_time(time.time())
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 324, in write
    self.flush()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 49948)
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 582, in process_request_thread
    self.finish_request(request, client_address)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 323, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/core/servers/basehttp.py", line 139, in __init__
    super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 640, in __init__
    self.finish()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 693, in finish
    self.wfile.flush()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------

and, Just in case, the javascript file that uses this code:

function delete_draft(id, name) {
    var text = 'Are you sure you want to delete "' + name + '"?';
    var confirm = noty({
        text: text,
        layout: 'center',
        type: 'confirm',
        modal: true,
        buttons: [

        {addClass: 'btn btn-danger', text: 'Cancel', onClick: function($noty) {
            $noty.close();
        }
        },
            {addClass: 'btn btn-primary', text: 'Delete', onClick: function($noty) {
                $.post('/ajax/drafts/delete', {id:id}, function(data) {
                    document.location.reload(true);
                });
                document.location.reload(true);
            }
            }
    ]});
}

I think, but don't know, that the page is calling the delete document fine, but then breaking on the reload. This seems to make sense from the logs when I'm using google chrome (because when I reload again, it's been deleted), but is not true for Firefox, it seems. I am running development server, but these problems persist and I can't put them off (Some people say this is a problem with the dev server, but I can't even develop when it's like this).

sinθ
  • 11,093
  • 25
  • 85
  • 121
  • I think it would help to be more concise with your code snippets. –  Aug 12 '12 at 13:32
  • Can you post the code for your view? – D.A Aug 12 '12 at 13:47
  • @D.A Sorry, I was unclear. The first function at the top is the view. The function under the error code is the function that causes the problem. – sinθ Aug 12 '12 at 13:50
  • @SeanKenny The first - third ones are all that I think are important. The code snippets are just the short functions. THe last one is javascript I posted just in case the problem was there. – sinθ Aug 12 '12 at 13:51
  • 2
    I am almost sure you mean `user.pk == Draft.objects.get(id = id).user_id` and not `is`. `is` is identity check, where you want equality check. – Burhan Khalid Aug 12 '12 at 14:07
  • Your traceback is of a different URL that what jquery is posting to. You can see the trackeback is after a `GET` request to `/posting/drafts`, but your javascript is posting to `/ajax/drafts/delete` – Burhan Khalid Aug 12 '12 at 14:10
  • @MikeG you said you think the error is occurring on the page reload. So it would be helpful to see the view that is rendering the page. – D.A Aug 12 '12 at 14:14
  • @D.A that view is now posted. Sorry. – sinθ Aug 12 '12 at 15:33
  • @BurhanKhalid It gets the number of alerts in the database. I don't think that's it's causing the problem. – sinθ Aug 12 '12 at 17:29

1 Answers1

1

I will post some comments on your code; but I think you should revise the question with the exact errors being sent; as I mentioned in the comments.

I'll edit your code - hopefully the comments will suffice:

# Here you mean to check for equality, `is` checks for identity
# Also, you want the return of the comparison; so you don't need
# an else.

def is_draft_owner(id = None, user = None):
    if id and user:
        return user.pk == Draft.objects.get(id = id).user_id

# You are POST-ing to this view, but
# you need to exempt it from csrf checks
# or edit your javascript to make sure it
# sends the csrf cookie.
def document_delete(request):
    if request.method == 'POST':
        if is_draft_owner(request.POST['id'], request.user):
            # Get raises an exception that you are not catching:
            try:
                 draft = Draft.objects.get(id = request.POST['id'])
            except Draft.DoesNotExist:
                 # Do something

            # OR you can do this
            draft = get_object_or_404(Draft,id=request.POST['id'])
            draft.delete()
            return HttpResponse("done")
    else:
         return HttpResponse('Not Allowed')
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • What do you mean exact errors. All there is is the consul output that I included with the post. There is no django errors page. – sinθ Aug 12 '12 at 15:35