<s> from couchdbkit import ResourceNotFound <EOL> import datetime <EOL> import dateutil <EOL> from django . core import cache <EOL> from django . core . urlresolvers import reverse , NoReverseMatch <EOL> from django . template . defaultfilters import yesno <EOL> from django . utils import html <EOL> from django . utils . translation import ugettext as _ <EOL> import json <EOL> from casexml . apps . case . models import CommCareCaseAction <EOL> from corehq . apps . groups . models import Group <EOL> from corehq . apps . locations . models import SQLLocation <EOL> from corehq . apps . users . models import CommCareUser , CouchUser <EOL> from corehq . util . dates import iso_string_to_datetime <EOL> from corehq . util . view_utils import absolute_reverse <EOL> from dimagi . utils . decorators . memoized import memoized <EOL> class CaseInfo ( object ) : <EOL> def __init__ ( self , report , case ) : <EOL> """<STR_LIT>""" <EOL> self . case = case <EOL> self . report = report <EOL> @ property <EOL> def case_type ( self ) : <EOL> return self . case [ '<STR_LIT:type>' ] <EOL> @ property <EOL> def case_name ( self ) : <EOL> return self . case [ '<STR_LIT:name>' ] <EOL> @ property <EOL> def case_name_display ( self ) : <EOL> return self . case_name or _ ( '<STR_LIT>' ) <EOL> @ property <EOL> def case_id ( self ) : <EOL> return self . case [ '<STR_LIT>' ] <EOL> @ property <EOL> def external_id ( self ) : <EOL> return self . case [ '<STR_LIT>' ] <EOL> @ property <EOL> def case_detail_url ( self ) : <EOL> try : <EOL> return absolute_reverse ( '<STR_LIT>' , args = [ self . report . domain , self . case_id ] ) <EOL> except NoReverseMatch : <EOL> return None <EOL> @ property <EOL> def is_closed ( self ) : <EOL> return self . case [ '<STR_LIT>' ] <EOL> def _dateprop ( self , prop , iso = True ) : <EOL> val = self . report . date_to_json ( self . parse_date ( self . case [ prop ] ) ) <EOL> if iso : <EOL> val = '<STR_LIT:T>' . join ( val . split ( '<STR_LIT:U+0020>' ) ) if val else None <EOL> return val <EOL> @ property <EOL> def opened_on ( self ) : <EOL> return self . _dateprop ( '<STR_LIT>' ) <EOL> @ property <EOL> def modified_on ( self ) : <EOL> return self . _dateprop ( '<STR_LIT>' ) <EOL> @ property <EOL> def closed_on ( self ) : <EOL> return self . _dateprop ( '<STR_LIT>' ) <EOL> @ property <EOL> def creating_user ( self ) : <EOL> try : <EOL> creator_id = self . case [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> creator_id = None <EOL> if '<STR_LIT>' in self . case : <EOL> for action in self . case [ '<STR_LIT>' ] : <EOL> if action [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> action_doc = CommCareCaseAction . wrap ( action ) <EOL> creator_id = action_doc . get_user_id ( ) <EOL> break <EOL> if not creator_id : <EOL> return None <EOL> return self . _user_meta ( creator_id ) <EOL> def _user_meta ( self , user_id ) : <EOL> return { '<STR_LIT:id>' : user_id , '<STR_LIT:name>' : self . _get_username ( user_id ) } <EOL> @ property <EOL> @ memoized <EOL> def location ( self ) : <EOL> try : <EOL> return SQLLocation . objects . get ( location_id = self . owner_id ) <EOL> except SQLLocation . DoesNotExist : <EOL> return None <EOL> @ property <EOL> def owner ( self ) : <EOL> if self . owning_group and self . owning_group . name : <EOL> return ( '<STR_LIT>' , { '<STR_LIT:id>' : self . owning_group . _id , '<STR_LIT:name>' : self . owning_group . name } ) <EOL> elif self . location : <EOL> return ( '<STR_LIT:location>' , { '<STR_LIT:id>' : self . location . location_id , <EOL> '<STR_LIT:name>' : self . location . display_name } ) <EOL> else : <EOL> return ( '<STR_LIT:user>' , self . _user_meta ( self . user_id ) ) <EOL> @ property <EOL> def owner_type ( self ) : <EOL> return self . owner [ <NUM_LIT:0> ] <EOL> @ property <EOL> def user_id ( self ) : <EOL> return self . report . individual or self . owner_id <EOL> @ property <EOL> def owner_id ( self ) : <EOL> if '<STR_LIT>' in self . case : <EOL> return self . case [ '<STR_LIT>' ] <EOL> elif '<STR_LIT>' in self . case : <EOL> return self . case [ '<STR_LIT>' ] <EOL> else : <EOL> return '<STR_LIT>' <EOL> @ property <EOL> @ memoized <EOL> def owning_group ( self ) : <EOL> mc = cache . caches [ '<STR_LIT:default>' ] <EOL> cache_key = "<STR_LIT>" % ( Group . __class__ . __name__ , self . owner_id ) <EOL> try : <EOL> if mc . has_key ( cache_key ) : <EOL> cached_obj = json . loads ( mc . get ( cache_key ) ) <EOL> wrapped = Group . wrap ( cached_obj ) <EOL> return wrapped <EOL> else : <EOL> group_obj = Group . get ( self . owner_id ) <EOL> mc . set ( cache_key , json . dumps ( group_obj . to_json ( ) ) ) <EOL> return group_obj <EOL> except Exception : <EOL> return None <EOL> @ memoized <EOL> def _get_username ( self , user_id ) : <EOL> username = self . report . usernames . get ( user_id ) <EOL> if not username : <EOL> mc = cache . caches [ '<STR_LIT:default>' ] <EOL> cache_key = "<STR_LIT>" % ( CouchUser . __class__ . __name__ , user_id ) <EOL> try : <EOL> if mc . has_key ( cache_key ) : <EOL> user_dict = json . loads ( mc . get ( cache_key ) ) <EOL> else : <EOL> user_obj = CouchUser . get_by_user_id ( user_id ) if user_id else None <EOL> if user_obj : <EOL> user_dict = user_obj . to_json ( ) <EOL> else : <EOL> user_dict = { } <EOL> cache_payload = json . dumps ( user_dict ) <EOL> mc . set ( cache_key , cache_payload ) <EOL> if user_dict == { } : <EOL> return None <EOL> else : <EOL> user_obj = CouchUser . wrap ( user_dict ) <EOL> username = user_obj . username <EOL> except Exception : <EOL> return None <EOL> return username <EOL> def parse_date ( self , date_string ) : <EOL> try : <EOL> return iso_string_to_datetime ( date_string ) <EOL> except : <EOL> try : <EOL> date_obj = dateutil . parser . parse ( date_string ) <EOL> if isinstance ( date_obj , datetime . datetime ) : <EOL> return date_obj . replace ( tzinfo = None ) <EOL> else : <EOL> return date_obj <EOL> except : <EOL> return date_string <EOL> class CaseDisplay ( CaseInfo ) : <EOL> @ property <EOL> def closed_display ( self ) : <EOL> return yesno ( self . is_closed , "<STR_LIT>" ) <EOL> @ property <EOL> def case_link ( self ) : <EOL> url = self . case_detail_url <EOL> if url : <EOL> return html . mark_safe ( "<STR_LIT>" % ( <EOL> self . case_detail_url , html . escape ( self . case_name_display ) ) ) <EOL> else : <EOL> return "<STR_LIT>" % self . case_name <EOL> @ property <EOL> def opened_on ( self ) : <EOL> return self . _dateprop ( '<STR_LIT>' , False ) <EOL> @ property <EOL> def modified_on ( self ) : <EOL> return self . _dateprop ( '<STR_LIT>' , False ) <EOL> @ property <EOL> def owner_display ( self ) : <EOL> owner_type , owner = self . owner <EOL> if owner_type == '<STR_LIT>' : <EOL> return '<STR_LIT>' % owner [ '<STR_LIT:name>' ] <EOL> else : <EOL> return owner [ '<STR_LIT:name>' ] <EOL> def user_not_found_display ( self , user_id ) : <EOL> return _ ( "<STR_LIT>" ) % user_id <EOL> @ property <EOL> def creating_user ( self ) : <EOL> user = super ( CaseDisplay , self ) . creating_user <EOL> if user is None : <EOL> return _ ( "<STR_LIT>" ) <EOL> else : <EOL> return user [ '<STR_LIT:name>' ] or self . user_not_found_display ( user [ '<STR_LIT:id>' ] ) </s>
