Ejemplo n.º 1
0
def group_add_edit(request, url=None):
    """
    Add or edit a group.  (If a url is passed in, we're editing.)
    """

    profile = request.user.userprofile
    is_manager = request.user.userprofile.is_manager

    if url:
        # Get the group to edit
        group = get_object_or_404(Group, url=url)
        # Only a group curator or an admin is allowed to edit a group
        is_curator = profile == group.curator
        if not (is_curator or is_manager):
            messages.error(request, _('You must be a curator or an admin to edit a group'))
            return redirect(reverse('groups:show_group', args=[group.url]))
    else:
        group = Group(curator=profile)

    form_class = SuperuserGroupForm if is_manager else GroupForm

    form = form_class(request.POST or None, instance=group)
    if form.is_valid():
        group = form.save()
        # Ensure curator is in the group when it's created
        if profile == group.curator and not group.has_member(profile):
            group.add_member(profile)
        return redirect(reverse('groups:show_group', args=[group.url]))

    context = {
        'form': form,
        'creating': url is None,
        'group': group if url else None,
    }
    return render(request, 'groups/add_edit.html', context)
Ejemplo n.º 2
0
def group_add_edit(request, url=None):
    """
    Add or edit a group.  (If a url is passed in, we're editing.)
    """

    profile = request.user.userprofile
    is_manager = request.user.userprofile.is_manager

    if url:
        # Get the group to edit
        group = get_object_or_404(Group, url=url)
        # Only a group curator or an admin is allowed to edit a group
        is_curator = profile in group.curators.all()
        if not (is_curator or is_manager):
            messages.error(
                request,
                _('You must be a curator or an admin to edit a group'))
            return redirect(reverse('groups:show_group', args=[group.url]))
    else:
        group = Group()

    form_class = SuperuserGroupForm if is_manager else GroupForm

    # Add the creator of a group as curator
    curators_ids = [profile.id]
    # If we are editing add the existing curators. If the group has no curator in edit
    # mode, append an empty list
    if url:
        curators_ids = group.curators.all().values_list('id', flat=True)

    form = form_class(request.POST or None,
                      instance=group,
                      initial={'curators': curators_ids})

    if form.is_valid():
        group = form.save()

        return redirect(reverse('groups:show_group', args=[group.url]))

    context = {
        'form': form,
        'creating': url is None,
        'group': group if url else None
    }
    return render(request, 'groups/add_edit.html', context)