<s> from gluon import current <EOL> from gluon . html import * <EOL> from gluon . storage import Storage <EOL> from s3 . s3utils import S3CustomController <EOL> THEME = "<STR_LIT>" <EOL> class index ( S3CustomController ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( self ) : <EOL> response = current . response <EOL> output = { } <EOL> s3 = response . s3 <EOL> s3 . jquery_ready . append ( '''<STR_LIT>''' ) <EOL> s3db = current . s3db <EOL> list_id = "<STR_LIT>" <EOL> layout = s3db . req_req_list_layout <EOL> limit = <NUM_LIT:4> <EOL> resource = s3db . resource ( "<STR_LIT>" ) <EOL> s3db . req_customise_req_fields ( ) <EOL> list_fields = s3db . get_config ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> from s3 . s3query import FS <EOL> resource . add_filter ( FS ( "<STR_LIT>" ) != True ) <EOL> orderby = "<STR_LIT>" <EOL> output [ "<STR_LIT>" ] = latest_records ( resource , layout , list_id , limit , list_fields , orderby ) <EOL> list_id = "<STR_LIT>" <EOL> layout = s3db . req_commit_list_layout <EOL> resource = s3db . resource ( "<STR_LIT>" ) <EOL> s3db . req_customise_commit_fields ( ) <EOL> list_fields = s3db . get_config ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> resource . add_filter ( FS ( "<STR_LIT>" ) != True ) <EOL> output [ "<STR_LIT>" ] = latest_records ( resource , layout , list_id , limit , list_fields , orderby ) <EOL> table = s3db . cms_post <EOL> ltable = s3db . cms_post_module <EOL> query = ( ltable . module == "<STR_LIT:default>" ) & ( ltable . resource == "<STR_LIT:index>" ) & ( ltable . post_id == table . id ) & ( table . deleted != True ) <EOL> item = current . db ( query ) . select ( table . id , <EOL> table . body , <EOL> limitby = ( <NUM_LIT:0> , <NUM_LIT:1> ) ) . first ( ) <EOL> if item : <EOL> what_we_do = DIV ( XML ( item . body ) ) <EOL> if current . auth . s3_has_role ( "<STR_LIT>" ) : <EOL> if s3 . crud . formstyle == "<STR_LIT>" : <EOL> _class = "<STR_LIT>" <EOL> else : <EOL> _class = "<STR_LIT>" <EOL> what_we_do . append ( A ( current . T ( "<STR_LIT>" ) , <EOL> _href = URL ( c = "<STR_LIT>" , f = "<STR_LIT>" , <EOL> args = [ item . id , "<STR_LIT>" ] , <EOL> vars = { "<STR_LIT>" : "<STR_LIT:default>" , <EOL> "<STR_LIT>" : "<STR_LIT:index>" , <EOL> } ) , <EOL> _class = "<STR_LIT>" % _class ) ) <EOL> else : <EOL> what_we_do = DIV ( ) <EOL> if current . auth . s3_has_role ( "<STR_LIT>" ) : <EOL> if s3 . crud . formstyle == "<STR_LIT>" : <EOL> _class = "<STR_LIT>" <EOL> else : <EOL> _class = "<STR_LIT>" <EOL> what_we_do . append ( A ( current . T ( "<STR_LIT>" ) , <EOL> _href = URL ( c = "<STR_LIT>" , f = "<STR_LIT>" , <EOL> args = [ "<STR_LIT>" ] , <EOL> vars = { "<STR_LIT>" : "<STR_LIT:default>" , <EOL> "<STR_LIT>" : "<STR_LIT:index>" , <EOL> } ) , <EOL> _class = "<STR_LIT>" % _class ) ) <EOL> output [ "<STR_LIT>" ] = what_we_do <EOL> self . _view ( THEME , "<STR_LIT>" ) <EOL> return output <EOL> def latest_records ( resource , layout , list_id , limit , list_fields , orderby ) : <EOL> """<STR_LIT>""" <EOL> datalist , numrows , ids = resource . datalist ( fields = list_fields , <EOL> start = None , <EOL> limit = limit , <EOL> list_id = list_id , <EOL> orderby = orderby , <EOL> layout = layout ) <EOL> if numrows == <NUM_LIT:0> : <EOL> from s3 . s3crud import S3CRUD <EOL> table = resource . table <EOL> if "<STR_LIT>" in table : <EOL> available_records = current . db ( table . deleted != True ) <EOL> else : <EOL> available_records = current . db ( table . _id > <NUM_LIT:0> ) <EOL> if available_records . select ( table . _id , <EOL> limitby = ( <NUM_LIT:0> , <NUM_LIT:1> ) ) . first ( ) : <EOL> msg = DIV ( S3CRUD . crud_string ( resource . tablename , <EOL> "<STR_LIT>" ) , <EOL> _class = "<STR_LIT>" ) <EOL> else : <EOL> msg = DIV ( S3CRUD . crud_string ( resource . tablename , <EOL> "<STR_LIT>" ) , <EOL> _class = "<STR_LIT>" ) <EOL> data = msg <EOL> else : <EOL> dl = datalist . html ( ) <EOL> data = dl <EOL> return data <EOL> class subscriptions ( S3CustomController ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( self ) : <EOL> """<STR_LIT>""" <EOL> T = current . T <EOL> auth = current . auth <EOL> if not auth . s3_logged_in ( ) : <EOL> auth . permission . fail ( ) <EOL> resources = [ dict ( resource = "<STR_LIT>" , <EOL> url = "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) ) , <EOL> ] <EOL> from s3 . s3filter import S3LocationFilter <EOL> filters = [ S3LocationFilter ( "<STR_LIT>" , <EOL> label = T ( "<STR_LIT>" ) , <EOL> levels = [ "<STR_LIT>" ] , <EOL> options = self . _options ( "<STR_LIT>" ) , <EOL> widget = "<STR_LIT>" , <EOL> cols = <NUM_LIT:3> , <EOL> resource = "<STR_LIT>" , <EOL> _name = "<STR_LIT>" ) , <EOL> ] <EOL> title = T ( "<STR_LIT>" ) <EOL> self . _view ( THEME , "<STR_LIT>" ) <EOL> form = self . _manage_subscriptions ( resources , filters ) <EOL> return dict ( title = title , form = form ) <EOL> @ staticmethod <EOL> def _options ( fieldname ) : <EOL> """<STR_LIT>""" <EOL> db = current . db <EOL> if fieldname == "<STR_LIT>" : <EOL> table = current . s3db . gis_location <EOL> query = ( table . deleted == False ) & ( table . level == "<STR_LIT>" ) <EOL> rows = db ( query ) . select ( table . id ) <EOL> options = [ row . id for row in rows ] <EOL> return options <EOL> def _manage_subscriptions ( self , resources , filters ) : <EOL> """<STR_LIT>""" <EOL> from gluon . sqlhtml import SQLFORM <EOL> from gluon . validators import IS_IN_SET <EOL> from s3 . s3widgets import S3GroupedOptionsWidget <EOL> T = current . T <EOL> labels = Storage ( <EOL> RESOURCES = T ( "<STR_LIT>" ) , <EOL> NOTIFY_ON = T ( "<STR_LIT>" ) , <EOL> FREQUENCY = T ( "<STR_LIT>" ) , <EOL> NOTIFY_BY = T ( "<STR_LIT>" ) , <EOL> MORE = T ( "<STR_LIT>" ) , <EOL> LESS = T ( "<STR_LIT>" ) , <EOL> ) <EOL> messages = Storage ( <EOL> ERROR = T ( "<STR_LIT>" ) , <EOL> SUCCESS = T ( "<STR_LIT>" ) , <EOL> ) <EOL> subscription = self . _get_subscription ( ) <EOL> formstyle = SQLFORM . formstyles . bootstrap <EOL> form = FORM ( _id = "<STR_LIT>" , <EOL> hidden = { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> from s3 . s3filter import S3FilterForm <EOL> filter_form = S3FilterForm ( filters , clear = False ) <EOL> fieldset = FIELDSET ( filter_form . fields ( None , <EOL> subscription [ "<STR_LIT>" ] ) , <EOL> _id = "<STR_LIT>" ) <EOL> form . append ( fieldset ) <EOL> rows = [ ] <EOL> stable = current . s3db . pr_subscription <EOL> selector = S3GroupedOptionsWidget ( cols = <NUM_LIT:1> ) <EOL> rows . append ( ( "<STR_LIT>" , <EOL> "<STR_LIT>" % labels . NOTIFY_ON , <EOL> selector ( stable . notify_on , <EOL> subscription [ "<STR_LIT>" ] , <EOL> _id = "<STR_LIT>" ) , <EOL> "<STR_LIT>" ) ) <EOL> switch = S3GroupedOptionsWidget ( cols = <NUM_LIT:1> , multiple = False , sort = False ) <EOL> rows . append ( ( "<STR_LIT>" , <EOL> "<STR_LIT>" % labels . FREQUENCY , <EOL> switch ( stable . frequency , <EOL> subscription [ "<STR_LIT>" ] , <EOL> _id = "<STR_LIT>" ) , <EOL> "<STR_LIT>" ) ) <EOL> fieldset = formstyle ( form , rows ) <EOL> fieldset . insert ( <NUM_LIT:0> , <EOL> DIV ( SPAN ( [ I ( _class = "<STR_LIT>" ) , labels . MORE ] , <EOL> _class = "<STR_LIT>" , <EOL> _style = "<STR_LIT>" ) , <EOL> SPAN ( [ I ( _class = "<STR_LIT>" ) , labels . LESS ] , <EOL> _class = "<STR_LIT>" ) , <EOL> _id = "<STR_LIT>" , <EOL> _class = "<STR_LIT>" ) ) <EOL> form . append ( fieldset ) <EOL> row = ( "<STR_LIT>" , "<STR_LIT>" , <EOL> INPUT ( _type = "<STR_LIT>" , _value = "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> fieldset = formstyle ( form , [ row ] ) <EOL> form . append ( fieldset ) <EOL> script = URL ( c = "<STR_LIT>" , f = "<STR_LIT>" , args = [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> response = current . response <EOL> response . s3 . scripts . append ( script ) <EOL> if form . accepts ( current . request . post_vars , <EOL> current . session , <EOL> formname = "<STR_LIT>" , <EOL> keepvalues = True ) : <EOL> formvars = form . vars <EOL> listify = lambda x : None if not x else x if type ( x ) is list else [ x ] <EOL> subscription [ "<STR_LIT>" ] = [ resources [ <NUM_LIT:0> ] ] <EOL> subscription [ "<STR_LIT>" ] = form . request_vars . get ( "<STR_LIT>" , None ) <EOL> subscription [ "<STR_LIT>" ] = listify ( formvars . notify_on ) <EOL> subscription [ "<STR_LIT>" ] = formvars . frequency <EOL> subscription [ "<STR_LIT>" ] = [ "<STR_LIT>" ] <EOL> success = self . _update_subscription ( subscription ) <EOL> if success : <EOL> response . confirmation = messages . SUCCESS <EOL> else : <EOL> response . error = messages . ERROR <EOL> return form <EOL> def _get_subscription ( self ) : <EOL> """<STR_LIT>""" <EOL> db = current . db <EOL> s3db = current . s3db <EOL> pe_id = current . auth . user . pe_id <EOL> stable = s3db . pr_subscription <EOL> ftable = s3db . pr_filter <EOL> query = ( stable . pe_id == pe_id ) & ( stable . deleted != True ) <EOL> left = ftable . on ( ftable . id == stable . filter_id ) <EOL> row = db ( query ) . select ( stable . id , <EOL> stable . notify_on , <EOL> stable . frequency , <EOL> ftable . id , <EOL> ftable . query , <EOL> left = left , <EOL> limitby = ( <NUM_LIT:0> , <NUM_LIT:1> ) ) . first ( ) <EOL> output = { "<STR_LIT>" : pe_id } <EOL> get_vars = { } <EOL> if row : <EOL> s = getattr ( row , "<STR_LIT>" ) <EOL> f = getattr ( row , "<STR_LIT>" ) <EOL> rtable = s3db . pr_subscription_resource <EOL> query = ( rtable . subscription_id == s . id ) & ( rtable . deleted != True ) <EOL> rows = db ( query ) . select ( rtable . id , <EOL> rtable . resource , <EOL> rtable . url , <EOL> rtable . last_check_time , <EOL> rtable . next_check_time ) <EOL> if f . query : <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> try : <EOL> import simplejson as json <EOL> except : <EOL> import gluon . contrib . simplejson as json <EOL> filters = json . loads ( f . query ) <EOL> for k , v in filters : <EOL> if v is None : <EOL> continue <EOL> if k in get_vars : <EOL> if type ( get_vars [ k ] ) is list : <EOL> get_vars [ k ] . append ( v ) <EOL> else : <EOL> get_vars [ k ] = [ get_vars [ k ] , v ] <EOL> else : <EOL> get_vars [ k ] = v <EOL> output . update ( { "<STR_LIT:id>" : s . id , <EOL> "<STR_LIT>" : f . id , <EOL> "<STR_LIT>" : get_vars , <EOL> "<STR_LIT>" : rows , <EOL> "<STR_LIT>" : s . notify_on , <EOL> "<STR_LIT>" : s . frequency , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] <EOL> } ) <EOL> else : <EOL> output . update ( { "<STR_LIT:id>" : None , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : get_vars , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : stable . notify_on . default , <EOL> "<STR_LIT>" : stable . frequency . default , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] <EOL> } ) <EOL> return output <EOL> def _update_subscription ( self , subscription ) : <EOL> """<STR_LIT>""" <EOL> db = current . db <EOL> s3db = current . s3db <EOL> pe_id = subscription [ "<STR_LIT>" ] <EOL> filter_id = subscription [ "<STR_LIT>" ] <EOL> filters = subscription . get ( "<STR_LIT>" ) <EOL> if filters : <EOL> ftable = s3db . pr_filter <EOL> if not filter_id : <EOL> success = ftable . insert ( pe_id = pe_id , query = filters ) <EOL> filter_id = success <EOL> else : <EOL> success = db ( ftable . id == filter_id ) . update ( query = filters ) <EOL> if not success : <EOL> return None <EOL> stable = s3db . pr_subscription <EOL> subscription_id = subscription [ "<STR_LIT:id>" ] <EOL> frequency = subscription [ "<STR_LIT>" ] <EOL> if not subscription_id : <EOL> success = stable . insert ( pe_id = pe_id , <EOL> filter_id = filter_id , <EOL> notify_on = subscription [ "<STR_LIT>" ] , <EOL> frequency = frequency , <EOL> method = subscription [ "<STR_LIT>" ] ) <EOL> subscription_id = success <EOL> else : <EOL> success = db ( stable . id == subscription_id ) . update ( <EOL> pe_id = pe_id , <EOL> filter_id = filter_id , <EOL> notify_on = subscription [ "<STR_LIT>" ] , <EOL> frequency = frequency , <EOL> method = subscription [ "<STR_LIT>" ] ) <EOL> if not success : <EOL> return None <EOL> rtable = s3db . pr_subscription_resource <EOL> subscribe = subscription . get ( "<STR_LIT>" ) <EOL> if subscribe : <EOL> from datetime import datetime , timedelta <EOL> now = datetime . utcnow ( ) <EOL> resources = subscription [ "<STR_LIT>" ] <EOL> subscribed = { } <EOL> timestamps = { } <EOL> if resources : <EOL> for r in resources : <EOL> subscribed [ ( r . resource , r . url ) ] = r . id <EOL> timestamps [ r . id ] = ( r . last_check_time , <EOL> r . next_check_time ) <EOL> intervals = s3db . pr_subscription_check_intervals <EOL> interval = timedelta ( minutes = intervals . get ( frequency , <NUM_LIT:0> ) ) <EOL> keep = set ( ) <EOL> fk = '''<STR_LIT>''' % subscription_id <EOL> for new in subscribe : <EOL> resource , url = new [ "<STR_LIT>" ] , new [ "<STR_LIT:url>" ] <EOL> if ( resource , url ) not in subscribed : <EOL> unsubscribed = { "<STR_LIT>" : True , <EOL> "<STR_LIT>" : fk , <EOL> "<STR_LIT>" : resource , <EOL> "<STR_LIT:url>" : url } <EOL> rtable . update_or_insert ( _key = unsubscribed , <EOL> deleted = False , <EOL> deleted_fk = None , <EOL> subscription_id = subscription_id , <EOL> resource = resource , <EOL> url = url , <EOL> last_check_time = now , <EOL> next_check_time = None ) <EOL> else : <EOL> record_id = subscribed [ ( resource , url ) ] <EOL> last_check_time , next_check_time = timestamps [ record_id ] <EOL> data = { } <EOL> if not last_check_time : <EOL> last_check_time = now <EOL> data [ "<STR_LIT>" ] = last_check_time <EOL> due = last_check_time + interval <EOL> if next_check_time != due : <EOL> data [ "<STR_LIT>" ] = due <EOL> if data : <EOL> db ( rtable . id == record_id ) . update ( ** data ) <EOL> keep . add ( record_id ) <EOL> unsubscribe = set ( subscribed . values ( ) ) - keep <EOL> db ( rtable . id . belongs ( unsubscribe ) ) . update ( deleted = True , <EOL> deleted_fk = fk , <EOL> subscription_id = None ) <EOL> subscription [ "<STR_LIT:id>" ] = subscription_id <EOL> subscription [ "<STR_LIT>" ] = filter_id <EOL> return subscription </s>
