mirror of
https://github.com/nikdoof/test-auth.git
synced 2025-12-14 06:42:16 +00:00
Switch Add/Update to use CBV, combine templates into one.
This commit is contained in:
@@ -6,7 +6,7 @@ from gargoyle import gargoyle
|
||||
from eve_api.models import EVEAccount, EVEPlayerCharacter, EVEPlayerCorporation
|
||||
|
||||
|
||||
class EveAPIForm(forms.ModelForm):
|
||||
class EVEAPIForm(forms.ModelForm):
|
||||
""" EVE API input form """
|
||||
|
||||
class Meta:
|
||||
@@ -15,12 +15,15 @@ class EveAPIForm(forms.ModelForm):
|
||||
widgets = {'user': forms.HiddenInput()}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(EveAPIForm, self).__init__(*args, **kwargs)
|
||||
super(EVEAPIForm, self).__init__(*args, **kwargs)
|
||||
instance = getattr(self, 'instance', None)
|
||||
|
||||
if instance and instance.pk:
|
||||
# We're editing a existing instance, readonly the userid
|
||||
self.fields['api_user_id'].widget.attrs['readonly'] = True
|
||||
else:
|
||||
# New instance, hide description
|
||||
del self.fields['description']
|
||||
|
||||
if gargoyle.is_active('eve-cak'):
|
||||
self.fields['api_user_id'].label = 'Key ID'
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}Add EVE API Key{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="page-header">
|
||||
<h1>Add EVE API Key</h1>
|
||||
</div>
|
||||
|
||||
<p>Please fill in your API key details and a optional description. You can create a API key on the <a href="https://support.eveonline.com/api/Key/CreatePredefined/59638024/0/false">API support site</a></p>
|
||||
|
||||
<form action="{% url eve_api.views.eveapi_add %}" method="post">
|
||||
|
||||
<fieldset>
|
||||
{% include "formtools/formerror.html" %}
|
||||
{% include "formtools/formfield.html" with field=form.api_user_id %}
|
||||
{% include "formtools/formfield.html" with field=form.api_key class="xxlarge"%}
|
||||
{% csrf_token %}
|
||||
<input type="submit" value="Add API Key" class="btn primary"/>
|
||||
</fieldset>
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
||||
24
app/eve_api/templates/eve_api/eveaccount_form.html
Normal file
24
app/eve_api/templates/eve_api/eveaccount_form.html
Normal file
@@ -0,0 +1,24 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}{% if object %}Update EVE API Key{% else %}Add EVE API Key{% endif %}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="page-header">
|
||||
<h1>{% if object %}Update EVE API Key{% else %}Add EVE API Key{% endif %}</h1>
|
||||
</div>
|
||||
|
||||
<p>Please fill in your API key details and a optional description. You can create a API key on the <a href="https://support.eveonline.com/api/Key/CreatePredefined/59638024/0/false">API support site</a></p>
|
||||
|
||||
<form action="{% if object %}{% url eveapi-update object.api_user_id %}{% else %}{% url eveapi-add %}{% endif %}" method="post">
|
||||
<fieldset>
|
||||
{% include "formtools/formerror.html" %}
|
||||
{% include "formtools/formfield.html" with field=form.api_user_id %}
|
||||
{% include "formtools/formfield.html" with field=form.api_key class="xxlarge" %}
|
||||
{% include "formtools/formfield.html" with field=form.description class="xxlarge" %}
|
||||
{% include "formtools/formfield.html" with field=form.user %}
|
||||
{% csrf_token %}
|
||||
<input type="submit" value="{% if object %}Update API Key{% else %}Add API Key{% endif %}" class="btn primary"/>
|
||||
</fieldset>
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
||||
@@ -1,24 +0,0 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}Update EVE API Key{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="page-header">
|
||||
<h1>Update EVE API Key</h1>
|
||||
</div>
|
||||
|
||||
<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 eveapi-update acc.api_user_id %}" method="post">
|
||||
<fieldset>
|
||||
{% include "formtools/formerror.html" %}
|
||||
{% include "formtools/formfield.html" with field=form.user %}
|
||||
{% include "formtools/formfield.html" with field=form.api_user_id %}
|
||||
{% include "formtools/formfield.html" with field=form.api_key class="xxlarge" %}
|
||||
{% include "formtools/formfield.html" with field=form.description class="xxlarge" %}
|
||||
{% csrf_token %}
|
||||
<input type="submit" value="Add API Key" class="btn primary"/>
|
||||
</fieldset>
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
||||
@@ -5,8 +5,8 @@ from django.contrib.auth.decorators import login_required
|
||||
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/add/$', login_required(views.EVEAPICreateView.as_view()), name="eveapi-add"),
|
||||
url(r'^eveapi/update/(?P<pk>\d+)/$', login_required(views.EVEAPIUpdateView.as_view()), name="eveapi-update"),
|
||||
url(r'^eveapi/delete/(?P<pk>\d+)/$', login_required(views.EVEAPIDeleteView.as_view()), name="eveapi-delete"),
|
||||
url(r'^eveapi/refresh/(?P<pk>\d+)/$', login_required(views.EVEAPIRefreshView.as_view()), name="eveapi-refresh"),
|
||||
url(r'^eveapi/log/(?P<userid>\d+)/$', login_required(views.EVEAPILogView.as_view()), name="eveapi-log"),
|
||||
|
||||
@@ -2,10 +2,10 @@ import csv
|
||||
|
||||
from django.core import serializers
|
||||
from django.core.urlresolvers import reverse, reverse_lazy
|
||||
from django.http import HttpResponse, Http404, HttpResponseForbidden, HttpResponseRedirect
|
||||
from django.shortcuts import render_to_response, get_object_or_404, redirect
|
||||
from django.http import HttpResponse, HttpResponseForbidden, HttpResponseRedirect
|
||||
from django.shortcuts import render_to_response, get_object_or_404
|
||||
from django.template import RequestContext
|
||||
from django.views.generic import TemplateView, DetailView, ListView, DeleteView, View
|
||||
from django.views.generic import TemplateView, CreateView, UpdateView, DetailView, ListView, DeleteView, View
|
||||
from django.views.generic.detail import SingleObjectMixin
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
@@ -16,23 +16,55 @@ from gargoyle import gargoyle
|
||||
from eve_proxy.models import ApiAccessLog, CachedDocument
|
||||
from eve_proxy.exceptions import DocumentRetrievalError
|
||||
from eve_api.app_defines import *
|
||||
from eve_api.forms import EveAPIForm
|
||||
from eve_api.forms import EVEAPIForm
|
||||
from eve_api.models import EVEAccount, EVEPlayerCharacter, EVEPlayerCorporation, EVEPlayerAlliance
|
||||
from eve_api.tasks import import_apikey_result
|
||||
from eve_api.utils import basic_xml_parse_doc
|
||||
from eve_api.views.mixins import DetailPaginationMixin
|
||||
|
||||
|
||||
@login_required
|
||||
def eveapi_add(request, post_save_redirect='/', template='eve_api/add.html'):
|
||||
""" Add a EVE API key to a user's account """
|
||||
class EVEAPICreateView(CreateView):
|
||||
|
||||
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)
|
||||
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)
|
||||
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 while trying to import key %s, Please check that you are using the correct information and try again." % form.cleaned_data['api_user_id']
|
||||
messages.success(request, msg, fail_silently=True)
|
||||
return HttpResponseRedirect(self.get_success_url())
|
||||
|
||||
def get_initial(self):
|
||||
return {'user': self.request.user.pk}
|
||||
|
||||
|
||||
class EVEAPIUpdateView(UpdateView):
|
||||
|
||||
model = EVEAccount
|
||||
form_class = EVEAPIForm
|
||||
success_url = reverse_lazy('sso-profile')
|
||||
|
||||
def get_initial(self):
|
||||
return {'user': self.request.user.pk}
|
||||
|
||||
def form_valid(self, form):
|
||||
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:
|
||||
out = task.wait(10)
|
||||
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:
|
||||
@@ -40,59 +72,14 @@ def eveapi_add(request, post_save_redirect='/', template='eve_api/add.html'):
|
||||
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 while trying to import key %s, Please check that you are using the correct information and try again." % form.cleaned_data['api_user_id']
|
||||
messages.success(request, msg, fail_silently=True)
|
||||
return HttpResponseRedirect(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))
|
||||
|
||||
|
||||
@login_required
|
||||
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
|
||||
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))
|
||||
messages.success(request, msg, fail_silently=True)
|
||||
return HttpResponseRedirect(self.get_success_url())
|
||||
|
||||
|
||||
class EVEAPIDeleteView(DeleteView):
|
||||
@@ -205,6 +192,7 @@ class EVEAPICharacterDetailView(DetailView):
|
||||
})
|
||||
return ctx
|
||||
|
||||
|
||||
class EVEAPICharacterListView(TemplateView):
|
||||
|
||||
template_name = 'eve_api/character_list.html'
|
||||
@@ -240,7 +228,7 @@ def eveapi_corporation_members_csv(request, corporationid):
|
||||
|
||||
corporation = get_object_or_404(EVEPlayerCorporation, id=corporationid)
|
||||
if not corporation.eveplayercharacter_set.filter(eveaccount__user=request.user, roles__name="roleDirector").count() and not request.user.is_superuser:
|
||||
raise Http404
|
||||
return HttpResponseForbidden()
|
||||
|
||||
response = HttpResponse(mimetype='text/csv')
|
||||
response['Content-Disposition'] = 'attachment; filename=%s-members_export.csv' % corporation.id
|
||||
|
||||
@@ -99,7 +99,7 @@
|
||||
<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>{% ifswitch api-disableprocessing %}{% else %}<a href="{% url eveapi-refresh acc.api_user_id %}" onclick="javascript:refresh_apikey({{ acc.api_user_id }}); return false;">Refresh</a>,
|
||||
<a href="{% url eve_api.views.eveapi_update acc.api_user_id %}">Update Key</a>, {% endifswitch %}
|
||||
<a href="{% url eveapi-update acc.api_user_id %}">Update Key</a>, {% endifswitch %}
|
||||
<a href="{% url eveapi-log acc.api_user_id %}">Logs</a>{% ifswitch eve-keydelete %},
|
||||
<a href="{% url eveapi-delete acc.api_user_id %}">Delete</a>{% endifswitch %}</td>
|
||||
</tr>
|
||||
@@ -112,7 +112,7 @@
|
||||
<p>
|
||||
{% ifswitch api-disableprocessing %}
|
||||
{% else %}
|
||||
<a href="{% url eve_api.views.eveapi_add %}" class="btn primary" title="Adds a new EVE API key to your account">Add a Eve API key</a>
|
||||
<a href="{% url eveapi-add %}" class="btn primary" title="Adds a new EVE API key to your account">Add a Eve API key</a>
|
||||
<a href="https://support.eveonline.com/api/Key/CreatePredefined/59638024/0/false" class="btn" title="Create a API key with all the permissions Auth needs">Create API Key</a>
|
||||
<a href="{% url sso.views.refresh_access %}" class="btn {% if not user.eveaccount_set.all %}disabled{% else %}danger{% endif %}" title="Forces auth to recheck your permissions and update your services as needed">Force Access Update</a>
|
||||
{% endifswitch %}
|
||||
|
||||
Reference in New Issue
Block a user