<commit_before>import copy
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.utils.safestring import mark_safe
from django.views.decorators.http import require_POST
from corehq.apps.commtrack.views import BaseCommTrackManageView

from corehq.apps.domain.decorators import domain_admin_required, login_and_domain_required
from corehq.apps.hqwebapp.utils import get_bulk_upload_form
from corehq.apps.locations.models import Location
from corehq.apps.locations.forms import LocationForm
from corehq.apps.locations.schema import LocationType
from corehq.apps.locations.util import load_locs_json, location_hierarchy_config, dump_locations
from corehq.apps.commtrack.models import Product, SupplyPointCase
from corehq.apps.commtrack.util import unicode_slug
from corehq.apps.facilities.models import FacilityRegistry
from django.core.urlresolvers import reverse
from django.shortcuts import render
from django.contrib import messages
from couchdbkit import ResourceNotFound
import urllib
import json

from django.utils.translation import ugettext as _, ugettext_noop
from dimagi.utils.decorators.memoized import memoized
from dimagi.utils.web import json_response
from custom.openlmis.tasks import bootstrap_domain_task
from soil.util import expose_download, get_download_context
from corehq.apps.commtrack.tasks import import_locations_async
from couchexport.models import Format
from corehq.apps.consumption.shortcuts import get_default_monthly_consumption


@domain_admin_required
def default(request, domain):
    return HttpResponseRedirect(reverse(LocationsListView.urlname, args=[domain]))


class BaseLocationView(BaseCommTrackManageView):

    @property
    def main_context(self):
        context = super(BaseLocationView, self).main_context
        context.update({
            'hierarchy': location_hierarchy_config(self.domain),
            'api_root': reverse('api_dispatch_list', kwargs={'domain': self.domain,
                                                             'resource_name': 'location',
                                                             'api_name': 'v0.3'}),
        })
        return context


class LocationsListView(BaseLocationView):
    urlname = 'manage_locations'
    page_title = ugettext_noop("Locations")
    template_name = 'locations/manage/locations.html'

    @property
    def show_inactive(self):
        return json.loads(self.request.GET.get('show_inactive', 'false'))

    @property
    def page_context(self):
        selected_id = self.request.GET.get('selected')
        return {
            'selected_id': selected_id,
            'locations': load_locs_json(
                self.domain, selected_id, self.show_inactive
            ),
            'show_inactive': self.show_inactive,
        }


class LocationSettingsView(BaseCommTrackManageView):
    urlname = 'location_settings'
    page_title = ugettext_noop("Location Types")
    template_name = 'locations/settings.html'

    @property
    def page_context(self):
        return {
            'settings': self.settings_context,
        }

    @property
    def settings_context(self):
        return {
            'loc_types': [self._get_loctype_info(l) for l in self.domain_object.commtrack_settings.location_types],
        }

    def _get_loctype_info(self, loctype):
        return {
            'name': loctype.name,
            'code': loctype.code,
            'allowed_parents': [p or None for p in loctype.allowed_parents],
            'administrative': loctype.administrative,
        }

    def post(self, request, *args, **kwargs):
        payload = json.loads(request.POST.get('json'))

        def mk_loctype(loctype):
            loctype['allowed_parents'] = [p or '' for p in loctype['allowed_parents']]
            cleaned_code = unicode_slug(loctype['code'])
            if cleaned_code != loctype['code']:
                err = _(
                    'Location type code "{code}" is invalid. No spaces or special characters are allowed. '
                    'It has been replaced with "{new_code}".'
                )
                messages.warning(request, err.format(code=loctype['code'], new_code=cleaned_code))
                loctype['code'] = cleaned_code
            return LocationType(**loctype)

        #TODO add server-side input validation here (currently validated on client)

        self.domain_object.commtrack_settings.location_types = [mk_loctype(l) for l in payload['loc_types']]

        self.domain_object.commtrack_settings.save()

        return self.get(request, *args, **kwargs)


class NewLocationView(BaseLocationView):
    urlname = 'create_location'
    page_title = ugettext_noop("New Location")
    template_name = 'locations/manage/location.html'

    @property
    def parent_pages(self):
        return [{
            'title': LocationsListView.page_title,
            'url': reverse(LocationsListView.urlname, args=[self.domain]),
        }]

    @property
    def parent_id(self):<commit_msg>Get location type list working<commit_after>import copy
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.utils.safestring import mark_safe
from django.views.decorators.http import require_POST
from corehq.apps.commtrack.views import BaseCommTrackManageView

from corehq.apps.domain.decorators import domain_admin_required, login_and_domain_required
from corehq.apps.hqwebapp.utils import get_bulk_upload_form
from corehq.apps.locations.models import Location
from corehq.apps.locations.forms import LocationForm
from corehq.apps.locations.schema import LocationType
from corehq.apps.locations.util import load_locs_json, location_hierarchy_config, dump_locations
from corehq.apps.commtrack.models import Product, SupplyPointCase
from corehq.apps.commtrack.util import unicode_slug
from corehq.apps.facilities.models import FacilityRegistry
from django.core.urlresolvers import reverse
from django.shortcuts import render
from django.contrib import messages
from couchdbkit import ResourceNotFound
import urllib
import json

from django.utils.translation import ugettext as _, ugettext_noop
from dimagi.utils.decorators.memoized import memoized
from dimagi.utils.web import json_response
from custom.openlmis.tasks import bootstrap_domain_task
from soil.util import expose_download, get_download_context
from corehq.apps.commtrack.tasks import import_locations_async
from couchexport.models import Format
from corehq.apps.consumption.shortcuts import get_default_monthly_consumption


@domain_admin_required
def default(request, domain):
    return HttpResponseRedirect(reverse(LocationsListView.urlname, args=[domain]))


class BaseLocationView(BaseCommTrackManageView):

    @property
    def main_context(self):
        context = super(BaseLocationView, self).main_context
        context.update({
            'hierarchy': location_hierarchy_config(self.domain),
            'api_root': reverse('api_dispatch_list', kwargs={'domain': self.domain,
                                                             'resource_name': 'location',
                                                             'api_name': 'v0.3'}),
        })
        return context


class LocationsListView(BaseLocationView):
    urlname = 'manage_locations'
    page_title = ugettext_noop("Locations")
    template_name = 'locations/manage/locations.html'

    @property
    def show_inactive(self):
        return json.loads(self.request.GET.get('show_inactive', 'false'))

    @property
    def page_context(self):
        selected_id = self.request.GET.get('selected')
        return {
            'selected_id': selected_id,
            'locations': load_locs_json(
                self.domain, selected_id, self.show_inactive
            ),
            'show_inactive': self.show_inactive,
        }


class LocationSettingsView(BaseCommTrackManageView):
    urlname = 'location_settings'
    page_title = ugettext_noop("Location Types")
    template_name = 'locations/settings.html'

    @property
    def page_context(self):
        return {
            'settings': self.settings_context,
        }

    @property
    def settings_context(self):
        return {
            'loc_types': [self._get_loctype_info(l) for l in self.domain_object.location_types],
        }

    def _get_loctype_info(self, loctype):
        return {
            'name': loctype.name,
            'code': loctype.code,
            'allowed_parents': [p or None for p in loctype.allowed_parents],
            'administrative': loctype.administrative,
        }

    def post(self, request, *args, **kwargs):
        payload = json.loads(request.POST.get('json'))

        def mk_loctype(loctype):
            loctype['allowed_parents'] = [p or '' for p in loctype['allowed_parents']]
            cleaned_code = unicode_slug(loctype['code'])
            if cleaned_code != loctype['code']:
                err = _(
                    'Location type code "{code}" is invalid. No spaces or special characters are allowed. '
                    'It has been replaced with "{new_code}".'
                )
                messages.warning(request, err.format(code=loctype['code'], new_code=cleaned_code))
                loctype['code'] = cleaned_code
            return LocationType(**loctype)

        #TODO add server-side input validation here (currently validated on client)

        self.domain_object.location_types = [mk_loctype(l) for l in payload['loc_types']]

        self.domain_object.save()

        return self.get(request, *args, **kwargs)


class NewLocationView(BaseLocationView):
    urlname = 'create_location'
    page_title = ugettext_noop("New Location")
    template_name = 'locations/manage/location.html'

    @property
    def parent_pages(self):
        return [{
            'title': LocationsListView.page_title,
            'url': reverse(LocationsListView.urlname, args=[self.domain]),
        }]

    @property
    def parent_id(self):
{'fraction_non_alphanumeric': 0.08810528430781596, 'fraction_numerical': 0.0010046212577858146, 'mean_word_length': 3.103462489694971, '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': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6934801', 'n_tokens_mistral': 2850, 'n_tokens_neox': 2705, 'n_words': 613}