mirror of
https://github.com/nikdoof/test-auth.git
synced 2025-12-14 14:52:15 +00:00
Reworking of API key importing, allow keys to be updated
This commit is contained in:
@@ -4,12 +4,13 @@ from django import forms
|
||||
from eve_api.models import EVEAccount, EVEPlayerCharacter, EVEPlayerCorporation
|
||||
|
||||
|
||||
class EveAPIForm(forms.Form):
|
||||
class EveAPIForm(forms.ModelForm):
|
||||
""" EVE API input form """
|
||||
|
||||
user_id = forms.IntegerField(label=u'User ID')
|
||||
api_key = forms.CharField(label=u'API Key', max_length=64)
|
||||
description = forms.CharField(max_length=100, required=False)
|
||||
class Meta:
|
||||
model = EVEAccount
|
||||
fields = ('api_user_id', 'api_key', 'description', 'user')
|
||||
widgets = {'user': forms.HiddenInput()}
|
||||
|
||||
def clean_api_key(self):
|
||||
|
||||
@@ -31,6 +32,7 @@ class EveAPIForm(forms.Form):
|
||||
except ValueError:
|
||||
raise forms.ValidationError("Please provide a valid user ID.")
|
||||
|
||||
if not self.update:
|
||||
try:
|
||||
eaccount = EVEAccount.objects.get(api_user_id=self.cleaned_data['user_id'])
|
||||
except EVEAccount.DoesNotExist:
|
||||
|
||||
@@ -12,8 +12,10 @@ class EVEAccount(EVEAPIModel):
|
||||
help_text="User that owns this account")
|
||||
description = models.CharField(max_length=50, blank=True,
|
||||
help_text="User-provided description.")
|
||||
api_key = models.CharField(max_length=64, verbose_name="API Key")
|
||||
api_user_id = models.IntegerField(verbose_name="API User ID")
|
||||
api_key = models.CharField(max_length=64, verbose_name="API Key",
|
||||
help_text="EVE API Key")
|
||||
api_user_id = models.IntegerField(verbose_name="API User ID",
|
||||
help_text="EVE API User ID")
|
||||
characters = models.ManyToManyField('eve_api.EVEPlayerCharacter', blank=True, null=True)
|
||||
api_status = models.IntegerField(choices=API_STATUS_CHOICES,
|
||||
default=API_STATUS_PENDING,
|
||||
|
||||
@@ -102,7 +102,8 @@ def import_apikey_func(api_userid, api_key, user=None, force_cache=False, log=lo
|
||||
|
||||
# Create or retrieve the account last to make sure everything
|
||||
# before here is good to go.
|
||||
account, created = EVEAccount.objects.get_or_create(id=api_userid, api_user_id=api_userid, api_key=api_key)
|
||||
account, created = EVEAccount.objects.get_or_create(id=api_userid, api_user_id=api_userid)
|
||||
account.api_key = api_key
|
||||
account.api_status = API_STATUS_OK
|
||||
if user and created:
|
||||
account.user = User.objects.get(id=user)
|
||||
|
||||
19
app/eve_api/templates/eve_api/update.html
Normal file
19
app/eve_api/templates/eve_api/update.html
Normal file
@@ -0,0 +1,19 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}Update EVE API Key{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<p>Please update your API key as provided on the <a
|
||||
href="http://eve-online.com/api">EVE Online API
|
||||
page</a> and a optional description.</p>
|
||||
|
||||
<form action="{% url eve_api.views.eveapi_update acc.api_user_id %}" method="post">
|
||||
<table>
|
||||
{{ form.as_table }}
|
||||
</table>
|
||||
<br />
|
||||
{% csrf_token %}
|
||||
<input type="submit" value="Add Key" />
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
||||
@@ -5,6 +5,7 @@ from eve_api import views
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^eveapi/add/', views.eveapi_add, name="eveapi-add"),
|
||||
url(r'^eveapi/update/(?P<userid>\d+)/$', views.eveapi_update, name="eveapi-update"),
|
||||
url(r'^eveapi/delete/(?P<userid>\d+)/$', views.eveapi_del, name="eveapi-delete"),
|
||||
url(r'^eveapi/refresh/(?P<userid>\d+)/$', views.eveapi_refresh, name="eveapi-refresh"),
|
||||
url(r'^eveapi/log/(?P<userid>\d+)/$', views.eveapi_log, name="eveapi-log"),
|
||||
|
||||
@@ -24,7 +24,9 @@ def eveapi_add(request, post_save_redirect='/'):
|
||||
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['user_id'], user=request.user.id)
|
||||
acc = form.save()
|
||||
print acc
|
||||
task = import_apikey_result.delay(api_key=acc.api_key, api_userid=acc.api_user_id, user=request.user.id)
|
||||
try:
|
||||
task.wait(10)
|
||||
except celery.exceptions.TimeoutError:
|
||||
@@ -34,15 +36,53 @@ def eveapi_add(request, post_save_redirect='/'):
|
||||
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 added." % form.cleaned_data['user_id']
|
||||
msg = "EVE API key %d successfully added." % form.cleaned_data['api_user_id']
|
||||
messages.success(request, msg, fail_silently=True)
|
||||
return redirect(post_save_redirect)
|
||||
else:
|
||||
form = EveAPIForm() # An unbound form
|
||||
form = EveAPIForm(initial={'user': request.user.id }) # An unbound form
|
||||
|
||||
return render_to_response('eve_api/add.html', locals(), context_instance=RequestContext(request))
|
||||
|
||||
|
||||
@login_required
|
||||
def eveapi_update(request, userid, post_save_redirect='/'):
|
||||
""" Update a EVE API Key """
|
||||
|
||||
acc = get_object_or_404(EVEAccount, id=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 or 'api_user_id' in form.changed_data):
|
||||
acc = form.save()
|
||||
task = import_apikey_result.delay(api_key=acc.api_key, api_userid=acc.api_user_id, user=request.user.id)
|
||||
try:
|
||||
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:
|
||||
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
|
||||
|
||||
return render_to_response('eve_api/update.html', locals(), context_instance=RequestContext(request))
|
||||
|
||||
|
||||
@login_required
|
||||
def eveapi_del(request, userid, post_save_redirect='/'):
|
||||
""" Delete a EVE API key from a account """
|
||||
|
||||
Reference in New Issue
Block a user