Delete poll from list via AJAX request - only owner or superadmin . Fixme : This is forbidden when an atomic block is active error .

def delete_poll(request, pk):
    """
    Delete poll from list via AJAX request - only owner or superadmin.
    Fixme: "This is forbidden when an 'atomic' block is active" error.
    """
    try:
        poll = Poll.objects.get(pk=pk)
    except Poll.DoesNotExist as ex:
        resp = {'success': False, 'message': str(ex), 'level': 'danger', }
        return HttpResponse(json.dumps(resp))

    if request.user != poll.creator and not request.user.is_superuser:
        resp = {
            'success': False,
            'message': _('Permission required'),
            'level': 'danger',
        }
    else:
        try:
            with transaction.commit_on_success():
                poll.delete()
            resp = {
                'success': True,
                'message': _('Entry deleted'),
                'level': 'success',
            }
        except Exception as ex:
            resp = {'success': False, 'message': str(ex), 'level': 'danger', }
    return HttpResponse(json.dumps(resp))

def
delete_poll
(
request
,
pk
)
:
try
:
poll
=
Poll
.
objects
.
get
(
pk
=
pk
)
except
Poll
.
DoesNotExist
as
ex
:
resp
=
{
'success'
:
False
,
'message'
:
str
(
ex
)
,
'level'
:
'danger'
,
}
return
HttpResponse
(
json
.
dumps
(
resp
)
)
if
request
.
user
!=
poll
.
creator
and
not
request
.
user
.
is_superuser
:
resp
=
{
'success'
:
False
,
'message'
:
_
(
'Permission required'
)
,
'level'
:
'danger'
,
}
else
:
try
:
with
transaction
.
commit_on_success
(
)
:
poll
.
delete
(
)
resp
=
{
'success'
:
True
,
'message'
:
_
(
'Entry deleted'
)
,
'level'
:
'success'
,
}
except
Exception
as
ex
:
resp
=
{
'success'
:
False
,
'message'
:
str
(
ex
)
,
'level'
:
'danger'
,
}
return
HttpResponse
(
json
.
dumps
(
resp
)
)


def get(self, request):
        """ Show modal form. """
        return render(request, self.template_name,
                      {'title': _("Invite people")})

def
get
(
self
,
request
)
:
return
render
(
request
,
self
.
template_name
,
{
'title'
:
_
(
"Invite people"
)
}
)


def post(self, request):
        """ Send emails. """
        emails = request.POST.get('emails').split(',')
        user_url = request.build_absolute_uri(
            request.user.profile.get_absolute_url())
        user_profile_img = request.build_absolute_uri(
            request.user.profile.avatar.url)
        message = {
            'link': {
                'name': request.POST.get('name'),
                'href': request.POST.get('link'),
            },
            'user_link': user_url,
            'user_name': request.user.get_full_name(),
            'user_img': user_profile_img,
            'lang': translation.get_language_from_request(request),
        }
        for address in emails:
            address = address.strip()
            try:
                email = mails.InviteToContentMail()
                email.send(address, message)
            except:
                # FIXME: Not valid email. Should be cleaned up in form
                pass
        if request.is_ajax():
            context = {
                'success': True,
                'message': self.success_message,
                'level': 'success',
            }
            return HttpResponse(json.dumps(context),
                                content_type="application/json")
        messages.add_message(request, messages.SUCCESS, self.success_message)
        return redirect('/invite-friends/')