<s> from django . utils . decorators import method_decorator <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import Http404 , HttpResponseRedirect , HttpResponseBadRequest <EOL> from django . views . generic . base import View <EOL> from django . utils . translation import ugettext <EOL> from corehq . apps . accounting . utils import domain_has_privilege <EOL> from corehq . apps . domain . decorators import login_and_domain_required , cls_to_view <EOL> from dimagi . utils . decorators . datespan import datespan_in_request <EOL> from django_prbac . exceptions import PermissionDenied <EOL> from django_prbac . utils import has_privilege <EOL> from corehq . apps . domain . models import Domain <EOL> from corehq . apps . reports . exceptions import BadRequestError <EOL> from corehq import privileges , toggles <EOL> from corehq . apps . accounting . decorators import requires_privilege_with_fallback <EOL> from corehq . apps . hqwebapp . templatetags . hq_shared_tags import toggle_enabled <EOL> datespan_default = datespan_in_request ( <EOL> from_param = "<STR_LIT>" , <EOL> to_param = "<STR_LIT>" , <EOL> default_days = <NUM_LIT:7> , <EOL> ) <EOL> _ = lambda message : ugettext ( message ) if message is not None else None <EOL> class ReportDispatcher ( View ) : <EOL> """<STR_LIT>""" <EOL> prefix = None <EOL> map_name = None <EOL> def __init__ ( self , ** kwargs ) : <EOL> if not self . map_name or not isinstance ( self . map_name , basestring ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> super ( ReportDispatcher , self ) . __init__ ( ** kwargs ) <EOL> @ property <EOL> def slug_aliases ( self ) : <EOL> """<STR_LIT>""" <EOL> return { } <EOL> def permissions_check ( self , report , request , domain = None , is_navigation_check = False ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def get_reports ( self , domain = None ) : <EOL> attr_name = self . map_name <EOL> from corehq import reports <EOL> domain_module = Domain . get_module_by_name ( domain ) <EOL> if domain : <EOL> project = Domain . get_by_name ( domain ) <EOL> else : <EOL> project = None <EOL> def process ( reports ) : <EOL> if project and callable ( reports ) : <EOL> reports = reports ( project ) <EOL> return tuple ( reports ) <EOL> corehq_reports = process ( getattr ( reports , attr_name , ( ) ) ) <EOL> custom_reports = process ( getattr ( domain_module , attr_name , ( ) ) ) <EOL> return corehq_reports + custom_reports <EOL> def get_reports_dict ( self , domain = None ) : <EOL> return dict ( ( report . slug , report ) <EOL> for name , group in self . get_reports ( domain ) <EOL> for report in group ) <EOL> def get_report ( self , domain , report_slug , * args ) : <EOL> """<STR_LIT>""" <EOL> return self . get_reports_dict ( domain ) . get ( report_slug , None ) <EOL> def _redirect_slug ( self , slug ) : <EOL> return self . slug_aliases . get ( slug ) is not None <EOL> def _slug_alias ( self , slug ) : <EOL> return self . slug_aliases . get ( slug ) <EOL> @ datespan_default <EOL> def dispatch ( self , request , domain = None , report_slug = None , render_as = None , <EOL> permissions_check = None , * args , ** kwargs ) : <EOL> render_as = render_as or '<STR_LIT>' <EOL> domain = domain or getattr ( request , '<STR_LIT>' , None ) <EOL> redirect_slug = self . _redirect_slug ( report_slug ) <EOL> if redirect_slug and render_as == '<STR_LIT:email>' : <EOL> raise Http404 <EOL> elif redirect_slug : <EOL> new_args = [ domain ] if domain else [ ] <EOL> if render_as != '<STR_LIT>' : <EOL> new_args . append ( render_as ) <EOL> new_args . append ( redirect_slug ) <EOL> return HttpResponseRedirect ( reverse ( self . name ( ) , args = new_args ) ) <EOL> report_kwargs = kwargs . copy ( ) <EOL> cls = self . get_report ( domain , report_slug ) <EOL> class_name = cls . __module__ + '<STR_LIT:.>' + cls . __name__ if cls else '<STR_LIT>' <EOL> permissions_check = permissions_check or self . permissions_check <EOL> if ( <EOL> cls <EOL> and permissions_check ( class_name , request , domain = domain ) <EOL> and self . toggles_enabled ( cls , request ) <EOL> ) : <EOL> report = cls ( request , domain = domain , ** report_kwargs ) <EOL> report . rendered_as = render_as <EOL> try : <EOL> if report . is_bootstrap3 : <EOL> report . bootstrap3_dispatcher ( <EOL> request , domain = domain , report_slug = report_slug , * args , ** kwargs <EOL> ) <EOL> return getattr ( report , '<STR_LIT>' % render_as ) <EOL> except BadRequestError , e : <EOL> return HttpResponseBadRequest ( e ) <EOL> else : <EOL> raise Http404 ( ) <EOL> @ staticmethod <EOL> def toggles_enabled ( report_class , request ) : <EOL> if not getattr ( report_class , '<STR_LIT>' , ( ) ) : <EOL> return True <EOL> return all ( toggle_enabled ( request , toggle ) for toggle in report_class . toggles ) <EOL> @ classmethod <EOL> def name ( cls ) : <EOL> prefix = "<STR_LIT:%s>" % cls . prefix if cls . prefix else "<STR_LIT>" <EOL> return "<STR_LIT>" % prefix <EOL> @ classmethod <EOL> def _rendering_pattern ( cls ) : <EOL> return "<STR_LIT>" . format ( <EOL> renderings = "<STR_LIT:|>" . join ( "<STR_LIT>" % r for r in cls . allowed_renderings ( ) ) <EOL> ) <EOL> @ classmethod <EOL> def pattern ( cls ) : <EOL> return r'<STR_LIT>' . format ( renderings = cls . _rendering_pattern ( ) ) <EOL> @ classmethod <EOL> def allowed_renderings ( cls ) : <EOL> return [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:email>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> @ classmethod <EOL> def navigation_sections ( cls , request , domain ) : <EOL> project = getattr ( request , '<STR_LIT>' , None ) <EOL> couch_user = getattr ( request , '<STR_LIT>' , None ) <EOL> nav_context = [ ] <EOL> dispatcher = cls ( ) <EOL> reports = dispatcher . get_reports ( domain ) <EOL> for section_name , report_group in reports : <EOL> report_contexts = [ ] <EOL> for report in report_group : <EOL> class_name = report . __module__ + '<STR_LIT:.>' + report . __name__ <EOL> if ( <EOL> dispatcher . permissions_check ( class_name , request , domain = domain , <EOL> is_navigation_check = True ) <EOL> and cls . toggles_enabled ( report , request ) <EOL> and report . show_in_navigation ( domain = domain , project = project , user = couch_user ) <EOL> ) : <EOL> report_contexts . append ( { <EOL> '<STR_LIT:url>' : report . get_url ( domain = domain , request = request ) , <EOL> '<STR_LIT:description>' : _ ( report . description ) , <EOL> '<STR_LIT>' : report . icon , <EOL> '<STR_LIT:title>' : _ ( report . name ) , <EOL> '<STR_LIT>' : report . get_subpages ( ) , <EOL> '<STR_LIT>' : report . display_in_dropdown ( project = project ) , <EOL> } ) <EOL> if report_contexts : <EOL> if hasattr ( section_name , '<STR_LIT>' ) : <EOL> section_name = section_name ( project , couch_user ) <EOL> nav_context . append ( ( section_name , report_contexts ) ) <EOL> return nav_context <EOL> @ classmethod <EOL> def url_pattern ( cls ) : <EOL> from django . conf . urls import url <EOL> return url ( cls . pattern ( ) , cls . as_view ( ) , name = cls . name ( ) ) <EOL> cls_to_view_login_and_domain = cls_to_view ( additional_decorator = login_and_domain_required ) <EOL> class ProjectReportDispatcher ( ReportDispatcher ) : <EOL> prefix = '<STR_LIT>' <EOL> map_name = '<STR_LIT>' <EOL> @ property <EOL> def slug_aliases ( self ) : <EOL> return { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> @ cls_to_view_login_and_domain <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( ProjectReportDispatcher , self ) . dispatch ( request , * args , ** kwargs ) <EOL> def permissions_check ( self , report , request , domain = None , is_navigation_check = False ) : <EOL> if domain is None : <EOL> return False <EOL> if not request . couch_user . is_active : <EOL> return False <EOL> return request . couch_user . can_view_report ( domain , report ) <EOL> class CustomProjectReportDispatcher ( ProjectReportDispatcher ) : <EOL> prefix = '<STR_LIT>' <EOL> map_name = '<STR_LIT>' <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> render_as = kwargs . get ( '<STR_LIT>' ) <EOL> if not render_as == '<STR_LIT:email>' : <EOL> return self . dispatch_with_priv ( request , * args , ** kwargs ) <EOL> if not domain_has_privilege ( request . domain , privileges . CUSTOM_REPORTS ) : <EOL> raise PermissionDenied ( ) <EOL> return super ( CustomProjectReportDispatcher , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ method_decorator ( requires_privilege_with_fallback ( privileges . CUSTOM_REPORTS ) ) <EOL> def dispatch_with_priv ( self , request , * args , ** kwargs ) : <EOL> return super ( CustomProjectReportDispatcher , self ) . dispatch ( request , * args , ** kwargs ) <EOL> def permissions_check ( self , report , request , domain = None , is_navigation_check = False ) : <EOL> if is_navigation_check and not has_privilege ( request , privileges . CUSTOM_REPORTS ) : <EOL> return False <EOL> return super ( CustomProjectReportDispatcher , self ) . permissions_check ( report , request , domain ) <EOL> class BasicReportDispatcher ( ReportDispatcher ) : <EOL> prefix = '<STR_LIT>' <EOL> map_name = '<STR_LIT>' <EOL> class DomainReportDispatcher ( ReportDispatcher ) : <EOL> prefix = '<STR_LIT>' <EOL> map_name = '<STR_LIT>' <EOL> @ cls_to_view_login_and_domain <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( DomainReportDispatcher , self ) . dispatch ( request , * args , ** kwargs ) <EOL> class AdminReportDispatcher ( ReportDispatcher ) : <EOL> prefix = '<STR_LIT>' <EOL> map_name = '<STR_LIT>' <EOL> def permissions_check ( self , report , request , domain = None , is_navigation_check = False ) : <EOL> return hasattr ( request , '<STR_LIT>' ) and request . user . has_perm ( "<STR_LIT>" ) <EOL> class QuestionTemplateDispatcher ( ProjectReportDispatcher ) : <EOL> prefix = '<STR_LIT>' <EOL> map_name = '<STR_LIT>' <EOL> def get_question_templates ( self , domain , report_slug ) : <EOL> question_templates = dict ( self . get_reports ( domain ) ) <EOL> return question_templates . get ( report_slug , None ) </s>
