app/soc/models/user.py
author Pawel Solyga <Pawel.Solyga@gmail.com>
Fri, 22 Aug 2008 13:44:50 +0000
changeset 99 8c38b546a3cf
parent 98 b2b823466a8b
child 118 d2e61a490969
permissions -rw-r--r--
Added public view support (not using controller yet) Changed Google Account variable from user to id in views and templates (no more confusions with Melange user). Added templates_helpers for makeSimblingTemplate function. Addded new template tag readonly_field_as_table_row used in public views Fixed one typo in roles.py
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
# 
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
# 
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
"""This module contains the User Model."""
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
__authors__ = [
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
  '"Todd Larsen" <tlarsen@google.com>',
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    21
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
78
206e6eeed6c4 Updated User and Person models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 54
diff changeset
    22
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
7
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
]
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    25
from google.appengine.ext import db
78
206e6eeed6c4 Updated User and Person models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 54
diff changeset
    26
from django.utils.translation import ugettext_lazy
7
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    27
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    28
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
class User(db.Model):
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    30
  """A user and associated login credentials, the fundamental identity entity.
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    31
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
  User is a separate Model class from Person because the same login 
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
  ID may be used to, for example, serve as Contributor in one Program 
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
  and a Reviewer in another.
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    35
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    36
  Also, this allows a Person to, in the future, re-associate that 
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    37
  Person entity with a different Google Account if necessary.
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    38
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
  A User entity participates in the following relationships implemented 
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
  as a db.ReferenceProperty elsewhere in another db.Model:
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    41
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    42
   persons)  a 1:many relationship of Person entities identified by the
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
     User.  This relation is implemented as the 'persons' back-reference
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
     Query of the Person model 'user' reference.
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    45
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    46
  """
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
  #: A Google Account, which also provides a "private" email address.
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    49
  #: This email address is only used in an automated fashion by 
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    50
  #: Melange web applications and is not made visible to other users 
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    51
  #: of any Melange application.
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    52
  id = db.UserProperty(required=True)
5c72db822ebb Initial revision of the user.py module, containing the User Model, from the
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    53
78
206e6eeed6c4 Updated User and Person models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 54
diff changeset
    54
  #: Required field storing a nickname; displayed publicly.
206e6eeed6c4 Updated User and Person models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 54
diff changeset
    55
  #: Nicknames can be any valid UTF-8 text.
206e6eeed6c4 Updated User and Person models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 54
diff changeset
    56
  nick_name = db.StringProperty(required=True,
206e6eeed6c4 Updated User and Person models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 54
diff changeset
    57
      verbose_name=ugettext_lazy('Nick name'))
206e6eeed6c4 Updated User and Person models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 54
diff changeset
    58
      
206e6eeed6c4 Updated User and Person models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 54
diff changeset
    59
  #: Required field storing linkname used in URLs to identify user.
206e6eeed6c4 Updated User and Person models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 54
diff changeset
    60
  #: Lower ASCII characters only.
206e6eeed6c4 Updated User and Person models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 54
diff changeset
    61
  link_name = db.StringProperty(required=True,
206e6eeed6c4 Updated User and Person models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 54
diff changeset
    62
      verbose_name=ugettext_lazy('Link name'))
206e6eeed6c4 Updated User and Person models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 54
diff changeset
    63
  link_name.help_text = ugettext_lazy(
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 78
diff changeset
    64
      'Field used in URLs to identify user. '
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 78
diff changeset
    65
      'Lower ASCII characters only.')
98
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    66
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    67
  @staticmethod
99
8c38b546a3cf Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 98
diff changeset
    68
  def doesUserExist(id=None):
98
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    69
    """Returns if user already exists in the Datastore.
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    70
    
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    71
    Args:
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    72
      user: a Google Account object,
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    73
    """
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    74
    #: let's do a gql query and check if user exists in datastore
99
8c38b546a3cf Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 98
diff changeset
    75
    data = self.getUserForId(id)
98
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    76
    if data:
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    77
      return True
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    78
    else:
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    79
      return False
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    80
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    81
  @staticmethod
99
8c38b546a3cf Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 98
diff changeset
    82
  def getUserForId(id=None):
98
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    83
    """Returns User entity from datastore, or None if not found.  
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    84
    
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    85
    Args:
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    86
      user: a Google Account object,
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    87
    """
99
8c38b546a3cf Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 98
diff changeset
    88
    return User.gql('WHERE id = :1', id).get()
98
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    89
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    90
  @staticmethod
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    91
  def getUserForLinkname(link_name=None):
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    92
    """Returns User entity for linkname or None if not found.
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    93
    
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    94
    Args:
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    95
      link_name: linkname used in URLs to identify user,
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    96
    """
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    97
    return User.gql('WHERE link_name = :1', link_name).get()
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 83
diff changeset
    98