示例#1
0
文件: sms.py 项目: alexnjoyce/myewb2
def send_sms(request, session=None):
    if not request.user.has_module_perms("conference"):
        return HttpResponseRedirect(reverse('conference_schedule'))
    
    s = get_object_or_none(ConferenceSession, id=session)
    response = None

    if request.method == 'POST':
        api = settings.TWILIO_API_VERSION
        sid = settings.TWILIO_ACCOUNT_SID
        token = settings.TWILIO_ACCOUNT_TOKEN
        
        form = ConferenceSmsForm(request.POST)
        
        if s:
            del(form.fields['grouping'])

        if form.is_valid():
            success = 0
            failed = 0
            
            registrations = []
            if s:
                registrations = ConferenceRegistration.objects.filter(user__in=list(s.attendees.all()))
            elif form.cleaned_data['grouping'] == 'all':
                registrations = ConferenceRegistration.objects.all()
            elif form.cleaned_data['grouping'] == 'internal':
                registrations = ConferenceRegistration.objects.filter(~Q(type__contains='day'))
            elif form.cleaned_data['grouping'] == 'external':
                registrations = ConferenceRegistration.objects.filter(type__contains='day')
            elif form.cleaned_data['grouping'] == 'alumni':
                registrations = ConferenceRegistration.objects.filter(type__contains='alumni')
            elif form.cleaned_data['grouping'] == 'hotel':
                registrations = ConferenceRegistration.objects.filter(Q(type__contains='single') | Q(type__contains='double'))
            elif form.cleaned_data['grouping'] == 'nohotel':
                registrations = ConferenceRegistration.objects.filter(Q(type__contains='nohotel') | Q(type__contains='alumni'))
            elif form.cleaned_data['grouping'] == 'nohotel-all':
                registrations = ConferenceRegistration.objects.filter(~Q(type__contains='single'), ~Q(type__contains='double'))
            
            registrations = registrations.filter(cancelled=False, cellphone__isnull=False, cellphone_optout__isnull=True)
            
            if not s and form.cleaned_data['grouping'] == 'all':
                registrations = list(registrations)
                registrations.extend(list(ConferenceCellNumber.objects.filter(cellphone_optout__isnull=True)))
            
            # Twilio
            account = twilio.Account(sid, token)
            thread_list = {}
            for r in registrations:
                if r.cellphone_optout or not r.cellphone:
                    continue
                
                fromnumber = r.cellphone_from
                if not fromnumber:
                    numbers = ConferencePhoneFrom.objects.order_by('accounts')
                    fromnumber = numbers[0]
                    r.cellphone_from=fromnumber
                    r.save()
                    fromnumber.accounts = fromnumber.accounts + 1
                    fromnumber.save() 
                        
                d = {'From': fromnumber.number,   #  '415-599-2671',
                     'To': r.cellphone,
                     'Body': form.cleaned_data['message']}
                
                if fromnumber.number not in thread_list:
                    thread_list[fromnumber.number] = []
                thread_list[fromnumber.number].append(d)
                
            try:
                for i in thread_list:
                    t = Thread(target=do_send_sms, args=(thread_list[i],))
                    t.start()
                
            except Exception, e:
                #response = e.read()
                failed = failed + 1
            else:
                success = success + 1

            response = ""
            if failed:
                response = "%s<br/>Messages queued for sending, but some errors encountered =(" % response
            else:
                response = "%s<br/>Messages queued for sending!" % response
            
            """
            # ThunderTexting, simple GET
            import urllib
            try:
                params = {}
                params['CellNumber'] = '000-000-0000'
                params['MessageBody'] = form.cleaned_data['message']
                params['AccountKey'] = 'PPD843rw14'
                
                encoded = urllib.urlencode(params)
                handle = urllib.urlopen("http://thundertexting.com/sendsms.aspx?%s" % encoded)
                response = handle.read()
                #response = "http://thundertexting.com/sendsms.aspx?%s" % encoded
                
            except Exception, e:
                response = r.read()
            """

            """
            # ThunderTexting, SOAP
            from suds.client import Client
            try:
                url = 'http://thundertexting.com/SendSMS.asmx?WSDL'
                client = Client(url)
                
                response = client.service.SendMessageWithReference(CellNumber='000-000-0000',
                                                                   MessageBody=form.cleaned_data['message'],
                                                                   AccountKey=settings.THUNDERTEXTING_KEY,
                                                                   Reference='test1')
            except Exception, e:
                response = r.read()
            """
            
            """
            # ThunderTexting, SOAP batch send
            try:
                from suds.client import Client
                url = 'http://thundertexting.com/SendSMS.asmx?WSDL'
                client = Client(url)

                numbers = []
                for r in registrations:
                    if r.cellphone and not r.cellphone_optout:
                        numbers.append(r.cellphone)
                
                # cell number should already be normalized to 123-456-7890
                response = client.service.SendBulkMessage(CellNumbers=numbers,
                                                          MessageBody=form.cleaned_data['message'],
                                                          AccountKey=settings.THUNDERTEXTING_KEY,
                                                          Reference=datetime.now())
            except Exception, e:
                response = r.read()
            """
            
            """
示例#2
0
                response = r.read()
            """
            """
            for r in registrations:
                if hasattr(r, 'user'):
                    if r.cellphone and not r.cellphone_optout:
                        response = "%s<br/>%s %s - %s\n" % (response, r.user.first_name, r.user.last_name, r.type)
                else:
                    response = "%s<br/>%s\n" % (response, r.cellphone)
                    
            if not response:
                response = "No recipients matched your query."
            """

    else:
        form = ConferenceSmsForm()
        if s:
            del (form.fields['grouping'])

    context = {}
    if s:
        context['session'] = s
    if response:
        context['response'] = response
    else:
        context['form'] = form

    return render_to_response("conference/schedule/sms.html",
                              context,
                              context_instance=RequestContext(request))
示例#3
0
def send_sms(request, session=None):
    if not request.user.has_module_perms("conference"):
        return HttpResponseRedirect(reverse('conference_schedule'))

    s = get_object_or_none(ConferenceSession, id=session)
    response = None

    if request.method == 'POST':
        api = settings.TWILIO_API_VERSION
        sid = settings.TWILIO_ACCOUNT_SID
        token = settings.TWILIO_ACCOUNT_TOKEN

        form = ConferenceSmsForm(request.POST)

        if s:
            del (form.fields['grouping'])

        if form.is_valid():
            success = 0
            failed = 0

            registrations = []
            if s:
                registrations = ConferenceRegistration.objects.filter(
                    user__in=list(s.attendees.all()))
            elif form.cleaned_data['grouping'] == 'all':
                registrations = ConferenceRegistration.objects.all()
            elif form.cleaned_data['grouping'] == 'internal':
                registrations = ConferenceRegistration.objects.filter(~Q(
                    type__contains='day'))
            elif form.cleaned_data['grouping'] == 'external':
                registrations = ConferenceRegistration.objects.filter(
                    type__contains='day')
            elif form.cleaned_data['grouping'] == 'alumni':
                registrations = ConferenceRegistration.objects.filter(
                    type__contains='alumni')
            elif form.cleaned_data['grouping'] == 'hotel':
                registrations = ConferenceRegistration.objects.filter(
                    Q(type__contains='single') | Q(type__contains='double'))
            elif form.cleaned_data['grouping'] == 'nohotel':
                registrations = ConferenceRegistration.objects.filter(
                    Q(type__contains='nohotel') | Q(type__contains='alumni'))
            elif form.cleaned_data['grouping'] == 'nohotel-all':
                registrations = ConferenceRegistration.objects.filter(
                    ~Q(type__contains='single'), ~Q(type__contains='double'))

            registrations = registrations.filter(cancelled=False,
                                                 cellphone__isnull=False,
                                                 cellphone_optout__isnull=True)

            if not s and form.cleaned_data['grouping'] == 'all':
                registrations = list(registrations)
                registrations.extend(
                    list(
                        ConferenceCellNumber.objects.filter(
                            cellphone_optout__isnull=True)))

            # Twilio
            account = twilio.Account(sid, token)
            thread_list = {}
            for r in registrations:
                if r.cellphone_optout or not r.cellphone:
                    continue

                fromnumber = r.cellphone_from
                if not fromnumber:
                    numbers = ConferencePhoneFrom.objects.order_by('accounts')
                    fromnumber = numbers[0]
                    r.cellphone_from = fromnumber
                    r.save()
                    fromnumber.accounts = fromnumber.accounts + 1
                    fromnumber.save()

                d = {
                    'From': fromnumber.number,  #  '415-599-2671',
                    'To': r.cellphone,
                    'Body': form.cleaned_data['message']
                }

                if fromnumber.number not in thread_list:
                    thread_list[fromnumber.number] = []
                thread_list[fromnumber.number].append(d)

            try:
                for i in thread_list:
                    t = Thread(target=do_send_sms, args=(thread_list[i], ))
                    t.start()

            except Exception, e:
                #response = e.read()
                failed = failed + 1
            else:
                success = success + 1

            response = ""
            if failed:
                response = "%s<br/>Messages queued for sending, but some errors encountered =(" % response
            else:
                response = "%s<br/>Messages queued for sending!" % response
            """
            # ThunderTexting, simple GET
            import urllib
            try:
                params = {}
                params['CellNumber'] = '000-000-0000'
                params['MessageBody'] = form.cleaned_data['message']
                params['AccountKey'] = 'PPD843rw14'
                
                encoded = urllib.urlencode(params)
                handle = urllib.urlopen("http://thundertexting.com/sendsms.aspx?%s" % encoded)
                response = handle.read()
                #response = "http://thundertexting.com/sendsms.aspx?%s" % encoded
                
            except Exception, e:
                response = r.read()
            """
            """
            # ThunderTexting, SOAP
            from suds.client import Client
            try:
                url = 'http://thundertexting.com/SendSMS.asmx?WSDL'
                client = Client(url)
                
                response = client.service.SendMessageWithReference(CellNumber='000-000-0000',
                                                                   MessageBody=form.cleaned_data['message'],
                                                                   AccountKey=settings.THUNDERTEXTING_KEY,
                                                                   Reference='test1')
            except Exception, e:
                response = r.read()
            """
            """
            # ThunderTexting, SOAP batch send
            try:
                from suds.client import Client
                url = 'http://thundertexting.com/SendSMS.asmx?WSDL'
                client = Client(url)

                numbers = []
                for r in registrations:
                    if r.cellphone and not r.cellphone_optout:
                        numbers.append(r.cellphone)
                
                # cell number should already be normalized to 123-456-7890
                response = client.service.SendBulkMessage(CellNumbers=numbers,
                                                          MessageBody=form.cleaned_data['message'],
                                                          AccountKey=settings.THUNDERTEXTING_KEY,
                                                          Reference=datetime.now())
            except Exception, e:
                response = r.read()
            """
            """
示例#4
0
文件: sms.py 项目: franciskung/myewb2
def send_sms(request, session=None):
    if not request.user.has_module_perms("conference"):
        return HttpResponseRedirect(reverse('conference_schedule'))
    
    s = get_object_or_none(ConferenceSession, id=session)
    response = None

    if request.method == 'POST':
        api = settings.TWILIO_API_VERSION
        sid = settings.TWILIO_ACCOUNT_SID
        token = settings.TWILIO_ACCOUNT_TOKEN
        number = settings.TWILIO_PHONE_NUMBER
        
        form = ConferenceSmsForm(request.POST)
        
        if s:
            del(form.fields['grouping'])

        if form.is_valid():
            success = 0
            failed = 0
            
            registrations = []
            if s:
                registrations = ConferenceRegistration.objects.filter(user__in=list(s.attendees.all()))
            elif form.cleaned_data['grouping'] == 'all':
                registrations = ConferenceRegistration.objects.all()

            #elif form.cleaned_data['grouping'] == 'internal':
            #    registrations = ConferenceRegistration.objects.filter(~Q(type__contains='day'))
            #elif form.cleaned_data['grouping'] == 'external':
            #    registrations = ConferenceRegistration.objects.filter(type__contains='day')

            elif form.cleaned_data['grouping'] == 'alumni':
                registrations = ConferenceRegistration.objects.filter(type__contains='alumni')
            elif form.cleaned_data['grouping'] == 'hotel':
                registrations = ConferenceRegistration.objects.filter(Q(type__contains='single') | Q(type__contains='double') | Q(type__contains='quad'))
            elif form.cleaned_data['grouping'] == 'nohotel':
                registrations = ConferenceRegistration.objects.filter(Q(type__contains='nohotel'))
            #elif form.cleaned_data['grouping'] == 'nohotel-all':
            #    registrations = ConferenceRegistration.objects.filter(~Q(type__contains='single'), ~Q(type__contains='double'))
            
            registrations = registrations.filter(cancelled=False, submitted=True, cellphone__isnull=False, cellphone_optout__isnull=True)

            # just for debugging...
            #registrations = registrations.filter(cellphone='mynumber')
            
            #if not s and form.cleaned_data['grouping'] == 'all':
            #    registrations = list(registrations)
            #    registrations.extend(list(ConferenceCellNumber.objects.filter(cellphone_optout__isnull=True)))
            
            # Twilio
            account = twilio.Account(sid, token)
            #thread_list = {}
            batch_messages = []
            for r in registrations:
                if r.cellphone_optout or not r.cellphone:
                    continue

                """
                fromnumber = r.cellphone_from
                if not fromnumber:
                    numbers = ConferencePhoneFrom.objects.order_by('accounts')
                    fromnumber = numbers[0]
                    r.cellphone_from=fromnumber
                    r.save()
                    fromnumber.accounts = fromnumber.accounts + 1
                    fromnumber.save() 
                        
                d = {'From': fromnumber.number,   #  '415-599-2671',
                     'To': r.cellphone,
                     'Body': form.cleaned_data['message']}
                
                if fromnumber.number not in thread_list:
                    thread_list[fromnumber.number] = []
                thread_list[fromnumber.number].append(d)
                
            try:
                for i in thread_list:
                    t = Thread(target=do_send_sms, args=(thread_list[i],))
                    t.start()
                
            except Exception, e:
                #response = e.read()
                failed = failed + 1
            else:
                success = success + 1
                """

                d = {'From': number,
                     'To': r.cellphone,
                     'Body': form.cleaned_data['message']}

                batch_messages.append(d)                
                
            try:
                t = Thread(target=do_send_sms, args=(batch_messages,))
                t.start()
                
            except Exception, e:
                #response = e.read()
                failed = failed + 1
            else:
                success = success + 1

            response = ""
            if failed:
                response = "%s<br/>%d messages queued for sending, but some errors encountered =(" % (response, len(batch_messages))
            else:
                response = "%s<br/>%d messages queued for sending!" % (response, len(batch_messages))