# Create your views here.
#encoding:utf-8
from django.shortcuts import *
from django.http import HttpResponse
from django.template.response import TemplateResponse
from oauth.forms import AplicacionForm, ResponsableForm
from datetime import datetime, timedelta
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from oauth.models import aplicacion
def loginView(request):
return TemplateResponse(request,'index.html',{'inicio':'active'})
@login_required
def oauthRegistro(request):
documento = {
'titulo': 'Aplicaciones'
}
panel = {
'titulo': 'Registro de aplicaciones',
'form':{
'method':'POST',
'action':''
}
}
breadcrumbs = {}
breadcrumbs = [
{
'li':{
'class':'active',
'a':{
'href':'/app/add',
'i':{
'class':'icol-application-add'
},
'value':'Nueva'
}
}
}
]
if request.method == 'POST':
a = aplicacion(responsable_id = request.user.id)
if 'aplicacion_id' in request.POST:
a = aplicacion.objects.get(pk = request.POST['aplicacion_id'] )
form = AplicacionForm(request.POST, request.FILES, instance = a)
print request.FILES
if form.is_valid():
try:
form.save()
messages.info(request, 'Aplicacion guardada')
return HttpResponseRedirect(request.path)
except Exception, e:
messages.error(request, e)
else:
messages.error(request, 'Corrija los siguientes errores')
else:
messages.info(request, 'Nueva aplicacion')
form = AplicacionForm()
return render_to_response(
'registro.html',
{'form': form,
'documento':documento,
'breadcrumbs':breadcrumbs,
'panel':panel,
'aplicacion':'active'
}, context_instance = RequestContext(request)
)
def ResponsableAdd(request):
documento = {
'titulo': 'Responsables'
}
panel = {
'titulo': 'Registro de responsables',
'form':{
'method':'POST',
'action':''
}
}
breadcrumbs = [
{
'li':{
'class':'active',
'a':{
'href':'/apps/perfil',
'i':{
'class':'icol-application-add'
},
'value':'Nuevo'
}
}
}
]
if request.method == 'POST':
r = None
if 'id_username' in request.POST:
r = responsable.objects.get(username = request.POST['id_username'] )
form = ResponsableForm(request.POST, request.FILES, instance = r)
if form.is_valid():
try:
form.save()
messages.info(request, 'Responsable guardado')
return HttpResponseRedirect(request.path)
except Exception, e:
messages.error(request, e)
else:
messages.error(request, 'Corrija los siguientes errores')
else:
messages.info(request, 'Nuevo responsable')
form = ResponsableForm()
return render_to_response(
'registro.html',
{'form': form,
'documento':documento,
'panel':panel,
'perfil':'active',
'breadcrumbs':breadcrumbs,
}, context_instance = RequestContext(request)
)
@login_required
def lista_aplicaciones_view(request):
documento = {
'titulo': 'Lista de aplicaciones'
}
panel = {
'titulo': 'Lista de aplicaciones',
'icon':'icol-text-list-bullets'
}
breadcrumbs = [
{
'li':{
'class':'active',
'a':{
'href':'/apps/list',
'i':{
'class':'icol-text-list-bullets'
},
'value':'Registradas'
}
}
}
]
aplicaciones = aplicacion.objects.filter(responsable = request.user).order_by('fecha_registro')
return render_to_response(
'lista.html',
{
'documento':documento,
'panel':panel,
'lista':aplicaciones,
'aplicacion':'active',
'breadcrumbs':breadcrumbs,
}, context_instance = RequestContext(request)
)
@login_required
def authorization_view(request):
return HttpResponse( str(request) )
@login_required
def generallave_view(request, app_id = 0):
from django.contrib.auth.hashers import make_password, get_random_string
from django.core.urlresolvers import reverse
if app_id != 0 :
try:
app = aplicacion.objects.get(pk=app_id)
if app.responsable.id == request.user.id:
app.consumer_key = make_password(app.url)
# pbkdf2_sha256
# sha1
# md5
# unsalted_md5
# unsalted_sha1
# crypt
# encoded = make_password('lètmein')
# print check_password('lètmein', encoded)
# print check_password('lètmeinz', encoded)
if not app.consumer_enc_method in ('unsalted_md5', 'unsalted_sha1'):
app.consumer_secret = get_random_string()
else:
app.consumer_secret = ''
encoded = make_password(
password = app.url,
salt = app.consumer_secret,
hasher = app.consumer_enc_method)
app.consumer_key = encoded
app.save()
else:
messages.error(request, 'Usuario no corresponde a la aplicacion')
except Exception, e:
messages.error(request, 'Aplicación no encontrada')
return HttpResponseRedirect( reverse ('lista_aplicaciones') )
def request_token_view(request):
contenido = ''
contenido += str(request)
contenido += '
'
if request.method == 'POST':
for item in request.POST:
contenido += item + ' ' + request.POST[item] + '
'
return HttpResponse( contenido )