<s> from datetime import datetime , timedelta , time <EOL> import logging <EOL> import uuid <EOL> from couchdbkit import ResourceNotFound <EOL> from corehq . apps . reports . datatables import DataTablesHeader , DataTablesColumn <EOL> from corehq . apps . reports . generic import GenericTabularReport <EOL> from corehq . apps . reports . standard import CustomProjectReport <EOL> from corehq . util . dates import iso_string_to_date <EOL> from dimagi . utils . parsing import json_format_date <EOL> from pact . models import PactPatientCase , CObservation <EOL> from pact . reports . dot import PactDOTPatientField <EOL> class PactDOTAdminPatientField ( PactDOTPatientField ) : <EOL> slug = "<STR_LIT>" <EOL> name = "<STR_LIT>" <EOL> default_option = "<STR_LIT>" <EOL> COUCH_CHUNK_LIMIT = <NUM_LIT> <EOL> COUCH_MAX_LIMIT = <NUM_LIT:1000> <EOL> class PactDOTAdminReport ( GenericTabularReport , CustomProjectReport ) : <EOL> fields = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> name = "<STR_LIT>" <EOL> slug = "<STR_LIT>" <EOL> emailable = True <EOL> exportable = True <EOL> report_template_path = "<STR_LIT>" <EOL> is_bootstrap3 = True <EOL> def tabular_data ( self , mode , case_id , start_date , end_date ) : <EOL> try : <EOL> case_doc = PactPatientCase . get ( case_id ) <EOL> except ResourceNotFound : <EOL> case_doc = None <EOL> if case_doc is not None : <EOL> startkey = [ case_id , '<STR_LIT>' , start_date . year , start_date . month , start_date . day ] <EOL> endkey = [ case_id , '<STR_LIT>' , end_date . year , end_date . month , end_date . day ] <EOL> elif case_doc is None : <EOL> startkey = [ start_date . year , start_date . month , start_date . day ] <EOL> endkey = [ end_date . year , end_date . month , end_date . day ] <EOL> skip = <NUM_LIT:0> <EOL> view_results = CObservation . view ( <EOL> '<STR_LIT>' , <EOL> startkey = startkey , <EOL> endkey = endkey , <EOL> limit = COUCH_CHUNK_LIMIT , <EOL> skip = skip , <EOL> classes = { None : CObservation } , <EOL> ) . all ( ) <EOL> while len ( view_results ) > <NUM_LIT:0> : <EOL> if skip > COUCH_MAX_LIMIT : <EOL> logging . error ( "<STR_LIT>" % ( startkey , endkey ) ) <EOL> break <EOL> for v in view_results : <EOL> yield v <EOL> skip += COUCH_CHUNK_LIMIT <EOL> view_results = CObservation . view ( <EOL> '<STR_LIT>' , <EOL> startkey = startkey , <EOL> endkey = endkey , <EOL> limit = COUCH_CHUNK_LIMIT , <EOL> skip = skip , <EOL> classes = { None : CObservation } , <EOL> ) . all ( ) <EOL> @ property <EOL> def headers ( self ) : <EOL> headers = DataTablesHeader ( <EOL> DataTablesColumn ( "<STR_LIT>" , prop_name = "<STR_LIT>" ) , <EOL> DataTablesColumn ( "<STR_LIT>" , prop_name = "<STR_LIT>" ) , <EOL> DataTablesColumn ( "<STR_LIT>" , prop_name = "<STR_LIT>" ) , <EOL> DataTablesColumn ( "<STR_LIT>" , prop_name = "<STR_LIT>" ) , <EOL> DataTablesColumn ( "<STR_LIT>" , prop_name = "<STR_LIT>" ) , <EOL> DataTablesColumn ( "<STR_LIT>" , prop_name = "<STR_LIT>" ) , <EOL> DataTablesColumn ( "<STR_LIT>" , prop_name = "<STR_LIT>" ) , <EOL> DataTablesColumn ( "<STR_LIT>" , prop_name = "<STR_LIT>" ) , <EOL> DataTablesColumn ( "<STR_LIT>" , prop_name = "<STR_LIT>" ) , <EOL> DataTablesColumn ( "<STR_LIT>" , prop_name = "<STR_LIT>" ) , <EOL> DataTablesColumn ( "<STR_LIT>" , prop_name = "<STR_LIT>" ) , <EOL> DataTablesColumn ( "<STR_LIT>" , prop_name = "<STR_LIT>" ) , <EOL> DataTablesColumn ( "<STR_LIT>" , prop_name = "<STR_LIT>" ) , <EOL> DataTablesColumn ( "<STR_LIT>" , prop_name = "<STR_LIT>" ) <EOL> ) <EOL> return headers <EOL> @ property <EOL> def rows ( self ) : <EOL> """<STR_LIT>""" <EOL> case_id = self . request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> start_date_str = self . request . GET . get ( '<STR_LIT>' , <EOL> json_format_date ( datetime . utcnow ( ) - timedelta ( days = <NUM_LIT:7> ) ) ) <EOL> end_date_str = self . request . GET . get ( '<STR_LIT>' , json_format_date ( datetime . utcnow ( ) ) ) <EOL> if case_id == '<STR_LIT>' : <EOL> mode = '<STR_LIT:all>' <EOL> else : <EOL> mode = '<STR_LIT>' <EOL> start_datetime = datetime . combine ( iso_string_to_date ( start_date_str ) , <EOL> time ( ) ) <EOL> end_datetime = datetime . combine ( iso_string_to_date ( end_date_str ) , <EOL> time ( ) ) <EOL> for num , obs in enumerate ( self . tabular_data ( mode , case_id , start_datetime , end_datetime ) ) : <EOL> dict_obj = obs . to_json ( ) <EOL> row = [ dict_obj [ x . prop_name ] . encode ( '<STR_LIT:utf-8>' ) if isinstance ( dict_obj [ x . prop_name ] , unicode ) else dict_obj [ x . prop_name ] for x in self . headers ] <EOL> yield row </s>
