HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythondjangoMinor

Handling SMS replies to register/unregister phone numbers

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
handlingnumbersregisterrepliesphonesmsunregister

Problem

I'm working on a Django Twilio app, and I believe it's coming along well. I just want to improve my code writing skills. How can I improve the below code?

def save_number(request):
    from_number = request.POST.get('From', '')
    try:
        obj = SaveNumber.objects.get(phone_number=from_number)
        if obj == obj:
            subscribe = request.POST.get('Body', )
            if subscribe.lower().strip() == "stellar":
                enter = SaveNumber.objects.get(phone_number=from_number)
                enter.subscribed = True
                enter.save()
                r = twiml.Response()
                r.message("You're in. Always text outtahere, and we'll remove you.")
                return r
            elif subscribe.lower().strip() == "outtahere":
                remove = SaveNumber.objects.get(phone_number=from_number)
                remove.subscribed = False
                remove.save()
    except SaveNumber.DoesNotExist:
        obj = SaveNumber(phone_number=from_number)
        obj.save()
        r1 = twiml.Response()
        r1.message("Reply with stellar and you're in! :-)")
        return r1

Solution

Here is my alternative. Your original code calls the DB for the same record twice (potentially) -- the get_or_create does the same as your try/except and creates the record.

def save_number(request):
    from_number = request.POST.get('From', '')
    subscribe = request.POST.get('Body', '').lower().strip()    
    obj, created = SaveNumber.objects.get_or_create(phone_number=from_number)
    response = twiml.Response()

    if created:
        response.message("Reply with stellar and you're in! :-)")
    else:
        if subscribe == "stellar":
            obj.subscribed = True
            obj.save()
            response.message("You're in. Always text outtahere, and we'll remove you.")
        elif subscribe == "outtahere":
            obj.subscribed = False
            obj.save()
            response.message("You're out")
    return response


Disclaimer: i didnt actually test it theyre may be bugs

Code Snippets

def save_number(request):
    from_number = request.POST.get('From', '')
    subscribe = request.POST.get('Body', '').lower().strip()    
    obj, created = SaveNumber.objects.get_or_create(phone_number=from_number)
    response = twiml.Response()

    if created:
        response.message("Reply with stellar and you're in! :-)")
    else:
        if subscribe == "stellar":
            obj.subscribed = True
            obj.save()
            response.message("You're in. Always text outtahere, and we'll remove you.")
        elif subscribe == "outtahere":
            obj.subscribed = False
            obj.save()
            response.message("You're out")
    return response

Context

StackExchange Code Review Q#117521, answer score: 5

Revisions (0)

No revisions yet.