mirror of
				https://github.com/bspeice/Melodia
				synced 2025-11-03 18:00:50 -05:00 
			
		
		
		
	Initial login templates and views
This commit is contained in:
		@ -1,18 +1,33 @@
 | 
			
		||||
from django.http import HttpResponse
 | 
			
		||||
from django.contrib.auth.decorators import login_required
 | 
			
		||||
from django.contrib.auth import authenticate, login, forms
 | 
			
		||||
from django import forms as django_forms
 | 
			
		||||
from django.contrib.auth import login as django_login
 | 
			
		||||
from django.contrib.auth import logout as django_logout
 | 
			
		||||
from django.contrib.auth import forms
 | 
			
		||||
from django.template import RequestContext
 | 
			
		||||
from django.shortcuts import render_to_response, redirect
 | 
			
		||||
 | 
			
		||||
#Melodia-specific utilities
 | 
			
		||||
from web_utils import json_response as json
 | 
			
		||||
 | 
			
		||||
@login_required
 | 
			
		||||
def main(request):
 | 
			
		||||
	return render_to_response("main", context_instance = RequestContext(request))
 | 
			
		||||
def _user_authenticate(request):
 | 
			
		||||
	form = forms.AuthenticationForm(data=request.POST)
 | 
			
		||||
	if form.is_valid():
 | 
			
		||||
		try:
 | 
			
		||||
			cleaned_data = form.clean()
 | 
			
		||||
			django_login(request, form.get_user())
 | 
			
		||||
			return redirect("/")
 | 
			
		||||
 | 
			
		||||
def login_page(request):
 | 
			
		||||
		except: 
 | 
			
		||||
			#Invalid login, or ValidationError
 | 
			
		||||
			return render_to_response("login", context_instance = RequestContext(request,
 | 
			
		||||
																				{"auth_form": forms.AuthenticationForm(data=request.POST),
 | 
			
		||||
																				"login_error": "Internal error trying to log in."} ))
 | 
			
		||||
	else:
 | 
			
		||||
		return render_to_response("login", context_instance = RequestContext(request,
 | 
			
		||||
																			{"auth_form": forms.AuthenticationForm(data=request.POST),
 | 
			
		||||
																			"login_error": "Unrecognized username or password."} ))
 | 
			
		||||
 | 
			
		||||
def login(request):
 | 
			
		||||
	if request.method == "POST":
 | 
			
		||||
		#Someone is trying to log in
 | 
			
		||||
		return _user_authenticate(request)
 | 
			
		||||
@ -20,21 +35,6 @@ def login_page(request):
 | 
			
		||||
		return render_to_response("login", context_instance = RequestContext(request,
 | 
			
		||||
																			{"auth_form": forms.AuthenticationForm()} ))
 | 
			
		||||
 | 
			
		||||
def _user_authenticate(request):
 | 
			
		||||
	form = forms.AuthenticationForm(data=request.POST)
 | 
			
		||||
	if form.is_valid():
 | 
			
		||||
		try:
 | 
			
		||||
			cleaned_data = form.clean()
 | 
			
		||||
			print cleaned_data
 | 
			
		||||
			login(request, form.get_user())
 | 
			
		||||
			return redirect("/")
 | 
			
		||||
 | 
			
		||||
		except django_forms.ValidationError:
 | 
			
		||||
			#Invalid login
 | 
			
		||||
			return json(success = False,
 | 
			
		||||
						message = "Either the username or the password was not recognized.")
 | 
			
		||||
 | 
			
		||||
	else:
 | 
			
		||||
		return render_to_response("login", context_instance = RequestContext(request,
 | 
			
		||||
																			{"auth_form": forms.AuthenticationForm(data=request.POST),
 | 
			
		||||
																			"login_error": form.errors} ))
 | 
			
		||||
def logout(request):
 | 
			
		||||
	django_logout(request)
 | 
			
		||||
	return redirect("/login")
 | 
			
		||||
							
								
								
									
										13
									
								
								web/views/index.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								web/views/index.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,13 @@
 | 
			
		||||
from django.http import HttpResponse
 | 
			
		||||
from django.contrib.auth.decorators import login_required
 | 
			
		||||
from django.contrib.auth import authenticate, login, forms
 | 
			
		||||
from django import forms as django_forms
 | 
			
		||||
from django.template import RequestContext
 | 
			
		||||
from django.shortcuts import render_to_response, redirect
 | 
			
		||||
 | 
			
		||||
#Melodia-specific utilities
 | 
			
		||||
from web_utils import json_response as json
 | 
			
		||||
 | 
			
		||||
@login_required
 | 
			
		||||
def main(request):
 | 
			
		||||
	return render_to_response("index", context_instance = RequestContext(request))
 | 
			
		||||
@ -36,7 +36,18 @@ List of block elements in this page:
 | 
			
		||||
				<div class="navbar-inner">
 | 
			
		||||
					<div class="container">
 | 
			
		||||
						<a class="brand" href="#">{% block navbar_title %}{% endblock %}</a>
 | 
			
		||||
						{% block navbar_content %}{% endblock %}
 | 
			
		||||
 | 
			
		||||
						{% block navbar_content %}
 | 
			
		||||
 | 
			
		||||
						<div class="btn-group pull-right">
 | 
			
		||||
							<a class="btn" href="#">{{ user.username }}</a>
 | 
			
		||||
							<a class="btn dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret" /></a>
 | 
			
		||||
							<ul class="dropdown-menu">
 | 
			
		||||
								<li id="btn_logout"><a href="/logout">Logout</a></li>
 | 
			
		||||
							</ul>
 | 
			
		||||
						</div>
 | 
			
		||||
 | 
			
		||||
						{% endblock %}
 | 
			
		||||
					</div>
 | 
			
		||||
				</div>
 | 
			
		||||
			</div>
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										11
									
								
								web/views/templates/components/playlist_sidebar
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								web/views/templates/components/playlist_sidebar
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,11 @@
 | 
			
		||||
{# This component assumes it has been placed in a sidebar, and controls options related to playlists. #}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
<ul class="nav nav-list">
 | 
			
		||||
	<li class="nav-header">Playlists</li>
 | 
			
		||||
	{% for playlist in Playlist.objects.all %}
 | 
			
		||||
	<li>{{ playlist.name }}</li>
 | 
			
		||||
	{% endfor %}
 | 
			
		||||
</ul>
 | 
			
		||||
 | 
			
		||||
<!-- vim: ft=htmldjango
 | 
			
		||||
@ -5,7 +5,7 @@
 | 
			
		||||
{% block navbar_title %}Melodia{% endblock %}
 | 
			
		||||
 | 
			
		||||
{% block sidebar_content %}
 | 
			
		||||
<a href="#">Sidebar content goes here.</a>
 | 
			
		||||
	{% include "components/playlist_sidebar" %}
 | 
			
		||||
{% endblock %}
 | 
			
		||||
 | 
			
		||||
{% block body_content %}
 | 
			
		||||
@ -3,8 +3,10 @@
 | 
			
		||||
{% block page_title  %}Login to Melodia{% endblock %}
 | 
			
		||||
 | 
			
		||||
{% block navbar_title %}Login to Melodia{% endblock %}
 | 
			
		||||
{# Disable navbar content other than the title #}
 | 
			
		||||
{% block navbar_content %}{% endblock %}
 | 
			
		||||
 | 
			
		||||
{# Specifying a blank sidebar, rather than sidebar_content, allows us to remove the sidebar entirely. #}
 | 
			
		||||
{# Similar to navbar, but remove entire sidebar #}
 | 
			
		||||
{% block sidebar %}{% endblock %}
 | 
			
		||||
 | 
			
		||||
{% block body %}
 | 
			
		||||
@ -22,8 +24,8 @@
 | 
			
		||||
		<button class="btn btn-primary">Login to Melodia</button>
 | 
			
		||||
	</form>
 | 
			
		||||
 | 
			
		||||
	{% if auth_form.non_field_errors %}
 | 
			
		||||
	<div class="alert alert-error span4 offset4">{{ auth_form.non_field_errors }}</div>
 | 
			
		||||
	{% if login_error %}
 | 
			
		||||
	<div class="alert alert-error span4 offset4">{{ login_error }}</div>
 | 
			
		||||
	{% endif %}
 | 
			
		||||
 | 
			
		||||
</center>
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user