<s> from OpenDataCatalog . opendata . models import Resource , DataType , Tag , CoordSystem , Url , UrlImage , Idea , IdeaImage <EOL> from OpenDataCatalog . suggestions . models import Suggestion <EOL> import simplejson as j <EOL> def tiny_resource_encoder ( obj ) : <EOL> return { "<STR_LIT:name>" : obj . name , <EOL> "<STR_LIT:id>" : obj . id , <EOL> "<STR_LIT:url>" : "<STR_LIT>" % ( obj . id ) <EOL> } <EOL> def short_resource_encoder ( obj ) : <EOL> return { "<STR_LIT:name>" : obj . name , <EOL> "<STR_LIT>" : obj . short_description , <EOL> "<STR_LIT>" : obj . release_date , <EOL> "<STR_LIT>" : obj . time_period , <EOL> "<STR_LIT>" : obj . organization , <EOL> "<STR_LIT>" : obj . division , <EOL> "<STR_LIT>" : list ( obj . tags . all ( ) ) , <EOL> "<STR_LIT>" : obj . area_of_interest , <EOL> "<STR_LIT>" : obj . is_published , <EOL> "<STR_LIT>" : obj . rating . score , <EOL> "<STR_LIT:id>" : obj . id , <EOL> "<STR_LIT:url>" : "<STR_LIT>" % ( obj . id ) <EOL> } <EOL> def full_resource_encoder ( obj ) : <EOL> return { "<STR_LIT:name>" : obj . name , <EOL> "<STR_LIT>" : obj . short_description , <EOL> "<STR_LIT>" : obj . release_date , <EOL> "<STR_LIT>" : obj . time_period , <EOL> "<STR_LIT>" : obj . organization , <EOL> "<STR_LIT>" : obj . division , <EOL> "<STR_LIT>" : obj . usage , <EOL> "<STR_LIT>" : list ( obj . tags . all ( ) ) , <EOL> "<STR_LIT>" : list ( obj . data_types . all ( ) ) , <EOL> "<STR_LIT>" : obj . data_formats , <EOL> "<STR_LIT:description>" : obj . description , <EOL> "<STR_LIT>" : obj . contact_phone , <EOL> "<STR_LIT>" : obj . contact_email , <EOL> "<STR_LIT>" : obj . contact_url , <EOL> "<STR_LIT>" : obj . updates . update_frequency if obj . updates else None , <EOL> "<STR_LIT>" : obj . update_frequency , <EOL> "<STR_LIT>" : obj . area_of_interest , <EOL> "<STR_LIT>" : obj . is_published , <EOL> "<STR_LIT>" : obj . created_by . username , <EOL> "<STR_LIT>" : obj . last_updated_by . username , <EOL> "<STR_LIT>" : obj . last_updated , <EOL> "<STR_LIT>" : obj . metadata_contact , <EOL> "<STR_LIT>" : obj . metadata_notes , <EOL> "<STR_LIT>" : list ( obj . coord_sys . all ( ) ) , <EOL> "<STR_LIT>" : obj . proj_coord_sys , <EOL> "<STR_LIT>" : obj . rating . score , <EOL> "<STR_LIT>" : list ( obj . url_set . all ( ) ) , <EOL> "<STR_LIT:id>" : obj . id <EOL> } <EOL> def encode_resource ( resource_encoder ) : <EOL> def encode_resource_with_encoder ( obj ) : <EOL> if isinstance ( obj , Resource ) : <EOL> return resource_encoder ( obj ) <EOL> elif isinstance ( obj , Suggestion ) : <EOL> return { "<STR_LIT:text>" : obj . text , <EOL> "<STR_LIT>" : obj . suggested_by . username , <EOL> "<STR_LIT>" : obj . suggested_date , <EOL> "<STR_LIT>" : obj . last_modified_date , <EOL> "<STR_LIT>" : obj . rating . votes , <EOL> "<STR_LIT:url>" : "<STR_LIT>" % ( obj . id ) , <EOL> "<STR_LIT:id>" : obj . pk <EOL> } <EOL> elif isinstance ( obj , Idea ) : <EOL> return { "<STR_LIT:title>" : obj . title , <EOL> "<STR_LIT:id>" : obj . pk , <EOL> "<STR_LIT:description>" : obj . description , <EOL> "<STR_LIT>" : obj . author , <EOL> "<STR_LIT>" : obj . created_by . username , <EOL> "<STR_LIT>" : obj . created_by_date , <EOL> "<STR_LIT>" : obj . updated_by . username , <EOL> "<STR_LIT>" : obj . updated_by_date , <EOL> "<STR_LIT>" : list ( obj . resources . all ( ) ) , <EOL> "<STR_LIT>" : list ( IdeaImage . objects . filter ( idea = obj ) . all ( ) ) <EOL> } <EOL> elif isinstance ( obj , Url ) : <EOL> return { "<STR_LIT:url>" : obj . url , <EOL> "<STR_LIT:label>" : obj . url_label , <EOL> "<STR_LIT:type>" : obj . url_type . url_type , <EOL> "<STR_LIT>" : list ( obj . urlimage_set . all ( ) ) <EOL> } <EOL> elif isinstance ( obj , CoordSystem ) : <EOL> return { "<STR_LIT:name>" : obj . name , <EOL> "<STR_LIT:description>" : obj . description , <EOL> "<STR_LIT>" : obj . EPSG_code <EOL> } <EOL> elif isinstance ( obj , UrlImage ) or isinstance ( obj , IdeaImage ) : <EOL> return { "<STR_LIT:title>" : obj . title , <EOL> "<STR_LIT:source>" : obj . source , <EOL> "<STR_LIT>" : obj . source_url , <EOL> "<STR_LIT>" : "<STR_LIT>" + obj . image . thumbnail . relative_url , <EOL> "<STR_LIT>" : obj . image . url <EOL> } <EOL> elif isinstance ( obj , DataType ) : <EOL> return obj . data_type <EOL> elif isinstance ( obj , Tag ) : <EOL> return { "<STR_LIT:name>" : obj . tag_name , <EOL> "<STR_LIT:url>" : "<STR_LIT>" % obj . tag_name } <EOL> elif hasattr ( obj , "<STR_LIT>" ) : <EOL> return obj . strftime ( "<STR_LIT>" ) <EOL> else : <EOL> raise TypeError ( repr ( obj ) + "<STR_LIT>" ) <EOL> return encode_resource_with_encoder <EOL> def json_encode ( obj , rsrc = short_resource_encoder ) : <EOL> return j . dumps ( obj , default = encode_resource ( rsrc ) ) <EOL> def json_load ( jsonstr ) : <EOL> return j . loads ( jsonstr ) </s>
