def sendSMS(phone, content, user): try: phone = checkValidPhoneNumber(phone) except Exception as e: raise e try: school = user.userprofile.organization if phone: from suds.client import Client url = settings.SMS_WSDL_URL username = settings.WSDL_USERNAME password = settings.WSDL_PASSWORD mt_username = settings.MT_USERNAME mt_password = settings.MT_PASSWORD time = '/'.join([ str(datetime.date.today().day), str(datetime.date.today().month), str(datetime.date.today().year) ]) content = to_en1(u'Trường ' + unicode(school) + u': Ngày ' + str(time) + u'TK:' + unicode(user) + '.\n' + content) s = sms(phone=phone, content=content, sender=user, recent=True, success=True) s.save() print content client = Client(url, username=username, password=password) message = \ '''<?xml version="1.0" encoding="UTF-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <InsertMT xmlns="http://tempuri.org/"> <User>%s</User> <Pass>%s</Pass> <CPCode>160</CPCode> <RequestID>4</RequestID> <UserID>%s</UserID> <ReceiveID>%s</ReceiveID> <ServiceID>8062</ServiceID> <CommandCode>CNHN1</CommandCode> <ContentType>0</ContentType> <Info>%s</Info> </InsertMT> </soap12:Body> </soap12:Envelope>''' % (mt_username, mt_password, phone, phone, content) return client.service.InsertMT(__inject={'msg': str(message)}) else: raise Exception("InvalidPhoneNumber") except Exception as e: print e raise e
def sendSMS(phone,content,user): try: phone = checkValidPhoneNumber(phone) except Exception as e: raise e try: school = user.userprofile.organization if phone: from suds.client import Client url = settings.SMS_WSDL_URL username = settings.WSDL_USERNAME password = settings.WSDL_PASSWORD mt_username = settings.MT_USERNAME mt_password = settings.MT_PASSWORD time = '/'.join([str(datetime.date.today().day), str(datetime.date.today().month), str(datetime.date.today().year) ]) content =to_en1(u'Trường ' + unicode(school) + u': Ngày ' + str(time) + u'TK:' + unicode(user) + '.\n' + content) s = sms(phone=phone, content=content, sender=user, recent=True, success=True) s.save() print content client = Client(url, username = username, password = password) message = \ '''<?xml version="1.0" encoding="UTF-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <InsertMT xmlns="http://tempuri.org/"> <User>%s</User> <Pass>%s</Pass> <CPCode>160</CPCode> <RequestID>4</RequestID> <UserID>%s</UserID> <ReceiveID>%s</ReceiveID> <ServiceID>8062</ServiceID> <CommandCode>CNHN1</CommandCode> <ContentType>0</ContentType> <Info>%s</Info> </InsertMT> </soap12:Body> </soap12:Envelope>''' % (mt_username, mt_password, phone, phone, content) return client.service.InsertMT(__inject= {'msg': str(message)}) else: raise Exception("InvalidPhoneNumber") except Exception as e: print e raise e
def send_sms_summary_mark(student, content, marks, dds, hk, user, cl=None, school=None): phone = check_phone_number(student.sms_phone) try: if not school: school = user.userprofile.organization except Exception: pass if phone: if school: if school.id in [42, 44]: raise Exception('NotAllowedSMS') if cl: sms_cont = to_ascii( u'Truong %s thong bao:\nEm %s lop %s co %s\nTinh den ngay %s'\ % (unicode(school), student.short_name(), cl, content, unicode(date.today().strftime('%d\%m')))) else: sms_cont = to_ascii( u'Truong %s thong bao:\nEm %s co %s\nTinh den ngay %s'\ % (unicode(school), student.short_name(), content, unicode(date.today().strftime('%d\%m')))) else: raise Exception('SchoolIsNone') s = sms.objects.filter(phone=phone, content=sms_cont, type='THONG_BAO') if len(s) > 0: s = s[0] s.recent = True s.success = False s.save() else: s = sms(phone=phone, content=sms_cont, type='THONG_BAO', receiver=student.user_id, sender=user, recent=True, success=False) s.save() if not settings.DEBUG: return s.send_mark_sms.delay(s, marks=marks, dds=dds, hk=hk, school=school) else: return s._send_mark_sms(marks, dds, hk, school=school) else: raise Exception('InvalidPhoneNumber')
def excel_sms(request): if request.method == 'POST': if 'upload' in request.POST: form = smsFromExcelForm(request.POST, request.FILES) if form.is_valid(): form.clean_file() filepath = os.path.join(TEMP_FILE_LOCATION, 'sms_input.xls') list = xlrd.open_workbook(filepath) sheet = list.sheet_by_index(0) data = [] for r in range(1, sheet.nrows): data.append({'number': sheet.cell_value(r,0), 'content': sheet.cell_value(r,1)}) t = loader.get_template('sms/excel_sms.html') c = RequestContext(request, {'data' : data}) return HttpResponse(t.render(c)) else: if request.FILES: file = request.FILES['file'] filepath = os.path.join(TEMP_FILE_LOCATION, 'sms_input.xls') if not file.content_type in CONTENT_TYPES: error = 'Bạn chỉ được phép tải lên file Excel.' elif os.path.getsize(filepath) == 0: os.remove(filepath) error = 'Hãy tải lên một file Excel đúng. File của bạn hiện đang trống.' elif xlrd.open_workbook(filepath).sheet_by_index(0).nrows == 0: os.remove(filepath) error = 'Hãy tải lên một file Excel đúng. File của bạn hiện đang trống.' t = loader.get_template('sms/excel_sms.html') c = RequestContext(request, {'error' : error}) return HttpResponse(t.render(c)) else: t = loader.get_template('sms/excel_sms.html') c = RequestContext(request) return HttpResponse(t.render(c)) elif 'delete' in request.POST: filepath = os.path.join(TEMP_FILE_LOCATION, 'sms_input.xls') os.remove(filepath) t = loader.get_template('sms/excel_sms.html') c = RequestContext(request) return HttpResponse(t.render(c)) elif 'send' in request.POST: # update all record in sms db: recent = false all_sms = sms.objects.filter(sender=request.user) for s in all_sms: s.recent = False s.save() filepath = os.path.join(TEMP_FILE_LOCATION, 'sms_input.xls') list = xlrd.open_workbook(filepath) sheet = list.sheet_by_index(0) open = urllib2.build_opener(urllib2.HTTPCookieProcessor()) urllib2.install_opener(open) para = urllib.urlencode({'u': 'VT_username', 'p': 'VT_password'}) # f = open.open('http://viettelvas.vn:7777/fromcp.asmx', para) # f.close(); for r in range(1, sheet.nrows): if checkValidPhoneNumber(sheet.cell_value(r,0)): '''Save to db''' s = sms(phone=sheet.cell_value(r,0), receiver=getUserFromPhone(sheet.cell_value(r,0)), content=sheet.cell_value(r,1), sender=request.user, recent=True, success=True) s.save() '''Send sms via Viettel system''' data = urllib.urlencode({ 'RequestID' : '4', 'CPCode' : '', 'UserID' : '', 'ReceiverID' : sheet.cell_value(r,0), 'ServiceID' : '', 'CommandCode' : '', 'Content' : sheet.cell_value(r,1), 'ContentType' : '', }) # f = open.open('http://viettelvas.vn:7777/fromcp.asmx', data) else: '''Save to db''' s = sms(phone=sheet.cell_value(r,0), receiver=getUserFromPhone(sheet.cell_value(r,0)), content=sheet.cell_value(r,1), sender=request.user, recent=True, success=False) s.save() os.remove(filepath) return HttpResponseRedirect('/sms/sent_sms/') else: if os.path.lexists(os.path.join(TEMP_FILE_LOCATION, 'sms_input.xls')): filepath = os.path.join(TEMP_FILE_LOCATION, 'sms_input.xls') list = xlrd.open_workbook(filepath) sheet = list.sheet_by_index(0) data = [] for r in range(1, sheet.nrows): data.append({'number': sheet.cell_value(r,0), 'content': sheet.cell_value(r,1)}) t = loader.get_template('sms/excel_sms.html') c = RequestContext(request, {'data' : data}) return HttpResponse(t.render(c)) else: t = loader.get_template('sms/excel_sms.html') c = RequestContext(request) return HttpResponse(t.render(c))
def excel_sms(request): if request.method == 'POST': if 'upload' in request.POST: form = smsFromExcelForm(request.POST, request.FILES) if form.is_valid(): form.clean_file() filepath = os.path.join(TEMP_FILE_LOCATION, 'sms_input.xls') list = xlrd.open_workbook(filepath) sheet = list.sheet_by_index(0) data = [] for r in range(1, sheet.nrows): data.append({ 'number': sheet.cell_value(r, 0), 'content': sheet.cell_value(r, 1) }) t = loader.get_template('sms/excel_sms.html') c = RequestContext(request, {'data': data}) return HttpResponse(t.render(c)) else: if request.FILES: file = request.FILES['file'] filepath = os.path.join(TEMP_FILE_LOCATION, 'sms_input.xls') if not file.content_type in CONTENT_TYPES: error = 'Bạn chỉ được phép tải lên file Excel.' elif os.path.getsize(filepath) == 0: os.remove(filepath) error = 'Hãy tải lên một file Excel đúng. File của bạn hiện đang trống.' elif xlrd.open_workbook(filepath).sheet_by_index( 0).nrows == 0: os.remove(filepath) error = 'Hãy tải lên một file Excel đúng. File của bạn hiện đang trống.' t = loader.get_template('sms/excel_sms.html') c = RequestContext(request, {'error': error}) return HttpResponse(t.render(c)) else: t = loader.get_template('sms/excel_sms.html') c = RequestContext(request) return HttpResponse(t.render(c)) elif 'delete' in request.POST: filepath = os.path.join(TEMP_FILE_LOCATION, 'sms_input.xls') os.remove(filepath) t = loader.get_template('sms/excel_sms.html') c = RequestContext(request) return HttpResponse(t.render(c)) elif 'send' in request.POST: # update all record in sms db: recent = false all_sms = sms.objects.filter(sender=request.user) for s in all_sms: s.recent = False s.save() filepath = os.path.join(TEMP_FILE_LOCATION, 'sms_input.xls') list = xlrd.open_workbook(filepath) sheet = list.sheet_by_index(0) open = urllib2.build_opener(urllib2.HTTPCookieProcessor()) urllib2.install_opener(open) para = urllib.urlencode({'u': 'VT_username', 'p': 'VT_password'}) # f = open.open('http://viettelvas.vn:7777/fromcp.asmx', para) # f.close(); for r in range(1, sheet.nrows): if checkValidPhoneNumber(sheet.cell_value(r, 0)): '''Save to db''' s = sms(phone=sheet.cell_value(r, 0), receiver=getUserFromPhone(sheet.cell_value(r, 0)), content=sheet.cell_value(r, 1), sender=request.user, recent=True, success=True) s.save() '''Send sms via Viettel system''' data = urllib.urlencode({ 'RequestID': '4', 'CPCode': '', 'UserID': '', 'ReceiverID': sheet.cell_value(r, 0), 'ServiceID': '', 'CommandCode': '', 'Content': sheet.cell_value(r, 1), 'ContentType': '', }) # f = open.open('http://viettelvas.vn:7777/fromcp.asmx', data) else: '''Save to db''' s = sms(phone=sheet.cell_value(r, 0), receiver=getUserFromPhone(sheet.cell_value(r, 0)), content=sheet.cell_value(r, 1), sender=request.user, recent=True, success=False) s.save() os.remove(filepath) return HttpResponseRedirect('/sms/sent_sms/') else: if os.path.lexists(os.path.join(TEMP_FILE_LOCATION, 'sms_input.xls')): filepath = os.path.join(TEMP_FILE_LOCATION, 'sms_input.xls') list = xlrd.open_workbook(filepath) sheet = list.sheet_by_index(0) data = [] for r in range(1, sheet.nrows): data.append({ 'number': sheet.cell_value(r, 0), 'content': sheet.cell_value(r, 1) }) t = loader.get_template('sms/excel_sms.html') c = RequestContext(request, {'data': data}) return HttpResponse(t.render(c)) else: t = loader.get_template('sms/excel_sms.html') c = RequestContext(request) return HttpResponse(t.render(c))