Ejemplo n.º 1
0
def eveapi_add(request, post_save_redirect='/', template='eve_api/add.html'):
    """ Add a EVE API key to a user's account """

    if request.method == 'POST':
        form = EveAPIForm(request.POST)
        if form.is_valid():
            task = import_apikey_result.delay(api_key=form.cleaned_data['api_key'], api_userid=form.cleaned_data['api_user_id'], user=request.user.id)
            try:
                out = task.wait(10)
            except celery.exceptions.TimeoutError:
                msg = "The addition of your API key is still processing, please check back in a minute or so."
            except DocumentRetrievalError:
                msg = "An issue with the EVE API was encountered while adding your API, please try again later."
            except:
                msg = "An unknown error was encountered while trying to add your API key, please try again later."
            else:
                if out:
                    msg = "Key %d successfully added." % form.cleaned_data['api_user_id']
                else:
                    msg = "An issue was encountered why trying to import key %s, please recheck and try again." % form.cleaned_data['api_user_id']
            messages.success(request, msg, fail_silently=True)
            return redirect(post_save_redirect)
    else:
        form = EveAPIForm(initial={'user': request.user.id }) # An unbound form

    context = {
        'form': form,
    }
    return render_to_response(template, context, context_instance=RequestContext(request))
Ejemplo n.º 2
0
def eveapi_update(request, userid, post_save_redirect='/', template='eve_api/update.html'):
    """ Update a EVE API Key """

    acc = get_object_or_404(EVEAccount, pk=userid)
    if not acc.user == request.user and not request.user.is_staff:
        raise Http404    

    if request.method == 'POST':
        form = EveAPIForm(request.POST, instance=acc)
        if form.is_valid():
            if form.has_changed() and ('api_key' in form.changed_data):
                task = import_apikey_result.delay(api_key=acc.api_key, api_userid=acc.api_user_id, user=request.user.id)
                try:
                    task.wait(30)
                except celery.exceptions.TimeoutError:
                    msg = "The addition of your API key is still processing, please check back in a minute or so."
                except DocumentRetrievalError:
                    msg = "An issue with the EVE API was encountered while adding your API, please try again later."
                except:
                    msg = "An unknown error was encountered while trying to add your API key, please try again later."
                else:
                    msg = "EVE API key %d successfully updated." % acc.api_user_id
            else:
                if form.has_changed():
                    form.save()
                msg = "EVE API key %d successfully updated." % acc.api_user_id

            messages.success(request, msg, fail_silently=True)
            return redirect(post_save_redirect)
    else:
        form = EveAPIForm(instance=acc) # An unbound form

    context = {
        'acc': acc,
        'form': form,
    }
    return render_to_response(template, context, context_instance=RequestContext(request))