app/soc/logic/models/user.py
changeset 448 075360be6743
parent 434 a4fc430fefac
child 459 2cfcedaf7c16
equal deleted inserted replaced
447:83b49d60effd 448:075360be6743
    20 __authors__ = [
    20 __authors__ = [
    21   '"Sverre Rabbelier" <sverre@rabbelier.nl>',
    21   '"Sverre Rabbelier" <sverre@rabbelier.nl>',
    22   ]
    22   ]
    23 
    23 
    24 
    24 
    25 from soc.logic import key_name
       
    26 from soc.logic.models import base
    25 from soc.logic.models import base
    27 
    26 
    28 import soc.models.user
    27 import soc.models.user
    29 
    28 
    30 
    29 
    38 
    37 
    39     self._name = "User"
    38     self._name = "User"
    40     self._model = soc.models.user.User
    39     self._model = soc.models.user.User
    41     self._skip_properties = ['former_ids']
    40     self._skip_properties = ['former_ids']
    42 
    41 
       
    42   def isFormerId(self, id):
       
    43     """Returns true if id is in former ids"""
       
    44     # TODO(pawel.solyga): replace 1000 with solution that works for any number of queries
       
    45     users_with_former_ids = soc.models.user.User.gql('WHERE former_ids != :1', None).fetch(1000)
       
    46     
       
    47     for former_id_user in users_with_former_ids: 
       
    48       if id in former_id_user.former_ids:
       
    49         return True
       
    50     
       
    51     return False
       
    52 
    43   def getKeyValues(self, entity):
    53   def getKeyValues(self, entity):
    44     """See base.Logic.getKeyNameValues.
    54     """See base.Logic.getKeyValues.
    45     """
    55     """
    46 
    56     
    47     return [entity.link_name]
    57     return [entity.id.email()]
    48 
    58 
    49   def getKeyValuesFromFields(self, fields):
    59   def getKeyValuesFromFields(self, fields):
    50     """See base.Logic.getKeyValuesFromFields.
    60     """See base.Logic.getKeyValuesFromFields.
    51     """
    61     """
    52 
    62 
    61     """See base.Logic.getKeyFieldNames
    71     """See base.Logic.getKeyFieldNames
    62     """
    72     """
    63 
    73 
    64     return ['email']
    74     return ['email']
    65 
    75 
       
    76   def updateOrCreateFromId(self, properties, id):
       
    77     """Like updateOrCreateFromKeyName, but resolves id to a key_name first.
       
    78     """
       
    79 
       
    80     # attempt to retrieve the existing entity
       
    81     user = soc.models.user.User.gql('WHERE id = :1', id).get()
       
    82     
       
    83     if user:
       
    84       key_name = user.key().name()
       
    85     else:
       
    86       key_name  = self.getKeyNameForFields({'email': id.email()})
       
    87 
       
    88     return self.updateOrCreateFromKeyName(properties, key_name)
       
    89 
    66   def _updateField(self, model, name, value):
    90   def _updateField(self, model, name, value):
    67     """Special case logic for id.
    91     """Special case logic for id.
    68 
    92 
    69     When the id is changed, the former_ids field should be appended
    93     When the id is changed, the former_ids field should be appended
    70     with the old id.
    94     with the old id.