<s> """<STR_LIT>""" <EOL> __authors__ = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> import json <EOL> import logging <EOL> from tornado import gen <EOL> from viewfinder . backend . base import util <EOL> from viewfinder . backend . db import vf_schema <EOL> from viewfinder . backend . db . base import DBObject <EOL> from viewfinder . backend . db . range_base import DBRangeObject <EOL> @ DBObject . map_table_attributes <EOL> class Notification ( DBRangeObject ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ ] <EOL> _table = DBObject . _schema . GetTable ( vf_schema . NOTIFICATION ) <EOL> def __init__ ( self , user_id = None , notification_id = None ) : <EOL> super ( Notification , self ) . __init__ ( ) <EOL> self . user_id = user_id <EOL> self . notification_id = notification_id <EOL> def GetInvalidate ( self ) : <EOL> """<STR_LIT>""" <EOL> return json . loads ( self . invalidate ) if self . invalidate is not None else None <EOL> def SetInvalidate ( self , invalidate_dict ) : <EOL> """<STR_LIT>""" <EOL> self . invalidate = json . dumps ( invalidate_dict ) <EOL> @ classmethod <EOL> @ gen . coroutine <EOL> def TryClearBadge ( cls , client , user_id , device_id , notification_id ) : <EOL> """<STR_LIT>""" <EOL> notification = Notification ( user_id , notification_id ) <EOL> notification . name = '<STR_LIT>' <EOL> notification . timestamp = util . GetCurrentTimestamp ( ) <EOL> notification . sender_id = user_id <EOL> notification . sender_device_id = device_id <EOL> notification . badge = <NUM_LIT:0> <EOL> success = yield notification . _TryUpdate ( client ) <EOL> raise gen . Return ( success ) <EOL> @ classmethod <EOL> @ gen . coroutine <EOL> def QueryLast ( cls , client , user_id , consistent_read = False ) : <EOL> """<STR_LIT>""" <EOL> notification_list = yield gen . Task ( Notification . RangeQuery , <EOL> client , <EOL> user_id , <EOL> range_desc = None , <EOL> limit = <NUM_LIT:1> , <EOL> col_names = None , <EOL> scan_forward = False , <EOL> consistent_read = consistent_read ) <EOL> raise gen . Return ( notification_list [ <NUM_LIT:0> ] if len ( notification_list ) > <NUM_LIT:0> else None ) <EOL> @ classmethod <EOL> @ gen . coroutine <EOL> def CreateForUser ( cls , client , operation , user_id , name , invalidate = None , <EOL> activity_id = None , viewpoint_id = None , seq_num_pair = None , <EOL> inc_badge = False , consistent_read = False ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> last_notification = yield Notification . QueryLast ( client , user_id , consistent_read = consistent_read ) <EOL> if last_notification is None : <EOL> notification_id = <NUM_LIT:1> <EOL> badge = <NUM_LIT:0> <EOL> else : <EOL> notification_id = last_notification . notification_id + <NUM_LIT:1> <EOL> badge = last_notification . badge <EOL> notification = Notification ( user_id , notification_id ) <EOL> notification . name = name <EOL> if invalidate is not None : <EOL> notification . SetInvalidate ( invalidate ) <EOL> notification . activity_id = activity_id <EOL> notification . viewpoint_id = viewpoint_id <EOL> if seq_num_pair is not None : <EOL> update_seq , viewed_seq = seq_num_pair <EOL> notification . update_seq = update_seq <EOL> if viewed_seq is not None and operation . user_id == user_id : <EOL> notification . viewed_seq = viewed_seq <EOL> if inc_badge : <EOL> badge += <NUM_LIT:1> <EOL> notification . badge = badge <EOL> notification . timestamp = operation . timestamp <EOL> notification . sender_id = operation . user_id <EOL> notification . sender_device_id = operation . device_id <EOL> notification . op_id = operation . operation_id <EOL> success = yield notification . _TryUpdate ( client ) <EOL> if success : <EOL> raise gen . Return ( notification ) <EOL> consistent_read = True <EOL> @ gen . coroutine <EOL> def _TryUpdate ( self , client ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> yield gen . Task ( self . Update , client , expected = { '<STR_LIT>' : False } ) <EOL> except Exception as e : <EOL> logging . info ( '<STR_LIT>' % ( self . notification_id , e ) ) <EOL> raise gen . Return ( False ) <EOL> raise gen . Return ( True ) </s>
