<s> import HTMLParser <EOL> import json <EOL> from xml . etree import ElementTree <EOL> from django . conf import settings <EOL> from django . contrib import messages <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponseRedirect , HttpResponse , HttpResponseBadRequest , Http404 <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . shortcuts import render <EOL> from django . template . loader import render_to_string <EOL> from django . utils . decorators import method_decorator <EOL> from django . utils . translation import ugettext as _ , ugettext_noop <EOL> from django . views . decorators . cache import cache_page <EOL> from django . views . generic import View <EOL> from couchdbkit import ResourceConflict , ResourceNotFound <EOL> from casexml . apps . case . models import CASE_STATUS_OPEN <EOL> from casexml . apps . case . xml import V2 <EOL> from casexml . apps . phone . fixtures import generator <EOL> from casexml . apps . stock . models import StockTransaction <EOL> from casexml . apps . stock . utils import get_current_ledger_transactions <EOL> from corehq . form_processor . utils import should_use_sql_backend <EOL> from dimagi . utils . logging import notify_exception <EOL> from dimagi . utils . parsing import string_to_boolean <EOL> from dimagi . utils . web import json_response , get_url_base , json_handler <EOL> from touchforms . formplayer . api import DjangoAuth , get_raw_instance , sync_db <EOL> from touchforms . formplayer . models import EntrySession <EOL> from xml2json . lib import xml2json <EOL> from corehq import toggles , privileges <EOL> from corehq . apps . accounting . decorators import requires_privilege_for_commcare_user , requires_privilege_with_fallback <EOL> from corehq . apps . app_manager . dbaccessors import get_app , get_latest_build_doc , get_brief_apps_in_domain <EOL> from corehq . apps . app_manager . exceptions import FormNotFoundException , ModuleNotFoundException <EOL> from corehq . apps . app_manager . models import Application , ApplicationBase <EOL> from corehq . apps . app_manager . suite_xml . sections . details import get_instances_for_module <EOL> from corehq . apps . app_manager . suite_xml . sections . entries import EntriesHelper <EOL> from corehq . apps . app_manager . util import get_cloudcare_session_data <EOL> from corehq . apps . cloudcare . api import ( <EOL> api_closed_to_status , <EOL> CaseAPIResult , <EOL> get_app_json , <EOL> get_filtered_cases , <EOL> get_filters_from_request_params , <EOL> get_open_form_sessions , <EOL> look_up_app_json , <EOL> ) <EOL> from corehq . apps . cloudcare . dbaccessors import get_cloudcare_apps <EOL> from corehq . apps . cloudcare . decorators import require_cloudcare_access <EOL> from corehq . apps . cloudcare . exceptions import RemoteAppError <EOL> from corehq . apps . cloudcare . models import ApplicationAccess <EOL> from corehq . apps . cloudcare . touchforms_api import BaseSessionDataHelper , CaseSessionDataHelper <EOL> from corehq . apps . domain . decorators import login_and_domain_required , login_or_digest_ex , domain_admin_required <EOL> from corehq . apps . groups . models import Group <EOL> from corehq . apps . reports . formdetails import readable <EOL> from corehq . apps . style . decorators import ( <EOL> use_datatables , <EOL> use_bootstrap3 , <EOL> use_jquery_ui , <EOL> ) <EOL> from corehq . apps . users . models import CouchUser , CommCareUser <EOL> from corehq . apps . users . views import BaseUserSettingsView <EOL> from corehq . form_processor . interfaces . dbaccessors import CaseAccessors , FormAccessors <EOL> from corehq . form_processor . exceptions import XFormNotFound , CaseNotFound <EOL> from corehq . util . couch import get_document_or_404 <EOL> from corehq . util . quickcache import skippable_quickcache <EOL> from corehq . util . xml_utils import indent_xml <EOL> @ require_cloudcare_access <EOL> def default ( request , domain ) : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = [ domain , '<STR_LIT>' ] ) ) <EOL> def insufficient_privilege ( request , domain , * args , ** kwargs ) : <EOL> context = { <EOL> '<STR_LIT>' : domain , <EOL> } <EOL> return render ( request , "<STR_LIT>" , context ) <EOL> class CloudcareMain ( View ) : <EOL> @ use_bootstrap3 <EOL> @ use_datatables <EOL> @ use_jquery_ui <EOL> @ method_decorator ( require_cloudcare_access ) <EOL> @ method_decorator ( requires_privilege_for_commcare_user ( privileges . CLOUDCARE ) ) <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( CloudcareMain , self ) . dispatch ( request , * args , ** kwargs ) <EOL> def get ( self , request , domain , urlPath ) : <EOL> try : <EOL> preview = string_to_boolean ( request . GET . get ( "<STR_LIT>" , "<STR_LIT:false>" ) ) <EOL> except ValueError : <EOL> preview = True <EOL> app_access = ApplicationAccess . get_by_domain ( domain ) <EOL> accessor = CaseAccessors ( domain ) <EOL> if not preview : <EOL> apps = get_cloudcare_apps ( domain ) <EOL> if request . project . use_cloudcare_releases : <EOL> apps = filter ( <EOL> lambda app : app . is_released , <EOL> [ get_app ( domain , app [ '<STR_LIT>' ] , latest = True ) for app in apps ] <EOL> ) <EOL> apps = [ get_app_json ( app ) for app in apps ] <EOL> else : <EOL> apps = [ get_latest_build_doc ( domain , app [ '<STR_LIT>' ] ) for app in apps ] <EOL> apps = [ get_app_json ( ApplicationBase . wrap ( app ) ) for app in apps if app ] <EOL> else : <EOL> apps = get_brief_apps_in_domain ( domain ) <EOL> apps = [ get_app_json ( app ) for app in apps if app and app . application_version == V2 ] <EOL> apps = filter ( lambda app : app , apps ) <EOL> apps = filter ( lambda app : app_access . user_can_access_app ( request . couch_user , app ) , apps ) <EOL> def _default_lang ( ) : <EOL> if apps : <EOL> return Application . get ( apps [ <NUM_LIT:0> ] [ "<STR_LIT>" ] ) . build_langs [ <NUM_LIT:0> ] <EOL> else : <EOL> return "<STR_LIT>" <EOL> language = request . couch_user . language or _default_lang ( ) <EOL> def _url_context ( ) : <EOL> split = urlPath . split ( '<STR_LIT:/>' ) <EOL> app_id = split [ <NUM_LIT:1> ] if len ( split ) >= <NUM_LIT:2> else None <EOL> if len ( split ) >= <NUM_LIT:5> and split [ <NUM_LIT:4> ] == "<STR_LIT>" : <EOL> parent_id = split [ <NUM_LIT:5> ] <EOL> case_id = split [ <NUM_LIT:7> ] if len ( split ) >= <NUM_LIT:7> else None <EOL> else : <EOL> parent_id = None <EOL> case_id = split [ <NUM_LIT:5> ] if len ( split ) >= <NUM_LIT:6> else None <EOL> app = None <EOL> if app_id : <EOL> if app_id in [ a [ '<STR_LIT>' ] for a in apps ] : <EOL> app = look_up_app_json ( domain , app_id ) <EOL> else : <EOL> messages . info ( request , _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> if app is None and len ( apps ) == <NUM_LIT:1> : <EOL> app = look_up_app_json ( domain , apps [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> def _get_case ( domain , case_id ) : <EOL> case = accessor . get_case ( case_id ) <EOL> assert case . domain == domain , "<STR_LIT>" % ( case_id , domain ) <EOL> return case . to_api_json ( ) <EOL> case = _get_case ( domain , case_id ) if case_id else None <EOL> if parent_id is None and case is not None : <EOL> parent_id = case . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , None ) <EOL> parent = _get_case ( domain , parent_id ) if parent_id else None <EOL> return { <EOL> "<STR_LIT>" : app , <EOL> "<STR_LIT>" : case , <EOL> "<STR_LIT>" : parent <EOL> } <EOL> context = { <EOL> "<STR_LIT>" : domain , <EOL> "<STR_LIT>" : language , <EOL> "<STR_LIT>" : apps , <EOL> "<STR_LIT>" : apps , <EOL> "<STR_LIT>" : preview , <EOL> "<STR_LIT>" : settings . GMAPS_API_KEY , <EOL> "<STR_LIT>" : toggles . OFFLINE_CLOUDCARE . enabled ( request . user . username ) , <EOL> "<STR_LIT>" : request . couch_user . is_commcare_user ( ) , <EOL> "<STR_LIT>" : request . project . use_cloudcare_releases , <EOL> "<STR_LIT:username>" : request . user . username , <EOL> } <EOL> context . update ( _url_context ( ) ) <EOL> return render ( request , "<STR_LIT>" , context ) <EOL> @ login_and_domain_required <EOL> @ requires_privilege_for_commcare_user ( privileges . CLOUDCARE ) <EOL> def form_context ( request , domain , app_id , module_id , form_id ) : <EOL> app = Application . get ( app_id ) <EOL> form_url = '<STR_LIT>' . format ( <EOL> settings . CLOUDCARE_BASE_URL or get_url_base ( ) , <EOL> reverse ( '<STR_LIT>' , args = [ domain , app_id , module_id , form_id ] ) <EOL> ) <EOL> case_id = request . GET . get ( '<STR_LIT>' ) <EOL> instance_id = request . GET . get ( '<STR_LIT>' ) <EOL> try : <EOL> form = app . get_module ( module_id ) . get_form ( form_id ) <EOL> except ( FormNotFoundException , ModuleNotFoundException ) : <EOL> raise Http404 ( ) <EOL> form_name = form . name . values ( ) [ <NUM_LIT:0> ] <EOL> session_name = u'<STR_LIT>' . format ( <EOL> app = app . name , <EOL> form = form_name , <EOL> ) <EOL> if case_id : <EOL> case = CaseAccessors ( domain ) . get_case ( case_id ) <EOL> session_name = u'<STR_LIT>' . format ( session_name , case . name ) <EOL> root_context = { <EOL> '<STR_LIT>' : form_url , <EOL> } <EOL> if instance_id : <EOL> try : <EOL> root_context [ '<STR_LIT>' ] = FormAccessors ( domain ) . get_form ( instance_id ) . get_xml ( ) <EOL> except XFormNotFound : <EOL> raise Http404 ( ) <EOL> session_extras = { '<STR_LIT>' : session_name , '<STR_LIT>' : app . _id } <EOL> session_extras . update ( get_cloudcare_session_data ( domain , form , request . couch_user ) ) <EOL> delegation = request . GET . get ( '<STR_LIT>' ) == '<STR_LIT:true>' <EOL> session_helper = CaseSessionDataHelper ( domain , request . couch_user , case_id , app , form , delegation = delegation ) <EOL> return json_response ( session_helper . get_full_context ( <EOL> root_context , <EOL> session_extras <EOL> ) ) <EOL> cloudcare_api = login_or_digest_ex ( allow_cc_users = True ) <EOL> def get_cases_vary_on ( request , domain ) : <EOL> request_params = request . GET <EOL> return [ <EOL> request . couch_user . get_id <EOL> if request . couch_user . is_commcare_user ( ) else request_params . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> request_params . get ( '<STR_LIT>' , '<STR_LIT:false>' ) , <EOL> request_params . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> request_params . get ( '<STR_LIT>' , '<STR_LIT:false>' ) , <EOL> request_params . get ( '<STR_LIT>' , '<STR_LIT:false>' ) , <EOL> json . dumps ( get_filters_from_request_params ( request_params ) ) , <EOL> domain , <EOL> ] <EOL> def get_cases_skip_arg ( request , domain ) : <EOL> """<STR_LIT>""" <EOL> if not toggles . CLOUDCARE_CACHE . enabled ( domain ) : <EOL> return True <EOL> request_params = request . GET <EOL> return ( not string_to_boolean ( request_params . get ( '<STR_LIT>' , '<STR_LIT:false>' ) ) or <EOL> not string_to_boolean ( request_params . get ( '<STR_LIT>' , '<STR_LIT:false>' ) ) ) <EOL> @ cloudcare_api <EOL> @ skippable_quickcache ( get_cases_vary_on , get_cases_skip_arg , timeout = <NUM_LIT> * <NUM_LIT> ) <EOL> def get_cases ( request , domain ) : <EOL> request_params = request . GET <EOL> if request . couch_user . is_commcare_user ( ) : <EOL> user_id = request . couch_user . get_id <EOL> else : <EOL> user_id = request_params . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if not user_id and not request . couch_user . is_web_user ( ) : <EOL> return HttpResponseBadRequest ( "<STR_LIT>" ) <EOL> ids_only = string_to_boolean ( request_params . get ( "<STR_LIT>" , "<STR_LIT:false>" ) ) <EOL> case_id = request_params . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> footprint = string_to_boolean ( request_params . get ( "<STR_LIT>" , "<STR_LIT:false>" ) ) <EOL> accessor = CaseAccessors ( domain ) <EOL> if toggles . HSPH_HACK . enabled ( domain ) : <EOL> hsph_case_id = request_params . get ( '<STR_LIT>' , None ) <EOL> if hsph_case_id != '<STR_LIT:None>' and hsph_case_id and user_id : <EOL> case = accessor . get_case ( hsph_case_id ) <EOL> usercase_id = CommCareUser . get_by_user_id ( user_id ) . get_usercase_id ( ) <EOL> usercase = accessor . get_case ( usercase_id ) if usercase_id else None <EOL> return json_response ( map ( <EOL> lambda case : CaseAPIResult ( id = case [ '<STR_LIT>' ] , couch_doc = case , id_only = ids_only ) , <EOL> filter ( None , [ case , case . parent , usercase ] ) <EOL> ) ) <EOL> if case_id and not footprint : <EOL> case = accessor . get_case ( case_id ) <EOL> assert case . domain == domain <EOL> cases = [ CaseAPIResult ( id = case_id , couch_doc = case , id_only = ids_only ) ] <EOL> else : <EOL> filters = get_filters_from_request_params ( request_params ) <EOL> status = api_closed_to_status ( request_params . get ( '<STR_LIT>' , '<STR_LIT:false>' ) ) <EOL> case_type = filters . get ( '<STR_LIT>' , None ) <EOL> cases = get_filtered_cases ( domain , status = status , case_type = case_type , <EOL> user_id = user_id , filters = filters , <EOL> footprint = footprint , ids_only = ids_only , <EOL> strip_history = True ) <EOL> return json_response ( cases ) <EOL> @ cloudcare_api <EOL> def filter_cases ( request , domain , app_id , module_id , parent_id = None ) : <EOL> app = Application . get ( app_id ) <EOL> module = app . get_module ( module_id ) <EOL> auth_cookie = request . COOKIES . get ( '<STR_LIT>' ) <EOL> requires_parent_cases = string_to_boolean ( request . GET . get ( '<STR_LIT>' , '<STR_LIT:false>' ) ) <EOL> xpath = EntriesHelper . get_filter_xpath ( module ) <EOL> instances = get_instances_for_module ( app , module , additional_xpaths = [ xpath ] ) <EOL> extra_instances = [ { '<STR_LIT:id>' : inst . id , '<STR_LIT:src>' : inst . src } for inst in instances ] <EOL> use_formplayer = toggles . USE_FORMPLAYER . enabled ( domain ) <EOL> accessor = CaseAccessors ( domain ) <EOL> xpath = HTMLParser . HTMLParser ( ) . unescape ( xpath ) <EOL> case_type = module . case_type <EOL> if xpath or should_use_sql_backend ( domain ) : <EOL> additional_filters = { <EOL> "<STR_LIT>" : case_type , <EOL> "<STR_LIT>" : True <EOL> } <EOL> helper = BaseSessionDataHelper ( domain , request . couch_user ) <EOL> result = helper . filter_cases ( xpath , additional_filters , DjangoAuth ( auth_cookie ) , <EOL> extra_instances = extra_instances , use_formplayer = use_formplayer ) <EOL> if result . get ( '<STR_LIT:status>' , None ) == '<STR_LIT:error>' : <EOL> code = result . get ( '<STR_LIT:code>' , <NUM_LIT> ) <EOL> message = result . get ( '<STR_LIT:message>' , _ ( "<STR_LIT>" ) ) <EOL> if code == <NUM_LIT> : <EOL> notify_exception ( None , message = message ) <EOL> return json_response ( message , status_code = code ) <EOL> case_ids = result . get ( "<STR_LIT>" , [ ] ) <EOL> else : <EOL> case_ids = [ res . id for res in get_filtered_cases ( <EOL> domain , <EOL> status = CASE_STATUS_OPEN , <EOL> case_type = case_type , <EOL> user_id = request . couch_user . _id , <EOL> footprint = True , <EOL> ids_only = True , <EOL> ) ] <EOL> cases = accessor . get_cases ( case_ids ) <EOL> if parent_id : <EOL> cases = filter ( lambda c : c . parent and c . parent . case_id == parent_id , cases ) <EOL> cases = filter ( lambda c : c . type == case_type , cases ) <EOL> cases = [ c . to_api_json ( lite = True ) for c in cases if c ] <EOL> response = { '<STR_LIT>' : cases } <EOL> if requires_parent_cases : <EOL> parent_ids = set ( map ( lambda c : c [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] , cases ) ) - set ( map ( lambda c : c [ '<STR_LIT>' ] , cases ) ) <EOL> parents = accessor . get_cases ( list ( parent_ids ) ) <EOL> parents = [ c . to_api_json ( lite = True ) for c in parents ] <EOL> response . update ( { '<STR_LIT>' : parents } ) <EOL> return json_response ( response ) <EOL> @ cloudcare_api <EOL> def get_apps_api ( request , domain ) : <EOL> return json_response ( get_cloudcare_apps ( domain ) ) <EOL> @ cloudcare_api <EOL> def get_app_api ( request , domain , app_id ) : <EOL> try : <EOL> return json_response ( look_up_app_json ( domain , app_id ) ) <EOL> except RemoteAppError : <EOL> raise Http404 ( ) <EOL> @ cloudcare_api <EOL> @ cache_page ( <NUM_LIT> * <NUM_LIT:30> ) <EOL> def get_fixtures ( request , domain , user_id , fixture_id = None ) : <EOL> try : <EOL> user = CommCareUser . get_by_user_id ( user_id ) <EOL> except CouchUser . AccountTypeError : <EOL> err = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % settings . WEB_USER_TERM , <EOL> return HttpResponse ( err , status = <NUM_LIT> , content_type = "<STR_LIT>" ) <EOL> if not user : <EOL> raise Http404 <EOL> assert user . is_member_of ( domain ) <EOL> casexml_user = user . to_casexml_user ( ) <EOL> if not fixture_id : <EOL> ret = ElementTree . Element ( "<STR_LIT>" ) <EOL> for fixture in generator . get_fixtures ( casexml_user , version = V2 ) : <EOL> ret . append ( fixture ) <EOL> return HttpResponse ( ElementTree . tostring ( ret ) , content_type = "<STR_LIT>" ) <EOL> else : <EOL> fixture = generator . get_fixture_by_id ( fixture_id , casexml_user , version = V2 ) <EOL> if not fixture : <EOL> raise Http404 <EOL> assert len ( fixture . getchildren ( ) ) == <NUM_LIT:1> , '<STR_LIT>' . format ( <EOL> fixture_id , len ( fixture . getchildren ( ) ) <EOL> ) <EOL> return HttpResponse ( ElementTree . tostring ( fixture . getchildren ( ) [ <NUM_LIT:0> ] ) , content_type = "<STR_LIT>" ) <EOL> @ cloudcare_api <EOL> def get_sessions ( request , domain ) : <EOL> skip = request . GET . get ( '<STR_LIT>' ) or <NUM_LIT:0> <EOL> limit = request . GET . get ( '<STR_LIT>' ) or <NUM_LIT:10> <EOL> return json_response ( get_open_form_sessions ( request . user , skip = skip , limit = limit ) ) <EOL> @ cloudcare_api <EOL> def get_session_context ( request , domain , session_id ) : <EOL> try : <EOL> session = EntrySession . objects . get ( session_id = session_id ) <EOL> except EntrySession . DoesNotExist : <EOL> session = None <EOL> if request . method == '<STR_LIT>' : <EOL> if session : <EOL> session . delete ( ) <EOL> return json_response ( { '<STR_LIT:status>' : '<STR_LIT:success>' } ) <EOL> else : <EOL> helper = BaseSessionDataHelper ( domain , request . couch_user ) <EOL> return json_response ( helper . get_full_context ( { <EOL> '<STR_LIT>' : session_id , <EOL> '<STR_LIT>' : session . app_id if session else None <EOL> } ) ) <EOL> @ cloudcare_api <EOL> def get_ledgers ( request , domain ) : <EOL> """<STR_LIT>""" <EOL> request_params = request . GET <EOL> case_id = request_params . get ( '<STR_LIT>' ) <EOL> if not case_id : <EOL> return json_response ( <EOL> { '<STR_LIT:message>' : '<STR_LIT>' } , <EOL> status_code = <NUM_LIT> <EOL> ) <EOL> try : <EOL> case = CaseAccessors ( domain ) . get_case ( case_id ) <EOL> except CaseNotFound : <EOL> raise Http404 ( ) <EOL> ledger_map = get_current_ledger_transactions ( case . case_id ) <EOL> def custom_json_handler ( obj ) : <EOL> if isinstance ( obj , StockTransaction ) : <EOL> return obj . stock_on_hand <EOL> return json_handler ( obj ) <EOL> return json_response ( <EOL> { <EOL> '<STR_LIT>' : case_id , <EOL> '<STR_LIT>' : ledger_map , <EOL> } , <EOL> default = custom_json_handler , <EOL> ) <EOL> @ cloudcare_api <EOL> def sync_db_api ( request , domain ) : <EOL> auth_cookie = request . COOKIES . get ( '<STR_LIT>' ) <EOL> username = request . GET . get ( '<STR_LIT:username>' ) <EOL> try : <EOL> sync_db ( username , domain , DjangoAuth ( auth_cookie ) ) <EOL> return json_response ( { <EOL> '<STR_LIT:status>' : '<STR_LIT:OK>' <EOL> } ) <EOL> except Exception , e : <EOL> return HttpResponse ( e , status = <NUM_LIT> , content_type = "<STR_LIT>" ) <EOL> @ cloudcare_api <EOL> def render_form ( request , domain ) : <EOL> session_id = request . GET . get ( '<STR_LIT>' ) <EOL> session = get_object_or_404 ( EntrySession , session_id = session_id ) <EOL> try : <EOL> raw_instance = get_raw_instance ( session_id , domain ) <EOL> except Exception , e : <EOL> return HttpResponse ( e , status = <NUM_LIT> , content_type = "<STR_LIT>" ) <EOL> xmlns = raw_instance [ "<STR_LIT>" ] <EOL> form_data_xml = raw_instance [ "<STR_LIT>" ] <EOL> _ , form_data_json = xml2json ( form_data_xml ) <EOL> pretty_questions = readable . get_questions ( domain , session . app_id , xmlns ) <EOL> readable_form = readable . get_readable_form_data ( form_data_json , pretty_questions ) <EOL> rendered_readable_form = render_to_string ( <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : readable_form } <EOL> ) <EOL> return json_response ( { <EOL> '<STR_LIT>' : rendered_readable_form , <EOL> '<STR_LIT>' : indent_xml ( form_data_xml ) <EOL> } ) <EOL> class HttpResponseConflict ( HttpResponse ) : <EOL> status_code = <NUM_LIT> <EOL> class EditCloudcareUserPermissionsView ( BaseUserSettingsView ) : <EOL> template_name = '<STR_LIT>' <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_noop ( "<STR_LIT>" ) <EOL> @ method_decorator ( domain_admin_required ) <EOL> @ method_decorator ( requires_privilege_with_fallback ( privileges . CLOUDCARE ) ) <EOL> @ use_bootstrap3 <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( EditCloudcareUserPermissionsView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> apps = get_cloudcare_apps ( self . domain ) <EOL> access = ApplicationAccess . get_template_json ( self . domain , apps ) <EOL> groups = Group . by_domain ( self . domain ) <EOL> return { <EOL> '<STR_LIT>' : apps , <EOL> '<STR_LIT>' : groups , <EOL> '<STR_LIT>' : access , <EOL> } <EOL> def put ( self , request , * args , ** kwargs ) : <EOL> j = json . loads ( request . body ) <EOL> old = ApplicationAccess . get_by_domain ( self . domain ) <EOL> new = ApplicationAccess . wrap ( j ) <EOL> old . restrict = new . restrict <EOL> old . app_groups = new . app_groups <EOL> try : <EOL> if old . _rev != new . _rev or old . _id != new . _id : <EOL> raise ResourceConflict ( ) <EOL> old . save ( ) <EOL> except ResourceConflict : <EOL> return HttpResponseConflict ( ) <EOL> else : <EOL> return json_response ( { '<STR_LIT>' : old . _rev } ) </s>
