google(五)User Module

google(五)User Module

1. prepare work:
install Eclipse YAML Editor plugin:
http://www.symfony.pl/yamleditor

I create a project named 4wargame, and svn import to my repository,command>
svn import 4wargame/ https://sillycat.googlecode.com/svn/trunck/4wargame [email protected] --password=xxxxx

2. create app
Create app named registration for account management.
Create app named portal for the front page of my application.

Modified the settings.py at the root of the project:
MIDDLEWARE_CLASSES = (
    'ragendja.middleware.ErrorMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    # Django authentication
    #'django.contrib.auth.middleware.AuthenticationMiddleware',
    # Google authentication
    #'ragendja.auth.middleware.GoogleAuthenticationMiddleware',
    # Hybrid Django/Google authentication
    'ragendja.auth.middleware.HybridAuthenticationMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'ragendja.sites.dynamicsite.DynamicSiteIDMiddleware',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
    'django.contrib.redirects.middleware.RedirectFallbackMiddleware',
)

AUTH_USER_MODULE = 'ragendja.auth.hybrid_models'
GLOBALTAGS = ('ragendja.templatetags.googletags',)

LOGIN_URL = '/registration/login/'
LOGOUT_URL = '/registration/logout/'
LOGIN_REDIRECT_URL = '/portal/'

INSTALLED_APPS = (
    # Add jquery support (app is in "common" folder). This automatically
    # adds jquery to your COMBINE_MEDIA['combined-%(LANGUAGE_CODE)s.js']
    # Note: the order of your INSTALLED_APPS specifies the order in which
    # your app-specific media files get combined, so jquery should normally
    # come first.
    'jquery',

    # Add blueprint CSS (http://blueprintcss.org/)
    #'blueprintcss',
    'django.contrib.auth',
    'django.contrib.sessions',
    #'django.contrib.admin',
    'django.contrib.webdesign',
    'django.contrib.flatpages',
    'django.contrib.redirects',
    'django.contrib.sites',
    'appenginepatcher',
    'ragendja',
    'mediautils',
    'registration',
    'portal',
)

Modified the urls.py file at the root of project:
urlpatterns = auth_patterns + patterns('',
    (r'^registration/', include('registration.urls')),
    (r'^portal/', include('portal.urls')),
    (r'^$', 'django.views.generic.simple.direct_to_template',
        {'template': 'main.html'}),
) + urlpatterns

my template file templates/base.html :
<html>
  <body>
  {% load googletags %}
  <div>
  {% if user.is_authenticated %}
            Welcome, {{ user.username }}!
            {% if user.is_active %}
      <a href="/registration/logout">
    {% else %}
      <a href="{% google_logout_url "/registration/login" %}">
    {% endif %}Logout</a>
        {% else %}
            <a href="{% google_login_url "/registration/login" %}">Login with your Google account</a>
            <a href="/registration/login">Login with normal account</a>
            <a href="/registration/signup">Create your normal account</a><br>
        {% endif %}
  </div>
  <div>
      {% block content %}{% endblock %}
  </div>
  </body>
</html>

the views.py controller of portal app:
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.views.generic.list_detail import object_list, object_detail
from django.views.generic.create_update import create_object


@login_required
def main(request):
    #print request.user.username
    return render_to_response('portal/main.html',{"user":request.user})

@login_required means that if you want to access this method, we need you to logon the system.

urls.py for registration app:
# -*- coding: utf-8 -*-
from django.conf.urls.defaults import *
from ragendja.urlsauto import urlpatterns
from ragendja.auth.urls import urlpatterns as auth_patterns
from django.contrib import admin

urlpatterns = patterns('',
(r'^$', 'django.contrib.auth.views.login', {'template_name': 'registration/user_form.html'}),
(r'^signup/$', 'registration.views.create_new_user'),
(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'registration/login.html'}),
(r'^logout/$', 'django.contrib.auth.views.logout', {'template_name': '/registration/logged_out.html'}),
)

login, logout, and registe method, views.py controller:
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.views.generic.list_detail import object_list, object_detail
from django.views.generic.create_update import create_object

def create_new_user(request):
    form = UserCreationForm()
    # if form was submitted, bind form instance.
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            # user must be active for login to work
            user.is_active = True
            user.put()
            return HttpResponseRedirect('/portal/')
    return render_to_response('registration/user_form.html', {'form': form})

the pages for the 3 function are customised and as follow
logged_out.html:
{% extends "base.html" %}
{% block title %}Login{% endblock %}
{% load googletags %}

{% block content %}
<h1>Logout page: logged_out.html</h1>

  You logout ! Thanks for visit our page.

{% endblock content %}

login.html:
{% extends "base.html" %}
{% block title %}Login{% endblock %}
{% load googletags %}

{% block content %}
<h1>Login page: login.html</h1>

  <form action="{{ request.get_full_path }}" method="post">
  <table>{{ form }}</table>
  <input type="submit" value="Login" />
</form>

{% endblock content %}

user_form.html:
{% extends "base.html" %}
{% block title %}Login{% endblock %}
{% load googletags %}

{% block content %}
<h1>Registration page: user_form.html</h1>
<form action="." method="post">
  <table>
    {{ form.as_table }}
  </table>
  You can also login with your <a href="{% google_login_url "/registration/login" %}">Google account.</a>
  <input type="submit" value="submit">
</form>

{% endblock content %}

It is my user management module for my demo system.

3 Other references:
method One- google account
document URL: http://code.google.com/intl/zh-CN/appengine/docs/python/users/overview.html

method Two- google appengine patch
error code:
SMTPServerDisconnected at /account/register/
solution:
send email error: python manage.py runserver --enable_sendmail

google django
http://gae-django-cms.appspot.com/article/571003/gae-django-cms
http://code.google.com/intl/zh-CN/appengine/articles/app-engine-patch.html

get current users:
http://gae-django-cms.appspot.com/article/575004/django%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E7%94%A8%E6%88%B7

你可能感兴趣的:(jquery,django,SVN,Google,GAE)