<commit_before>from couchdbkit import ResourceNotFound
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import render
from django.utils.translation import ugettext as _
from corehq.apps.userreports.reports.view import ConfigurableReport
from corehq.apps.userreports.models import ReportConfiguration
from corehq.apps.domain.decorators import domain_admin_required
from corehq.apps.userreports.reports.factory import ReportFactory
from corehq.apps.userreports.ui.forms import ConfigurableReportEditForm


@domain_admin_required
def edit_report(request, domain, report_id):
    try:
        config = ReportConfiguration.get(report_id)
    except ResourceNotFound:
        raise Http404()

    if request.method == 'POST':
        form = ConfigurableReportEditForm(domain, config, request.POST)
        if form.is_valid():
            for attr in (
                'config_id',
                'display_name',
                'description',
                'aggregation_columns',
                'filters',
                'columns'
            ):
                setattr(config, attr, form.cleaned_data[attr])
            try:
                ReportFactory.from_spec(config)
            except Exception, e:
                messages.error(request, _(u'Problem with report spec: {}').format(e))
            else:
                config.save()
                messages.success(request, _(u'Report {} saved!').format(config.display_name))
                return HttpResponseRedirect(reverse(ConfigurableReport.slug, args=[domain, config._id]))
    else:
        form = ConfigurableReportEditForm(domain, config)
    return render(request, "userreports/edit_report_config.html", {
        'domain': domain,
        'form': form,
        'reports': ReportConfiguration.by_domain(domain)
    })<commit_msg>more checks for editing conditions/permissions<commit_after>from couchdbkit import ResourceNotFound
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import render
from django.utils.translation import ugettext as _
from jsonobject.exceptions import WrappingAttributeError
from corehq.apps.userreports.reports.view import ConfigurableReport
from corehq.apps.userreports.models import ReportConfiguration, IndicatorConfiguration
from corehq.apps.domain.decorators import domain_admin_required
from corehq.apps.userreports.reports.factory import ReportFactory
from corehq.apps.userreports.ui.forms import ConfigurableReportEditForm


@domain_admin_required
def edit_report(request, domain, report_id):
    try:
        config = ReportConfiguration.get(report_id)
        assert config.domain == domain
        assert config.doc_type == 'ReportConfiguration'
    except (ResourceNotFound, WrappingAttributeError, AssertionError):
        raise Http404()

    if request.method == 'POST':
        form = ConfigurableReportEditForm(domain, config, request.POST)
        if form.is_valid():
            for attr in (
                'config_id',
                'display_name',
                'description',
                'aggregation_columns',
                'filters',
                'columns'
            ):
                setattr(config, attr, form.cleaned_data[attr])
            try:
                ReportFactory.from_spec(config)
            except Exception, e:
                messages.error(request, _(u'Problem with report spec: {}').format(e))
            else:
                config.save()
                messages.success(request, _(u'Report {} saved!').format(config.display_name))
                return HttpResponseRedirect(reverse(ConfigurableReport.slug, args=[domain, config._id]))
    else:
        form = ConfigurableReportEditForm(domain, config)
    context = _shared_context(domain)
    context.update({
        'domain': domain,
        'form': form,
    })
    return render(request, "userreports/edit_report_config.html", context)


def _shared_context(domain):
    return {
        'reports': ReportConfiguration.by_domain(domain),
        'data_sources': IndicatorConfiguration.by_domain(domain),
    }
{'fraction_non_alphanumeric': 0.08572774375883184, 'fraction_numerical': 0.002826189354686764, 'mean_word_length': 2.8927589367552704, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6179158', 'n_tokens_mistral': 1118, 'n_tokens_neox': 1070, 'n_words': 250}