Ejemplo n.º 1
0
def org_member(request, org_id):
    org = get_object_or_404(Organization, id=org_id)
    if not (not org.locked or request.user.is_staff or org.can_edit(request.user)):
        messages.add_message(request, messages.ERROR, "You do not have permission to add yourself to this organization")
        return HttpResponseRedirect(reverse('member:profile:view', kwargs={'username': request.username}))

    # We require a POST and we require an action
    if not request.method == "POST" or 'action' not in request.POST:
        return HttpResponseForbidden("Forbidden")
    action = request.POST['action']

    full_name = None
    org_member = None
    new_user = None
    new_username = request.POST.get('username', None)
    if new_username:
        new_user = User.objects.get(username=new_username)
        full_name = new_user.get_full_name()
        # print("user: %s" % full_name)
    member_id = request.POST.get('member_id', None)
    if member_id:
        org_member = org.organizationmember_set.get(id=member_id)
        full_name = org_member.user.get_full_name()
        # print("member: %s" % full_name)

    try:
        # form = OrganizationMemberForm()
        if 'edit' == action:
            form = OrganizationMemberForm(instance=org_member)
        if 'add' == action:
            initial_data = {'username': new_username,
                            'start_date': timezone.now()
                            }

            form = OrganizationMemberForm(initial=initial_data)
        if 'save' == action:
            form = OrganizationMemberForm(request.POST, request.FILES)
            if form.is_valid():
                form.save()
                return HttpResponseRedirect(reverse('member:org:view', kwargs={'org_id': org.id}))
            else:
                print(form)
    except Exception as e:
        messages.add_message(request, messages.ERROR, "Could not save: %s" % str(e))

    context = {'organization': org,
               'member': org_member,
               'username': new_username,
               'full_name': full_name,
               'form': form,
               'action': action,
               }
    return render(request, 'member/organization/org_member.html', context)
Ejemplo n.º 2
0
def org_member(request, org_id):
    org = get_object_or_404(Organization, id=org_id)
    if not (not org.locked or request.user.is_staff
            or org.can_edit(request.user)):
        messages.add_message(
            request, messages.ERROR,
            "You do not have permission to add yourself to this organization")
        return HttpResponseRedirect(
            reverse('member:profile:view',
                    kwargs={'username': request.username}))

    # We require a POST and we require an action
    if not request.method == "POST" or 'action' not in request.POST:
        return HttpResponseForbidden("Forbidden")
    action = request.POST['action']

    full_name = None
    org_member = None
    new_user = None
    new_username = request.POST.get('username', None)
    if new_username:
        new_user = User.objects.get(username=new_username)
        full_name = new_user.get_full_name()
        # print("user: %s" % full_name)
    member_id = request.POST.get('member_id', None)
    if member_id:
        org_member = org.organizationmember_set.get(id=member_id)
        full_name = org_member.user.get_full_name()
        # print("member: %s" % full_name)

    try:
        # form = OrganizationMemberForm()
        if 'edit' == action:
            form = OrganizationMemberForm(instance=org_member)
        if 'add' == action:
            initial_data = {
                'username': new_username,
                'start_date': timezone.now()
            }

            form = OrganizationMemberForm(initial=initial_data)
        if 'save' == action:
            form = OrganizationMemberForm(request.POST, request.FILES)
            if form.is_valid():
                form.save()
                return HttpResponseRedirect(
                    reverse('member:org:view', kwargs={'org_id': org.id}))
            else:
                print(form)
    except Exception as e:
        messages.add_message(request, messages.ERROR,
                             "Could not save: %s" % str(e))

    context = {
        'organization': org,
        'member': org_member,
        'username': new_username,
        'full_name': full_name,
        'form': form,
        'action': action,
    }
    return render(request, 'member/organization/org_member.html', context)