Skip to content

Instantly share code, notes, and snippets.

@rcotrina94
Last active Dec 15, 2015
Embed
What would you like to do?
{% extends "base/main.html" %}{% load staticfiles bootstrap %}
{% block header %}
{% endblock %}
{% block content %}
<div id="login-form" class="vcenter">
<div class="container-fluid">
<form method="POST" enctype="multipart/form-data" autocomplete="off">
{% csrf_token %}
<div class="col-xs-10 col-xs-offset-1 col-sm-6 col-sm-offset-3 col-lg-4 col-lg-offset-4">
<div class="panel panel-default">
<div class="panel-heading">
<strong> Inicio de Sesión</strong>
</div>
<div class="panel-body">
<form role="form" action="#" method="POST">
<fieldset>
<div class="row">
<div class="center-block">
<img class="profile-img" src="{% static 'img/photo.png' %}" alt="">
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-10 col-sm-offset-1 col-lg-12 col-lg-offset-0">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-user"></i>
</span>
{{ login_form.user | bootstrap_inline }}
</div>
<span class="text-danger">{{ login_form.user.errors }}</span>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-lock"></i>
</span>
{{ login_form.password | bootstrap_inline }}
</div>
<span class="text-danger">{{ login_form.password.errors }}</span>
</div>
{% if messages %}
<ul class="text-danger">
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
<div class="form-group">
<input type="submit" class="btn btn-lg btn-primary btn-block" value="Ingresar">
</div>
</div>
</div>
</fieldset>
</form>
</div>
<div class="panel-footer ">
&copy; Powered by <a href="">Familia Albitres</a>
</div>
</div>
</div>
</form>
</div>
</div>
{% endblock %}
{% block footer %}
{% endblock %}
# coding=utf-8
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib import admin
from erp.apps.main.views import HomeView, login_view, logout_view
urlpatterns = patterns(
'',
url(r'^$', HomeView.as_view(), name='home'),
url(r'^login$', login_view, name='login'),
url(r'^logout$', logout_view, name='logout'),
url(r'^admin/', include('erp.apps.main.urls')),
url(r'^djadmin/', include(admin.site.urls)),
)
# -*- coding: utf-8 -*-
from django.contrib.auth.decorators import login_required
from django.views.generic import (
ListView as dj_ListView, DetailView as dj_DetailView,
CreateView as dj_CreateView, DeleteView as dj_DeleteView,
TemplateView as dj_TemplateView, RedirectView as dj_RedirectView,
)
class LoginRequiredMixin(object):
@classmethod
def as_view(cls, **initkwargs):
view = super(LoginRequiredMixin, cls).as_view(**initkwargs)
return login_required(view)
class ERPBaseMixin(LoginRequiredMixin):
nav = ""
sub_nav = ""
def get_context_data(self, **kwargs):
context = super(ERPBaseMixin, self).get_context_data(**kwargs)
if self.nav:
context['nav'] = self.nav
if self.sub_nav:
context['sub_nav'] = self.sub_nav
return context
class ListView(ERPBaseMixin, dj_ListView):
pass
class DetailView(ERPBaseMixin, dj_DetailView):
pass
class CreateView(ERPBaseMixin, dj_CreateView):
pass
class DeleteView(ERPBaseMixin, dj_DeleteView):
pass
class TemplateView(ERPBaseMixin, dj_TemplateView):
pass
class RedirectView(ERPBaseMixin, dj_RedirectView):
pass
# coding=utf-8
from .views_mixins import TemplateView, RedirectView
from django.template.response import TemplateResponse
from django.shortcuts import redirect
from django.contrib import messages
from django.contrib.auth import authenticate, login as auth_login, logout as auth_logout
from .forms import LoginForm
class HomeView(RedirectView):
""" Ruta principal / que redirecciona a la vista que corresponda al usuario """
pattern_name = 'dashboard'
class MainDashboardView(TemplateView):
""" Vista de Escritorio Principal """
template_name = 'base/dashboard.html'
nav = "main_dashboard"
def login_view(request):
""" Procesa la autenticación del usuario """
login_form = LoginForm(request.POST or None)
if request.method.upper() == 'POST' and login_form.is_valid():
usuario = request.POST['user']
clave = request.POST['password']
user = authenticate(username=usuario, password=clave)
if user is not None:
if user.is_active:
auth_login(request, user)
return redirect('dashboard') # return redirect('dashboard')
else:
messages.error(request, u'El usuario no está activo')
else:
messages.error(request, u'Usuario y/o contraseña no coinciden')
# else:
# messages.error(request, u'Un error ha ocurrido. Intente nuevamente')
data = {'login_form': login_form}
return TemplateResponse(request, 'base/login.html', data)
def logout_view(request):
""" Cierra la sesión del usuario """
auth_logout(request)
return redirect('login')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment