Correct a few issues with adding/updating API keys.

This commit is contained in:
2012-06-06 21:27:51 +01:00
parent f4e08ca8ad
commit 3b28d3a1a3
2 changed files with 26 additions and 11 deletions

View File

@@ -52,7 +52,7 @@ def queue_apikey_updates(update_delay=86400, batch_size=50):
@task(ignore_result=True)
def import_apikey(api_userid, api_key, user=None, force_cache=False, **kwargs):
def import_apikey(api_userid, api_key, user=None, force_cache=False, retry=True, **kwargs):
"""
Imports a EVE Account from the API, doesn't return a result
"""
@@ -61,11 +61,14 @@ def import_apikey(api_userid, api_key, user=None, force_cache=False, **kwargs):
import_apikey_func(api_userid, api_key, user, force_cache, log)
except (APIAccessException, DocumentRetrievalError), exc:
log.error('Error importing API Key - flagging for retry', exc_info=sys.exc_info(), extra={'data': {'api_userid': api_userid, 'api_key': api_key}})
import_apikey.retry(args=[api_userid, api_key, user, force_cache], exc=exc, kwargs=kwargs)
if retry:
import_apikey.retry(args=[api_userid, api_key, user, force_cache], exc=exc, kwargs=kwargs)
else:
raise
@task()
def import_apikey_result(api_userid, api_key, user=None, force_cache=False, callback=None, **kwargs):
def import_apikey_result(api_userid, api_key, user=None, force_cache=False, callback=None, retry=True, **kwargs):
"""
Imports a EVE Account from the API and returns the account object when completed
"""
@@ -75,7 +78,10 @@ def import_apikey_result(api_userid, api_key, user=None, force_cache=False, call
results = import_apikey_func(api_userid, api_key, user, force_cache, log)
except (APIAccessException, DocumentRetrievalError), exc:
log.error('Error importing API Key - flagging for retry', exc_info=sys.exc_info(), extra={'data': {'api_userid': api_userid, 'api_key': api_key}})
import_apikey_result.retry(args=[api_userid, api_key, user, force_cache, callback], exc=exc, kwargs=kwargs)
if retry:
import_apikey_result.retry(args=[api_userid, api_key, user, force_cache, callback], exc=exc, kwargs=kwargs)
else:
raise
else:
if callback:
subtask(callback).delay(account=results)

View File

@@ -24,13 +24,14 @@ from eve_api.views.mixins import DetailPaginationMixin
class EVEAPICreateView(CreateView):
"""Adds a EVE API key to the system"""
model = EVEAccount
form_class = EVEAPIForm
success_url = reverse_lazy('sso-profile')
def form_valid(self, form):
task = import_apikey_result.delay(api_key=form.cleaned_data['api_key'], api_userid=form.cleaned_data['api_user_id'], user=request.user.id)
task = import_apikey_result.delay(api_key=form.cleaned_data['api_key'], api_userid=form.cleaned_data['api_user_id'], user=request.user.id, retry=False)
try:
out = task.wait(10)
except celery.exceptions.TimeoutError:
@@ -52,6 +53,7 @@ class EVEAPICreateView(CreateView):
class EVEAPIUpdateView(UpdateView):
"""Updates a existing API key stored in the system"""
model = EVEAccount
form_class = EVEAPIForm
@@ -61,10 +63,12 @@ class EVEAPIUpdateView(UpdateView):
return {'user': self.request.user.pk}
def form_valid(self, form):
msg = None
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)
print "import"
task = import_apikey_result.delay(api_key=form.cleaned_data['api_key'], api_userid=form.cleaned_data['api_user_id'], user=form.cleaned_data['user'], retry=False)
try:
task.wait(30)
acc = 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:
@@ -72,13 +76,18 @@ class EVEAPIUpdateView(UpdateView):
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
#form.save()
if acc:
msg = "EVE API key %d successfully updated." % acc.api_user_id
else:
messages.error(self.request, "An error was encountered while trying to update your API key, Please check your key and try again.", fail_silently=True)
else:
if form.has_changed():
form.save()
msg = "EVE API key %d successfully updated." % acc.api_user_id
acc = form.save()
msg = "EVE API key %d successfully updated." % acc.api_user_id
messages.success(request, msg, fail_silently=True)
if msg:
messages.success(self.request, msg, fail_silently=True)
return HttpResponseRedirect(self.get_success_url())