login/logout/register

This commit is contained in:
j 2013-02-13 16:55:56 +05:30
parent b91cbbe8fb
commit d3e68f5ba0
8 changed files with 101 additions and 1 deletions

0
app/__init__.py Normal file
View file

3
app/models.py Normal file
View file

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

16
app/tests.py Normal file
View file

@ -0,0 +1,16 @@
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)

62
app/views.py Normal file
View file

@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from datetime import datetime
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
from django.template import RequestContext
from django.conf import settings
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib import auth
from django.contrib.auth.models import User, Group
from django.contrib.auth import authenticate
from django.core.mail import send_mail
from django.core.cache import cache
from ox.django.shortcuts import render_to_json_response
def login(request):
message = ''
if 'username' in request.POST and 'password' in request.POST:
user = authenticate(username=request.POST['username'], password=request.POST['password'])
if user and user.is_active:
auth.login(request, user)
return HttpResponseRedirect(settings.PREFIX+'/')
else:
message = 'Login failed.'
context = RequestContext(request, {
'settings': settings,
'message': message
})
return render_to_response('login.html', context)
def logout(request):
auth.logout(request)
return HttpResponseRedirect(settings.PREFIX+'/')
def register(request):
message = ''
if 'username' in request.POST and 'password' in request.POST:
username = request.POST['username']
password = request.POST['password']
email = request.POST['email']
#fixme validate email
if User.objects.filter(username__iexact=username).count() > 0:
message = 'Username already exists'
elif User.objects.filter(email__iexact=email).count() > 0:
message = 'Email address already exits'
elif not password:
message = 'Password can not be empty'
else:
user = User(username=username, email=email)
user.set_password(password)
user.save()
user = authenticate(username=username, password=password)
auth.login(request, user)
return HttpResponseRedirect(settings.PREFIX+'/')
context = RequestContext(request, {
'settings': settings,
'message': message
})
return render_to_response('register.html', context)

View file

@ -85,9 +85,13 @@ def add(request):
return HttpResponseRedirect('/')
def index(request):
items = None
if not request.user.is_anonymous():
items = request.user.items.order_by('filename')
context = RequestContext(request, {
'PREFIX': settings.PREFIX,
'anonymous': request.user.is_anonymous(),
'items': items,
})
return render_to_response('index.html', context)

View file

@ -13,7 +13,10 @@ var UPLOAD_URL = "{{PREFIX}}/add";
<h1>videopdf</h1>
{% if anonymous %}
<div>
you need an account
You have to
<a href="{{PREFIX}}/login">login</a>
or <a href="{{PREFIX}}register">register</a>.
</div>
{% else %}
<div>
@ -23,6 +26,11 @@ var UPLOAD_URL = "{{PREFIX}}/add";
<input id="upload" type="button" value="Upload" style="display: none" />
</form>
</div>
<ul>
{% for item in items %}
<li><a href="{{item.get_absolute_url}}">{{item.filename}}</a></li>
{% endfor %}
</ul>
{% endif %}
</body>
</html>

View file

@ -122,6 +122,7 @@ INSTALLED_APPS = (
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
'django.contrib.admindocs',
'app',
'item'
)

View file

@ -34,6 +34,12 @@ if settings.DEBUG:
{'document_root': settings.STATIC_ROOT}),
)
urlpatterns += patterns('app.views',
(p('login$'), 'login'),
(p('logout$'), 'logout'),
(p('register$'), 'register'),
)
urlpatterns += patterns('item.views',
(p('$'), 'index'),
(p('add$'), 'add'),