def hourly_service_email_analysis(): """ This function should be run in hourly interval only, It converts UTC to local time and check whether it is right time to send email or not """ result_set = get_all_subscriptions() for subscription in result_set: if get_local_datetime(subscription.user.timezone_offset).hour == 20: print(subscription.user.email, subscription.topic) if (subscription.subscription_status == 'ACTIVE') and ( get_local_datetime(subscription.user.timezone_offset, subscription.subscription_from) <= get_local_datetime(subscription.user.timezone_offset) <= get_local_datetime(subscription.user.timezone_offset, subscription.subscription_to)): analysis_data = prepare_twitter_analysis(subscription.topic) send_analysis(subscription, analysis_data) elif get_local_datetime( subscription.user.timezone_offset, subscription.subscription_to) < get_local_datetime( subscription.user.timezone_offset ) and subscription.subscription_status not in [ 'SUSPENDED', 'UNSUBSCRIBED', 'EXPIRED' ]: unsubscribe(subscription.user.email, subscription.id, status='EXPIRED')
def universal_service_email_analysis(): result_set = get_all_subscriptions() for subscription in result_set: print(subscription.user.email, subscription.topic) if (subscription.subscription_status == 'ACTIVE') and ( get_local_datetime(subscription.user.timezone_offset, subscription.subscription_from) <= get_local_datetime(subscription.user.timezone_offset) <= get_local_datetime(subscription.user.timezone_offset, subscription.subscription_to)): analysis_data = prepare_twitter_analysis(subscription.topic) send_analysis(subscription, analysis_data) elif get_local_datetime( subscription.user.timezone_offset, subscription.subscription_to) < get_local_datetime( subscription.user.timezone_offset ) and subscription.subscription_status not in [ 'SUSPENDED', 'UNSUBSCRIBED', 'EXPIRED' ]: unsubscribe(subscription.user.email, subscription.id, status='EXPIRED')
def hourly_service_plan_update(): upcoming_plan_set = get_all_upcoming_user_plans() for user in get_all_users(): if user.plan_subscribed: if get_local_datetime( user.timezone_offset, (user.plan_subscribed_at + timedelta(days=user.plan_subscribed.plan_duration) )) <= get_local_datetime(user.timezone_offset): Subscribers.objects.filter(id=user.id).update( plan_status='EXPIRED') SubscriptionModel.objects.filter(user=user).update( subscription_status='EXPIRED') for upcoming in upcoming_plan_set: if get_local_datetime(upcoming.user.timezone_offset, upcoming.plan_starts_from) <= get_local_datetime( upcoming.user.timezone_offset): if upcoming.status == 'IN_QUEUE': SubscriptionModel.objects.filter(user=upcoming.user).update( subscription_status='EXPIRED') update_user_plan(upcoming.user, upcoming.plan) update_upcoming_plan_status(upcoming)
def send_analysis(subscription, analysis): current_date = get_local_datetime(subscription.user.timezone_offset) token = generate_token(subscription.user.email, expire=1, subscription_id=subscription.id) analysis[ 'unsubscribe_link'] = 'https://tweet-summary.herokuapp.com/api/account/unsubscribe?verification_code={}'.format( token) template = get_template("app_ui/email_templates/tweet_analysis.html") mail_data = { 'subject': '{} Tweet analysis on {}'.format(current_date.date(), subscription.topic), 'email': subscription.user.email, 'html_text': template.render(analysis), } mail_data['plain_text'] = strip_tags(mail_data['html_text']) return send_email(mail_data)
def post(self, request, format=None): if not request.user.is_authenticated: return Response( data={'data': 'Authentication Failed, Please login'}, status=status.HTTP_401_UNAUTHORIZED) if request_contain_keys( request.POST, ['subscribe_start_date', 'subscribe_end_date', 'search_topic']): current_datetime = get_local_datetime(request.user.timezone_offset) start_date = datetime.strptime( request.POST['subscribe_start_date'], '%Y-%m-%d') end_date = datetime.strptime(request.POST['subscribe_end_date'], '%Y-%m-%d') if current_datetime.date() > start_date.date(): return Response( status=status.HTTP_400_BAD_REQUEST, data={ 'data': 'Subscription start date cannot be less than current date' }) elif start_date > end_date: return Response( status=status.HTTP_400_BAD_REQUEST, data={ 'data': 'Subscription start date cannot be greater than end date' }) if ( end_date - start_date ).days > request.user.plan_subscribed.subscription_period_max_days: return Response( status=status.HTTP_400_BAD_REQUEST, data={ 'data': 'Subscription period exceeded select plan {} days limit!!' .format(request.user.plan_subscribed. subscription_period_max_days) }) response = is_action_allowed(request.user) if response is True: subscription, status_msg = add_subscription( request.user, request.POST['search_topic'], start_date, end_date) else: return Response(data={'data': response}, status=status.HTTP_400_BAD_REQUEST) if isinstance(subscription, SubscriptionModel): if subscription.subscription_status == 'IDLE': token = generate_token(email=subscription.user.email, subscription_id=subscription.id) confirmation_url = '{}/api/account/confirm_subscription?verification_code={}&format=json'.format( get_host_origin(request), token) send_subscription_verification_link( subscription, confirmation_url) if status_msg == 'CREATED': return Response( status=status.HTTP_200_OK, data={ 'data': 'Subscription created, please check email to verify the subscription' }) elif status_msg == 'UPDATED': return Response(status=status.HTTP_200_OK, data={'data': 'Subscription updated'}) else: if status_msg == 'NOT_VERIFIED': return Response(status=status.HTTP_400_BAD_REQUEST, data={'data': 'Email is not verified!!'}) if status_msg == 'QUOTA_EXHAUSTED': return Response( status=status.HTTP_400_BAD_REQUEST, data={ 'data': 'You have reached your ACTIVE subscriptions quota!!' }) return Response(status=status.HTTP_400_BAD_REQUEST, data={'data': 'Something went wrong!!'})