Created
October 20, 2012 16:01
-
-
Save Aaron1011/3923745 to your computer and use it in GitHub Desktop.
Django texting_wall
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create your views here. | |
from django.shortcuts import render_to_response | |
from django.contrib.auth.forms import UserCreationForm | |
from django.contrib.auth import forms as auth_forms | |
from django.core.context_processors import csrf | |
from django.contrib.auth.models import User | |
from django.template import RequestContext | |
from django.contrib.auth import authenticate | |
from django.contrib.auth import login as auth_login | |
from django.http import HttpResponse, HttpResponseRedirect | |
from forms import WallForm | |
import forms as local_forms | |
import models | |
from django.contrib.auth.decorators import login_required | |
from django.views.decorators.csrf import csrf_exempt | |
import random, string, re | |
from Pubnub import Pubnub | |
@login_required(login_url='/login', redirect_field_name='/create_wall') | |
def display_wall(request, id): | |
wall = models.Wall.objects.get(pk=id) | |
print str(wall.sms_keyword) | |
return render_to_response("wall.html", {'wall': wall}) | |
@csrf_exempt | |
def sms_message(request): | |
PUBLISH_KEY = "pub-8a8223f4-631c-4484-a118-2b01232307cc" | |
SUBSCRIBE_KEY = "sub-e754ed6b-133d-11e2-91f2-b58e6c804094" | |
SECRET = "sec-ZjcxZGVjNDAtZWQyMC00MGZmLTg1Y2MtNmJkNGE3YTJiYjlj" | |
message = request.POST['Body'] | |
matched_message = re.match("^\s*(\w*)\s+(.*)", message) | |
pubnub = Pubnub(PUBLISH_KEY, SUBSCRIBE_KEY, SECRET, False) | |
info = pubnub.publish({ | |
'channel' : matched_message.group(1), | |
'message' : { | |
'message' : matched_message.group(2) | |
} | |
}) | |
print matched_message.groups() | |
def create_account(request): | |
form = local_forms.UserCreation() | |
if request.POST: | |
form = UserCreationForm(data=request.POST) | |
if form.is_valid(): | |
form.save() | |
user = authenticate(username=form.cleaned_data["username"], | |
password=form.cleaned_data["password1"]) | |
auth_login(request,user) | |
return render_to_response('finish.html') | |
else: | |
return render_to_response( | |
"create_account.html", | |
{ "form": form }, RequestContext(request)) | |
def finish(request): | |
return HttpResponse("Login Successfull!") | |
@login_required(login_url="/login", redirect_field_name='/create_wall/' ) | |
def new_wall(request): | |
if request.POST: | |
f = WallForm(data=request.POST) | |
wallform = f.save(commit=False) | |
wallform.user = request.user | |
print wallform.sms_keyword | |
wallform.save() | |
return HttpResponseRedirect('/wall/' + str(wallform.id)) | |
else: | |
keyword = "".join(random.choice(string.lowercase) for i in range(1,4)) | |
form = WallForm(data={'sms_keyword': keyword}) | |
return render_to_response( | |
"create_wall.html", | |
{"form": form, "sms_keyword": keyword}, RequestContext(request)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment