コード例 #1
0
ファイル: profile.py プロジェクト: Nullicopter/tethr
def add_data(real_user, user, profile, **kw):
    """
    can pass in data like 'phone:work'='555-555-5555'
    """
    if not profile:
        abort(403)
        
    forbidden_keys = ['latitude', 'longitude']
    
    class KVForm(formencode.Schema):
        key = fv.UnicodeString(not_empty=True, min=2, max=64)
        type = fv.OneOf([TYPE_WORK, TYPE_HOME, TYPE_MOBILE], not_empty=False)
        value = fv.UnicodeString(not_empty=True, min=2, max=1024)
    
    for k, v in kw.items():
        type = None
        s = k.split(':', 1)
        if len(s) == 2:
            k, type = s
        
        if k in forbidden_keys: continue
        
        if v:
            scrubbed = validate(KVForm, key=k, value=v, type=type)
            
            profile.add_data(user, scrubbed.key, scrubbed.value, type=scrubbed.type)
    
    Session.flush()
    
    return profile
コード例 #2
0
ファイル: profile.py プロジェクト: Nullicopter/tethr
def teather(real_user, user, profile=None, email=None, latitude=None, longitude=None, **kw):
    """
    Will teather/follow another user
    
    kw can be extra data to be asscoiated with the teather. Passing a type is done
    by using a colon. i.e.
    
    kw['phone'] = '415-343-1234' # would just add a phone number
    kw['phone:work'] = '415-343-1234' # would add a phone number with type work
    
    also, notes are associated the same way as everything else.
    
    kw['notes'] = 'We met at the xyz blah'
    """
    if (not profile and not email) or not user.profile:
        abort(403)
    
    profile = get(real_user, user, profile=profile, email=email)
    if not profile and not email:
        abort(403)
    
    #create unclaimed
    elif not profile:
        email = validate(TeatherForm, email=email).email
        
        profile = profiles.Profile(user=None)
        Session.add(profile)
        Session.flush()
    
    add_data(real_user, user, profile, **kw)
    
    #attach an email address...
    if not email and profile.user:
        email = profile.user.email
    
    
    #all_data = profile.fetch_data()
    #has_email = False
    #for d in all_data:
    #    if d.key == 'email':
    #        has_email = True
    #        if (email and data.EmailHandler.normalize(email) != d.value):
    #            profile.add_data(user, 'email', email, type=d.type)
    
    #if not has_email and not email:
    #    raise ClientException('Need to specify an email address!')
    #elif not has_email:
    
    if email:
        profile.add_data(user, 'email', email)
    
    t = user.profile.teather(profile, latitude=latitude, longitude=longitude)
    Session.flush()
    
    return t
コード例 #3
0
ファイル: user.py プロジェクト: Nullicopter/tethr
def set_pref(real_user, user, key, value, use_real_user=True):
    
    class Pref(formencode.Schema):
        key     = fv.MaxLength(64, not_empty=True)
        value   = fv.MaxLength(64, not_empty=False)
    scrubbed = validate(Pref, key=key, value=value)
    
    u = user
    if use_real_user:
        u = real_user
    u.set_preference(scrubbed.key, scrubbed.value or '')
コード例 #4
0
ファイル: profile.py プロジェクト: Nullicopter/tethr
def create(user, **params):
    """
    Creates a profile.
    
    DO NOT EXPOSE THIS to the web api. Please.
    """
    if user and user.profile:
        raise AppException('User cannot have an existing profile.', field='user', code=INVALID)
    
    params.setdefault('is_active', True)
    
    scrubbed = validate(CreateForm, **params)
    
    profile = profiles.Profile(user=user, **scrubbed)
    Session.add(profile)
    
    return profile
コード例 #5
0
ファイル: user.py プロジェクト: Nullicopter/tethr
def create(**params):
    """
    Creates a user.
    
    DO NOT EXPOSE THIS to the web api. Please.
    """
    numusers = len(Session.query(users.User).all())
    
    scrubbed = validate(RegisterForm, **params)
    
    user = users.User()
    Session.add(user)
    
    user.email = scrubbed.email
    user.username = '******' in scrubbed and scrubbed.username or scrubbed.email
    user.password = scrubbed.password
    user.set_timezone_int(scrubbed.default_timezone)
    
    if scrubbed.get('name'):
        name = scrubbed.get('name').split(' ', 1)
        user.first_name = name[0].strip()
        user.last_name = len(name) == 2 and name[1].strip() or u''
    else:
        user.first_name = scrubbed.get('first_name')
        user.last_name = scrubbed.get('last_name')
    
    #first user is an admin. 
    if numusers == 0:
        user.role = users.ROLE_ADMIN
    
    #this will need some thought. We should have some kind of verification.
    profile = profiles.Profile.find_unclaimed(user.email)
    if profile:
        profile.user = user
    else:
        from tethr import api
        profile = api.profile.create(user)
    
    if user.first_name or user.last_name:
        profile.add_data(user, 'name', u'%s %s' % (user.first_name, user.last_name))
    
    profile.add_data(user, 'email', user.email)
    
    Session.flush()
    
    return user
コード例 #6
0
ファイル: error.py プロジェクト: Nullicopter/tethr
def explode_no_auth(real_user, user, type=None, **kw):
    """
    An action to test the error handling of the stack. The tests use this.
    """
    if type == 'app':
        raise AppException('This is an app exception!', code=INVALID, field='type')
    elif type == 'client':
        raise ClientException('Oh Noes, ClientException!', code=INVALID, field='type')
    elif type == 'client404':
        raise ClientException('Oh Noes, ClientException NOT FOUND!', code=NOT_FOUND, field='type')
    elif type == 'client403':
        raise ClientException('Oh Noes, ClientException FORBIDDEN!', code=FORBIDDEN, field='type')
    elif type == 'explosion':
        1/0
    elif type == 'http':
        abort(404, 'This thing was not found!')
    elif type == 'validation':
        class Rawr(formencode.Schema):
            meow = fv.Number()
        scrubbed = validate(Rawr, meow='zzzz')
    
    return kw