#NEWLINE# @ staticmethod #NEWLINE# def parse_rss( message ) : #NEWLINE# #INDENT# '\n            Parse Feeds into the CMS Module\n        ' #NEWLINE# db = current . db #NEWLINE# s3db = current . s3db #NEWLINE# table = s3db . msg_rss #NEWLINE# record = db ( ( table . message_id == message . message_id ) ) . select ( table . channel_id , table . title , table . from_address , table . body , table . date , table . location_id , table . tags , table . author , limitby = ( 0 , 1 ) ) . first ( ) #NEWLINE# if ( not record ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# post_table = s3db . cms_post #NEWLINE# body = ( record . body or record . title ) #NEWLINE# url = record . from_address #NEWLINE# if url : #NEWLINE# #INDENT# doc_table = s3db . doc_document #NEWLINE# exists = db ( ( doc_table . url == url ) ) . select ( doc_table . doc_id , limitby = ( 0 , 1 ) ) . first ( ) #NEWLINE# if exists : #NEWLINE# #INDENT# exists = db ( ( post_table . doc_id == exists . doc_id ) ) . select ( post_table . id , limitby = ( 0 , 1 ) ) . first ( ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# exists = db ( ( post_table . body == body ) ) . select ( post_table . id , limitby = ( 0 , 1 ) ) . first ( ) #NEWLINE# #UNINDENT# channel_id = record . channel_id #NEWLINE# tags = record . tags #NEWLINE# author = record . author #NEWLINE# if author : #NEWLINE# #INDENT# ptable = s3db . pr_person #NEWLINE# from nameparser import HumanName #NEWLINE# name = HumanName ( author ) #NEWLINE# first_name = name . first #NEWLINE# middle_name = name . middle #NEWLINE# last_name = name . last #NEWLINE# query = ( ( ( ptable . first_name == first_name ) & ( ptable . middle_name == middle_name ) ) & ( ptable . last_name == last_name ) ) #NEWLINE# pexists = db ( query ) . select ( ptable . id , limitby = ( 0 , 1 ) ) . first ( ) #NEWLINE# if pexists : #NEWLINE# #INDENT# person_id = pexists . id #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# person_id = ptable . insert ( first_name = first_name , middle_name = middle_name , last_name = last_name ) #NEWLINE# s3db . update_super ( ptable , dict ( id = person_id ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# person_id = None #NEWLINE# #UNINDENT# if exists : #NEWLINE# #INDENT# post_id = exists . id #NEWLINE# db ( ( post_table . id == post_id ) ) . update ( title = record . title , body = body , created_on = record . date , location_id = record . location_id , person_id = person_id ) #NEWLINE# ttable = db . cms_tag #NEWLINE# ltable = db . cms_tag_post #NEWLINE# query = ( ( ( ltable . post_id == post_id ) & ( ltable . mci == 1 ) ) & ( ltable . tag_id == ttable . id ) ) #NEWLINE# rows = db ( query ) . select ( ttable . name ) #NEWLINE# old_tags = [ r . name for r in rows ] #NEWLINE# new_tags = [ ] #NEWLINE# delete_tags = [ ] #NEWLINE# for tag in tags : #NEWLINE# #INDENT# if ( tag not in old_tags ) : #NEWLINE# #INDENT# new_tags . append ( tag ) #NEWLINE# #UNINDENT# #UNINDENT# for tag in old_tags : #NEWLINE# #INDENT# if ( tag not in tags ) : #NEWLINE# #INDENT# delete_tags . append ( tag ) #NEWLINE# #UNINDENT# #UNINDENT# if ( new_tags or delete_tags ) : #NEWLINE# #INDENT# lookup_tags = [ ] #NEWLINE# lookup_tags . extend ( new_tags ) #NEWLINE# lookup_tags . extend ( delete_tags ) #NEWLINE# _tags = db ( ttable . name . belongs ( lookup_tags ) ) . select ( ttable . id , ttable . name ) . as_dict ( key = 'name' ) #NEWLINE# #UNINDENT# for t in new_tags : #NEWLINE# #INDENT# tag = _tags . get ( t , None ) #NEWLINE# if tag : #NEWLINE# #INDENT# tag_id = tag [ 'id' ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# tag_id = ttable . insert ( name = t ) #NEWLINE# #UNINDENT# ltable . insert ( post_id = post_id , tag_id = tag_id , mci = 1 ) #NEWLINE# #UNINDENT# for t in delete_tags : #NEWLINE# #INDENT# tag = _tags . get ( t , None ) #NEWLINE# if tag : #NEWLINE# #INDENT# query = ( ( ( ( ltable . post_id == post_id ) & ( ltable . tag_id == tag [ 'id' ] ) ) & ( ltable . mci == 1 ) ) & ( ltable . deleted == False ) ) #NEWLINE# db ( query ) . delete ( ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# table = db . cms_series #NEWLINE# series_id = db ( ( table . name == 'News' ) ) . select ( table . id , cache = s3db . cache , limitby = ( 0 , 1 ) ) . first ( ) . id #NEWLINE# post_id = post_table . insert ( title = record . title , body = body , created_on = record . date , location_id = record . location_id , person_id = person_id , series_id = series_id , mci = 1 ) #NEWLINE# record = dict ( id = post_id ) #NEWLINE# s3db . update_super ( post_table , record ) #NEWLINE# if url : #NEWLINE# #INDENT# doc_table . insert ( doc_id = record [ 'doc_id' ] , url = url ) #NEWLINE# #UNINDENT# def lookup_pe( channel_id ) : #NEWLINE# #INDENT# ctable = s3db . msg_rss_channel #NEWLINE# channel_url = db ( ( ctable . channel_id == channel_id ) ) . select ( ctable . url , limitby = ( 0 , 1 ) ) . first ( ) . url #NEWLINE# ctable = s3db . pr_contact #NEWLINE# ptable = s3db . pr_pentity #NEWLINE# query = ( ( ( ctable . contact_method == 'RSS' ) & ( ctable . value == channel_url ) ) & ( ctable . pe_id == ptable . pe_id ) ) #NEWLINE# pe = db ( query ) . select ( ptable . pe_id , ptable . instance_type , limitby = ( 0 , 1 ) ) . first ( ) #NEWLINE# if pe : #NEWLINE# #INDENT# pe_type = pe . instance_type #NEWLINE# otable = s3db [ pe_type ] #NEWLINE# org_id = db ( ( otable . pe_id == pe . pe_id ) ) . select ( otable . id , limitby = ( 0 , 1 ) ) . first ( ) . id #NEWLINE# return ( pe_type , org_id ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ( None , None ) #NEWLINE# #UNINDENT# #UNINDENT# ( pe_type , org_id ) = current . cache . ram ( ( 'pe_channel_%s' % channel_id ) , ( lambda : lookup_pe ( channel_id ) ) , time_expire = 120 ) #NEWLINE# if ( pe_type == 'org_organisation' ) : #NEWLINE# #INDENT# s3db . cms_post_organisation . insert ( post_id = post_id , organisation_id = org_id ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( pe_type == 'org_group' ) : #NEWLINE# #INDENT# s3db . cms_post_organisation_group . insert ( post_id = post_id , group_id = org_id ) #NEWLINE# #UNINDENT# #UNINDENT# if tags : #NEWLINE# #INDENT# ttable = db . cms_tag #NEWLINE# ltable = db . cms_tag_post #NEWLINE# _tags = db ( ttable . name . belongs ( tags ) ) . select ( ttable . id , ttable . name ) . as_dict ( key = 'name' ) #NEWLINE# for t in tags : #NEWLINE# #INDENT# tag = _tags . get ( t , None ) #NEWLINE# if tag : #NEWLINE# #INDENT# tag_id = tag [ 'id' ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# tag_id = ttable . insert ( name = t ) #NEWLINE# #UNINDENT# ltable . insert ( post_id = post_id , tag_id = tag_id , mci = 1 ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return