Revision 62d0c01e edumanage/views.py

b/edumanage/views.py
22 22

  
23 23
from django.db.models import Max
24 24

  
25
from django.views.decorators.cache import never_cache
26
from django.utils.translation import ugettext as _
27
from django.contrib.auth import authenticate, login
28

  
25 29

  
26 30
def index(request):
27 31
    return render_to_response('front/index.html', context_instance=RequestContext(request))
......
666 670
        resp['success'] = "Service successfully deleted"
667 671
        return HttpResponse(json.dumps(resp), mimetype='application/json')
668 672
    
669

  
673
@never_cache
674
def user_login(request):
675
    try:
676
        error_username = False
677
        error_orgname = False
678
        error_entitlement = False
679
        error_mail = False
680
        has_entitlement = False
681
        error = ''
682
        username = request.META['HTTP_EPPN']
683
        if not username:
684
            error_username = True
685
        firstname = request.META['HTTP_SHIB_INETORGPERSON_GIVENNAME']
686
        lastname = request.META['HTTP_SHIB_PERSON_SURNAME']
687
        mail = request.META['HTTP_SHIB_INETORGPERSON_MAIL']
688
        #organization = request.META['HTTP_SHIB_HOMEORGANIZATION']
689
        entitlement = request.META['HTTP_SHIB_EP_ENTITLEMENT']
690
        if settings.SHIB_AUTH_ENTITLEMENT in entitlement.split(";"):
691
            has_entitlement = True
692
        if not has_entitlement:
693
            error_entitlement = True
694
#        if not organization:
695
#            error_orgname = True
696
        if not mail:
697
            error_mail = True
698
        if error_username:
699
            error = _("Your idP should release the HTTP_EPPN attribute towards this service<br>")
700
        if error_orgname:
701
            error = error + _("Your idP should release the HTTP_SHIB_HOMEORGANIZATION attribute towards this service<br>")
702
        if error_entitlement:
703
            error = error + _("Your idP should release an appropriate HTTP_SHIB_EP_ENTITLEMENT attribute towards this service<br>")
704
        if error_mail:
705
            error = error + _("Your idP should release the HTTP_SHIB_INETORGPERSON_MAIL attribute towards this service")
706
        if error_username or error_orgname or error_entitlement or error_mail:
707
            return render_to_response('error.html', {'error': error, "missing_attributes": True},
708
                                  context_instance=RequestContext(request))
709
        try:
710
            user = User.objects.get(username__exact=username)
711
            user.email = mail
712
            user.first_name = firstname
713
            user.last_name = lastname
714
            user.save()
715
            user_exists = True
716
        except User.DoesNotExist:
717
            user_exists = False
718
        user = authenticate(username=username, firstname=firstname, lastname=lastname, mail=mail, authsource='shibboleth')
719
        if user is not None:
720
#            try:
721
#                peer = Peer.objects.get(domain_name=organization)
722
#                up = UserProfile.objects.get_or_create(user=user,peer=peer)
723
#            except:
724
#                error = _("Your organization's domain name does not match our peers' domain names<br>Please contact Helpdesk to resolve this issue")
725
#                return render_to_response('error.html', {'error': error}, context_instance=RequestContext(request))
726
#            if not user_exists:
727
#                user_activation_notify(user)
728
            # user does not exist... forward to an institution selection form to create profile
729
            try:
730
                profile = user.get_profile()
731
                inst = profile.institution
732
            except UserProfile.DoesNotExist:
733
                form = UserProfileForm()
734
                form.fields['user'] = forms.ModelChoiceField(queryset=User.objects.filter(pk=user.pk), empty_label=None)
735
                form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.all(), empty_label=None)
736
                return render_to_response('registration/select_institution.html', {'form': form}, context_instance=RequestContext(request))
737
            if user.is_active:
738
               login(request, user)
739
               return HttpResponseRedirect(reverse("manage"))
740
            else:
741
                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") %user.username
742
                return render_to_response('error.html', {'error': error, 'inactive': True},
743
                                  context_instance=RequestContext(request))
744
        else:
745
            error = _("Something went wrong during user authentication. Contact your administrator %s" %user)
746
            return render_to_response('error.html', {'error': error,},
747
                                  context_instance=RequestContext(request))
748
    except Exception as e:
749
        error = _("Invalid login procedure %s" %e)
750
        return render_to_response('error.html', {'error': error,},
751
                                  context_instance=RequestContext(request))
752
        # Return an 'invalid login' error message.
753
#    return HttpResponseRedirect(reverse("user-routes"))
670 754

  
671 755
def geolocate(request):
672 756
    return render_to_response('front/geolocate.html',
673 757
                                  context_instance=RequestContext(request))
674 758

  
759
def selectinst(request):
760
    if request.method == 'POST':
761
        request_data = request.POST.copy()
762
        user = request_data['user']
763
        form = UserProfileForm(request_data)
764
        if form.is_valid():
765
            userprofile = form.save()
766
            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
767
            return render_to_response('error.html', {'error': error, 'inactive': True},
768
                                  context_instance=RequestContext(request))
769
        else:
770
            form.fields['user'] = forms.ModelChoiceField(queryset=User.objects.filter(pk=user.pk), empty_label=None)
771
            form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.all(), empty_label=None)
772
            return render_to_response('registration/select_institution.html', {'form': form}, context_instance=RequestContext(request))
773

  
675 774

  
676 775
def closest(request):
677 776
    if request.method == 'GET':

Also available in: Unified diff