app/soc/logic/models/base.py
changeset 402 021e86368600
parent 399 b82852e6963e
child 403 d3e545a8bd26
equal deleted inserted replaced
401:37d0b6c25f3e 402:021e86368600
    24   ]
    24   ]
    25 
    25 
    26 
    26 
    27 from google.appengine.ext import db
    27 from google.appengine.ext import db
    28 
    28 
       
    29 from django.utils.translation import ugettext_lazy
       
    30 
    29 from soc.logic import out_of_band
    31 from soc.logic import out_of_band
    30 
    32 
    31 
    33 
    32 class Logic:
    34 class Logic:
    33   """Base logic for entity classes.
    35   """Base logic for entity classes.
    96       # an entity exist for this link_name, so return that entity
    98       # an entity exist for this link_name, so return that entity
    97       return entity
    99       return entity
    98 
   100 
    99     fields = []
   101     fields = []
   100 
   102 
   101     msg = 'There is no %s where ' % self._name
   103     format_text = ugettext_lazy('"%(key)s" is "%(value)s"')
   102 
   104 
   103     for index, pair in enumerate(kwargs.iteritems()):
   105     msg_pairs = [format_text % {'key': key, 'value': value}
   104       if index != 0:
   106       for key, value in kwargs.iteritems()]
   105         msg += ' and '
   107 
   106 
   108     joined_pairs = ' and '.join(msg_pairs)
   107       msg += '"%s" is "%s"' % pair
   109 
   108 
   110     msg = ugettext_lazy(
   109     msg += '.'
   111       'There is no "%(name)s" where %(pairs)s.') % {
       
   112         'name': self._name, 'pairs': joined_pairs}
       
   113 
   110 
   114 
   111     # else: fields were supplied, but there is no Entity that has it
   115     # else: fields were supplied, but there is no Entity that has it
   112     raise out_of_band.ErrorResponse(msg, status=404)
   116     raise out_of_band.ErrorResponse(msg, status=404)
   113 
   117 
   114   def getKeyNameForFields(self, **kwargs):
   118   def getKeyNameForFields(self, **kwargs):
   134     Args:
   138     Args:
   135       fields: a dictionary with the values for all the key_fields
   139       fields: a dictionary with the values for all the key_fields
   136           of this entity.
   140           of this entity.
   137     """
   141     """
   138 
   142 
   139     suffix = ''
   143     suffix = []
   140 
   144 
   141     for field in self._model.key_fields:
   145     for field in self._model.KEY_FIELDS:
   142       suffix += fields[field]
   146       suffix.append(fields[field])
   143       suffix += '/'
   147 
   144 
   148     return '/'.join(suffix)
   145     if suffix.endswith('/'):
       
   146       suffix = suffix[:-1]
       
   147 
       
   148     return suffix
       
   149 
   149 
   150   def getEmptyKeyFields(self):
   150   def getEmptyKeyFields(self):
   151     """Returns an dict with all the entities key_fields set to None
   151     """Returns an dict with all the entities key_fields set to None
   152     """
   152     """
   153 
   153 
   154     kwargs = {}
   154     kwargs = {}
   155 
   155 
   156     for field in self._model.key_fields:
   156     for field in self._model.KEY_FIELDS:
   157       kwargs[field] = None
   157       kwargs[field] = None
   158 
   158 
   159     return kwargs
   159     return kwargs
   160 
   160 
   161   def extractKeyFields(self, fields):
   161   def extractKeyFields(self, fields):
   166     """
   166     """
   167 
   167 
   168     key_fields = {}
   168     key_fields = {}
   169 
   169 
   170     for key, value in fields.iteritems():
   170     for key, value in fields.iteritems():
   171       if key in self._model.key_fields:
   171       if key in self._model.KEY_FIELDS:
   172         key_fields[key] = value
   172         key_fields[key] = value
   173 
   173 
   174     return key_fields
   174     return key_fields
   175 
   175 
   176   def getForLimitAndOffset(self, limit, offset=0):
   176   def getForLimitAndOffset(self, limit, offset=0):