<commit_before>from functools import wraps
import json
import re
from smtplib import SMTPRecipientsRefused
import urllib
from datetime import datetime
import csv
import io
from corehq.apps.registration.forms import NewWebUserRegistrationForm
from corehq.apps.registration.utils import activate_new_user
from corehq.apps.users.util import format_username, normalize_username, raw_username
from dimagi.utils.decorators.view import get_file
from dimagi.utils.excel import Excel2007DictReader
from django.contrib.auth import logout
from django.contrib.auth.forms import PasswordChangeForm, SetPasswordForm
from django.contrib.auth.models import User
from django.contrib.auth.views import redirect_to_login
from django.core.urlresolvers import reverse
from django.db import transaction
from django.http import Http404, HttpResponseRedirect, HttpResponse, HttpResponseForbidden, HttpResponseBadRequest
from django.template.loader import render_to_string
from django.utils.decorators import method_decorator
from django.views.decorators.http import require_POST
from corehq.apps.registration.user_registration_backend import register_user
from corehq.apps.registration.user_registration_backend.forms import AdminRegistersUserForm,\
    AdminInvitesUserForm
from corehq.apps.prescriptions.models import Prescription
from corehq.apps.sms.views import get_sms_autocomplete_context
from corehq.apps.domain.models import Domain
from corehq.apps.users.decorators import require_permission
from corehq.apps.users.forms import UserForm, CommCareAccountForm, ProjectSettingsForm
from corehq.apps.users.models import CouchUser, Invitation, CommCareUser, WebUser, RemoveWebUserRecord, Permissions
from corehq.apps.groups.models import Group
from corehq.apps.domain.decorators import login_and_domain_required, require_superuser
from dimagi.utils.web import render_to_response, json_response
import calendar
from corehq.apps.reports.schedule.config import ScheduledReportFactory
from corehq.apps.reports.models import WeeklyReportNotification, DailyReportNotification, ReportNotification
from django.contrib import messages
from corehq.apps.reports.tasks import send_report
from django.views.generic.base import TemplateView
from django_digest.decorators import httpdigest


def require_permission_to_edit_user(view_func):
    @wraps(view_func)
    def _inner(request, domain, couch_user_id, *args, **kwargs):
        go_ahead = False
        if hasattr(request, "couch_user"):
            user = request.couch_user
            if user.is_superuser or user.user_id == couch_user_id or user.is_domain_admin():
                go_ahead = True
            else:
                couch_user = CouchUser.get_by_user_id(couch_user_id)
                if couch_user.is_commcare_user() and request.couch_user.can_edit_commcare_users():
                    go_ahead = True
                elif couch_user.is_web_user() and request.couch_user.can_edit_web_users():
                    go_ahead = True
        if go_ahead:
            return login_and_domain_required(view_func)(request, domain, couch_user_id, *args, **kwargs)
        else:
            raise Http404()
    return _inner

require_can_edit_web_users = require_permission(Permissions.EDIT_WEB_USERS)
require_can_edit_commcare_users = require_permission(Permissions.EDIT_COMMCARE_USERS)

def _users_context(request, domain):
    couch_user = request.couch_user
    web_users = WebUser.by_domain(domain)

    for user in [couch_user] + list(web_users):
        user.current_domain = domain

    return {
        'web_users': web_users,
        'domain': domain,
        'couch_user': couch_user,
    }

@login_and_domain_required
def users(request, domain):
    response = reverse("user_account", args=[domain, request.couch_user._id])
    if request.couch_user:
        user = WebUser.get_by_user_id(request.couch_user._id, domain)
        if user and user.has_permission(domain, Permissions.EDIT_WEB_USERS):
            response = reverse("web_users", args=[domain])
        elif user and user.has_permission(domain, Permissions.EDIT_COMMCARE_USERS):
            response = reverse("commcare_users", args=[domain])
    return HttpResponseRedirect(response)

@require_can_edit_web_users
def web_users(request, domain, template="users/web_users.html"):
    context = _users_context(request, domain)
    return render_to_response(request, template, context)

@require_can_edit_web_users
@require_POST
def remove_web_user(request, domain, couch_user_id):
    user = WebUser.get_by_user_id(couch_user_id, domain)<commit_msg>try catching attempt to grab user object...a fail defaults to the normal account settings page<commit_after>from functools import wraps
import json
import re
from smtplib import SMTPRecipientsRefused
import urllib
from datetime import datetime
import csv
import io
import logging
from corehq.apps.registration.forms import NewWebUserRegistrationForm
from corehq.apps.registration.utils import activate_new_user
from corehq.apps.users.util import format_username, normalize_username, raw_username
from dimagi.utils.decorators.view import get_file
from dimagi.utils.excel import Excel2007DictReader
from django.contrib.auth import logout
from django.contrib.auth.forms import PasswordChangeForm, SetPasswordForm
from django.contrib.auth.models import User
from django.contrib.auth.views import redirect_to_login
from django.core.urlresolvers import reverse
from django.db import transaction
from django.http import Http404, HttpResponseRedirect, HttpResponse, HttpResponseForbidden, HttpResponseBadRequest
from django.template.loader import render_to_string
from django.utils.decorators import method_decorator
from django.views.decorators.http import require_POST
from corehq.apps.registration.user_registration_backend import register_user
from corehq.apps.registration.user_registration_backend.forms import AdminRegistersUserForm,\
    AdminInvitesUserForm
from corehq.apps.prescriptions.models import Prescription
from corehq.apps.sms.views import get_sms_autocomplete_context
from corehq.apps.domain.models import Domain
from corehq.apps.users.decorators import require_permission
from corehq.apps.users.forms import UserForm, CommCareAccountForm, ProjectSettingsForm
from corehq.apps.users.models import CouchUser, Invitation, CommCareUser, WebUser, RemoveWebUserRecord, Permissions
from corehq.apps.groups.models import Group
from corehq.apps.domain.decorators import login_and_domain_required, require_superuser
from dimagi.utils.web import render_to_response, json_response
import calendar
from corehq.apps.reports.schedule.config import ScheduledReportFactory
from corehq.apps.reports.models import WeeklyReportNotification, DailyReportNotification, ReportNotification
from django.contrib import messages
from corehq.apps.reports.tasks import send_report
from django.views.generic.base import TemplateView
from django_digest.decorators import httpdigest


def require_permission_to_edit_user(view_func):
    @wraps(view_func)
    def _inner(request, domain, couch_user_id, *args, **kwargs):
        go_ahead = False
        if hasattr(request, "couch_user"):
            user = request.couch_user
            if user.is_superuser or user.user_id == couch_user_id or user.is_domain_admin():
                go_ahead = True
            else:
                couch_user = CouchUser.get_by_user_id(couch_user_id)
                if couch_user.is_commcare_user() and request.couch_user.can_edit_commcare_users():
                    go_ahead = True
                elif couch_user.is_web_user() and request.couch_user.can_edit_web_users():
                    go_ahead = True
        if go_ahead:
            return login_and_domain_required(view_func)(request, domain, couch_user_id, *args, **kwargs)
        else:
            raise Http404()
    return _inner

require_can_edit_web_users = require_permission(Permissions.EDIT_WEB_USERS)
require_can_edit_commcare_users = require_permission(Permissions.EDIT_COMMCARE_USERS)

def _users_context(request, domain):
    couch_user = request.couch_user
    web_users = WebUser.by_domain(domain)

    for user in [couch_user] + list(web_users):
        user.current_domain = domain

    return {
        'web_users': web_users,
        'domain': domain,
        'couch_user': couch_user,
    }

@login_and_domain_required
def users(request, domain):
    response = reverse("user_account", args=[domain, request.couch_user._id])
    if request.couch_user:
        try:
            user = WebUser.get_by_user_id(request.couch_user._id, domain)
            if user and user.has_permission(domain, Permissions.EDIT_WEB_USERS):
                response = reverse("web_users", args=[domain])
            elif user and user.has_permission(domain, Permissions.EDIT_COMMCARE_USERS):
                response = reverse("commcare_users", args=[domain])
        except Exception as e:
            logging.exception("Failed to grab user object: %s", e)
    return HttpResponseRedirect(response)

@require_can_edit_web_users
def web_users(request, domain, template="users/web_users.html"):
    context = _users_context(request, domain)
    return render_to_response(request, template, context)

@require_can_edit_web_users
@require_POST
def remove_web_user(request, domain, couch_user_id):
    user = WebUser.get_by_user_id(couch_user_id, domain)
{'fraction_non_alphanumeric': 0.06738082485270487, 'fraction_numerical': 0.0021424745581146223, 'mean_word_length': 4.689213893967093, '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': '3797767', 'n_tokens_mistral': 2734, 'n_tokens_neox': 2668, 'n_words': 629}