app/soc/logic/models/user.py
author Pawel Solyga <Pawel.Solyga@gmail.com>
Fri, 07 Nov 2008 22:24:01 +0000
changeset 448 075360be6743
parent 434 a4fc430fefac
child 459 2cfcedaf7c16
permissions -rw-r--r--
Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing). Patch by: Pawel Solyga
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
"""User (Model) query functions.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
"""
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
__authors__ = [
429
e50e18936f06 Fixed typo in e-mail address
Sverre Rabbelier <srabbelier@gmail.com>
parents: 316
diff changeset
    21
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
  ]
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
from soc.logic.models import base
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 308
diff changeset
    26
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
import soc.models.user
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
class Logic(base.Logic):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
  """Logic methods for the User model
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
  """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
  def __init__(self):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
    """Defines the name, key_name and model for this entity.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
432
1851d67a1004 Add a generic method for getting a Keyname
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
    38
    self._name = "User"
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
    self._model = soc.models.user.User
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
    self._skip_properties = ['former_ids']
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
448
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    42
  def isFormerId(self, id):
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    43
    """Returns true if id is in former ids"""
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    44
    # TODO(pawel.solyga): replace 1000 with solution that works for any number of queries
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    45
    users_with_former_ids = soc.models.user.User.gql('WHERE former_ids != :1', None).fetch(1000)
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    46
    
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    47
    for former_id_user in users_with_former_ids: 
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    48
      if id in former_id_user.former_ids:
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    49
        return True
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    50
    
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    51
    return False
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    52
434
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    53
  def getKeyValues(self, entity):
448
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    54
    """See base.Logic.getKeyValues.
434
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    55
    """
448
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    56
    
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    57
    return [entity.id.email()]
434
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    58
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    59
  def getKeyValuesFromFields(self, fields):
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    60
    """See base.Logic.getKeyValuesFromFields.
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    61
    """
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    62
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    63
    properties = {
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    64
        'link_name': fields['link_name']
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    65
        }
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    66
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    67
    entity = self.getForFields(properties, unique=True)
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    68
    return [entity.id.email()]
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    69
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    70
  def getKeyFieldNames(self):
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    71
    """See base.Logic.getKeyFieldNames
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    72
    """
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    73
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    74
    return ['email']
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
    75
448
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    76
  def updateOrCreateFromId(self, properties, id):
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    77
    """Like updateOrCreateFromKeyName, but resolves id to a key_name first.
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    78
    """
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    79
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    80
    # attempt to retrieve the existing entity
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    81
    user = soc.models.user.User.gql('WHERE id = :1', id).get()
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    82
    
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    83
    if user:
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    84
      key_name = user.key().name()
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    85
    else:
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    86
      key_name  = self.getKeyNameForFields({'email': id.email()})
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    87
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    88
    return self.updateOrCreateFromKeyName(properties, key_name)
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 434
diff changeset
    89
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    90
  def _updateField(self, model, name, value):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    91
    """Special case logic for id.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    92
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    93
    When the id is changed, the former_ids field should be appended
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
    with the old id.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    95
    """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    96
    if name == 'id' and model.id != value:
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    97
      model.former_ids.append(model.id)
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    98
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    99
    return True
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   100
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   101
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   102
logic = Logic()