Skip to content

Instantly share code, notes, and snippets.

@camilosanchez
Created January 7, 2014 10:04
Show Gist options
  • Save camilosanchez/8297242 to your computer and use it in GitHub Desktop.
Save camilosanchez/8297242 to your computer and use it in GitHub Desktop.
views.py
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.template import RequestContext
from django.shortcuts import render_to_response, render
from django.core.exceptions import ObjectDoesNotExist
from django.views.generic import ListView
from django.views.generic.edit import UpdateView
from django.core.urlresolvers import reverse
from forms import *
from django import forms
from django.shortcuts import redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from .models import Offer
# forms
class LoginForm(forms.Form):
username = forms.EmailField()
password = forms.CharField(widget=forms.PasswordInput())
#view for the login
def login_usr(request):
if request.method=="POST":
form = LoginForm(request.POST)
if form.is_valid():
print "form is valid"
# user registration or login
username,pwd=request.POST.get("username",None),request.POST.get("password")
if not username or not pwd:
return HttpResponse("Username or password not present")
try:
user=User.objects.get(username=username)
except ObjectDoesNotExist,ex:
# print "Creating new user ..."
print "error ..."
return HttpResponse("No estas registrado en el sistema, registrate")
# user=User.objects.create_user(username,username,pwd)
if user:
print "Authenticating ..."
user=authenticate(username=username,password=pwd)
print "Logging in user"
login(request,user)
return redirect("dashboard")
else:
print "form is not valid"
return render_to_response("login.html", {'form':form}, RequestContext(request))
else:
return render_to_response("login.html", {'form':LoginForm()}, RequestContext(request))
def logout_usr(request):
logout(request)
return redirect("login")
#view for the dashboard
@login_required(login_url="/offers/login/")
def dashboard(request, *args, **kwargs):
offer_list = Offer.objects.all()
template_name = "dashboard.html"
context = {
"offer_list": offer_list
}
return render(request, template_name, context)
#view for the dashboard list of offers
class OfferListView(ListView):
model = Offer
#view for the dashboard's form detail from the list of offers
class OfferUpdateView(UpdateView):
model = Offer
form_class = OfferForm
template_name = "offers_update_form.html"
# public view for the offer, this could be in a facebook canvas, or a public website that will be mobile or desktop.
@csrf_exempt
def app_public(request, pk, *args, **kwargs):
offer = Offer.objects.get(pk=pk, published=True)
template_name = "app_public.html"
context = {
"offer" : offer
}
return render(request, template_name, context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment