38
loading...
This website collects cookies to deliver better user experience
django-admin startproject tenants_project .
python manage.py startapp invoices
invoices
to the list of installed apps in tenants_project/settings.py
from django.db import models
# Create your models here.
class Tenant(models.Model):
name=models.CharField(max_length=250)
phone=models.CharField(max_length=250)
def __str__(self):
return self.name
makemigrations
then migrate
. Register the model in admin.py
. Also create a superuser, login to the admin section of the project and add a few tenant instances. After that we can begin sending them invoices messages every month!pip install africastalking
pip install apscheduler
invoices
called invoice_update.py
and add the code below to it:from .models import Tenant
import africastalking
# Initialize SDK
username = "sandbox"
api_key = "YOUR SANDBOX API KEY"
africastalking.initialize(username, api_key)
sms = africastalking.SMS
def send_reminders():
tenants=Tenant.objects.all()
for tenant in tenants:
invoice_message=f'Hello, {tenant.name}, your rent payment of shs 10,000 is due on demand. Please plan to pay before 5th.'
response =sms.send(invoice_message, [str(tenant.phone)])
print(response)
invoices
called updater.py
and add the code below to it:from apscheduler.schedulers.background import BackgroundScheduler
from . import invoice_update
def start():
scheduler = BackgroundScheduler()
scheduler.add_job(invoice_update.send_reminders, 'interval', seconds=10)
scheduler.start()
invoices/apps.py
and update it like so:from django.apps import AppConfig
class InvoicesConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'invoices'
def ready(self):
from . import updater
updater.start()
python manage.py runserver