<s> """<STR_LIT>""" <EOL> from base import * <EOL> from django . utils import simplejson <EOL> from django . conf import settings <EOL> from smart . common . rdf_tools . rdf_ontology import ontology <EOL> from smart . common . rdf_tools . util import rdf , foaf , vcard , sp , serialize_rdf , parse_rdf , bound_graph , URIRef , Namespace <EOL> from smart . lib import utils <EOL> from smart . models . apps import * <EOL> from smart . models . accounts import * <EOL> from smart . triplestore import * <EOL> from string import Template <EOL> import re <EOL> import datetime <EOL> class Record ( Object ) : <EOL> Meta = BaseMeta ( ) <EOL> full_name = models . CharField ( max_length = <NUM_LIT> , null = False ) <EOL> def __unicode__ ( self ) : <EOL> return '<STR_LIT>' % self . id <EOL> def generate_direct_access_token ( self , account , token_secret = None ) : <EOL> u = RecordDirectAccessToken . objects . create ( <EOL> record = self , <EOL> account = account , <EOL> token_secret = token_secret <EOL> ) <EOL> u . save ( ) <EOL> return u <EOL> @ classmethod <EOL> def search_records ( cls , query ) : <EOL> try : <EOL> c = TripleStore ( ) <EOL> ids = parse_rdf ( c . sparql ( query ) ) <EOL> except Exception , e : <EOL> return None <EOL> from smart . models . record_object import RecordObject <EOL> demographics = RecordObject [ sp . Demographics ] <EOL> subjects = [ p [ <NUM_LIT:0> ] for p in ids . triples ( ( None , rdf [ '<STR_LIT:type>' ] , <EOL> sp . Demographics ) ) ] <EOL> ret = c . get_contexts ( subjects ) <EOL> return ret <EOL> @ classmethod <EOL> def rdf_to_objects ( cls , res ) : <EOL> if res is None : <EOL> return None <EOL> m = parse_rdf ( res ) <EOL> record_list = [ ] <EOL> q = """<STR_LIT>""" <EOL> people = list ( m . query ( q ) ) <EOL> for p in people : <EOL> record = Record ( ) <EOL> record . id = re . search ( <EOL> "<STR_LIT>" , str ( p [ <NUM_LIT:5> ] ) ) . group ( <NUM_LIT:1> ) <EOL> record . fn , record . ln , record . dob , record . gender , record . zipcode = p [ : <NUM_LIT:5> ] <EOL> record_list . append ( record ) <EOL> return record_list <EOL> class AccountApp ( Object ) : <EOL> account = models . ForeignKey ( Account ) <EOL> app = models . ForeignKey ( PHA ) <EOL> class Meta : <EOL> app_label = APP_LABEL <EOL> unique_together = ( ( '<STR_LIT>' , '<STR_LIT>' ) , ) <EOL> class RecordDirectAccessToken ( Object ) : <EOL> record = models . ForeignKey ( <EOL> Record , related_name = '<STR_LIT>' , null = False ) <EOL> account = models . ForeignKey ( <EOL> Account , related_name = '<STR_LIT>' , null = False ) <EOL> token = models . CharField ( max_length = <NUM_LIT> , unique = True ) <EOL> token_secret = models . CharField ( max_length = <NUM_LIT> , null = True ) <EOL> expires_at = models . DateTimeField ( null = False ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> if not self . token : <EOL> self . token = utils . random_string ( <NUM_LIT:30> ) <EOL> if self . expires_at is None : <EOL> minutes_to_expire = <NUM_LIT:30> <EOL> try : <EOL> minutes_to_expire = settings . MINUTES_TO_EXPIRE_DIRECT_ACCESS <EOL> except : <EOL> pass <EOL> self . expires_at = datetime . datetime . utcnow ( <EOL> ) + datetime . timedelta ( minutes = minutes_to_expire ) <EOL> super ( RecordDirectAccessToken , self ) . save ( * args , ** kwargs ) <EOL> class Meta : <EOL> app_label = APP_LABEL <EOL> class RecordAlert ( Object ) : <EOL> record = models . ForeignKey ( Record ) <EOL> alert_text = models . TextField ( null = False ) <EOL> alert_time = models . DateTimeField ( auto_now_add = True , null = False ) <EOL> triggering_app = models . ForeignKey ( <EOL> '<STR_LIT>' , null = False , related_name = '<STR_LIT>' ) <EOL> acknowledged_by = models . ForeignKey ( '<STR_LIT>' , null = True ) <EOL> acknowledged_at = models . DateTimeField ( null = True ) <EOL> class Meta : <EOL> app_label = APP_LABEL <EOL> @ classmethod <EOL> def from_rdf ( cls , rdfstring , record , app ) : <EOL> s = parse_rdf ( rdfstring ) <EOL> q = """<STR_LIT>""" <EOL> r = list ( s . query ( q ) ) <EOL> assert len ( r ) == <NUM_LIT:1> , "<STR_LIT>" % len ( r ) <EOL> ( notes , severity ) = r [ <NUM_LIT:0> ] <EOL> assert type ( notes ) == Literal <EOL> spcodes = Namespace ( "<STR_LIT>" ) <EOL> assert severity in [ spcodes . information , spcodes . warning , <EOL> spcodes . critical ] <EOL> a = RecordAlert ( <EOL> record = record , <EOL> alert_text = str ( notes ) , <EOL> triggering_app = app <EOL> ) <EOL> a . save ( ) <EOL> return a <EOL> def acknowledge ( self , account ) : <EOL> self . acknowledged_by = account <EOL> self . acknowledged_at = datetime . datetime . now ( ) <EOL> self . save ( ) <EOL> class LimitedAccount ( Account ) : <EOL> records = models . ManyToManyField ( Record , related_name = "<STR_LIT:+>" ) </s>
