def location_notification_to_user(request): uid = request.POST.get('id', None) if uid is None: return reply(False, "Id not found", 400) u = Users.objects.get(id=uid) if u.device_arn is None: return reply(False, "User device arn is Null", 400) NotificationHelper().silent_location_push(u.device_arn) return reply(True, 'location notifications sent', 200)
def save_subscription_email(request): em = find_key_in_request('email', request) if em is not False: try: sub = SubscriptionEmail.objects.get(email=em) except: sub = SubscriptionEmail() sub.email = em sub.created_at = now() sub.save() else: return reply(False, 'Already subscribed', 400) return reply(True, 'success', 200)
def send_undelivered_hifis(request): posts = Hifireceived.objects.filter( flag=Hifireceived.FLAG_RECEIVED).filter( delivered=Hifireceived.NOT_DELIVERED).filter(receiver__status__in=[ Users.FLAG_ACTIVE, Users.FLAG_BACKGROUND ]).filter(post__sender__isnull=False) posts2 = [] tim = now() for p in posts: posts2.append(p) p.delivered = Hifireceived.DELIVERED p.received_at = tim p.updated_at = tim p.save() # Looping again because multiple notifications would come with slow processing posts3 = [] for p in posts2: if p.post.condition == 0 or p.post.condition == '0': pass receiver = p.receiver # Send receiver notification if receiver.device_arn is not None: try: sender_name = p.post.sender.fullname sender_image = p.post.sender.avatar.name message = p.post.post resp = NotificationHelper().notify_receiver( receiver.device_arn, sender_name, sender_image, message, p.post.id) if type(resp) == tuple: if 'EndpointDisabled' in resp[1]: receiver.delete_device_arn() except Exception as e: logger.error(e.with_traceback(None)) # Send activity notification friends = Users.objects.filter(friend_from__to_user=p.receiver) for f in friends: background = AsyncNotification(device_arn=f.device_arn, command='silent_activity_push') background.start() # Send hifi as chat sender = p.post.sender if sender != receiver: background2 = AsyncChatCommands(digits_id=sender.digitsid, from_user_did=sender.digitsid, to_user_did=receiver.digitsid, post=p.post.post, command='send_hifi_as_chat') background2.start() posts3.append(p) Hifireceived().notify_sender(posts3) return reply(True, 'pending messages delivered', 200)
def create_campaign(request): if request.method != 'POST': return reply(False, 'Incorrect request method, should be POST', 405) post = request.POST file_name = settings.BASE_DIR + '/' + "campaign.log" f = open(file_name, 'a') keys = [ 'type', 'name', 'budget', 'daily_cap', 'proximity', 'message', 'has_image', 'image_path', 'start_date', 'end_date', 'start_time', 'end_time', 'repeat', 'interest', 'store_id' ] strin = '' for k in keys: strin += k + ": " + post.get(k, '') + '\n' strin += "_" * 120 + '\n' f.write(strin) f.close() return reply(True, 'Campaign successfully created', 200)
def save_campus_ambassador(request): c = CampusAmbassador() c.first_name = find_key_in_request('first_name', request) c.last_name = find_key_in_request('last_name', request) c.email = find_key_in_request('email', request) c.university = find_key_in_request('university', request) c.major = find_key_in_request('major', request) c.phone = find_key_in_request('phone', request) c.fb = find_key_in_request('fb', request) c.country = find_key_in_request('country', request) c.how_popular = find_key_in_request('how_popular', request) c.referral = generate_referral(c.first_name) c.created_at = now() try: c.save() except Exception as e: logger.error(e.with_traceback(None)) return reply(False, 'failure', 501) msg_plain = render_to_string( settings.BASE_DIR + '/utils/templates/utils/verification.txt', { 'username': c.first_name, 'referral': c.referral }) msg_html = render_to_string( settings.BASE_DIR + '/utils/templates/utils/verification.html', { 'name': c.first_name, 'referral': c.referral }) try: send_mail('Welcome to Campus Ambassador program', msg_plain, '*****@*****.**', [c.email], html_message=msg_html) except Exception as e: logger.debug(e.with_traceback(None)) return reply(True, 'success', 200, {})
def view_campaign(request): """ :param request: :return: """ if request.method != 'GET': return reply(False, 'Incorrect request method, should be GET', 405) y = '' file_name = settings.BASE_DIR + '/' + "campaign.log" try: f = open(file_name, 'r') except FileNotFoundError: return HttpResponse('') for line in list(f): y += line.rstrip() + '<br>' f.close() return HttpResponse(y)
def location_notification(request): boto3.setup_default_session(region_name='us-east-1') client = boto3.client('sns', aws_access_key_id=settings.AWS_ACCESS_KEY_ID, aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY) # 1. Get all users from db whose location hasn't been updated in last 30 seconds and who are not flagged as logged # out and are not subscribed. Subscribe them to topic if not subscribed, update subscription status time_threshold = now() - timedelta(seconds=300) excluded_status = [Users.FLAG_LOGGED_OUT] to_sub = Users.objects.filter( location__updated_at__lt=time_threshold).exclude( status__in=excluded_status).filter(location_sub=0).exclude( device_arn__isnull=True) for u in to_sub: response = client.subscribe(TopicArn=settings.LOCATION_TOPIC_ARN, Protocol='application', Endpoint=u.device_arn) u.location_sub = response.get('SubscriptionArn') u.save() # 2. unsubscribe users who have a) updated location or b)are logged out or c) have no device arn to_unsub = Users.objects.filter( location__updated_at__gt=time_threshold) | Users.objects.filter( status__in=excluded_status) | Users.objects.filter( device_arn__isnull=True) to_unsub = to_unsub.exclude(location_sub=0) for u in to_unsub: response = client.unsubscribe(SubscriptionArn=u.location_sub, ) to_unsub.update(location_sub=0) # 3. send notification to all users in the topic message = r'''{ "default": "\"content-available\": \"1\"", "GCM": "{\"data\":{\"type\":\"silent\"}}", "APNS": "{ \"aps\" : { \"content-available\": \"1\"} }" }''' response = client.publish(TopicArn=settings.LOCATION_TOPIC_ARN, Message=message, MessageStructure='json') logger.info(response) return reply(True, 'location notifications sent', 200)