Skip to content

Instantly share code, notes, and snippets.

@m3ck0
Created March 21, 2016 18:36
Show Gist options
  • Save m3ck0/5742960eb05f1cb8f998 to your computer and use it in GitHub Desktop.
Save m3ck0/5742960eb05f1cb8f998 to your computer and use it in GitHub Desktop.
from django.http import HttpResponse
from django.http import JsonResponse
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.shortcuts import render
from django.shortcuts import render_to_response
from django.shortcuts import redirect
from django.views.generic import View
from django.views.generic import TemplateView
from django.views.generic import RedirectView
from django.views.generic import CreateView
from django.views.generic import UpdateView
from django.views.generic import DeleteView
from django.views.decorators.csrf import csrf_exempt
from django.template.response import TemplateResponse
from django.contrib import messages
from django.contrib.auth import authenticate
from django.contrib.auth import login
from django.contrib.auth import logout
from django.utils.translation import ugettext_lazy as _
from django.utils.decorators import method_decorator
from .models import Account
from .forms import AccountForm
class HomeView(TemplateView):
template_name = 'authentication/index.html'
def get(self, request, *args, **kwargs):
return render(request, self.template_name)
class SigninView(TemplateView):
template_name = 'authentication/signin.html'
def get(self, request, *args, **kwargs):
return render(request, self.template_name)
def post(self, request, *args, **kwargs):
context = {}
email = request.POST.get('email')
password = request.POST.get('password')
try:
user = Account.objects.get(email=email)
user = authenticate(username=email, password=password)
if user is not None and user.is_active:
login(request, user)
return redirect('auth:index')
else:
context['error'] = _("Invalid Credentials")
except Account.DoesNotExist:
context['error'] = _("Invalid Credentials")
return render(request, self.template_name, context)
class SignupView(CreateView):
template_name = 'authentication/signup.html'
model = Account
form_class = AccountForm
object = None
response_class = TemplateResponse
def form_valid(self, form):
self.object = form.save()
return HttpResponseRedirect(self.get_success_url())
def form_invalid(self, form):
return self.render_to_response(self.get_context_data())
def get_context_data(self, **kwargs):
context = super(SignupView, self).get_context_data(**kwargs)
return context
def get_success_url(self):
slug = 'registration_success_en'
url = reverse('common:message', kwargs={'slug':slug})
return url
def render_to_response(self, context, **response_kwargs):
#response_kwargs.setdefault('content_type', self.content_type)
return self.response_class(
request=self.request,
template=self.template_name,
context=context,
using=self.template_engine,
**response_kwargs)
class SignoutView(RedirectView):
def get(self, request, *args, **kwargs):
url = reverse('auth:index')
logout(request)
return HttpResponseRedirect(url)
class ActivationView(RedirectView):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment