Skip to content

Commit

Permalink
Added Django app
Browse files Browse the repository at this point in the history
  • Loading branch information
shannonturner committed Jun 28, 2016
1 parent a3a1e95 commit 9b18051
Show file tree
Hide file tree
Showing 17 changed files with 282 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
.Python
bin/
include/
lib/
*.pyc
Empty file added budgiecam/__init__.py
Empty file.
123 changes: 123 additions & 0 deletions budgiecam/settings.py
@@ -0,0 +1,123 @@
"""
Django settings for budgiecam project.
Generated by 'django-admin startproject' using Django 1.9.4.
For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ''

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'txt2pi',
]

MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'budgiecam.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['txt2pi'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'budgiecam.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_URL = '/static/'

24 changes: 24 additions & 0 deletions budgiecam/urls.py
@@ -0,0 +1,24 @@
"""budgiecam URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin

from txt2pi.views import BudgieCamView

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^budgiecam/', BudgieCamView.as_view()),
]
16 changes: 16 additions & 0 deletions budgiecam/wsgi.py
@@ -0,0 +1,16 @@
"""
WSGI config for budgiecam project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "budgiecam.settings")

application = get_wsgi_application()
10 changes: 10 additions & 0 deletions manage.py
@@ -0,0 +1,10 @@
#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "budgiecam.settings")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)
6 changes: 6 additions & 0 deletions requirements.txt
@@ -0,0 +1,6 @@
Django==1.9.7
httplib2==0.9.2
pytz==2016.4
six==1.10.0
twilio==5.4.0
wsgiref==0.1.2
Empty file added txt2pi/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions txt2pi/admin.py
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
7 changes: 7 additions & 0 deletions txt2pi/apps.py
@@ -0,0 +1,7 @@
from __future__ import unicode_literals

from django.apps import AppConfig


class Txt2PiConfig(AppConfig):
name = 'txt2pi'
13 changes: 13 additions & 0 deletions txt2pi/budgie_settings.py
@@ -0,0 +1,13 @@
# Budgiecam Constants

# Validates that the request is coming from a trusted source
BUDGIE_PASSPHRASE = ''

# File path of where the images are stored
BUDGIE_FILE_PATH = ''

# Web path of where the images are stored
BUDGIE_WEB_PATH = ''

# IP Address of the Raspberry Pi as accessible from the internet
RASPI_IP = ''
Empty file added txt2pi/migrations/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions txt2pi/models.py
@@ -0,0 +1,5 @@
from __future__ import unicode_literals

from django.db import models

# Create your models here.
1 change: 1 addition & 0 deletions txt2pi/response.html
@@ -0,0 +1 @@
{{ response }}
3 changes: 3 additions & 0 deletions txt2pi/tests.py
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
2 changes: 2 additions & 0 deletions txt2pi/twilio_credentials.py
@@ -0,0 +1,2 @@
ACCOUNT_SID = ''
AUTH_TOKEN = ''
64 changes: 64 additions & 0 deletions txt2pi/views.py
@@ -0,0 +1,64 @@
from django.shortcuts import render
from django.views.generic.base import TemplateView

import subprocess
import time

from twilio.rest import TwilioRestClient

from budgie_settings import BUDGIE_PASSPHRASE, BUDGIE_FILE_PATH, BUDGIE_WEB_PATH, RASPI_IP
from twilio_credentials import ACCOUNT_SID, AUTH_TOKEN

class BudgieCamView(TemplateView):

def get(self, request, **kwargs):

""" Response Code 418: I'm a teapot
"""

template = 'response.html'
context = {
'response': '418'
}
return render(request, template, context)

def post(self, request, **kwargs):

""" Twilio is configured to POST to this URL when a text message is received.
1. Receive text message
2. Verify text message and continue if verified
3. Snap photo (use subprocess module)
4. Photo needs to be accessible via a URL
5. Use Twilio API to attach photo to SMS
"""

text_message = request.POST.get('Body')
requesting_phone_number = request.POST.get('From')
budgiecam_phone_number = request.POST.get('To')

context = {}

if text_message:
if BUDGIE_PASSPHRASE in text_message:
try:
budgie_filename = '{1}.png'.format(''.join([str(x) for x in time.localtime()[:6]]))
subprocess.call(['raspistill', '-o', "{0}{1}".format(BUDGIE_FILE_PATH, budgie_filename)])
except Exception:
print "[ERROR] Call to raspistill failed; could not take photo"
context['response'] = '500'
else:
client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
client.messages.create(
to=budgiecam_phone_number,
from_=requesting_phone_number,
body="{0}".format(budgie_filename),
media_url="{0}{1}{2}".format(RASPI_IP, BUDGIE_WEB_PATH, budgie_filename),
)
context['response'] = '200'
else:
context['response'] = '401'
else:
context['response'] = '400'

template = 'response.html'
return render(request, template, context)

0 comments on commit 9b18051

Please sign in to comment.