Esempio n. 1
0
File: views.py Progetto: xasetl/1327
def permissions(request, title):
	document = get_object_or_404(Document, url_title=title)
	content_type = ContentType.objects.get_for_model(document)
	check_permissions(document, request.user, [document.edit_permission_name])
	if not document.show_permissions_editor():
		raise PermissionDenied()
	PermissionForm = get_permission_form(document)
	PermissionFormset = formset_factory(get_permission_form(document), extra=0)

	initial_data = PermissionForm.prepare_initial_data(Group.objects.all(), content_type, document)
	formset = PermissionFormset(request.POST or None, initial=initial_data)
	if request.POST and formset.is_valid():
		for form in formset:
			form.save(document)
		messages.success(request, _("Permissions have been changed successfully."))

		if request.user.has_perm(document.edit_permission_name, document):
			return HttpResponseRedirect(reverse(document.get_permissions_url_name(), args=[document.url_title]))
		if request.user.has_perm(document.view_permission_name, document):
			return HttpResponseRedirect(reverse(document.get_view_url_name(), args=[document.url_title]))
		return HttpResponseRedirect(reverse('index'))

	return render(request, 'documents_permissions.html', {
		'document': document,
		'formset_header': PermissionForm.header(content_type),
		'formset': formset,
		'active_page': 'permissions',
		'permission_overview': document_permission_overview(request.user, document),
	})
Esempio n. 2
0
def permissions(request, title):
    document = get_object_or_404(Document, url_title=title)
    content_type = ContentType.objects.get_for_model(document)
    check_permissions(document, request.user, [document.edit_permission_name])
    if not document.show_permissions_editor():
        raise PermissionDenied()
    PermissionForm = get_permission_form(document)
    PermissionFormset = formset_factory(get_permission_form(document), extra=0)

    initial_data = PermissionForm.prepare_initial_data(Group.objects.all(),
                                                       content_type, document)
    formset = PermissionFormset(request.POST or None, initial=initial_data)
    if request.POST and formset.is_valid():
        for form in formset:
            form.save(document)
        messages.success(request,
                         _("Permissions have been changed successfully."))

        if request.user.has_perm(document.edit_permission_name, document):
            return HttpResponseRedirect(
                reverse(document.get_permissions_url_name(),
                        args=[document.url_title]))
        if request.user.has_perm(document.view_permission_name, document):
            return HttpResponseRedirect(
                reverse(document.get_view_url_name(),
                        args=[document.url_title]))
        return HttpResponseRedirect(reverse('index'))

    return render(
        request, 'documents_permissions.html', {
            'document':
            document,
            'formset_header':
            PermissionForm.header(content_type),
            'formset':
            formset,
            'active_page':
            'permissions',
            'permission_overview':
            document_permission_overview(request.user, document),
        })