Skip to content

Instantly share code, notes, and snippets.

@amackera
Created March 29, 2012 16:40
Show Gist options
  • Save amackera/2239640 to your computer and use it in GitHub Desktop.
Save amackera/2239640 to your computer and use it in GitHub Desktop.
get_outgoing_number
def get_outgoing_number(provider, incoming_number):
'''
Returns the outgoing number associated with this incoming number.
'''
def get_sms_settings():
# Memcached closure returns outgoing number for this student
try:
settings = SmsSettings.objects.get(phone_number = incoming_number)
except SmsSettings.DoesNotExist:
# Create settings record for this number
outgoing_number = get_provider_outgoing_number(provider)
safe_call_task(tq_save_sms_settings,
[incoming_number, outgoing_number])
return outgoing_number
if settings.sms_reply_number == None:
# Settings exist but no sms_reply_number has been set. Set it
outgoing_number = get_provider_outgoing_number(provider)
safe_call_task(tq_save_sms_settings,
[incoming_number, outgoing_number])
return outgoing_number
else:
return settings.sms_reply_number
number = get_object_through_memcache("sms_reply_number_%s" %incoming_number,
get_sms_settings,
timeout = 120)
if number:
# settings exist, and outgoing number exists for setting
return number
else:
# Something bad happened...
return False
@jskopek
Copy link

jskopek commented Mar 29, 2012

def get_outgoing_number(provider, incoming_number):
    def get_sms_settings():
        #get the user's outgoing phone number from their sms settings
        try:
            settings = SmsSettings.objects.get(phone_number = incoming_number)
        except SmsSettings.DoesNotExist:
            outgoing_number = None
        else:
            outgoing_number = settings.sms_reply_number

        # If no outgoing number is registered to this phone number, register one
        if not outgoing_number:
            outgoing_number = get_provider_outgoing_number(provider)
            safe_call_task(tq_save_sms_settings, [incoming_number, outgoing_number])

        return outgoing_number

    mc_string = "sms_reply_number_%s"%incoming_number
    timeout = 60 * 60 * 3 #lectures shouldn't take longer than 3 hours
    return get_object_through_memcache( mc_string, get_sms_settings, timeout = timeout)

@amackera
Copy link
Author

A good improvement, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment