def listable_children(self): authenticated = authenticated_userid(self.request) submitter = get_submitter(self.request) if submitter and not authenticated: def query(cls): return session.query(cls.uid)\ .filter(cls.submitter == submitter).all() else: creator = authenticated def query(cls): return session.query(cls.uid)\ .filter(cls.creator == creator).all() root = self.model.root def fetch_node(container, uid): return container[str(uid)] nodes = list() session = get_session(self.request) attachments = root['attachments'] for record in query(AttachmentRecord): nodes.append(fetch_node(attachments, record[0])) facilities = root['facilities'] for record in query(FacilityRecord): nodes.append(fetch_node(facilities, record[0])) locations = root['locations'] for record in query(LocationRecord): nodes.append(fetch_node(locations, record[0])) occasions = root['occasions'] for record in query(OccasionRecord): nodes.append(fetch_node(occasions, record[0])) return nodes
def add_category(request, name): session = get_session(request) category = CategoryRecord() category.uid = uuid.uuid4() category.name = name session.add(category) return category
def occasions_by_uid(request, uids): if not uids: return list() uids = [ensure_uuid(uid) for uid in uids] session = get_session(request) return session.query(OccasionRecord)\ .filter(OccasionRecord.uid.in_(uids))\ .all()
def results_to_instances(request, results): instances = [] for r in results: cls = pickle.loads('{0}'.format(r.get('cls'))) uid = r.get('uid') instance = get_session(request).query(cls).get(uid) instances.append(instance) return instances
def search_categories(request, term, limit=None): session = get_session(request) query = session.query(CategoryRecord)\ .filter(CategoryRecord.name.like(u'%{0}%'.format(term)))\ .order_by(CategoryRecord.name) if limit is not None: query = query.limit(limit) return query.all()
def categories_by_uid(request, uids): if not uids: return list() uids = [ensure_uuid(uid) for uid in uids] session = get_session(request) return session.query(CategoryRecord)\ .filter(CategoryRecord.uid.in_(uids))\ .all()
def json_related_locations(model, request): uid = uuid.UUID(request.params['uid']) actions = dict([(v, k) for k, v in value_actions.items()]) cls = actions[request.params['action']] record = get_session(request).query(cls).get(uid) if not record: return list() result = dict() extract_locations(request, record, result) return result.values()
def search_occasions(request, term, state=[], submitter=None, limit=None): session = get_session(request) query = session.query(OccasionRecord) query = query.filter(OccasionRecord.title.like(u'%{0}%'.format(term))) if state: query = query.filter(OccasionRecord.state.in_(state)) if submitter: query = query.filter(OccasionRecord.submitter == submitter) query = query.order_by(OccasionRecord.title) if limit is not None: query = query.limit(limit) return query.all()
def locations_in_bounds(request, north, south, west, east, state=[], submitter=None, limit=None): session = get_session(request) query = session.query(LocationRecord)\ .filter(LocationRecord.lon.between(west, east))\ .filter(LocationRecord.lat.between(south, north)) if state: query = query.filter(LocationRecord.state.in_(state)) if submitter: query = query.filter(LocationRecord.submitter == submitter) if limit is not None: query = query.limit(limit) return query.all()
def search_locations(request, term, state=[], submitter=None, limit=None): session = get_session(request) query = session.query(LocationRecord) query = query.filter(or_(LocationRecord.street.like(u'%{0}%'.format(term)), LocationRecord.zip.like(u'%{0}%'.format(term)), LocationRecord.city.like(u'%{0}%'.format(term)), LocationRecord.lat.like(u'%{0}%'.format(term)), LocationRecord.lon.like(u'%{0}%'.format(term)))) if state: query = query.filter(LocationRecord.state.in_(state)) if submitter: query = query.filter(LocationRecord.submitter == submitter) query = query.order_by(LocationRecord.city) if limit is not None: query = query.limit(limit) return query.all()
def occasion_by_uid(request, uid): session = get_session(request) return session.query(OccasionRecord).get(ensure_uuid(uid))
def category_by_name(request, name): session = get_session(request) res = session.query(CategoryRecord).filter(CategoryRecord.name==name).all() if res: return res[0]
def category_by_uid(request, uid): session = get_session(request) return session.query(CategoryRecord).get(ensure_uuid(uid))
def delete_category(request, category): session = get_session(request) session.delete(category)
def facility_by_uid(request, uid): session = get_session(request) return session.query(FacilityRecord).get(ensure_uuid(uid))