app/soc/logic/site/settings.py
changeset 206 832335761384
parent 202 b8b4a83788d4
equal deleted inserted replaced
205:4a86df751222 206:832335761384
    27 
    27 
    28 import soc.models.site_settings
    28 import soc.models.site_settings
    29 import soc.logic.model
    29 import soc.logic.model
    30 
    30 
    31 
    31 
    32 def getSiteSettingsFromPath(path):
    32 def getSiteSettings(path):
    33   """Returns SiteSettings entity for a given path, or None if not found.  
    33   """Returns SiteSettings entity for a given path, or None if not found.  
    34     
    34     
    35   Args:
    35   Args:
    36     path: a request path of the SiteSettings that uniquely identifies it
    36     path: a request path of the SiteSettings that uniquely identifies it
    37   """
    37   """
    38   # lookup by Settings:path key name
    38   # lookup by Settings:path key name
    39   key_name = getSiteSettingsKeyNameForPath(path)
    39   name = key_name.nameSiteSettings(path)
    40   
    40   
    41   if key_name:
    41   if name:
    42     site_settings = soc.models.site_settings.SiteSettings.get_by_key_name(key_name)
    42     site_settings = soc.models.site_settings.SiteSettings.get_by_key_name(name)
    43   else:
    43   else:
    44     site_settings = None
    44     site_settings = None
    45   
    45   
    46   return site_settings
    46   return site_settings
    47 
    47 
    48 
    48 
    49 def getSiteSettingsKeyNameForPath(path):
    49 def updateOrCreateSiteSettings(path, **site_settings_properties):
    50   """Return a Datastore key_name for a SiteSettings from the path.
       
    51   
       
    52   Args:
       
    53     path: a request path of the SiteSettings that uniquely identifies it
       
    54   """
       
    55   if not path:
       
    56     return None
       
    57 
       
    58   return key_name.nameSiteSettings(path)
       
    59 
       
    60 
       
    61 def updateOrCreateSiteSettingsFromPath(path, **site_settings_properties):
       
    62   """Update existing SiteSettings entity, or create new one with supplied properties.
    50   """Update existing SiteSettings entity, or create new one with supplied properties.
    63 
    51 
    64   Args:
    52   Args:
    65     path: a request path of the SiteSettings that uniquely identifies it
    53     path: a request path of the SiteSettings that uniquely identifies it
    66     **site_settings_properties: keyword arguments that correspond to Document entity
    54     **site_settings_properties: keyword arguments that correspond to Document entity
    70     the SiteSettings entity corresponding to the path, with any supplied
    58     the SiteSettings entity corresponding to the path, with any supplied
    71     properties changed, or a new SiteSettings entity now associated with the 
    59     properties changed, or a new SiteSettings entity now associated with the 
    72     supplied path and properties.
    60     supplied path and properties.
    73   """
    61   """
    74   # attempt to retrieve the existing Site Settings
    62   # attempt to retrieve the existing Site Settings
    75   site_settings = getSiteSettingsFromPath(path)
    63   site_settings = getSiteSettings(path)
    76 
    64 
    77   if not site_settings:
    65   if not site_settings:
    78     # site settings did not exist, so create one in a transaction
    66     # site settings did not exist, so create one in a transaction
    79     key_name = getSiteSettingsKeyNameForPath(path)
    67     name = key_name.nameSiteSettings(path)
    80     site_settings = soc.models.site_settings.SiteSettings.get_or_insert(
    68     site_settings = soc.models.site_settings.SiteSettings.get_or_insert(
    81       key_name, **site_settings_properties)
    69       name, **site_settings_properties)
    82 
    70 
    83   # there is no way to be sure if get_or_insert() returned a new SiteSettings or
    71   # there is no way to be sure if get_or_insert() returned a new SiteSettings or
    84   # got an existing one due to a race, so update with site_settings_properties anyway,
    72   # got an existing one due to a race, so update with site_settings_properties anyway,
    85   # in a transaction
    73   # in a transaction
    86   return soc.logic.model.updateModelProperties(site_settings, **site_settings_properties)
    74   return soc.logic.model.updateModelProperties(site_settings, **site_settings_properties)