Commit 04df82f7 authored by Leonidas Poulopoulos's avatar Leonidas Poulopoulos

Added realms in admin view with full functionality

parent 38f2278d
......@@ -19,6 +19,11 @@ class InstServerForm(forms.ModelForm):
class Meta:
model = InstServer
class InstRealmForm(forms.ModelForm):
class Meta:
model = InstRealm
class ServiceLocForm(forms.ModelForm):
class Meta:
......
......@@ -93,19 +93,18 @@ class InstRealm(models.Model):
Realm (including wildcards) of an IdP Institution
'''
realmexpr = models.CharField(max_length=20)
priority = models.PositiveIntegerField(max_length=3)
realm = models.CharField(max_length=160)
instid = models.ForeignKey("Institution")
# accept if instid.ertype: 1 (idp) or 3 (idpsp)
proxyto = models.ManyToManyField("InstServer")
def __unicode__(self):
return _('Institution: %(inst)s, Realm: %(realmexpr)s, Priority: %(priority)s') % {
# but name is many-to-many from institution
'inst': self.instid.name,
'realmexpr': self.realmexpr,
'priority': self.priority,
}
return 'Realm: %s' % self.realm
def get_servers(self):
return ",".join(["%s"%x for x in self.proxyto.all()])
class InstServer(models.Model):
'''
......@@ -135,9 +134,9 @@ class InstServer(models.Model):
ts = models.DateTimeField(auto_now=True)
def __unicode__(self):
return _('Institution: %(inst)s, Server: %(servername)s, Type: %(ertype)s') % {
return _('Server: %(servername)s, Type: %(ertype)s') % {
# but name is many-to-many from institution
'inst': self.instid,
#'inst': self.instid,
'servername': self.name,
# the human-readable name would be nice here
'ertype': self.ertype,
......
......@@ -341,22 +341,100 @@ def add_server(request, server_pk):
return HttpResponseRedirect(reverse("servers"))
else:
form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None)
print form.errors
return render_to_response('edumanage/servers_edit.html', { 'institution': inst, 'form': form},
context_instance=RequestContext(request, base_response(request)))
@login_required
def realms(request):
user = request.user
servers = False
try:
profile = user.get_profile()
inst = profile.institution
except UserProfile.DoesNotExist:
inst = False
if inst:
realms = InstRealm.objects.filter(instid=inst)
return render_to_response('edumanage/realms.html', { 'realms': realms},
context_instance=RequestContext(request, base_response(request)))
@login_required
def add_realm(request, realm_pk):
user = request.user
server = False
try:
profile = user.get_profile()
inst = profile.institution
except UserProfile.DoesNotExist:
inst = False
if request.method == "GET":
# Determine add or edit
try:
realm = InstRealm.objects.get(instid=inst, pk=realm_pk)
form = InstRealmForm(instance=realm)
except InstRealm.DoesNotExist:
form = InstRealmForm()
form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None)
form.fields['proxyto'] = forms.ModelMultipleChoiceField(queryset=InstServer.objects.filter(pk__in=getInstServers(inst)))
return render_to_response('edumanage/realms_edit.html', { 'form': form},
context_instance=RequestContext(request, base_response(request)))
elif request.method == 'POST':
request_data = request.POST.copy()
try:
realm = InstRealm.objects.get(instid=inst, pk=realm_pk)
form = InstRealmForm(request_data, instance=realm)
except InstRealm.DoesNotExist:
form = InstRealmForm(request_data)
if form.is_valid():
instserverf = form.save()
return HttpResponseRedirect(reverse("realms"))
else:
form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None)
form.fields['proxyto'] = forms.ModelMultipleChoiceField(queryset=InstServer.objects.filter(pk__in=getInstServers(inst)))
return render_to_response('edumanage/realms_edit.html', { 'institution': inst, 'form': form},
context_instance=RequestContext(request, base_response(request)))
@login_required
def del_realm(request):
if request.method == 'GET':
user = request.user
req_data = request.GET.copy()
realm_pk = req_data['realm_pk']
profile = user.get_profile()
institution = profile.institution
resp = {}
try:
realm = InstRealm.objects.get(instid=institution, pk=realm_pk)
except InstRealm.DoesNotExist:
resp['error'] = "Could not get realm or you have no rights to delete"
return HttpResponse(json.dumps(resp), mimetype='application/json')
try:
realm.delete()
except:
resp['error'] = "Could not delete realm"
return HttpResponse(json.dumps(resp), mimetype='application/json')
resp['success'] = "Realm successfully deleted"
return HttpResponse(json.dumps(resp), mimetype='application/json')
@login_required
def base_response(request):
user = request.user
inst = []
server = []
services = []
instrealms = []
try:
profile = user.get_profile()
institution = profile.institution
inst.append(institution)
server = InstServer.objects.filter(instid=institution)
services = ServiceLoc.objects.filter(institutionid=institution)
instrealms = InstRealm.objects.filter(instid=institution)
except UserProfile.DoesNotExist:
pass
......@@ -364,6 +442,7 @@ def base_response(request):
'inst_num': len(inst),
'servers_num': len(server),
'services_num': len(services),
'realms_num': len(instrealms),
}
......@@ -461,5 +540,13 @@ def getInstContacts(inst):
contact_pks.append(contact.contact.pk)
return list(set(contact_pks))
def getInstServers(inst):
servers = InstServer.objects.filter(instid=inst)
server_pks = []
for server in servers:
server_pks.append(server.pk)
return list(set(server_pks))
def rad(x):
return x*math.pi/180
\ No newline at end of file
{% extends "edumanage/welcome.html"%}
{% block crumbs %}
<li><a href="{% url manage %}">Home</a><span class="divider">/</span></li>
<li class="active">Realms</li>
{% endblock %}
{% load i18n %}
{% block extrahead %}
<script type="text/javascript" src="/static/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="/static/js/datatables_bootstrap.js"></script>
<script type="text/javascript">
$(document).ready(function(){
{% if realms %}
var oTable = $('#table').dataTable({
"sPaginationType": "bootstrap",
"sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
"aoColumns": [{
"bSearchable": true,
"bSortable": true
}, {
"bSearchable": true,
"bSortable": true
}, {
"bVisible": true,
"bSearchable": false,
"bSortable": false
}],
"aaSorting": [[0, 'desc']],
"iDisplayLength": 25,
"oSearch": {"bSmart": false, "bRegex":true},
"oLanguage": {
"sLengthMenu": '{% trans "Display" %} <select><option value="25">25</option><option value="50">50</option><option value="-1">{% trans "All" %}</option></select> {% trans "realms" %}',
"sProcessing": "Processing...",
"sZeroRecords": '{% trans "No records to display" %}',
"sInfo": "Showing _START_ to _END_ of _TOTAL_ entries",
"sInfoEmpty": "{% trans "Showing 0 to 0 of 0 entries" %}",
"sInfoFiltered": "(filtered from _MAX_ total entries)",
"sInfoPostFix": "",
"sSearch": '{% trans "Search:" %}',
"sUrl": "",
"oPaginate": {
"sFirst": '{% trans "First" %}',
"sPrevious": '{% trans "Previous" %}',
"sNext": '{% trans "Next" %}',
"sLast": '{% trans "Last" %}'
}
}
});
oTable.fnDraw();
$('[id^=del_realm_]').click(function(){
realm_id = (this.id).replace("del_realm_", '');
name = this.getAttribute("data-realmname");
$("#mymodalbody").html("You are about to delete realm: <b>"+name+"</b><br>Press Delete to proceed or Cancel to cancel deletion");
$("#myModalLabel").html("Delete Realm "+name);
$('#myModal').modal('show');
$(".modal-footer").show();
$("#realm_name_del").html(name)
return false;
});
$("#delrealmSubmit").click(function(){
$.ajax({
url:"{% url del-realm %}/?realm_pk="+realm_id,
type: "GET",
success: function(data){
if (data.error){
$(".modal-footer").hide();
$("#mymodalbody").html("<font style='color:#B94A48'>"+data.error+"</font>");
}
if (data.success){
$(".modal-footer").hide();
$("#mymodalbody").html("Realm "+name+" successfully deleted");
window.setTimeout('location.reload()', 1000);
}
}
});
});
{% endif %}
});
</script>
{% endblock %}
{% block navbar %}
{% endblock %}
{% block homeactive %}{% endblock %}
{% block realmsactive %}class="active"{% endblock %}
{% block subcontent %}
<h4>Realms</h4>
<hr>
<div><a href="{% url edit-realms %}" class="btn btn-primary">Add new realm</a></div>
<div class="span10"></div>
{% if realms %}
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" width="100%" id="table">
<thead>
<tr>
<th>Realm</th>
<th>Server(s)</th>
<th style="text-align: center;">Action</th>
</tr>
</thead>
<tbody>
{% for realm in realms %}
<tr class="GradeC">
<td>{{realm.realm}}</td>
<td>{{realm.get_servers}}</td>
<td style="text-align: center;"><a href="{% url edit-realms realm.pk %}" class="btn btn-small">edit</a> <a href="#" id="del_realm_{{realm.pk}}" data-realmname="{{realm.realm}}" class="btn btn-small btn-warning">delete</a></td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<div>No realms defined yet (<a href="{% url edit-realms %}">add</a>)</div>
{% endif %}
<div class="modal hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Delete Realm</h3>
</div>
<div class="modal-body" id="mymodalbody">
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
<a class="btn btn-warning" id="delrealmSubmit" href="#">Delete</a>
</div>
</div>
{% endblock %}
{% extends "edumanage/welcome.html"%}
{% block crumbs %}
<li><a href="{% url manage %}">Home</a><span class="divider">/</span></li>
<li><a href="{% url realms %}">Realms</a><span class="divider">/</span></li>
<li class="active">Add-Edit</li>
{% endblock %}
{% block extrahead %}
<script type="text/javascript" src="/static/js/jquery.min.js"></script>
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
{% endblock %}
{% block homeactive %}{% endblock %}
{% block realmsactive %}class="active"{% endblock %}
{% block subcontent %} <h4>Realms Add/Edit</h4>
<hr>
<form method="POST" class="form-horizontal">
{% csrf_token %}
{% if form.non_field_errors %}
<p class="error">
{{ form.non_field_errors}}
</p>
{% endif %}
<div style="display: none">
{{form.instid}}
</div>
<div class="control-group {% if form.realm.errors %} error {% endif %}">
<label class="control-label" for="id_realm"><b>Realm</b></label>
<div class="controls">
{{ form.realm }}
{% if form.realm.errors %} <span class="help-inline"> {{ form.realm.errors|join:", " }} </span>
{% endif %} <span class="help-block"> {{ form.realm.help_text }}</span>
</div>
</div>
<div class="control-group {% if form.proxyto.errors %} error {% endif %}">
<label class="control-label" for="id_proxyto"><b>Proxy to Servers</b></label>
<div class="controls">
{{ form.proxyto }}
{% if form.proxyto.errors %} <span class="help-inline"> {{ form.proxyto.errors|join:", " }} </span>
{% endif %} <span class="help-block"> {{ form.proxyto.help_text }}</span>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" id="applybutton" value="Apply" class="btn btn-large btn-primary"/>
Apply</button>
</div>
</div>
</form>
{% endblock %}
......@@ -192,6 +192,9 @@ $.get("{% url get-service-points %}", function(data){
<li {% block serversactive %}{% endblock %}>
<a href="{% url servers %}">Servers ({{servers_num}})</a>
</li>
<li {% block realmsactive %}{% endblock %}>
<a href="{% url realms %}">Realms ({{realms_num}})</a>
</li>
</ul>
</div><!--/.well -->
</div><!--/span-->
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment