@require_objs_permissions_for_view(
    perm_names=["can_add_members"],
    permittee_func=get_user_from_req,
    target_func=get_queryset(Project, "proj_id"),
)
def add_member(request, proj_id):
    """Add a member to the project"""
    
    project = get_object_or_404(Project, id=proj_id)
    
    if request.method == "POST":
        form = AddMemberForm(project=project, giver=request.user, data=request.POST)
        if form.is_valid():
            user = User.objects.get(id = request.POST['user'] )
            form.save()
            try:
                #Sync LDAP
                project.save()
            except:
                logger.warning("User '%s' may have not been added to project '%s'. It could be a bug within LDAP." % (user.username, project.name))
                DatedMessage.objects.post_message_to_user(
                "User '%s' may not own the requested permissions. It could be a bug within LDAP." % user.username,
                request.user, msg_type=DatedMessage.TYPE_ERROR)
                return HttpResponseRedirect(reverse("project_detail", args=[proj_id]))
            #Send mail notification to the user
            roles = ', '.join(repr(role.encode('ascii')) for role in ProjectRole.objects.filter( id__in = request.POST.getlist('roles')).values_list('name', flat=True))
            #XXX: Not sure about this...  maybe  give_permission_to...
            for aggregate in project._get_aggregates():
                if not has_permission(user, aggregate, "can_use_aggregate"):
                    aggregate.add_to_user(user,"/")
            try:
                # Get project detail URL to send via e-mail
                from expedient.clearinghouse.project import urls
                project_detail_url = reverse("project_detail", args=[project.id]) or "/"
                # No "https://" check should be needed if settings are OK
                site_domain_url = "https://" + Site.objects.get_current().domain + project_detail_url
                send_mail(
                         settings.EMAIL_SUBJECT_PREFIX + "Project %s membership notification" % (project.name),
                         "You have been added to project '%s' as a user with the following roles: %s.\nYou may start experimenting now by going to %s\n\n" % (project.name, roles, site_domain_url),
                         from_email=settings.DEFAULT_FROM_EMAIL,
                         recipient_list=[user.email],
                 )
            except Exception as e:
                print "[WARNING] User e-mail notification could not be sent. Details: %s" % str(e)
            
            return HttpResponseRedirect(reverse("project_detail", args=[proj_id]))

    else:
        form = AddMemberForm(project=project, giver=request.user)
    
    return simple.direct_to_template(
        request,
        template=TEMPLATE_PATH+"/add_member.html",
        extra_context={
            "form": form,
            "project": project,
            "breadcrumbs": (
                ("Home", reverse("home")),
                ("Project %s" % project.name, reverse("project_detail", args=[project.id])),
                ("Add Member", request.path),
            ),
        },
    )
dataset/ETHPy150Open fp7-ofelia/ocf/expedient/src/python/expedient/clearinghouse/project/views.py/add_member/original