def network_relationships_advertiser_assign(request, user_id):
    ''' View to assign Advertisers to a Network Admin '''
    from atrinsic.base.models import User, Organization
    from forms import NetworkAdvertiserAssignForm
    admin = get_object_or_404(User, id=user_id)

    if request.method == 'POST':
        form = NetworkAdvertiserAssignForm(request.POST)

        if form.is_valid():
            for o in admin.get_profile().admin_assigned_organizations.filter(
                    org_type=ORGTYPE_ADVERTISER):
                admin.get_profile().admin_assigned_organizations.remove(o)

            for id in form.cleaned_data['advertiser']:
                try:
                    org = Organization.objects.get(id=id)
                    admin.get_profile().admin_assigned_organizations.add(org)
                except User.DoesNotExist:
                    pass

            return HttpResponseRedirect('/network/relationships/advertiser/')

    else:
        form = NetworkAdvertiserAssignForm(
            initial={
                'advertiser': [
                    o.id
                    for o in admin.get_profile().admin_assigned_organizations.
                    filter(org_type=ORGTYPE_ADVERTISER)
                ],
            })

    return AQ_render_to_response(request,
                                 'network/advertiser-assign.html', {
                                     'form': form,
                                     'admin': admin,
                                 },
                                 context_instance=RequestContext(request))
def network_relationships_publisher_assign(request, publisher_id):
    """ View to assign Publishers to Network Admins """

    publisher = get_object_or_404(Organization, id=publisher_id)

    if request.method == "POST":
        form = NetworkAdvertiserAssignForm(request.POST)

        if form.is_valid():
            publisher.assigned_admins.clear()
            for uid in form.cleaned_data["user"]:
                try:
                    u = User.objects.get(id=uid)
                    # XXX
                    request.user.get_profile().admin_assigned_organizations.add(publisher)
                except User.DoesNotExist:
                    pass

        return HttpResponseRedirect("/network/relationships/publisher/")

    else:
        # XXX: initial =
        form = NetworkAdvertiserAssignForm(
            initial={
                "users": [
                    u.id
                    for u in request.user.get_profile()
                    .admin_assigned_organizations.all()
                    .filter(org_type=ORGTYPE_PUBLISHER)
                ]
            }
        )

    return AQ_render_to_response(
        request,
        "network/publisher-assign.html",
        {"form": form, "publisher": publisher},
        context_instance=RequestContext(request),
    )
def network_relationships_publisher_assign(request, publisher_id):
    ''' View to assign Publishers to Network Admins '''

    publisher = get_object_or_404(Organization, id=publisher_id)

    if request.method == 'POST':
        form = NetworkAdvertiserAssignForm(request.POST)

        if form.is_valid():
            publisher.assigned_admins.clear()
            for uid in form.cleaned_data['user']:
                try:
                    u = User.objects.get(id=uid)
                    # XXX
                    request.user.get_profile(
                    ).admin_assigned_organizations.add(publisher)
                except User.DoesNotExist:
                    pass

        return HttpResponseRedirect('/network/relationships/publisher/')

    else:
        # XXX: initial =
        form = NetworkAdvertiserAssignForm(
            initial={
                'users': [
                    u.id for u in request.user.get_profile().
                    admin_assigned_organizations.all().filter(
                        org_type=ORGTYPE_PUBLISHER)
                ],
            })

    return AQ_render_to_response(request,
                                 'network/publisher-assign.html', {
                                     'form': form,
                                     'publisher': publisher,
                                 },
                                 context_instance=RequestContext(request))
def network_relationships_advertiser_assign(request, user_id):
    """ View to assign Advertisers to a Network Admin """
    from atrinsic.base.models import User, Organization
    from forms import NetworkAdvertiserAssignForm

    admin = get_object_or_404(User, id=user_id)

    if request.method == "POST":
        form = NetworkAdvertiserAssignForm(request.POST)

        if form.is_valid():
            for o in admin.get_profile().admin_assigned_organizations.filter(org_type=ORGTYPE_ADVERTISER):
                admin.get_profile().admin_assigned_organizations.remove(o)

            for id in form.cleaned_data["advertiser"]:
                try:
                    org = Organization.objects.get(id=id)
                    admin.get_profile().admin_assigned_organizations.add(org)
                except User.DoesNotExist:
                    pass

            return HttpResponseRedirect("/network/relationships/advertiser/")

    else:
        form = NetworkAdvertiserAssignForm(
            initial={
                "advertiser": [
                    o.id for o in admin.get_profile().admin_assigned_organizations.filter(org_type=ORGTYPE_ADVERTISER)
                ]
            }
        )

    return AQ_render_to_response(
        request,
        "network/advertiser-assign.html",
        {"form": form, "admin": admin},
        context_instance=RequestContext(request),
    )