commit 1698da77c6ed2a08d5f3990e02d23fbfffe35d11
Author: Leonidas Poulopoulos <leopoul@noc.grnet.gr>
Date:   Thu Nov 22 13:53:21 2012 +0200

    Changed the user registration proccess. Admins now validate institution

diff --git a/accounts/views.py b/accounts/views.py
index c525964..e1b8119 100644
--- a/accounts/views.py
+++ b/accounts/views.py
@@ -5,24 +5,76 @@ from django.shortcuts import render_to_response
 from django.template.context import RequestContext
 from django.template.loader import render_to_string
 from django.utils.translation import ugettext_lazy as _
-
+from flowspy.accounts.models import *
+from flowspy.peers.models import *
+from flowspy.flowspec.forms import *
 from registration.models import RegistrationProfile
 from registration.views import activate as registration_activate
+from django.views.decorators.cache import never_cache
 
+@never_cache
 def activate(request, activation_key):
-    activation_key = activation_key.lower() # Normalize before trying anything with it.
-    account = RegistrationProfile.objects.activate_user(activation_key)
-    context = RequestContext(request)
-
-    if account:
-        # A user has been activated
-        email = render_to_string("registration/activation_complete.txt",
-                                 {"site": Site.objects.get_current(),
-                                  "user": account})
-        send_mail(_("%sUser account activated") % settings.EMAIL_SUBJECT_PREFIX,
+    account = None
+    if request.method == "GET":
+        activation_key = activation_key.lower() # Normalize before trying anything with it.
+        context = RequestContext(request)
+        try:
+            rp = RegistrationProfile.objects.get(activation_key=activation_key)
+            
+        except RegistrationProfile.DoesNotExist:
+            return render_to_response("registration/activate.html",
+                                  { 'account': account,
+                                    'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS },
+                                  context_instance=context)
+        try:
+            userProfile = rp.user.get_profile()
+        except UserProfile.DoesNotExist:
+            return render_to_response("registration/activate.html",
+                                  { 'account': account,
+                                    'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS },
+                                  context_instance=context)
+        
+        form = UserProfileForm(instance=userProfile)
+        form.fields['user'] = forms.ModelChoiceField(queryset=User.objects.filter(pk=rp.user.pk), empty_label=None)
+        form.fields['peer'] = forms.ModelChoiceField(queryset=Peer.objects.all(), empty_label=None)
+        
+        return render_to_response("registration/activate_edit.html",
+                                  { 'account': account,
+                                    'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
+                                    'form': form },
+                                  context_instance=context)
+            
+    if request.method == "POST":
+        context = RequestContext(request)
+        request_data = request.POST.copy()
+        try:
+            user = User.objects.get(pk=request_data['user'])
+            up = user.get_profile()
+            up.peer = Peer.objects.get(pk=request_data['peer'])
+            up.save()
+            
+        except:
+            return render_to_response("registration/activate_edit.html",
+                                  { 'account': account,
+                                    'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS
+                                     },
+                                  context_instance=context)
+        activation_key = activation_key.lower() # Normalize before trying anything with it.
+        try:
+            rp = RegistrationProfile.objects.get(activation_key=activation_key)
+            account = RegistrationProfile.objects.activate_user(activation_key)
+        except Exception as e:
+            pass
+    
+        if account:
+            # A user has been activated
+            email = render_to_string("registration/activation_complete.txt",
+                                     {"site": Site.objects.get_current(),
+                                      "user": account})
+            send_mail(_("%sUser account activated") % settings.EMAIL_SUBJECT_PREFIX,
                   email, settings.SERVER_EMAIL, [account.email])
-
-    return render_to_response("registration/activate.html",
-                              { 'account': account,
-                                'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS },
-                              context_instance=context)
+        context = RequestContext(request)
+        return render_to_response("registration/activate.html",
+                                  { 'account': account,
+                                    'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS },
+                                  context_instance=context)
\ No newline at end of file
diff --git a/flowspec/forms.py b/flowspec/forms.py
index 77b98df..a91e7a9 100644
--- a/flowspec/forms.py
+++ b/flowspec/forms.py
@@ -5,6 +5,7 @@ from django.utils.translation import ugettext_lazy
 from django.template.defaultfilters import filesizeformat
 from flowspy.flowspec.models import *
 from flowspy.peers.models import *
+from flowspy.accounts.models import *
 from ipaddr import *
 from django.core.urlresolvers import reverse
 from django.contrib.auth.models import User
@@ -12,6 +13,9 @@ from django.conf import settings
 import datetime
 from django.core.mail import mail_admins, mail_managers, send_mail
 
+class UserProfileForm(forms.ModelForm):
+    class Meta:
+        model = UserProfile
 
 class RouteForm(forms.ModelForm):
 #    name = forms.CharField(help_text=ugettext_lazy("A unique route name,"
diff --git a/flowspec/views.py b/flowspec/views.py
index 83c526e..ff8f21a 100644
--- a/flowspec/views.py
+++ b/flowspec/views.py
@@ -291,20 +291,20 @@ def user_login(request):
         lastname = lookupShibAttr(settings.SHIB_LASTNAME, request.META)
         mail = lookupShibAttr(settings.SHIB_MAIL, request.META)
         entitlement = lookupShibAttr(settings.SHIB_ENTITLEMENT, request.META)
-        organization = request.META['HTTP_SHIB_HOMEORGANIZATION']
+        #organization = request.META['HTTP_SHIB_HOMEORGANIZATION']
         
         if settings.SHIB_AUTH_ENTITLEMENT in entitlement.split(";"):
             has_entitlement = True
         if not has_entitlement:
             error_entitlement = True
-        if not organization:
-            error_orgname = True
+#        if not organization:
+#            error_orgname = True
         if not mail:
             error_mail = True
         if error_username:
             error = _("Your idP should release the HTTP_EPPN attribute towards this service<br>")
-        if error_orgname:
-            error = error + _("Your idP should release the HTTP_SHIB_HOMEORGANIZATION attribute towards this service<br>")
+#        if error_orgname:
+#            error = error + _("Your idP should release the HTTP_SHIB_HOMEORGANIZATION attribute towards this service<br>")
         if error_entitlement:
             error = error + _("Your idP should release an appropriate HTTP_SHIB_EP_ENTITLEMENT attribute towards this service<br>")
         if error_mail:
@@ -324,11 +324,14 @@ def user_login(request):
         user = authenticate(username=username, firstname=firstname, lastname=lastname, mail=mail, authsource='shibboleth')
         if user is not None:
             try:
-                peer = Peer.objects.get(domain_name=organization)
-                up = UserProfile.objects.get_or_create(user=user,peer=peer)
+                peer = user.get_profile().peer
+#                peer = Peer.objects.get(domain_name=organization)
+#                up = UserProfile.objects.get_or_create(user=user,peer=peer)
             except:
-                error = _("Your organization's domain name does not match our peers' domain names<br>Please contact Helpdesk to resolve this issue")
-                return render_to_response('error.html', {'error': error}, context_instance=RequestContext(request))
+                form = UserProfileForm()
+                form.fields['user'] = forms.ModelChoiceField(queryset=User.objects.filter(pk=user.pk), empty_label=None)
+                form.fields['peer'] = forms.ModelChoiceField(queryset=Peer.objects.all(), empty_label=None)
+                return render_to_response('registration/select_institution.html', {'form': form}, context_instance=RequestContext(request))
             if not user_exists:
                 user_activation_notify(user)
             if user.is_active:
@@ -407,6 +410,31 @@ def add_port(request):
             return render_to_response('add_port.html', {'form': form,},
                                       context_instance=RequestContext(request))
 
+@never_cache
+def selectinst(request):
+    if request.method == 'POST':
+        request_data = request.POST.copy()
+        user = request_data['user']
+        try:
+            existingProfile = UserProfile.objects.get(user=user)
+            error = _("Violation warning: User account is already associated with an institution.The event has been logged and our administrators will be notified about it")
+            return render_to_response('error.html', {'error': error, 'inactive': True},
+                                  context_instance=RequestContext(request))
+        except UserProfile.DoesNotExist:
+            pass
+            
+        form = UserProfileForm(request_data)
+        if form.is_valid():
+            userprofile = form.save()
+            user_activation_notify(userprofile.user)
+            error = _("User account <strong>%s</strong> is pending activation. Administrators have been notified and will activate this account within the next days. <br>If this account has remained inactive for a long time contact your technical coordinator or GRNET Helpdesk") %userprofile.user.username
+            return render_to_response('error.html', {'error': error, 'inactive': True},
+                                  context_instance=RequestContext(request))
+        else:
+            form.fields['user'] = forms.ModelChoiceField(queryset=User.objects.filter(pk=user.pk), empty_label=None)
+            form.fields['institution'] = forms.ModelChoiceField(queryset=Peer.objects.all(), empty_label=None)
+            return render_to_response('registration/select_institution.html', {'form': form}, context_instance=RequestContext(request))
+
 @login_required
 @never_cache
 def user_logout(request):
diff --git a/templates/registration/activate_edit.html b/templates/registration/activate_edit.html
new file mode 100644
index 0000000..70690f3
--- /dev/null
+++ b/templates/registration/activate_edit.html
@@ -0,0 +1,57 @@
+{% extends "base.html" %}
+{% load i18n %}
+   
+   
+       {% block content %}
+	<div class="container-fluid">
+      <div class="row-fluid">
+        <div class="span2">
+        </div><!--/span-->
+
+		<div class="span10">
+			<div class="row-fluid">
+				<!--/span-->
+				<h3 class="muted">{% trans "Activate User" %}</h3>
+				<hr>
+					<div class="span10">
+						<form method="POST" class="form-horizontal">
+						    {% csrf_token %}
+						    {% if form.non_field_errors %}
+						    <p class="error">
+						        {{ form.non_field_errors}}
+						    </p>
+						    {% endif %}
+						        <div class="control-group {% if form.user.errors %} error {% endif %}">
+						        <label class="control-label" for="id_user"><b>{% trans "User" %}</b></label>
+						        <div class="controls">
+						            {{ form.user }}
+						            {% if form.user.errors %} <span class="help-inline"> {{ form.user.errors|join:", " }} </span>
+						            {% endif %} <span class="help-block"> {{ form.user.help_text }}</span>
+						        </div>
+						    </div>
+						    <div class="control-group {% if form.peer.errors %} error {% endif %}">
+						        <label class="control-label" for="id_peer"><b>{% trans "Institution" %}</b></label>
+						        <div class="controls">
+						            {{ form.peer }}
+						            {% if form.peer.errors %} <span class="help-inline"> {{ form.peer.errors|join:", " }} </span>
+						            {% endif %} <span class="help-block"> {{ form.peer.help_text }}</span>
+						        </div>
+						    </div>
+						
+						    <div class="control-group">
+						        <div class="controls">
+						            <button type="submit" id="applybutton" value="Apply" class="btn btn-primary"/>
+						            {% trans "Activate" %}</button>
+						        </div>
+						    </div>
+						</form>
+					</div>
+				<!--/span-->
+			</div><!--/row-->
+		</div><!--/span-->
+
+        </div><!--/span-->
+    </div><!--/.fluid-container-->
+	{% endblock %}
+   
+   
\ No newline at end of file
diff --git a/templates/registration/select_institution.html b/templates/registration/select_institution.html
new file mode 100644
index 0000000..241dd04
--- /dev/null
+++ b/templates/registration/select_institution.html
@@ -0,0 +1,51 @@
+{% extends "base.html" %}
+{% load i18n %}
+   
+   
+       {% block content %}
+	<div class="container-fluid">
+      <div class="row-fluid">
+        <div class="span2">
+        </div><!--/span-->
+
+		<div class="span10">
+			<div class="row-fluid">
+				<!--/span-->
+				<h3 class="muted">{% trans "Select your institution" %}</h3>
+				<hr>
+					<div class="span10 alert alert-info">Excellent! You are a step away from joining the service. Select your institution from the dropdown list.</div>
+					<div class="span10">
+						<form method="POST" class="form-horizontal"	action="{% url selectinst %}">
+							{% csrf_token %} {% if form.non_field_errors %}
+							<p class="error">{{ form.non_field_errors}}</p>
+							{% endif %}
+							<div style="display: none">{{form.user}}</div>
+							<div
+								class="control-group {% if form.peer.errors %} error {% endif %}">
+								<label class="control-label" for="id_peer"><b>{% trans "Institution" %}</b></label>
+								<div class="controls">
+									{{ form.peer }} {% if form.peer.errors %} <span
+										class="help-inline"> {{ form.peer.errors|join:", " }} </span>
+									{% endif %} <span class="help-block"> {{form.peer.help_text }}</span>
+								</div>
+							</div>
+
+							<div class="control-group">
+								<div class="controls">
+									<button type="submit" id="applybutton" value="Apply"
+										class="btn btn-primary" />
+									{% trans "Apply" %}
+									</button>
+        						</div>
+							</div>
+						</form>
+					</div>
+				<!--/span-->
+			</div><!--/row-->
+		</div><!--/span-->
+
+        </div><!--/span-->
+    </div><!--/.fluid-container-->
+	{% endblock %}
+   
+   
\ No newline at end of file
