· 7 years ago · May 10, 2018, 02:22 PM
1import os
2
3enter code here`# Build paths inside the project like this:
4os.path.join(BASE_DIR, ...)
5BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
6
7
8# Quick-start development settings - unsuitable for production
9# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
10
11# SECURITY WARNING: keep the secret key used in production secret!
12SECRET_KEY = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
13
14# SECURITY WARNING: don't run with debug turned on in production!
15DEBUG = True
16
17ALLOWED_HOSTS = []
18
19
20# Application definition
21
22INSTALLED_APPS = [
23 'twilio',
24 'sms_send',
25 'django.contrib.admin',
26 'django.contrib.auth',
27 'django.contrib.contenttypes',
28 'django.contrib.sessions',
29 'django.contrib.messages',
30 'django.contrib.staticfiles',
31]
32
33MIDDLEWARE = [
34 'django.middleware.security.SecurityMiddleware',
35 'django.contrib.sessions.middleware.SessionMiddleware',
36 'django.middleware.common.CommonMiddleware',
37 'django.middleware.csrf.CsrfViewMiddleware',
38 'django.contrib.auth.middleware.AuthenticationMiddleware',
39 'django.contrib.messages.middleware.MessageMiddleware',
40 'django.middleware.clickjacking.XFrameOptionsMiddleware',
41]
42
43ROOT_URLCONF = 'WebsiteSMS.urls'
44
45TEMPLATES = [
46 {
47 'BACKEND': 'django.template.backends.django.DjangoTemplates',
48 'DIRS': [],
49 'APP_DIRS': True,
50 'OPTIONS': {
51 'context_processors': [
52 'django.template.context_processors.debug',
53 'django.template.context_processors.request',
54 'django.contrib.auth.context_processors.auth',
55 'django.contrib.messages.context_processors.messages',
56 ],
57 },
58 },
59]
60
61# WSGI_APPLICATION = 'wsgi.application'
62WSGI_APPLICATION = 'WebsiteSMS.wsgi.application'
63
64
65# Database
66# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
67
68DATABASES = {
69 'default': {
70 'ENGINE': 'django.db.backends.sqlite3',
71 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
72 }
73}
74
75
76# Password validation
77# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-
78validators
79
80AUTH_PASSWORD_VALIDATORS = [
81 {
82 'NAME':
83'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
84 },
85 {
86 'NAME':
87'django.contrib.auth.password_validation.MinimumLengthValidator',
88 },
89 {
90 'NAME':
91'django.contrib.auth.password_validation.CommonPasswordValidator',
92 },
93 {
94 'NAME':
95'django.contrib.auth.password_validation.NumericPasswordValidator',
96 },
97]
98
99
100# Internationalization
101# https://docs.djangoproject.com/en/2.0/topics/i18n/
102
103LANGUAGE_CODE = 'en-us'
104
105TIME_ZONE = 'UTC'
106
107USE_I18N = True
108
109USE_L10N = True
110
111USE_TZ = True
112
113
114# Static files (CSS, JavaScript, Images)
115# https://docs.djangoproject.com/en/2.0/howto/static-files/
116
117STATIC_URL = '/static/'
118
119#!/usr/bin/env python
120import os
121import sys
122
123if __name__ == "__main__":
124 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "WebsiteSMS.settings")
125 try:
126 from django.core.management import execute_from_command_line
127 except ImportError as exc:
128 raise ImportError(
129 "Couldn't import Django. Are you sure it's installed and "
130 "available on your PYTHONPATH environment variable? Did you "
131 "forget to activate a virtual environment?"
132 ) from exc
133 execute_from_command_line(sys.argv)
134
135from twilio.rest import Client
136from credentials import account_sid, auth_token, my_cell, my_twilio
137
138client = Client(account_sid, auth_token)
139
140my_msg = "Test message"
141
142message = client.messages.create(to=my_cell, from_=my_twilio, body=my_msg)
143
144from .models import SmsUser
145from django.contrib import admin
146
147
148class SmsUserAdmin(admin.ModelAdmin):
149 list_display = ('name', 'number')
150 search_fields = ['name', 'number']
151
152
153admin.site.register(SmsUser, SmsUserAdmin)
154
155from django.db import models
156
157class SmsUser(models.Model):
158
159 name = models.TextField(null=True, blank=True)
160 number = models.CharField(max_length=13, null=True, blank=True)
161
162 def __str__(self):
163 return ' {}'.format(self.name)
164
165 def __str__(self):
166 return ' {}'.format(self.number)
167
168from twilio.rest import Client
169from credentials import account_sid, auth_token, my_cell, my_twilio
170from models import SmsUser
171
172client = Client(account_sid, auth_token)
173
174recipients = SmsUser.objects.all()
175
176for recipient in recipients:
177 client.messages.create(body='Sample text', to=recipient.number,
178from_=my_twilio)
179
180# Django holds a specific management commands path like eg.:
181# send_sms.management.commands.send_pending_sms_messages
182# which would be as a specific path send_sms/management/commands/send_pending_sms_messages.py
183
184from django.core.management.base import BaseCommand
185
186from twilio.rest import Client
187from credentials import account_sid, auth_token, my_cell, my_twilio
188from models import SmsUser
189
190
191class Command(BaseCommand):
192 help = 'Send pending SMS messages'
193
194 def handle(self, *args, **options):
195 client = Client(account_sid, auth_token)
196
197 recipients = SmsUser.objects.all()
198
199 for recipient in recipients:
200 client.messages.create(body='Sample text', to=recipient.number,
201 from_=my_twilio)