def get_to_emails(params): """Get a list of email address to send the alert to, in this priority: 1. get_emails_master() pulls email addresses from the user's public Github account. 2. If an email address is included in the Tips/Kudos form, append that to the email list. Args: params (dict): A dictionary parsed form the POST request. Typically this is a POST request coming in from a Tips/Kudos send. Returns: list: An array of email addresses to send the email to. """ to_emails = [] to_username = params['username'].lstrip('@') to_emails = get_emails_master(to_username) if params.get('email'): to_emails.append(params['email']) return list(set(to_emails))
def send_tip_3(request): """Handle the third stage of sending a tip (the POST) Returns: JsonResponse: response with success state. """ response = { 'status': 'OK', 'message': _('Tip Created'), } is_user_authenticated = request.user.is_authenticated from_username = request.user.username if is_user_authenticated else '' primary_from_email = request.user.email if is_user_authenticated else '' access_token = request.user.profile.get_access_token( ) if is_user_authenticated else '' to_emails = [] params = json.loads(request.body) to_username = params['username'].lstrip('@') to_emails = get_emails_master(to_username) if params.get('email'): to_emails.append(params['email']) # If no primary email in session, try the POST data. If none, fetch from GH. if params.get('fromEmail'): primary_from_email = params['fromEmail'] elif access_token and not primary_from_email: primary_from_email = get_github_primary_email(access_token) to_emails = list(set(to_emails)) expires_date = timezone.now() + timezone.timedelta( seconds=params['expires_date']) # db mutations tip = Tip.objects.create( emails=to_emails, tokenName=params['tokenName'], amount=params['amount'], comments_priv=params['comments_priv'], comments_public=params['comments_public'], ip=get_ip(request), expires_date=expires_date, github_url=params['github_url'], from_name=params['from_name'], from_email=params['from_email'], from_username=from_username, username=params['username'], network=params['network'], tokenAddress=params['tokenAddress'], from_address=params['from_address'], is_for_bounty_fulfiller=params['is_for_bounty_fulfiller'], metadata=params['metadata'], recipient_profile=get_profile(to_username), sender_profile=get_profile(from_username), ) is_over_tip_tx_limit = False is_over_tip_weekly_limit = False max_per_tip = request.user.profile.max_tip_amount_usdt_per_tx if request.user.is_authenticated and request.user.profile else 500 if tip.value_in_usdt_now: is_over_tip_tx_limit = tip.value_in_usdt_now > max_per_tip if request.user.is_authenticated and request.user.profile: tips_last_week_value = tip.value_in_usdt_now tips_last_week = Tip.objects.exclude(txid='').filter( sender_profile=get_profile(from_username), created_on__gt=timezone.now() - timezone.timedelta(days=7)) for this_tip in tips_last_week: if this_tip.value_in_usdt_now: tips_last_week_value += this_tip.value_in_usdt_now is_over_tip_weekly_limit = tips_last_week_value > request.user.profile.max_tip_amount_usdt_per_week if is_over_tip_tx_limit: response['status'] = 'error' response['message'] = _( 'This tip is over the per-transaction limit of $') + str( max_per_tip) + ( '. Please try again later or contact support.') elif is_over_tip_weekly_limit: response['status'] = 'error' response['message'] = _( 'You are over the weekly tip send limit of $') + str( request.user.profile.max_tip_amount_usdt_per_week) + ( '. Please try again later or contact support.') return JsonResponse(response)
def send_3(request): """Handle the third stage of sending a kudos (the POST). This function is derived from send_tip_3. The request to send the kudos is added to the database, but the transaction has not happened yet. The txid is added in `send_kudos_4`. Returns: JsonResponse: The response with success state. """ response = { 'status': 'OK', 'message': _('Kudos Created'), } is_user_authenticated = request.user.is_authenticated from_username = request.user.username if is_user_authenticated else '' primary_from_email = request.user.email if is_user_authenticated else '' access_token = request.user.profile.get_access_token( ) if is_user_authenticated and request.user.profile else '' to_emails = [] params = json.loads(request.body) to_username = params.get('username', '').lstrip('@') to_emails = get_emails_master(to_username) email = params.get('email') if email: to_emails.append(email) # If no primary email in session, try the POST data. If none, fetch from GH. primary_from_email = params.get('fromEmail') if access_token and not primary_from_email: primary_from_email = get_github_primary_email(access_token) to_emails = list(set(to_emails)) # Validate that the token exists on the back-end kudos_id = params.get('kudosId') if not kudos_id: raise Http404 try: kudos_token_cloned_from = Token.objects.get(pk=kudos_id) except Token.DoesNotExist: raise Http404 # db mutations KudosTransfer.objects.create( emails=to_emails, # For kudos, `token` is a kudos.models.Token instance. kudos_token_cloned_from=kudos_token_cloned_from, amount=params['amount'], comments_public=params['comments_public'], ip=get_ip(request), github_url=params['github_url'], from_name=params['from_name'], from_email=params['from_email'], from_username=from_username, username=params['username'], network=params['network'], tokenAddress=params.get('tokenAddress', ''), from_address=params['from_address'], is_for_bounty_fulfiller=params['is_for_bounty_fulfiller'], metadata=params['metadata'], recipient_profile=get_profile(to_username), sender_profile=get_profile(from_username), ) return JsonResponse(response)
def assign_tip_to(tip, handle): tip.username = handle tip.emails = get_emails_master(handle) maybe_market_tip_to_email(tip, tip.emails) tip.metadata['is_for_bounty_fulfiller_handled'] = True return tip