def accept(self): if self.status != STATUS_ACCEPTED and not self.reciprocated_teather_id: new = Teather(owning_profile=self.teathered_profile, teathered_profile=self.owning_profile, status=STATUS_ACCEPTED, reciprocated_teather=None) Session.add(new) self.status = STATUS_ACCEPTED self.reciprocated_teather = new return new return None
def __call__(self, environ, start_response): """Invoke the Controller""" # WSGIController.__call__ dispatches to the Controller method # the request is routed to. This routing information is # available in environ['pylons.routes_dict'] try: self._session = Session if '_debug_frontend' in request.params: #now we can force this no matter the environment. c.debug_frontend = request.params['_debug_frontend'] == 'True' else: c.debug_frontend = not h.is_production() #this is used by timer proxy and the templates c.show_debug = bool(session.get('show_debug')) request.environ['USER'] = session.get('username', '') request.environ['REAL_USER'] = session.get('real_username', '') # set the start of the rendering c.render_start = time.time() c.requested_url = request.environ.get('PATH_INFO') if request.environ.get('QUERY_STRING'): c.requested_url += '?' + request.environ['QUERY_STRING'] logger.info(c.requested_url) # Capture IP address in non-ssl mode, so we can use it in SSL mode see ticket #2275 ip = auth.get_user_ip() if not session.get('IP_ADDRESS') and ip: session['IP_ADDRESS'] = ip elif not session.get('IP_ADDRESS') and request.environ.get('HTTP_RLNCLIENTIPADDR'): session['IP_ADDRESS'] = request.environ.get('HTTP_RLNCLIENTIPADDR') elif not session.get('IP_ADDRESS') and request.environ.get('REMOTE_ADDR'): session['IP_ADDRESS'] = request.environ.get('REMOTE_ADDR') # Save the first referer we see to store in user record when/if we create one. if not session.get('referer'): session['referer'] = environ.get('HTTP_REFERER','').decode('utf-8','ignore') session.save() return WSGIController.__call__(self, environ, start_response) finally: if 'paste.testing_variables' not in request.environ: Session.remove()
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 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 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 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]
def init_model(engine): """Call me before using any of the tables or classes in the model""" Session.configure(bind=engine) from psycopg2 import extensions extensions.register_type(extensions.UNICODE)