app/soc/models/linkable.py
author Pawel Solyga <Pawel.Solyga@gmail.com>
Thu, 20 Nov 2008 22:51:07 +0000
changeset 524 774b379e058c
parent 510 6cd4f44891e5
child 533 ba3309b2fd30
permissions -rw-r--r--
Small comment fix in Linkable model. Patch by: Pawel Solyga
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
467
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
# 
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
# 
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
"""This module contains the Linkable base class Model."""
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
__authors__ = [
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
  '"Todd Larsen" <tlarsen@google.com>',
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    21
]
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
475
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    24
import re
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    25
503
65e4d3ed3fc3 Derive Linkable Model class from PolyModel.
Todd Larsen <tlarsen@google.com>
parents: 475
diff changeset
    26
import polymodel
65e4d3ed3fc3 Derive Linkable Model class from PolyModel.
Todd Larsen <tlarsen@google.com>
parents: 475
diff changeset
    27
467
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    28
from google.appengine.ext import db
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    30
from django.utils.translation import ugettext_lazy
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    31
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
475
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    33
# start with ASCII digit or lowercase
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    34
#   (additional ASCII digit or lowercase
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    35
#     -OR-
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    36
#   underscore and ASCII digit or lowercase)
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    37
#     zero or more of OR group
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    38
#
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    39
# * starting or ending underscores are *not* permitted
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    40
# * double internal underscores are *not* permitted
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    41
#
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    42
LINK_ID_PATTERN_CORE = r'[0-9a-z](?:[0-9a-z]|_[0-9a-z])*'
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    43
LINK_ID_ARG_PATTERN = r'(?P<link_id>%s)' % LINK_ID_PATTERN_CORE
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    44
LINK_ID_PATTERN = r'^%s$' % LINK_ID_PATTERN_CORE
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    45
LINK_ID_REGEX = re.compile(LINK_ID_PATTERN)
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    46
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    47
503
65e4d3ed3fc3 Derive Linkable Model class from PolyModel.
Todd Larsen <tlarsen@google.com>
parents: 475
diff changeset
    48
class Linkable(polymodel.PolyModel):
467
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    49
  """A base class for Model classes that are "linkable".
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    50
  
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    51
  Many entities in Melange are identified by a "link path" that is formed
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    52
  by two components:  a "link scope" and a "link ID".
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    53
  
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    54
  The link scope is a reference to another Linkable entity, but its exact
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    55
  usage varies depending on:
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    56
  
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    57
   * the Model type of the entity
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    58
   * the "ownership" of the entity
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    59
   
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    60
  This scope represents the "context" of the entity and is *not* user-
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    61
  editable (site Developers will be able to *carefully* edit the scope
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    62
  of a Linkable entity, but implementing this will be tricky).
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    63
  
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    64
  Appended to this "link path prefix" generated from the transitive
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    65
  closure of the link scopes is a link ID.  Unlike the rest of the link
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    66
  path, this ID, which must be unique within the scope defined by the link
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    67
  path, is *not* determined by context and *is* supplied by the user.
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    68
  
468
198105fb00bc Fix typo in linkable model and add some missing comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 467
diff changeset
    69
  For example, a Document containing the FAQs for the Apache Software 
198105fb00bc Fix typo in linkable model and add some missing comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 467
diff changeset
    70
  Foundation participation in GSoC 2009 program sponsored
467
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    71
  by Google could be given a link ID by the Apache organization
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    72
  administrator of "faqs", but the rest of the link path would be
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    73
  determined by the transitive closure of the scopes of the Document:
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    74
  
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    75
    google/gsoc2009/asf/faqs
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    76
      ^       ^      ^   ^
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    77
      |       |      |   +---------  link ID assigned by Apache admin
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    78
      |       |      |
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    79
      |       |      +-------------  Apache org link ID (immutable)
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    80
      |       |
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    81
      |       +--------------------  GSoC 2009 program link ID (immutable)
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    82
      |
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    83
      +----------------------------  Google sponsor link ID (immutable)
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    84
      
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    85
  For many entities, link IDs, once specified, are immutable, since
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    86
  changing them can break bookmarked URLs.  Changing the link IDs of
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    87
  "leaf" entities (such as the Document in the example above) could
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    88
  be allowed. 
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    89
  """
524
774b379e058c Small comment fix in Linkable model.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 510
diff changeset
    90
  #: Required field storing "ID" used in URL links. Lower ASCII characters,
475
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    91
  #: digits and underscores only.  Valid link IDs successfully match
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    92
  #: the LINK_ID_REGEX.
510
6cd4f44891e5 id() is a built-in function in Python, so rename the property to link_id. Add
Todd Larsen <tlarsen@google.com>
parents: 503
diff changeset
    93
  link_id = db.StringProperty(required=True,
467
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    94
      verbose_name=ugettext_lazy('Link ID'))
510
6cd4f44891e5 id() is a built-in function in Python, so rename the property to link_id. Add
Todd Larsen <tlarsen@google.com>
parents: 503
diff changeset
    95
  link_id.help_text = ugettext_lazy(
6cd4f44891e5 id() is a built-in function in Python, so rename the property to link_id. Add
Todd Larsen <tlarsen@google.com>
parents: 503
diff changeset
    96
      '"ID" used when creating URL links.'
475
8bd9db1d7a30 Copy the link_name regex patterns into linkable.py, in preparation for
Todd Larsen <tlarsen@google.com>
parents: 468
diff changeset
    97
      ' Lower ASCII characters, digits, and underscores only.')
467
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    98
468
198105fb00bc Fix typo in linkable model and add some missing comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 467
diff changeset
    99
  #: Optional Self Reference property to another Linkable entity which defines
198105fb00bc Fix typo in linkable model and add some missing comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 467
diff changeset
   100
  #: the "scope" of this Linkable entity. The back-reference in the Linkable 
198105fb00bc Fix typo in linkable model and add some missing comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 467
diff changeset
   101
  #: model is a Query named 'links'.
467
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   102
  scope = db.SelfReferenceProperty(required=False,
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   103
      collection_name='links', verbose_name=ugettext_lazy('Link Scope'))
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   104
  scope.help_text = ugettext_lazy(
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   105
      'Reference to another Linkable entity that defines the "scope" of'
07441582717a Initial thoughts on a Linkable Model class. Please comment.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   106
      ' this Linkable entity.')
510
6cd4f44891e5 id() is a built-in function in Python, so rename the property to link_id. Add
Todd Larsen <tlarsen@google.com>
parents: 503
diff changeset
   107
6cd4f44891e5 id() is a built-in function in Python, so rename the property to link_id. Add
Todd Larsen <tlarsen@google.com>
parents: 503
diff changeset
   108
  #: Hidden (not displayed to users or editable in forms) cache of the string
6cd4f44891e5 id() is a built-in function in Python, so rename the property to link_id. Add
Todd Larsen <tlarsen@google.com>
parents: 503
diff changeset
   109
  #: representation of the transitive closure of scopes, for use in URLs.
6cd4f44891e5 id() is a built-in function in Python, so rename the property to link_id. Add
Todd Larsen <tlarsen@google.com>
parents: 503
diff changeset
   110
  #: The multiple queries required to produce this string for entities in
6cd4f44891e5 id() is a built-in function in Python, so rename the property to link_id. Add
Todd Larsen <tlarsen@google.com>
parents: 503
diff changeset
   111
  #: deeply-nested scopes can be prohibitively expensive.  The scope of an
6cd4f44891e5 id() is a built-in function in Python, so rename the property to link_id. Add
Todd Larsen <tlarsen@google.com>
parents: 503
diff changeset
   112
  #: entity is not expected to change frequently (only for move, copy, and
6cd4f44891e5 id() is a built-in function in Python, so rename the property to link_id. Add
Todd Larsen <tlarsen@google.com>
parents: 503
diff changeset
   113
  #: maybe re-parenting operations), so this property is not likely to need
6cd4f44891e5 id() is a built-in function in Python, so rename the property to link_id. Add
Todd Larsen <tlarsen@google.com>
parents: 503
diff changeset
   114
  #: updating.
6cd4f44891e5 id() is a built-in function in Python, so rename the property to link_id. Add
Todd Larsen <tlarsen@google.com>
parents: 503
diff changeset
   115
  scope_path = db.StringProperty(required=False,
6cd4f44891e5 id() is a built-in function in Python, so rename the property to link_id. Add
Todd Larsen <tlarsen@google.com>
parents: 503
diff changeset
   116
      verbose_name=ugettext_lazy('Scope path'))
6cd4f44891e5 id() is a built-in function in Python, so rename the property to link_id. Add
Todd Larsen <tlarsen@google.com>
parents: 503
diff changeset
   117
  scope_path.help_text = ugettext_lazy(
6cd4f44891e5 id() is a built-in function in Python, so rename the property to link_id. Add
Todd Larsen <tlarsen@google.com>
parents: 503
diff changeset
   118
      'Cache of the string form of the entity scope.')
6cd4f44891e5 id() is a built-in function in Python, so rename the property to link_id. Add
Todd Larsen <tlarsen@google.com>
parents: 503
diff changeset
   119