Commit c2b3014a authored by Leonidas Poulopoulos's avatar Leonidas Poulopoulos
Browse files

Added xml generation plus user management

parent 04df82f7
......@@ -7,6 +7,10 @@ from django.contrib.contenttypes import generic
class NameInline(generic.GenericTabularInline):
model = Name_i18n
class UrlInline(generic.GenericTabularInline):
model = URL_i18n
class InstitutionAdmin(admin.ModelAdmin):
inlines = [
......@@ -18,6 +22,10 @@ class ServiceLocAdmin(admin.ModelAdmin):
NameInline,
]
class RealmInLine(admin.ModelAdmin):
inlines = [
UrlInline,
]
admin.site.register(Name_i18n)
......@@ -32,5 +40,5 @@ admin.site.register(MonLocalEAPOLData)
admin.site.register(ServiceLoc, ServiceLocAdmin)
admin.site.register(Institution, InstitutionAdmin)
admin.site.register(InstitutionDetails)
admin.site.register(Realm)
admin.site.register(Realm, RealmInLine)
admin.site.register(RealmData)
\ No newline at end of file
......@@ -19,6 +19,11 @@ class InstServerForm(forms.ModelForm):
class Meta:
model = InstServer
class ContactForm(forms.ModelForm):
class Meta:
model = Contact
class InstRealmForm(forms.ModelForm):
class Meta:
......
......@@ -14,7 +14,7 @@ from django.forms.models import inlineformset_factory
from django.contrib.contenttypes.generic import generic_inlineformset_factory
import json
import math
from xml.etree import cElementTree as ET
from xml.etree import ElementTree as ET
from django.conf import settings
......@@ -421,6 +421,97 @@ def del_realm(request):
return HttpResponse(json.dumps(resp), mimetype='application/json')
@login_required
def contacts(request):
user = request.user
servers = False
instcontacts = []
try:
profile = user.get_profile()
inst = profile.institution
except UserProfile.DoesNotExist:
inst = False
if inst:
instcontacts.extend([x.contact.pk for x in InstitutionContactPool.objects.filter(institution=inst)])
print instcontacts
contacts = Contact.objects.filter(pk__in=instcontacts)
print contacts
return render_to_response('edumanage/contacts.html', { 'contacts': contacts},
context_instance=RequestContext(request, base_response(request)))
@login_required
def add_contact(request, contact_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:
contactinst = InstitutionContactPool.objects.get(institution=inst, contact__pk=contact_pk)
contact = contactinst.contact
form = ContactForm(instance=contact)
except InstitutionContactPool.DoesNotExist:
form = ContactForm()
return render_to_response('edumanage/contacts_edit.html', { 'form': form},
context_instance=RequestContext(request, base_response(request)))
elif request.method == 'POST':
request_data = request.POST.copy()
try:
contactinst = InstitutionContactPool.objects.get(institution=inst, contact__pk=contact_pk)
contact = contactinst.contact
form = ContactForm(request_data, instance=contact)
except InstitutionContactPool.DoesNotExist:
form = ContactForm(request_data)
if form.is_valid():
contact = form.save()
instContPool, created = InstitutionContactPool.objects.get_or_create(contact=contact, institution=inst)
instContPool.save()
return HttpResponseRedirect(reverse("contacts"))
return render_to_response('edumanage/contacts_edit.html', { 'form': form},
context_instance=RequestContext(request, base_response(request)))
@login_required
def del_contact(request):
if request.method == 'GET':
user = request.user
req_data = request.GET.copy()
contact_pk = req_data['contact_pk']
profile = user.get_profile()
institution = profile.institution
resp = {}
try:
contactinst = InstitutionContactPool.objects.get(institution=institution, contact__pk=contact_pk)
contact = contactinst.contact
except InstitutionContactPool.DoesNotExist:
resp['error'] = "Could not get contact or you have no rights to delete"
return HttpResponse(json.dumps(resp), mimetype='application/json')
try:
for service in ServiceLoc.objects.filter(institutionid=institution):
if (contact in service.contact.all() and len(service.contact.all()) == 1):
resp['error'] = "Could not delete contact. It is the only contact in service <b>%s</b>.<br>Fix it and try again" %service.get_name(lang="en")
return HttpResponse(json.dumps(resp), mimetype='application/json')
if (contact in institution.institutiondetails.contact.all() and len(institution.institutiondetails.contact.all()) == 1):
resp['error'] = "Could not delete contact. It is the only contact your institution.<br>Fix it and try again"
return HttpResponse(json.dumps(resp), mimetype='application/json')
contact.delete()
except Exception:
resp['error'] = "Could not delete contact"
return HttpResponse(json.dumps(resp), mimetype='application/json')
resp['success'] = "Contact successfully deleted"
return HttpResponse(json.dumps(resp), mimetype='application/json')
@login_required
def base_response(request):
user = request.user
......@@ -428,6 +519,8 @@ def base_response(request):
server = []
services = []
instrealms = []
instcontacts = []
contacts = []
try:
profile = user.get_profile()
institution = profile.institution
......@@ -435,6 +528,8 @@ def base_response(request):
server = InstServer.objects.filter(instid=institution)
services = ServiceLoc.objects.filter(institutionid=institution)
instrealms = InstRealm.objects.filter(instid=institution)
instcontacts.extend([x.contact.pk for x in InstitutionContactPool.objects.filter(institution=institution)])
contacts = Contact.objects.filter(pk__in=instcontacts)
except UserProfile.DoesNotExist:
pass
......@@ -443,6 +538,7 @@ def base_response(request):
'servers_num': len(server),
'services_num': len(services),
'realms_num': len(instrealms),
'contacts_num': len(contacts),
}
......@@ -531,8 +627,185 @@ def closest(request):
closestMarker = {"name": pointname, "lat": pointlat, "lng": pointlng, "text": j[1].text}
return HttpResponse(json.dumps(closestMarker), mimetype='application/json')
def instxml(request):
ET._namespace_map["http://www.w3.org/2001/XMLSchema-instance"] = 'xsi'
root = ET.Element("institutions")
NS_XSI = "{http://www.w3.org/2001/XMLSchema-instance}"
root.set(NS_XSI + "noNamespaceSchemaLocation", "institutions.xsd")
#root.attrib["xsi:noNamespaceSchemaLocation"] = "institution.xsd"
institutions = Institution.objects.all()
for institution in institutions:
try:
inst = institution.institutiondetails
if not inst:
pass
except InstitutionDetails.DoesNotExist:
pass
instElement = ET.SubElement(root, "institution")
instCountry = ET.SubElement(instElement, "country")
instCountry.text = ("%s" %inst.institution.realmid.country).upper()
instType = ET.SubElement(instElement, "type")
instType.text = "%s" %inst.ertype
for realm in institution.instrealm_set.all():
instRealm = ET.SubElement(instElement, "inst_realm")
instRealm.text = realm.realm
for name in inst.institution.org_name.all():
instOrgName = ET.SubElement(instElement, "org_name")
instOrgName.attrib["lang"] = name.lang
instOrgName.text = u"%s" %name.name
instAddress = ET.SubElement(instElement, "address")
instAddrStreet = ET.SubElement(instAddress, "street")
instAddrStreet.text = inst.address_street
instAddrCity = ET.SubElement(instAddress, "city")
instAddrCity.text = inst.address_city
for contact in inst.contact.all():
instContact = ET.SubElement(instElement, "contact")
instContactName = ET.SubElement(instContact, "name")
instContactName.text = "%s %s" %(contact.firstname, contact.lastname)
instContactEmail = ET.SubElement(instContact, "email")
instContactEmail.text = contact.email
instContactPhone = ET.SubElement(instContact, "phone")
instContactPhone.text = contact.phone
for url in inst.url.all():
instUrl = ET.SubElement(instElement, "%s_URL"%(url.urltype))
instUrl.attrib["lang"] = url.lang
instUrl.text = url.url
#Let's go to Institution Service Locations
for serviceloc in inst.institution.serviceloc_set.all():
instLocation = ET.SubElement(instElement, "location")
instLong = ET.SubElement(instLocation, "longitude")
instLong.text = "%s" %serviceloc.longitude
instLat = ET.SubElement(instLocation, "latitude")
instLat.text = "%s" %serviceloc.latitude
for instlocname in serviceloc.loc_name.all():
instLocName = ET.SubElement(instLocation, "loc_name")
instLocName.attrib["lang"] = instlocname.lang
instLocName.text = instlocname.name
instLocAddress = ET.SubElement(instLocation, "address")
instLocAddrStreet = ET.SubElement(instLocAddress, "street")
instLocAddrStreet.text = serviceloc.address_street
instLocAddrCity = ET.SubElement(instLocAddress, "city")
instLocAddrCity.text = serviceloc.address_city
instLocSSID = ET.SubElement(instLocation, "SSID")
instLocSSID.text = serviceloc.SSID
instLocEncLevel = ET.SubElement(instLocation, "enc_level")
instLocEncLevel.text = serviceloc.enc_level
instLocPortRestrict = ET.SubElement(instLocation, "port_restrict")
instLocPortRestrict.text = ("%s" %serviceloc.port_restrict).lower()
instLocTransProxy = ET.SubElement(instLocation, "transp_proxy")
instLocTransProxy.text = ("%s" %serviceloc.transp_proxy).lower()
instLocIpv6 = ET.SubElement(instLocation, "IPv6")
instLocIpv6.text = ("%s" %serviceloc.IPv6).lower()
instLocNAT = ET.SubElement(instLocation, "NAT")
instLocNAT.text = ("%s" %serviceloc.NAT).lower()
instLocAP_no = ET.SubElement(instLocation, "AP_no")
instLocAP_no.text = "%s" %int(serviceloc.AP_no)
instLocWired = ET.SubElement(instLocation, "wired")
instLocWired.text = ("%s" %serviceloc.wired).lower()
for url in serviceloc.url.all():
instLocUrl = ET.SubElement(instLocation, "%s_URL"%(url.urltype))
instLocUrl.attrib["lang"] = url.lang
instLocUrl.text = url.url
instTs = ET.SubElement(instElement, "ts")
instTs.text = "%s" %inst.ts.isoformat()
return render_to_response("general/institution.xml", {"xml":to_xml(root)},context_instance=RequestContext(request,), mimetype="application/xml")
def realmxml(request):
realm = Realm.objects.all()[0]
ET._namespace_map["http://www.w3.org/2001/XMLSchema-instance"] = 'xsi'
root = ET.Element("realms")
NS_XSI = "{http://www.w3.org/2001/XMLSchema-instance}"
root.set(NS_XSI + "noNamespaceSchemaLocation", "realm.xsd")
#root.attrib["xsi:noNamespaceSchemaLocation"] = "institution.xsd"
realmElement = ET.SubElement(root, "realm")
realmCountry = ET.SubElement(realmElement, "country")
realmCountry.text = realm.country
realmStype = ET.SubElement(realmElement, "stype")
realmStype.text = "%s" %realm.stype
for name in realm.org_name.all():
realmOrgName = ET.SubElement(realmElement, "org_name")
realmOrgName.attrib["lang"] = name.lang
realmOrgName.text = u"%s" %name.name
realmAddress = ET.SubElement(realmElement, "address")
realmAddrStreet = ET.SubElement(realmAddress, "street")
realmAddrStreet.text = realm.address_street
realmAddrCity = ET.SubElement(realmAddress, "city")
realmAddrCity.text = realm.address_city
for contact in realm.contact.all():
realmContact = ET.SubElement(realmElement, "contact")
realmContactName = ET.SubElement(realmContact, "name")
realmContactName.text = "%s %s" %(contact.firstname, contact.lastname)
realmContactEmail = ET.SubElement(realmContact, "email")
realmContactEmail.text = contact.email
realmContactPhone = ET.SubElement(realmContact, "phone")
realmContactPhone.text = contact.phone
for url in realm.url.all():
realmUrl = ET.SubElement(realmElement, "%s_URL"%(url.urltype))
realmUrl.attrib["lang"] = url.lang
realmUrl.text = url.url
return render_to_response("general/realm.xml", {"xml":to_xml(root)},context_instance=RequestContext(request,), mimetype="application/xml")
def realmdataxml(request):
realm = Realm.objects.all()[0]
ET._namespace_map["http://www.w3.org/2001/XMLSchema-instance"] = 'xsi'
root = ET.Element("realm-data")
NS_XSI = "{http://www.w3.org/2001/XMLSchema-instance}"
root.set(NS_XSI + "noNamespaceSchemaLocation", "realm-data.xsd")
return render_to_response("general/realm_data.xml", {"xml":to_xml(root)},context_instance=RequestContext(request,), mimetype="application/xml")
def to_xml(ele, encoding="UTF-8"):
"Convert and return the XML for an *ele* (:class:`~xml.etree.ElementTree.Element`) with specified *encoding*."
xml = ET.tostring(ele, encoding)
return xml if xml.startswith('<?xml') else '<?xml version="1.0" encoding="%s"?>%s' % (encoding, xml)
def getInstContacts(inst):
contacts = InstitutionContactPool.objects.filter(institution=inst)
contact_pks = []
......@@ -549,4 +822,4 @@ def getInstServers(inst):
def rad(x):
return x*math.pi/180
\ No newline at end of file
return x*math.pi/180
{% extends "edumanage/welcome.html"%}
{% block crumbs %}
<li><a href="{% url manage %}">Home</a><span class="divider">/</span></li>
<li class="active">Contacts</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 contacts %}
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
}, {
"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 "contacts" %}',
"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_contact_]').click(function(){
contact_id = (this.id).replace("del_contact_", '');
name = this.getAttribute("data-contactname");
$("#mymodalbody").html("You are about to delete contact: <b>"+name+"</b><br>Press Delete to proceed or Cancel to cancel deletion");
$("#myModalLabel").html("Delete contact "+name);
$('#myModal').modal('show');
$(".modal-footer").show();
$("#contact_name_del").html(name)
return false;
});
$("#delcontactSubmit").click(function(){
$.ajax({
url:"{% url del-contact %}/?contact_pk="+contact_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("contact "+name+" successfully deleted");
window.setTimeout('location.reload()', 1000);
}
}
});
});
{% endif %}
});
</script>
{% endblock %}
{% block navbar %}
{% endblock %}
{% block homeactive %}{% endblock %}
{% block contactsactive %}class="active"{% endblock %}
{% block subcontent %}
<h4>contacts</h4>
<hr>
<div><a href="{% url edit-contacts %}" class="btn btn-primary">Add new contact</a></div>
<div class="span10"></div>
{% if contacts %}
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" width="100%" id="table">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th style="text-align: center;">Action</th>
</tr>
</thead>
<tbody>
{% for contact in contacts %}
<tr class="GradeC">
<td>{{contact.firstname}} {{contact.lastname}}</td>
<td>{{contact.email}}</td>
<td>{{contact.phone}}</td>
<td style="text-align: center;"><a href="{% url edit-contacts contact.pk %}" class="btn btn-small">edit</a> <a href="#" id="del_contact_{{contact.pk}}" data-contactname="{{contact.firstname}} {{contact.lastname}}" class="btn btn-small btn-warning">delete</a></td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<div>No contacts defined yet (<a href="{% url edit-contacts %}">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 contact</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="delcontactSubmit" 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 contactsactive %}class="active"{% endblock %}
{% block subcontent %} <h4>Contacts 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.firstname.errors %} error {% endif %}">
<label class="control-label" for="id_firstname"><b>First name</b></label>
<div class="controls">
{{ form.firstname }}
{% if form.firstname.errors %} <span class="help-inline"> {{ form.firstname.errors|join:", " }} </span>
{% endif %} <span class="help-block"> {{ form.firstname.help_text }}</span>
</div>
</div>
<div class="control-group {% if form.lastname.errors %} error {% endif %}">
<label class="control-label" for="id_lastname"><b>Last name</b></label>
<div class="controls">
{{ form.lastname }}
{% if form.lastname.errors %} <span class="help-inline"> {{ form.lastname.errors|join:", " }} </span>
{% endif %} <span class="help-block"> {{ form.lastname.help_text }}</span>
</div>
</div>
<div class="control-group {% if form.email.errors %} error {% endif %}">
<label class="control-label" for="id_email"><b>Email</b></label>
<div class="controls">
{{ form.email }}
{% if form.email.errors %} <span class="help-inline"> {{ form.email.errors|join:", " }} </span>
{% endif %} <span class="help-block"> {{ form.email.help_text }}</span>
</div>
</div>
<div class="control-group {% if form.phone.errors %} error {% endif %}">
<label class="control-label" for="id_phone"><b>Phone</b></label>
<div class="controls">
{{ form.phone }}
{% if form.phone.errors %} <span class="help-inline"> {{ form.phone.errors|join:", " }} </span>
{% endif %} <span class="help-block"> {{ form.phone.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 %}
......@@ -195,6 +195,9 @@ $.get("{% url get-service-points %}", function(data){
<li {% block realmsactive %}{% endblock %}>
<a href="{% url realms %}">Realms ({{realms_num}})</a>
</li>
<li {% block contactsactive %}{% endblock %}>
<a href="{% url contacts %}">Contacts ({{contacts_num}})</a>
</li>
</ul>
</div><!--/.well -->
</div><!--/span-->
......
{% autoescape off %}{{xml}}{% endautoescape %}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="eduroam_institution_type">
<xs:restriction base="xs:int">
<xs:enumeration value="1">
<xs:annotation>
<xs:documentation>IdP</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="2">
<xs:annotation>
<xs:documentation>SP</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="3">