app/soc/logic/document.py
changeset 206 832335761384
parent 143 b419121f7b3e
child 225 2590d6b83568
equal deleted inserted replaced
205:4a86df751222 206:832335761384
    19 
    19 
    20 __authors__ = [
    20 __authors__ = [
    21   '"Pawel Solyga" <pawel.solyga@gmail.com>',
    21   '"Pawel Solyga" <pawel.solyga@gmail.com>',
    22   ]
    22   ]
    23 
    23 
       
    24 
    24 from google.appengine.ext import db
    25 from google.appengine.ext import db
       
    26 
       
    27 from soc.logic import key_name
    25 
    28 
    26 import soc.models.document
    29 import soc.models.document
    27 import soc.logic.model
    30 import soc.logic.model
    28 
    31 
    29 def getDocumentFromPath(path):
    32 
       
    33 def getDocument(path, link_name):
    30   """Returns Document entity for a given path, or None if not found.  
    34   """Returns Document entity for a given path, or None if not found.  
    31     
    35     
    32   Args:
    36   Args:
    33     path: a request path of the Document that uniquely identifies it
    37     path: a request path of the Document that uniquely identifies it
    34   """
    38   """
    35   # lookup by Doc:path key name
    39   # lookup by Doc:path key name
    36   key_name = getDocumentKeyNameForPath(path)
    40   name = key_name.nameDocument(path, link_name)
    37   
    41   
    38   if key_name:
    42   if name:
    39     document = soc.models.document.Document.get_by_key_name(key_name)
    43     document = soc.models.document.Document.get_by_key_name(name)
    40   else:
    44   else:
    41     document = None
    45     document = None
    42   
    46   
    43   return document
    47   return document
    44 
    48 
    45 def getDocumentKeyNameForPath(path):
       
    46   """Return a Datastore key_name for a Document from the path.
       
    47   
       
    48   Args:
       
    49     path: a request path of the Document that uniquely identifies it
       
    50   """
       
    51   if not path:
       
    52     return None
       
    53 
    49 
    54   return 'Doc:%s' % path
    50 def updateOrCreateDocument(path, **document_properties):
    55 
       
    56 
       
    57 def updateOrCreateDocumentFromPath(path, **document_properties):
       
    58   """Update existing Document entity, or create new one with supplied properties.
    51   """Update existing Document entity, or create new one with supplied properties.
    59 
    52 
    60   Args:
    53   Args:
    61     path: a request path of the Document that uniquely identifies it
    54     path: a request path of the Document that uniquely identifies it
    62     **document_properties: keyword arguments that correspond to Document entity
    55     **document_properties: keyword arguments that correspond to Document entity
    66     the Document entity corresponding to the path, with any supplied
    59     the Document entity corresponding to the path, with any supplied
    67     properties changed, or a new Document entity now associated with the 
    60     properties changed, or a new Document entity now associated with the 
    68     supplied path and properties.
    61     supplied path and properties.
    69   """
    62   """
    70   # attempt to retrieve the existing Document
    63   # attempt to retrieve the existing Document
    71   document = getDocumentFromPath(path)
    64   document = getDocument(path, document_properties['link_name'])
    72 
    65 
    73   if not document:
    66   if not document:
    74     # document did not exist, so create one in a transaction
    67     # document did not exist, so create one in a transaction
    75     key_name = getDocumentKeyNameForPath(path)
    68     name = key_name.nameDocument(path, document_properties['link_name'])
    76     document = soc.models.document.Document.get_or_insert(
    69     document = soc.models.document.Document.get_or_insert(
    77       key_name, **document_properties)
    70         name, **document_properties)
    78 
    71 
    79   # there is no way to be sure if get_or_insert() returned a new Document or
    72   # there is no way to be sure if get_or_insert() returned a new Document or
    80   # got an existing one due to a race, so update with document_properties anyway,
    73   # got an existing one due to a race, so update with document_properties anyway,
    81   # in a transaction
    74   # in a transaction
    82   return soc.logic.model.updateModelProperties(document, **document_properties)
    75   return soc.logic.model.updateModelProperties(document, **document_properties)