def find_unclaimed(cls, email): P = cls DP = data.DataPoint handler = data.get_handler('email', email) profile = Session.query(P).join(DP).filter(P.user==None).filter(DP.key==u'email').filter(DP.value==handler.normalized).first() return profile
def fetch_teathers(self, status=None, order_by='teathers.created_date', order_sort=sa.asc, teathered_profile=None): q = Session.query(Teather).filter(Teather.owning_profile_id==self.id) if status: q = q.filter(status=status) if teathered_profile: q = q.filter(Teather.teathered_profile==teathered_profile) q.order_by(order_sort(order_by)) return q.all()
def authenticate(username, password, redirect_after=True, from_http_auth=False): q = Session.query(users.User) q = q.filter(sa.or_(users.User.username==username, users.User.email==username)) u = q.first() if u and u.is_active and u.does_password_match(password): return login(u, redirect_after=redirect_after, from_http_auth=from_http_auth) else: raise exceptions.ClientException('Email and password do not match.', code=exceptions.MISMATCH, field='password') return None
def teather(self, other_profile, latitude=None, longitude=None): """ same as following. self.teather(other) means the current profile is following 'other' """ q = Session.query(Teather) q = q.filter(Teather.teathered_profile_id==other_profile.id) existing = q.filter(Teather.owning_profile_id==self.id).first() if existing: return existing q = Session.query(Teather).filter(Teather.owning_profile_id==other_profile.id) recip = q.filter(Teather.teathered_profile_id==self.id).first() if recip: new = recip.accept() else: new = Teather(owning_profile=self, teathered_profile=other_profile, status=STATUS_PENDING, latitude=latitude, longitude=longitude) Session.add(new) return new
def get_user(key='user'): """ Gets the user model object if user has logged on. Will be the pretend user if an admin is pretending to be someone. Returns/sets a cached copy (from the context c var) """ if getattr(c, key): return getattr(c, key) user_id = session.get(key) if user_id: setattr(c, key, Session.query(users.User).outerjoin(users.UserPreference).filter(users.User.id == user_id).first()) if session['user'] == session['real_user']: c.user = c.real_user = getattr(c, key) else: setattr(c, key, None) return getattr(c, key)
def add_data(self, user, key, value, type=u''): """ Blindly adds data to a profile """ if not user: raise exceptions.AppException('Gimmie a user', field=u'user', code=exceptions.INVALID) #we overwrite any keys that were created by this user. q = Session.query(data.DataPoint).filter(data.DataPoint.profile==self) q = q.filter(data.DataPoint.key==key) q = q.filter(data.DataPoint.owner==user) d = q.first() handler = data.get_handler(key, value) if d: d.value = handler.normalized else: d = data.DataPoint(profile=self, owner=user, key=key, value=handler.normalized, type=type) Session.add(d) return d
def fetch_data(self, user=None): """ :param user: user who will see the data. When user == None, will get ALL data points """ q = Session.query(data.DataPoint).filter(data.DataPoint.profile_id==self.id) if user: owners = [user.id] if self.user: owners.append(self.user.id) q = q.filter(data.DataPoint.owner_id.in_(owners)) data_points = q.all() # if this profile wanted to be connected to the viewing user, the viewing user can # see all the user's info. teather = self.fetch_teathers(teathered_profile=user.profile) if not teather: #we have a special case where we should return the user's name always points = [] for dp in data_points: if dp.owner == user or dp.key == 'name': points.append(dp) data_points = points # else, can see all user's info else: data_points = q.all() points = dd(lambda: []) for dp in data_points: sig = '%s:%s' % (dp.key, dp.type) #the data from user who owns the profile is more important if dp.owner == self.user: points[sig] = [dp]+points[sig] else: points[sig].append(dp) return [v[0] for k, v in points.items() if v]