mirror of
https://github.com/nikdoof/test-auth.git
synced 2025-12-14 14:52:15 +00:00
Finish moving the eve_api functions out to the eve_api app
This commit is contained in:
33
eve_api/forms.py
Normal file
33
eve_api/forms.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
from django import forms
|
||||||
|
from eve_api.models import EVEAccount, EVEPlayerCharacter, EVEPlayerCorporation
|
||||||
|
|
||||||
|
|
||||||
|
class EveAPIForm(forms.Form):
|
||||||
|
""" 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)
|
||||||
|
|
||||||
|
def clean_api_key(self):
|
||||||
|
|
||||||
|
if not len(self.cleaned_data['api_key']) == 64:
|
||||||
|
raise forms.ValidationError("Provided API Key is not 64 characters long.")
|
||||||
|
|
||||||
|
if re.search(r'[^\.a-zA-Z0-9]', self.cleaned_data['api_key']):
|
||||||
|
raise forms.ValidationError("Provided API Key has invalid characters.")
|
||||||
|
|
||||||
|
def clean_user_id(self):
|
||||||
|
|
||||||
|
if not 'user_id' in self.cleaned_data or self.cleaned_data['user_id'] == '':
|
||||||
|
raise forms.ValidationError("Please provide a valid User ID")
|
||||||
|
|
||||||
|
try:
|
||||||
|
eaccount = EVEAccount.objects.get(api_user_id=self.cleaned_data['user_id'])
|
||||||
|
except EVEAccount.DoesNotExist:
|
||||||
|
return self.cleaned_data
|
||||||
|
else:
|
||||||
|
raise forms.ValidationError("This API User ID is already registered")
|
||||||
|
|
||||||
11
eve_api/urls.py
Normal file
11
eve_api/urls.py
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
from django.conf.urls.defaults import *
|
||||||
|
from django.core.urlresolvers import reverse
|
||||||
|
|
||||||
|
from eve_api import views
|
||||||
|
|
||||||
|
urlpatterns = patterns('',
|
||||||
|
url(r'^eveapi/add/', views.eveapi_add, name="eveapi-add"),
|
||||||
|
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"),
|
||||||
|
)
|
||||||
92
eve_api/views.py
Normal file
92
eve_api/views.py
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
import celery
|
||||||
|
|
||||||
|
from django.shortcuts import render_to_response, get_object_or_404, redirect
|
||||||
|
from django.core.urlresolvers import reverse
|
||||||
|
from django.contrib import messages
|
||||||
|
from django.contrib.auth.decorators import login_required
|
||||||
|
from django.template import RequestContext
|
||||||
|
from django.http import Http404
|
||||||
|
|
||||||
|
from eve_api.forms import EveAPIForm
|
||||||
|
from eve_api.models import EVEAccount, EVEPlayerCharacter
|
||||||
|
from eve_api.tasks import import_apikey_result
|
||||||
|
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
def eveapi_add(request, post_save_redirect='/'):
|
||||||
|
""" 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['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."
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
msg = "EVE API key %d successfully added." % form.cleaned_data['user_id']
|
||||||
|
messages.success(request, msg, fail_silently=True)
|
||||||
|
return redirect(post_save_redirect)
|
||||||
|
else:
|
||||||
|
form = EveAPIForm() # An unbound form
|
||||||
|
|
||||||
|
return render_to_response('eve_api/add.html', locals(), context_instance=RequestContext(request))
|
||||||
|
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
def eveapi_del(request, userid, post_save_redirect='/'):
|
||||||
|
""" Delete a EVE API key from a account """
|
||||||
|
|
||||||
|
try:
|
||||||
|
acc = EVEAccount.objects.get(id=userid)
|
||||||
|
except EVEAccount.DoesNotExist:
|
||||||
|
return redirect(post_save_redirect)
|
||||||
|
if acc.user == request.user:
|
||||||
|
acc.delete()
|
||||||
|
messages.success(request, "EVE API key successfully deleted.", fail_silently=True)
|
||||||
|
|
||||||
|
return redirect(post_save_redirect)
|
||||||
|
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
def eveapi_refresh(request, userid, post_save_redirect='/'):
|
||||||
|
""" Force refresh a EVE API key """
|
||||||
|
|
||||||
|
if userid > 0:
|
||||||
|
try:
|
||||||
|
acc = EVEAccount.objects.get(id=userid)
|
||||||
|
except EVEAccount.DoesNotExist:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
if acc.user == request.user or request.user.is_superuser:
|
||||||
|
task = import_apikey_result.delay(api_key=acc.api_key, api_userid=acc.api_user_id, force_cache=True, user=request.user.id)
|
||||||
|
|
||||||
|
if request.is_ajax():
|
||||||
|
try:
|
||||||
|
acc = task.wait(30)
|
||||||
|
except celery.exceptions.TimeoutError:
|
||||||
|
acc = EVEAccount.objects.get(id=userid)
|
||||||
|
return HttpResponse(serializers.serialize('json', [acc]), mimetype='application/javascript')
|
||||||
|
else:
|
||||||
|
messages.add_message(request, messages.INFO, "Key %s has been queued to be refreshed from the API" % acc.api_user_id)
|
||||||
|
|
||||||
|
return redirect(post_save_redirect)
|
||||||
|
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
def eveproxy_log(request, userid):
|
||||||
|
""" Provides a list of access logs for a specific EVE API key """
|
||||||
|
try:
|
||||||
|
acc = EVEAccount.objects.get(id=userid)
|
||||||
|
except EVEAccount.DoesNotExist:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
if acc and (acc.user == request.user or request.user.is_staff):
|
||||||
|
logs = ApiAccessLog.objects.filter(userid=userid).order_by('-time_access')[:50]
|
||||||
|
return render_to_response('eve_api/log.html', locals(), context_instance=RequestContext(request))
|
||||||
|
|
||||||
|
raise Http404
|
||||||
|
|
||||||
@@ -6,16 +6,11 @@ from sso import views
|
|||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
('^$', views.profile),
|
('^$', views.profile),
|
||||||
(r'^profile/$', views.profile),
|
(r'^profile/$', views.profile),
|
||||||
(r'^profile/add/eveapi', views.eveapi_add),
|
|
||||||
(r'^profile/del/eveapi/$', views.eveapi_del),
|
|
||||||
(r'^profile/del/eveapi/(?P<userid>\d+)/$', views.eveapi_del),
|
|
||||||
(r'^profile/add/service', views.service_add),
|
(r'^profile/add/service', views.service_add),
|
||||||
(r'^profile/del/service/$', views.service_del),
|
(r'^profile/del/service/$', views.service_del),
|
||||||
(r'^profile/del/service/(?P<serviceid>\d+)/$', views.service_del),
|
(r'^profile/del/service/(?P<serviceid>\d+)/$', views.service_del),
|
||||||
(r'^profile/reset/service/(?P<serviceid>\d+)/$', views.service_reset),
|
(r'^profile/reset/service/(?P<serviceid>\d+)/$', views.service_reset),
|
||||||
(r'^profile/reset/service/(?P<serviceid>\d+)/(?P<accept>\d+)$', views.service_reset),
|
(r'^profile/reset/service/(?P<serviceid>\d+)/(?P<accept>\d+)$', views.service_reset),
|
||||||
(r'^profile/refresh/eveapi/(?P<userid>\d+)/$', views.eveapi_refresh),
|
|
||||||
(r'^profile/log/eveapi/(?P<userid>\d+)/$', views.eveapi_log),
|
|
||||||
(r'^profile/characters$', views.characters),
|
(r'^profile/characters$', views.characters),
|
||||||
(r'^profile/characters/(?P<charid>.*)/$', views.characters),
|
(r'^profile/characters/(?P<charid>.*)/$', views.characters),
|
||||||
(r'^profile/apipassword/', views.set_apipasswd),
|
(r'^profile/apipassword/', views.set_apipasswd),
|
||||||
|
|||||||
83
sso/views.py
83
sso/views.py
@@ -22,7 +22,7 @@ from eve_api.tasks import import_apikey, import_apikey_result
|
|||||||
from eve_proxy.models import ApiAccessLog
|
from eve_proxy.models import ApiAccessLog
|
||||||
|
|
||||||
from sso.models import ServiceAccount, Service, SSOUser, ExistingUser, ServiceError
|
from sso.models import ServiceAccount, Service, SSOUser, ExistingUser, ServiceError
|
||||||
from sso.forms import EveAPIForm, UserServiceAccountForm, ServiceAccountResetForm, UserLookupForm, APIPasswordForm
|
from sso.forms import UserServiceAccountForm, ServiceAccountResetForm, UserLookupForm, APIPasswordForm
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def profile(request):
|
def profile(request):
|
||||||
@@ -51,87 +51,6 @@ def characters(request, charid=0):
|
|||||||
return render_to_response('sso/characterlist.html', locals(), context_instance=RequestContext(request))
|
return render_to_response('sso/characterlist.html', locals(), context_instance=RequestContext(request))
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
|
||||||
def eveapi_add(request):
|
|
||||||
""" 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.delay(api_key=form.cleaned_data['api_key'], api_userid=form.cleaned_data['user_id'], user=request.user.id)
|
|
||||||
try:
|
|
||||||
task.wait(5)
|
|
||||||
except celery.exceptions.TimeoutError:
|
|
||||||
messages.add_message(request, messages.INFO, "The addition of your API key is still processing, please check back in a minute or so")
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
messages.add_message(request, messages.INFO, "EVE API successfully added.")
|
|
||||||
|
|
||||||
return redirect('sso.views.profile')
|
|
||||||
else:
|
|
||||||
form = EveAPIForm() # An unbound form
|
|
||||||
|
|
||||||
return render_to_response('sso/eveapi.html', locals(), context_instance=RequestContext(request))
|
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
|
||||||
def eveapi_del(request, userid=0):
|
|
||||||
""" Delete a EVE API key from a account """
|
|
||||||
|
|
||||||
if userid > 0:
|
|
||||||
try:
|
|
||||||
acc = EVEAccount.objects.get(id=userid)
|
|
||||||
except EVEAccount.DoesNotExist:
|
|
||||||
return redirect('sso.views.profile')
|
|
||||||
if acc.user == request.user:
|
|
||||||
acc.delete()
|
|
||||||
messages.add_message(request, messages.INFO, "EVE API key successfully deleted.")
|
|
||||||
|
|
||||||
return redirect('sso.views.profile')
|
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
|
||||||
def eveapi_refresh(request, userid=0):
|
|
||||||
""" Force refresh a EVE API key """
|
|
||||||
|
|
||||||
if userid > 0:
|
|
||||||
try:
|
|
||||||
acc = EVEAccount.objects.get(id=userid)
|
|
||||||
except EVEAccount.DoesNotExist:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
if acc.user == request.user or request.user.is_superuser:
|
|
||||||
task = import_apikey_result.delay(api_key=acc.api_key, api_userid=acc.api_user_id, force_cache=True, user=request.user.id)
|
|
||||||
|
|
||||||
if request.is_ajax():
|
|
||||||
try:
|
|
||||||
acc = task.wait(30)
|
|
||||||
except celery.exceptions.TimeoutError:
|
|
||||||
acc = EVEAccount.objects.get(id=userid)
|
|
||||||
return HttpResponse(serializers.serialize('json', [acc]), mimetype='application/javascript')
|
|
||||||
else:
|
|
||||||
messages.add_message(request, messages.INFO, "Key %s has been queued to be refreshed from the API" % acc.api_user_id)
|
|
||||||
|
|
||||||
return redirect('sso.views.profile')
|
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
|
||||||
def eveapi_log(request, userid=0):
|
|
||||||
""" Provides a list of access logs for a specific EVE API key """
|
|
||||||
if userid > 0:
|
|
||||||
try:
|
|
||||||
acc = EVEAccount.objects.get(id=userid)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if acc and (acc.user == request.user or request.user.is_staff):
|
|
||||||
logs = ApiAccessLog.objects.filter(userid=userid).order_by('-time_access')[:50]
|
|
||||||
return render_to_response('sso/eveapi_log.html', locals(), context_instance=RequestContext(request))
|
|
||||||
|
|
||||||
return redirect('sso.views.profile')
|
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def service_add(request):
|
def service_add(request):
|
||||||
""" Add a service to a user's account """
|
""" Add a service to a user's account """
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
href="http://eve-online.com/api">EVE Online API
|
href="http://eve-online.com/api">EVE Online API
|
||||||
page</a> and a optional description.</p>
|
page</a> and a optional description.</p>
|
||||||
|
|
||||||
<form action="{% url sso.views.eveapi_add %}" method="post">
|
<form action="{% url eve_api.views.eveapi_add %}" method="post">
|
||||||
<table>
|
<table>
|
||||||
{{ form.as_table }}
|
{{ form.as_table }}
|
||||||
</table>
|
</table>
|
||||||
@@ -15,6 +15,5 @@ page</a> and a optional description.</p>
|
|||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<input type="submit" value="Add Key" />
|
<input type="submit" value="Add Key" />
|
||||||
</form>
|
</form>
|
||||||
<p>Once you have added your EVE API key, don't forget to apply in game, as well as add an application through our
|
|
||||||
<a href="{% url hr.applications.add}">HR system.</a></p>
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
function refresh_apikey(key) {
|
function refresh_apikey(key) {
|
||||||
$("#api-status-" + key).html("<center><img src='/static/img/spinner.gif'/></center>");
|
$("#api-status-" + key).html("<center><img src='/static/img/spinner.gif'/></center>");
|
||||||
$("#api-time-" + key).html("<center><img src='/static/img/spinner.gif'/></center>");
|
$("#api-time-" + key).html("<center><img src='/static/img/spinner.gif'/></center>");
|
||||||
$.getJSON("/profile/refresh/eveapi/" + key + "/", function(json) {
|
$.getJSON("/eve/eveapi/refresh/" + key + "/", function(json) {
|
||||||
$("#api-time-" + json[0].fields.api_user_id).html("a moment ago");
|
$("#api-time-" + json[0].fields.api_user_id).html("a moment ago");
|
||||||
|
|
||||||
switch(json[0].fields.api_status) {
|
switch(json[0].fields.api_status) {
|
||||||
@@ -107,15 +107,15 @@ setup.</p>
|
|||||||
<td id="api-status-{{ acc.api_user_id }}">{{ acc.get_api_status_display }}</td>
|
<td id="api-status-{{ acc.api_user_id }}">{{ acc.get_api_status_display }}</td>
|
||||||
<td id="api-time-{{ acc.api_user_id }}">{{ acc.api_last_updated|naturaltimediff }}</td>
|
<td id="api-time-{{ acc.api_user_id }}">{{ acc.api_last_updated|naturaltimediff }}</td>
|
||||||
<td><a href="javascript:refresh_apikey({{ acc.api_user_id }})">Refresh</a>,
|
<td><a href="javascript:refresh_apikey({{ acc.api_user_id }})">Refresh</a>,
|
||||||
<a href="{% url sso.views.eveapi_log acc.api_user_id %}">Logs</a>,
|
<a href="{% url eve_api.views.eveapi_log acc.api_user_id %}">Logs</a>,
|
||||||
<a href="{% url sso.views.eveapi_del acc.api_user_id %}">Delete</a></td>
|
<a href="{% url eve_api.views.eveapi_del acc.api_user_id %}">Delete</a></td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<p>
|
<p>
|
||||||
<a href="{% url sso.views.eveapi_add %}">Add a Eve API key</a>
|
<a href="{% url eve_api.views.eveapi_add %}">Add a Eve API key</a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<br/>
|
<br/>
|
||||||
|
|||||||
1
urls.py
1
urls.py
@@ -15,6 +15,7 @@ urlpatterns = patterns('',
|
|||||||
(r'^admin/', include(admin.site.urls)),
|
(r'^admin/', include(admin.site.urls)),
|
||||||
('', include('registration.urls')),
|
('', include('registration.urls')),
|
||||||
('', include('sso.urls')),
|
('', include('sso.urls')),
|
||||||
|
(r'^eve/', include('eve_api.urls')),
|
||||||
(r'^eveapi/', include('eve_proxy.urls')),
|
(r'^eveapi/', include('eve_proxy.urls')),
|
||||||
(r'^api/', include('api.urls')),
|
(r'^api/', include('api.urls')),
|
||||||
(r'^hr/', include('hr.urls')),
|
(r'^hr/', include('hr.urls')),
|
||||||
|
|||||||
Reference in New Issue
Block a user