from django.views.decorators.http import require_http_methods


from helios_auth.security import *
from heliosinstitution.models import InstitutionUserProfile


from view_utils import *

def home(request):
    pass

@login_required
def manage_users(request):  
    user = get_user(request)
    institution = user.institutionuserprofile_set.get().institution

    if request.method == "GET":
        return render_template(request, "manage_users", {
            "institution": institution,
        })


@login_required
@require_http_methods(["POST",])
def delegate_institution_admin(request, institution_id):
    """
    Delegate an user to administer institution
    """
    user = get_user(request)
    pass


@login_required
@require_http_methods([ "POST,"])
def revoke_institution_admin(request, institution_id):
    """
    Revoke an user as institution admin
    """
    user = get_user(request)
    pass


@login_required
@require_http_methods(["POST",])
def delegate_election_admin(request, institution_id):
    """
    Enable an user to administer elections
    """
    user = get_user(request)
    pass


@login_required
@require_http_methods(["POST",])
def revoke_election_admin(request, institution_id):
    """
    Revoke an user as elections admin
    """
    user = get_user(request)
    pass

from django.views.decorators.http import require_http_methods
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse


from helios_auth.security import *
from heliosinstitution.models import InstitutionUserProfile


from view_utils import *

def home(request):
    pass

@login_required
def manage_users(request):  
    user = get_user(request)
    institution = user.institutionuserprofile_set.get().institution

    if request.method == "GET":
        return render_template(request, "manage_users", {
            "institution": institution,
        })


@login_required
@require_http_methods(["POST",])
def delegate_institution_admin(request, institution_id):
    """
    Delegate an user to administer institution
    """
    user = get_user(request)
    email = request.POST.get('inst_mngt_email', '') 
    if user.institutionuserprofile_set.get().is_institution_admin:
        institution_user_profile, created = InstitutionUserProfile.objects.get_or_create(email=email, 
            institution=user.institutionuserprofile_set.get().institution)
        institution_user_profile.save()
        #TODO: add to institution admin group
    return HttpResponseRedirect(reverse(manage_users))
    


@login_required
@require_http_methods([ "POST,"])
def revoke_institution_admin(request, institution_id):
    """
    Revoke an user as institution admin
    """
    user = get_user(request)
    pass


@login_required
@require_http_methods(["POST",])
def delegate_election_admin(request, institution_id):
    """
    Enable an user to administer elections
    """
    user = get_user(request)
    pass


@login_required
@require_http_methods(["POST",])
def revoke_election_admin(request, institution_id):
    """
    Revoke an user as elections admin
    """
    user = get_user(request)
    pass

Implement delegate_institution_admin method