Best Python code snippet using localstack_python
push_notif.py
Source:push_notif.py
1import time, os2from apns import APNs, Frame, Payload3from mpns import MPNSTile, MPNSToast, MPNSRaw4from gcm import GCM5ANDROID_API_KEY = "AIzaSyDwyrdLCMru6MrmFZqAjIDEwRsPTON4lPc"6gcm_client = GCM(ANDROID_API_KEY)7# apns_client = APNs(use_sandbox=True, cert_file='app/lib/certs/push_cert.pem', key_file='app/lib/certs/push_key_no_pass.pem')8apns_client = APNs(cert_file='app/lib/certs/PushUguruCert.pem', key_file='app/lib/certs/PushUguruKeyNoPass.pem', use_sandbox=True)9### TODO: SEND TO USERS MULTIPLE DEVICES10### Edge Test Push Cases:11### 1. User has multiple devices (they receive all of them)12def send_windows_notification(message, user_mpns_token):13 toast = MPNSToast()14 toast.send(user_mpns_token, {"text1": message})15def send_ios_notification(message, user_apns_token):16 token_hex = user_apns_token17 if not os.environ.get('PRODUCTION'):18 message = 'ADMIN: ' + message19 import random20 identifier = random.getrandbits(32)21 payload = Payload(alert=message, sound="default", badge=1)22 apns_client.gateway_server.send_notification(user_apns_token, payload)23def send_android_notification(message, registration_id):24 data = {'message': message}25 gcm_client.plaintext_request(registration_id=registration_id, data=data)26def compose_push_notif_message(notif_key, args_tuple):27 return str(push_notif_copy[notif_key] % args_tuple)28def send_push_for_user_devices(user, notif_key, args_tuple):29 #TODO add delay30 message = compose_push_notif_message(notif_key, args_tuple)31 for device in user.devices:32 print str(user.name), 'is receiving', message33 print device.id, device.platform, device.push_notif, device.push_notif_enabled34 # if device is ios & push enabled35 if device.platform and device.push_notif and device.push_notif_enabled:36 device_os = device.platform.lower()37 if device_os == 'ios' and device.push_notif != "ERROR":38 user_apns_token = device.push_notif39 send_ios_notification(message, user_apns_token)40 if device_os == 'android':41 android_reg_id = device.push_notif42 send_android_notification(message, android_reg_id)43def send_message_to_receiver_support(sender, receiver):44 args_tuple = (45 sender.name.split(' ')[0].title()46 )47 if not delay_seconds:48 send_push_for_user_devices(receiver, 'support_message_received', args_tuple)49 else:50 send_push_for_user_devices.delay(user=receiver, \51 notif_key='support_message_received',52 args_tuple=args_tuple,53 countdown= delay_seconds )54def send_message_to_receiver(sender, receiver, course, delay_seconds=None):55 args_tuple = (56 sender.name.split(' ')[0].title(),57 course.short_name.upper()58 )59 if not delay_seconds:60 send_push_for_user_devices(receiver, 'message_received', args_tuple)61 else:62 send_push_for_user_devices.delay(user=sender, \63 notif_key='message_received',64 args_tuple=args_tuple,65 countdown= delay_seconds )66def send_student_has_accepted_to_guru(session, guru, delay_seconds=None):67 total_hours = session.hours68 #TODO FIX69 if not session.hours:70 session.hours = 171 session.minutes = 072 if session.minutes:73 total_hours += int((session.minutes / 60.0 * 100)/100)74 else:75 total_hours = session.hours76 estimated_price = session.request.DEFAULT_PRICE * total_hours77 args_tuple = (78 str(estimated_price)79 )80 if not delay_seconds:81 send_push_for_user_devices(guru, 'student_chose_guru', args_tuple)82 else:83 send_push_for_user_devices.delay(user=guru, \84 notif_key='student_chose_guru',85 args_tuple=args_tuple,86 countdown= delay_seconds )87def send_guru_proposal_to_student(proposal, student, delay_seconds=None):88 copy_string = 'guru_can_help'89 args_tuple = (90 proposal.guru.name.split(' ')[0].title(),91 str(10)92 )93 # if it is a question94 if proposal.request._type == 1:95 copy_string = "question_answered"96 args_tuple = (97 proposal.request.course.short_name.upper()98 )99 if not delay_seconds:100 send_push_for_user_devices(student, copy_string, args_tuple)101 else:102 send_push_for_user_devices.delay(user=student, \103 notif_key='guru_can_help',104 args_tuple=args_tuple,105 countdown= delay_seconds )106def send_student_request_to_guru(_request, guru, delay_seconds=None):107 #if the request is a question108 copy_string = 'student_request'109 print "type", _request._type110 args_tuple = None111 if _request.course:112 args_tuple = (113 _request.DEFAULT_PRICE,114 _request.time_estimate,115 _request.student.name.split(" ")[0],116 _request.course.short_name.upper()117 )118 # if it is a question119 if _request._type == 1:120 copy_string = "student_question"121 args_tuple = (122 _request.course.short_name.upper()123 )124 # if it is a question125 if _request._type == 2:126 copy_string = "student_task"127 args_tuple = (128 _request.category129 )130 # send push to to guru131 if not delay_seconds:132 send_push_for_user_devices(guru, copy_string, args_tuple)133 else:134 send_push_for_user_devices.delay(user=guru, \135 notif_key='student_request',136 args_tuple=args_tuple,137 countdown= delay_seconds )138# TODO , finish the copy139push_notif_copy = {140 "student_request": """Make $%s total in %smin helping %s in %s. Swipe for more details & increase response rate""",141 "student_question": """A student posted a question for %s. Answer it now before it expires!""",142 "student_task": """A student posted a %s task. Check it out now before its taken!""",143 "question_answered": """A guru has answered your %s question! Check it out now and accept or reject.""",144 "guru_can_help": """%s can help! Swipe for more details. %s min until this expires.""",145 "student_chose_guru": """Congrats! You're one step away from earning $%s, Swipe & start preparing now.""",146 "guru_student_canceled": "",147 "guru_student_rejected": "",148 "message_received":"""You have one new message from %s about %s""",149 "support_message_received":"""You have 1 new message from Uguru Support""",150 "ratings": """Please confirm that your %s session with %s is over""",...
listing2_6.py
Source:listing2_6.py
1import asyncio2async def delay(delay_seconds: int) -> int:3 print(f'sleeping for {delay_seconds} second(s)')4 await asyncio.sleep(delay_seconds)5 print(f'finished sleeping for {delay_seconds} second(s)')...
delay_functions.py
Source:delay_functions.py
1import asyncio2async def delay(delay_seconds: int) -> int:3 print(f'sleeping for {delay_seconds} second(s)')4 await asyncio.sleep(delay_seconds)5 print(f'finished sleeping for {delay_seconds} second(s)')...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!