Exemplo n.º 1
0
    def get(self):
        from mysms import MessageSchedule, Messages, SMSContacts, Groups, SMSAccount, SMSPortalVodacom, DeliveryReport,SMSPortalBudget
        from church import ChurchMember
        Today = datetime.datetime.now()
        thisDate = Today.date()
        thisTime = Today.time()

        findRequest = MessageSchedule.query(MessageSchedule.strStatus == "Scheduled",
                                            MessageSchedule.strStartDate == thisDate,
                                            MessageSchedule.strStartTime >= thisTime)
        thisMessageScheduleList = findRequest.fetch()

        for thisSchedule in thisMessageScheduleList:
            findRequest = Messages.query(Messages.strMessageID == thisSchedule.strMessageID,
                                         Messages.strSubmitted == False)
            thisMessageList = findRequest.fetch()

            if len(thisMessageList) > 0:
                thisMessage = thisMessageList[0]
            else:
                thisMessage = Messages()

            findRequest = Groups.query(Groups.strGroupID == thisMessage.strGroupID)
            thisGroupList = findRequest.fetch()

            if len(thisGroupList) > 0:
                thisGroup = thisGroupList[0]
            else:
                thisGroup = Groups()

            findRequest = SMSAccount.query(SMSAccount.strChurchID == thisGroup.strChurchID)
            thisSMSAccountList = findRequest.fetch()

            if len(thisSMSAccountList) > 0:
                thisSMSAccount = thisSMSAccountList[0]
            else:
                thisSMSAccount = SMSAccount()
                thisSMSAccount.writeChurchID(strinput=thisGroup.strChurchID)
                thisSMSAccount.put()

            findRequest = SMSContacts.query(SMSContacts.strGroupID == thisMessage.strGroupID)
            thisContactsList = findRequest.fetch()

            ReceipientList = []
            for thisContact in thisContactsList:
                if thisContact.strCellNumber != None:
                    ReceipientList.append(thisContact.strCellNumber)

            if thisSchedule.strNotifyOnStart == True:
                findRequest = ChurchMember.query(ChurchMember.strMemberID == thisSchedule.strMemberID)
                thisChurchMemberList = findRequest.fetch()

                if len(thisChurchMemberList) > 0:
                    thisChurchMember = thisChurchMemberList[0]
                    if thisChurchMember.strCell in ReceipientList:
                        pass
                    else:
                        ReceipientList.append(thisChurchMember.strCell)

            if thisSMSAccount.strTotalSMS >= len(ReceipientList):
                if thisSMSAccount.strUsePortal == "Vodacom":
                    findRequest = SMSPortalVodacom.query()
                    thisVodaList = findRequest.fetch()

                    if len(thisVodaList) > 0:
                        thisVoda = thisVodaList[0]
                    else:
                        thisVoda = SMSPortalVodacom()
                        thisVoda.put()

                    if thisVoda.CronSendMessages(strCellNumberList=ReceipientList, strMessage=thisMessage.strMessage,
                                                 strAccountID=thisSMSAccount.strChurchID):
                        thisSchedule.writeStatus(strinput="Completed")
                        thisSchedule.put()
                        thisMessage.writeDateSubmitted(strinput=thisDate)
                        thisMessage.writeTimeSubmitted(strinput=thisTime)
                        thisMessage.writeSubmitted(strinput=True)
                        thisMessage.put()
                        for StrCell in ReceipientList:
                            #TODO- Investigate if Vodacom uses cell number for reference to obtain response
                            thisDeliveryReport = DeliveryReport()
                            thisDeliveryReport.writeMessageID(strinput=thisMessage.strMessageID)
                            thisDeliveryReport.writeGroupID(strinput=thisMessage.strGroupID)
                            thisDeliveryReport.writeCell(strinput=StrCell)
                            thisDeliveryReport.writeDate(strinput=thisDate)
                            thisDeliveryReport.writeTime(strinput=thisTime)
                            thisDeliveryReport.writeDelivered(strinput=True)
                            thisDeliveryReport.put()
                        logging.info("Bulk SMS Schedule executed")

                        # thisSMSAccount.strTotalSMS = thisSMSAccount.strTotalSMS - len(ReceipientList)
                        # Vodacom Portal Subtracts its own Credits


                elif thisSMSAccount.strUsePortal == "Budget":
                    findRequest = SMSPortalBudget.query()
                    thisPortalList = findRequest.fetch()

                    if len(thisPortalList) > 0:
                        thisPortal = thisPortalList[0]
                    else:
                        thisPortal = SMSPortalBudget()

                    for thisCell in ReceipientList:
                        strRef = thisPortal.SendCronMessage(strMessage=thisMessage.strMessage,strCell=thisCell)
                        if not(strRef == None):
                            thisDeliveryReport = DeliveryReport()
                            thisDeliveryReport.writeMessageID(strinput=thisMessage.strMessageID)
                            thisDeliveryReport.writeGroupID(strinput=thisMessage.strGroupID)
                            thisDeliveryReport.writeCell(strinput=thisCell)
                            thisDeliveryReport.writeRef(strinput=strRef)
                            thisDeliveryReport.writeDate(strinput=thisDate)
                            thisDeliveryReport.writeTime(strinput=thisTime)
                            thisDeliveryReport.writeDelivered(strinput=True)
                            thisDeliveryReport.put()
                            thisSMSAccount.strTotalSMS = thisSMSAccount.strTotalSMS - 1
                        else:
                            thisDeliveryReport = DeliveryReport()
                            thisDeliveryReport.writeMessageID(strinput=thisMessage.strMessageID)
                            thisDeliveryReport.writeGroupID(strinput=thisMessage.strGroupID)
                            thisDeliveryReport.writeCell(strinput=thisCell)
                            thisDeliveryReport.writeRef(strinput="")
                            thisDeliveryReport.writeDate(strinput=thisDate)
                            thisDeliveryReport.writeTime(strinput=thisTime)
                            thisDeliveryReport.writeDelivered(strinput=False)
                            thisDeliveryReport.put()


                    thisMessage.writeDateSubmitted(strinput=thisDate)
                    thisMessage.writeTimeSubmitted(strinput=thisTime)
                    thisMessage.writeSubmitted(strinput=True)
                    thisMessage.put()



                            #TODO- Include Budget Sending Capability to enable the Messages to be sent on schedule

                thisSMSAccount.put()
Exemplo n.º 2
0
    def get(self):

        from dashboard import AppRequests,BlueITMarketingAdminStaff
        from mysms import SMSAccount,SMSPortalVodacom,SMSPortalBudget

        findRequests = AppRequests.query(AppRequests.strRequestAccepted == False,AppRequests.strNotificationSent == False)
        thisAppRequestList = findRequests.fetch()

        findRequests = BlueITMarketingAdminStaff.query(BlueITMarketingAdminStaff.strPosition == "Admin",BlueITMarketingAdminStaff.strPosition == "Marketing")
        thisBlueStaffList = findRequests.fetch()

        strTotalRequests = len(thisAppRequestList)

        findRequests = SMSPortalVodacom.query()
        thisVodaPortalList = findRequests.fetch()

        if len(thisVodaPortalList) > 0:
            thisVoda = thisVodaPortalList[0]
        else:
            thisVoda = SMSPortalVodacom()


        findRequests = SMSPortalBudget.query()
        thisBudgetPortalList = findRequests.fetch()

        if len(thisBudgetPortalList) > 0:
            thisBudget = thisBudgetPortalList[0]
        else:
            thisBudget = SMSPortalBudget()

        strMessage = "Church Admin" + "%0A"
        strNotifications = "System Notifications" + "%0A"
        NewClients  ="New Clients : " + str(strTotalRequests) + "%0A"
        Vodacom_Credit ="Vodacom Credit : " + str(thisVoda.strAvailableCredit) + "%0A"
        Budget_Credit = "Budget Credit : " + str(thisBudget.CheckCredits())

        strMessage = strMessage + strNotifications + NewClients  + Vodacom_Credit + Budget_Credit




        thisAdminStaff = BlueITMarketingAdminStaff()
        findRequests = SMSAccount.query(SMSAccount.strChurchID == thisAdminStaff.strStaffID)
        thisSMSAccountList = findRequests.fetch()

        if len(thisSMSAccountList) > 0:
            thisSMSAccount = thisSMSAccountList[0]
        else:
            thisSMSAccount = SMSAccount()

        ReceipientList = []

        if len(thisBlueStaffList) > 0:
            for thisStaff in thisBlueStaffList:
                ReceipientList.append(thisStaff.strCell)

        if "0790471559" in ReceipientList:
            pass
        else:
            ReceipientList.append("0790471559")


        if thisSMSAccount.strUsePortal == "Vodacom":
            findRequests = SMSPortalVodacom.query()
            thisVodaPortalList = findRequests.fetch()

            if len(thisVodaPortalList) > 0:
                Voda = thisVodaPortalList[0]
            else:
                Voda = SMSPortalVodacom()
                Voda.put()

            if Voda.CronSendMessages(strCellNumberList=ReceipientList,strMessage=strMessage,strAccountID=thisAdminStaff.strStaffID):
                logging.info("Successfully sent system messages")
            else:
                logging.info("Error sending system messages")

        elif thisSMSAccount.strUsePortal == "Budget":

            for thisNumber in ReceipientList:
                thisBudget.SendCronMessage(strCell=thisNumber,strMessage=strMessage)
                thisSMSAccount.strTotalSMS = thisSMSAccount.strTotalSMS - 1
            thisSMSAccount.put()
    def post(self):

        Guser = users.get_current_user()
        from church import ChurchMember, UserRights
        if Guser:

            findRequests = ChurchMember.query(
                ChurchMember.strMemberID == Guser.user_id())
            thisChurchMemberList = findRequests.fetch()

            if len(thisChurchMemberList) > 0:
                thisChurchMember = thisChurchMemberList[0]
            else:
                thisChurchMember = ChurchMember()

            findRequests = UserRights.query(
                UserRights.strMemberID == Guser.user_id())
            thisUserRightsList = findRequests.fetch()

            if len(thisUserRightsList) > 0:
                thisUserRight = thisUserRightsList[0]
            else:
                thisUserRight = UserRights()

            if thisUserRight.strAdminUser or thisUserRight.strSuperUser:
                vstrChoice = self.request.get('vstrChoice')

                if vstrChoice == "0":
                    vstrCell = self.request.get('vstrCell')

                    findRequests = Contacts.query(
                        Contacts.strCell == vstrCell,
                        Contacts.strChurchID == thisChurchMember.strChurchID)
                    thisContactList = findRequests.fetch()

                    if len(thisContactList) > 0:
                        thisContact = thisContactList[0]
                    else:
                        thisContact = Contacts()

                    findRequests = Notes.query(
                        Notes.strContactID == thisContact.strContactID)
                    thisNotesList = findRequests.fetch()

                    findRequests = PostalAddress.query(
                        PostalAddress.strContactID == thisContact.strContactID)
                    thisPostalAddressList = findRequests.fetch()

                    if len(thisPostalAddressList) > 0:
                        thisPostalAddress = thisPostalAddressList[0]
                    else:
                        thisPostalAddress = PostalAddress()
                        thisPostalAddress.writeContactID(
                            strinput=thisContact.strContactID)
                        thisPostalAddress.writeChurchID(
                            strinput=thisContact.strChurchID)
                        thisPostalAddress.put()

                    findRequests = PhysicalAddress.query(
                        PhysicalAddress.strContactID ==
                        thisContact.strContactID)
                    thisPhysicalAddressList = findRequests.fetch()

                    if len(thisPhysicalAddressList) > 0:
                        thisPhysicalAddress = thisPhysicalAddressList[0]
                    else:
                        thisPhysicalAddress = PhysicalAddress()
                        thisPhysicalAddress.writeContactID(
                            strinput=thisContact.strContactID)
                        thisPhysicalAddress.writeChurchID(
                            strinput=thisContact.strChurchID)
                        thisPhysicalAddress.put()

                    template = template_env.get_template(
                        'templates/contacts/sub/manage.html')
                    context = {
                        'thisContact': thisContact,
                        'thisNotesList': thisNotesList,
                        'thisPostalAddress': thisPostalAddress,
                        'thisPhysicalAddress': thisPhysicalAddress
                    }
                    self.response.write(template.render(context))

                elif vstrChoice == "1":

                    vstrContactID = self.request.get('vstrContactID')
                    vstrPostalCode = self.request.get('vstrPostalCode')
                    vstrCountry = self.request.get('vstrCountry')
                    vstrProvince = self.request.get('vstrProvince')
                    vstrCityTown = self.request.get('vstrCityTown')
                    vstrBox = self.request.get('vstrBox')

                    findRequests = PostalAddress.query(
                        PostalAddress.strContactID == vstrContactID)
                    thisPostalAddressList = findRequests.fetch()

                    if len(thisPostalAddressList) > 0:
                        thisPostalAddress = thisPostalAddressList[0]
                    else:
                        thisPostalAddress = PostalAddress()

                    thisPostalAddress.writeChurchID(
                        strinput=thisChurchMember.strChurchID)
                    thisPostalAddress.writeContactID(strinput=vstrContactID)
                    thisPostalAddress.writeBox(strinput=vstrBox)
                    thisPostalAddress.writeCityTown(strinput=vstrCityTown)
                    thisPostalAddress.writeProvince(strinput=vstrProvince)
                    thisPostalAddress.writeCountry(strinput=vstrCountry)
                    thisPostalAddress.writePostalCode(strinput=vstrPostalCode)
                    thisPostalAddress.put()

                    self.response.write("Postal Address updated successfully")

                elif vstrChoice == "2":
                    vstrSubject = self.request.get('vstrSubject')
                    vstrNotes = self.request.get('vstrNotes')
                    vstrContactID = self.request.get('vstrContactID')

                    findRequests = Notes.query(
                        Notes.strContactID == vstrContactID,
                        Notes.strSubject == vstrSubject,
                        Notes.strNotes == vstrNotes)
                    thisNotesList = findRequests.fetch()

                    if len(thisNotesList) > 0:
                        thisNote = thisNotesList[0]
                    else:
                        thisNote = Notes()

                    thisNote.writeContactID(strinput=vstrContactID)
                    thisNote.writeChurchID(
                        strinput=thisChurchMember.strChurchID)
                    thisDate = datetime.datetime.now()
                    thisDate = thisDate.date()
                    thisNote.writeDateTaken(strinput=thisDate)
                    thisTime = datetime.datetime.now()
                    thisTime = thisTime.time()
                    thisTime = datetime.time(hour=thisTime.hour,
                                             minute=thisTime.minute,
                                             second=thisTime.second)
                    thisNote.writeTimeTaken(strinput=thisTime)
                    thisNote.writeSubject(strinput=vstrSubject)
                    thisNote.writeNotes(strinput=vstrNotes)
                    thisNote.put()

                    self.response.write("Note successfully uploaded")

                elif vstrChoice == "3":
                    vstrContactID = self.request.get('vstrContactID')
                    vstrStandNumber = self.request.get('vstrStandNumber')
                    vstrStreetName = self.request.get('vstrStreetName')
                    vstrPhyCityTown = self.request.get('vstrPhyCityTown')
                    vstrPhyProvince = self.request.get('vstrPhyProvince')
                    vstrPhyCountry = self.request.get('vstrPhyCountry')
                    vstrPhyPostalCode = self.request.get('vstrPhyPostalCode')

                    findRequests = PhysicalAddress.query(
                        PhysicalAddress.strContactID == vstrContactID)
                    thisPhysicalAddressList = findRequests.fetch()

                    if len(thisPhysicalAddressList) > 0:
                        thisPhysicalAddress = thisPhysicalAddressList[0]
                    else:
                        thisPhysicalAddress = PhysicalAddress()

                    thisPhysicalAddress.writeContactID(strinput=vstrContactID)
                    thisPhysicalAddress.writeChurchID(
                        strinput=thisChurchMember.strChurchID)
                    thisPhysicalAddress.writeStandNumber(
                        strinput=vstrStandNumber)
                    thisPhysicalAddress.writeStreetName(
                        strinput=vstrStreetName)
                    thisPhysicalAddress.writeCityTown(strinput=vstrPhyCityTown)
                    thisPhysicalAddress.writeProvince(strinput=vstrPhyProvince)
                    thisPhysicalAddress.writeCountry(strinput=vstrPhyCountry)
                    thisPhysicalAddress.writePostalCode(
                        strinput=vstrPhyPostalCode)

                    thisPhysicalAddress.put()

                    self.response.write(
                        "Physical Address successfully updated")

                elif vstrChoice == "4":
                    vstrCell = self.request.get('vstrCell')

                    findRequests = Contacts.query(
                        Contacts.strChurchID == thisChurchMember.strChurchID,
                        Contacts.strCell == vstrCell)
                    thisContactList = findRequests.fetch()

                    if len(thisContactList) > 0:
                        thisContact = thisContactList[0]
                    else:
                        thisContact = Contacts()

                    findRequests = SMSInBox.query(
                        SMSInBox.strContactID == thisContact.strContactID)
                    thisInBoxMessagesList = findRequests.fetch()

                    findRequests = SMSOutBox.query(
                        SMSOutBox.strContactID == thisContact.strContactID)
                    thisSMSOutBoxList = findRequests.fetch()

                    template = template_env.get_template(
                        'templates/contacts/sub/sms.html')
                    context = {
                        'thisInBoxMessagesList': thisInBoxMessagesList,
                        'thisSMSOutBoxList': thisSMSOutBoxList,
                        'thisContact': thisContact
                    }
                    self.response.write(template.render(context))

                elif vstrChoice == "5":
                    from mysms import SMSAccount, SMSPortalVodacom, SMSPortalBudget, DeliveryReport
                    vstrMessage = self.request.get('vstrMessage')
                    vstrContactID = self.request.get('vstrContactID')
                    vstrCell = self.request.get('vstrCell')

                    findRequest = SMSAccount.query(
                        SMSAccount.strChurchID == thisChurchMember.strChurchID)
                    thisSMSAccountList = findRequest.fetch()
                    if len(thisSMSAccountList) > 0:
                        thisSMSAccount = thisSMSAccountList[0]
                    else:
                        thisSMSAccount = SMSAccount()

                    if thisSMSAccount.strUsePortal == "Vodacom":

                        findRequest = SMSPortalVodacom.query()
                        thisPortalList = findRequest.fetch()

                        if len(thisPortalList) > 0:
                            thisPortal = thisPortalList[0]
                        else:
                            thisPortal = SMSPortalVodacom()

                        i = 0

                        message = mail.EmailMessage()
                        message.sender = thisPortal.strSenderAddress
                        message.to = thisPortal.strEmailAddress
                        message.subject = vstrCell
                        message.body = vstrMessage
                        message.send()
                        thisPortal.strAvailableCredit = thisPortal.strAvailableCredit - 1

                        thisDeliveryReport = DeliveryReport()
                        thisDeliveryReport.writeGroupID(vstrContactID)
                        thisDeliveryReport.writeCell(vstrCell)
                        thisDeliveryReport.writeDelivered(strinput=True)
                        thisDate = datetime.datetime.now()
                        strThisDate = thisDate.date()
                        strThisTime = thisDate.time()
                        strThisTime = datetime.time(hour=strThisTime.hour,
                                                    minute=strThisTime.minute,
                                                    second=strThisTime.second)
                        thisDeliveryReport.writeDate(strinput=strThisDate)
                        thisDeliveryReport.writeTime(strinput=strThisTime)
                        thisDeliveryReport.writeMessageID(strinput=vstrMessage)
                        thisDeliveryReport.put()
                        thisPortal.put()

                        thisSMSAccount.strTotalSMS = thisSMSAccount.strTotalSMS - 1
                        thisSMSAccount.put()

                        thisOutBox = SMSOutBox()
                        thisOutBox.writeMessage(strinput=vstrMessage)
                        thisOutBox.writeContactID(strinput=vstrContactID)
                        thisOutBox.writeMessageID(
                            strinput=thisOutBox.CreateMessageID())
                        thisOutBox.writeDateSent(strinput=strThisDate)
                        thisOutBox.writeTimeSent(strinput=strThisTime)
                        thisOutBox.put()

                        self.response.write("SMS Successfully sent")

                elif vstrChoice == "6":
                    vstrCell = self.request.get('vstrCell')

                    findRequest = Contacts.query(Contacts.strCell == vstrCell)
                    thisContactList = findRequest.fetch()

                    if len(thisContactList) > 0:
                        thisContact = thisContactList[0]
                    else:
                        thisContact = Contacts()

                    findRequest = EmailInBox.query(
                        EmailInBox.strContactID == thisContact.strContactID)
                    thisEmailInboxList = findRequest.fetch()

                    findRequest = EmailOutBox.query(
                        EmailOutBox.strContactID == thisContact.strContactID)
                    thisEmailOutBoxList = findRequest.fetch()

                    template = template_env.get_template(
                        'templates/contacts/sub/email.html')
                    context = {
                        'vstrCell': vstrCell,
                        'thisEmailInboxList': thisEmailInboxList,
                        'thisEmailOutBoxList': thisEmailOutBoxList
                    }
                    self.response.write(template.render(context))

                elif vstrChoice == "7":

                    vstrCell = self.request.get('vstrCell')
                    vstrSubject = self.request.get('vstrSubject')
                    vstrMessage = self.request.get('vstrMessage')

                    findRequest = Contacts.query(Contacts.strCell == vstrCell)
                    thisContactList = findRequest.fetch()

                    if len(thisContactList) > 0:
                        thisContact = thisContactList[0]
                    else:
                        thisContact = Contacts()

                    findRequest = EmailOutBox.query(
                        EmailOutBox.strSubject == vstrSubject,
                        EmailOutBox.strMessage == vstrMessage)
                    thisEmailOutBoxList = findRequest.fetch()

                    if len(thisEmailOutBoxList) > 0:
                        thisEmailOutBox = thisEmailOutBoxList[0]
                    else:
                        thisEmailOutBox = EmailOutBox()

                    thisEmailOutBox.writeContactID(
                        strinput=thisContact.strContactID)
                    thisEmailOutBox.writeMessageID(
                        strinput=thisEmailOutBox.CreateMessageID())
                    thisEmailOutBox.writeMessage(strinput=vstrMessage)
                    thisDate = datetime.datetime.now()
                    thisDate = thisDate.date()
                    thisTime = datetime.datetime.now()
                    thisTime = datetime.time(hour=thisTime.hour,
                                             minute=thisTime.minute,
                                             second=thisTime.second)
                    if thisEmailOutBox.sendEmail():
                        thisEmailOutBox.writeIsSent(strinput=True)
                        thisEmailOutBox.writeTimeSent(strinput=thisTime)
                        thisEmailOutBox.writeDateSent(strinput=thisDate)

                        thisEmailOutBox.put()
                        self.response.write("Successfully sent an Email")
                    else:
                        self.response.write("Error Sending Email")