Exemplo n.º 1
0
def group_edit(request, id):

    instance = get_object_or_404(Group, id=id)
    form = GroupEditForm(request.POST or None, instance=instance)

    if request.method == 'POST':
        if form.is_valid():
            form.save()
            return redirect('students.views.index')

    return TemplateResponse(request, 'group_add.html', {'form': form})
Exemplo n.º 2
0
def group_edit(request, id):
	
	instance = get_object_or_404(Group, id=id)
	form = GroupEditForm(request.POST or None, instance=instance)
	
	if request.method == 'POST':
		if form.is_valid():
			form.save()
			return redirect('students.views.index')
	
	return TemplateResponse(request, 'group_add.html', {'form': form})
Exemplo n.º 3
0
def group(action):
    """group
    """
    if action == 'add':
        form = GroupAddForm()
        if form.validate_on_submit():
            name = form.data['name']
            description = form.data['description']
            if GroupCtl.add(current_user.id, name, description):
                flash(u'操作成功', 'success')
            else:
                flash(u'操作失败', 'error')
        kwargs = {'form': form}
        return render_template('customer_group_add.html', **kwargs)
    if action == 'edit':
        group_id = request.args.get('group_id', '')
        group = GroupCtl.get(group_id)
        form = GroupEditForm()
        if group:
            form.group_id.data = group.id
            form.name.data = group.name
            form.description.data = group.description
        kwargs = {'form': form}
        return render_template('customer_group_add.html', **kwargs)
    if action == 'delete':
        group_id = request.args.get('group_id', '')
        group = GroupCtl.delete(group_id)
        if group:
            flash(u'操作成功', 'success')
        else:
            flash(u'操作失败', 'error')
        return redirect(url_for('Common.group', action='list'))
    if action == 'list':
        groups = GroupCtl.get_all()
        owner_dict = {}
        for i in groups:
            try:
                owner_dict[i.owner] = AdminCtl.get(i.owner).name
            except:
                # 如果用户被删除,则AdminCtl.get可能引发异常
                pass
        kwargs = {'groups': groups, 'owner_dict': owner_dict}

        return render_template('customer_group.html', **kwargs)
    return render_template("404.html")
Exemplo n.º 4
0
def group_edit(request, group_id):
    logger.debug("group_edit called by user %s for group id %s" % (request.user, group_id))
    exgroup = get_object_or_404(ExtendedGroup, id=group_id)
    if exgroup.owner == request.user:
        choices = [(None, 'None'),]
        for g in ExtendedGroup.objects.all().exclude(id=exgroup.id).exclude(parent=exgroup):
            logger.debug(g)
            if g.owner == request.user or request.user in g.admins.all():
                choices.append((g.id, str(g)))
        if request.method == 'POST':
            form = GroupEditForm(request.POST)
            form.fields['parent'].choices = choices
            logger.debug("Received POST request containing form, is valid: %s" % form.is_valid())
            if form.is_valid():
                desc = form.cleaned_data['description']
                hidden = form.cleaned_data['hidden']
                app = form.cleaned_data['applications']
                parent_id = form.cleaned_data['parent']
                if parent_id:
                    parent = get_object_or_404(ExtendedGroup, id=parent_id)
                else:
                    parent = None
                update_fields=[]
                if exgroup.description != desc:
                    exgroup.description = desc
                    update_fields.append('description')
                if exgroup.hidden != hidden:
                    exgroup.hidden = hidden
                    update_fields.append('hidden')
                if exgroup.require_application != app:
                    exgroup.require_application = app
                    update_fields.append('require_application')
                if exgroup.parent != parent:
                    exgroup.parent = parent
                    update_fields.append('parent')
                if update_fields:
                    logger.info("User %s updating group %s fields %s" % (request.user, exgroup, update_fields))
                    exgroup.save()
                else:
                    logger.debug("Detected no changes between group %s and supplied form." % exgroup)
                return redirect('groupmanagement_group_list_management')
        else:
            parent = None
            if exgroup.parent:
                parent = exgroup.parent.id
            data = {
                'description': exgroup.description,
                'hidden': exgroup.hidden,
                'parent': parent,
                'applications': exgroup.require_application,
            }
            form = GroupEditForm(initial=data)
            form.fields['parent'].choices = choices
        context = {
            'form': form,
            'title': 'Edit Group',
            'button_text': 'Edit',
        }
        return render(request, 'public/form.html', context=context)
    else:
        logger.warn("User %s not eligible to edit group %s" % (request.user, exgroup))
        return redirect('groupmanagement_group_list_management')