diff views.py @ 107:2bca07be6e51

Add login popup box So you can log in and immediately be redirected to the same page. Sort of like what reddit has. If Javascript is disabled, the user is simply taken to the standard login page.
author dellsystem <ilostwaldo@gmail.com>
date Tue, 11 Sep 2012 20:23:51 -0400
parents
children b8e0bdc37e32
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/views.py	Tue Sep 11 20:23:51 2012 -0400
@@ -0,0 +1,59 @@
+from django.contrib.auth import login, authenticate
+from django.contrib.auth.forms import AuthenticationForm
+from django.shortcuts import render, redirect
+from django.core.urlresolvers import reverse
+from registration.forms import RegistrationForm
+
+from agora.apps.snippet.models import Snippet
+
+
+def code(request):
+    context = {
+        'snippets': Snippet.objects.all()[:5],
+        'modules': None, # temp
+        'forge': None, # temp
+    }
+
+    return render(request, 'code.djhtml', context)
+
+
+def login_register(request):
+    form = None
+    next_url = None
+
+    if request.method == 'POST':
+        action = request.POST.get('action')
+        next_url = request.GET.get('next') or reverse('login')
+
+        if action == 'login':
+            username = request.POST.get('username', '')
+            password = request.POST.get('password1', '')
+
+            if username and password:
+                user = authenticate(username=username, password=password)
+                login(request, user)
+
+                return redirect(next_url)
+            else:
+                form = {
+                    'password1': {
+                        'errors': 'Please enter a username and password.',
+                    },
+                }
+        elif action == 'register':
+            form = RegistrationForm(request.POST)
+
+            if form.is_valid():
+                user = form.save()
+                login(request, user)
+                return redirect(next_url)
+        else:
+            # The action is not set. Malicious submission?
+            pass
+
+    context = {
+        'next_url': next_url,
+        'form': form,
+    }
+
+    return render(request, 'login.djhtml', context)