def minister_questions_combined(): """ Mixture of old QuestionReplies and new CommitteeQuestion objects folded together in date order to support pagination. """ filters = get_filters() # To make pagination possible, we grab a combined list of IDs, # paginate that list, and then fetch the details. # get a combined list of IDs q1 = db.session.query(CommitteeQuestion.id, CommitteeQuestion.date.label("date"), literal_column("'cq'").label("type")) for f in filters: q1 = q1.filter_by(**f) q2 = db.session.query(QuestionReply.id, QuestionReply.start_date.label("date"), literal_column("'qr'").label("type")) for f in filters: q2 = q2.filter_by(**f) query = q1.union_all(q2).order_by(desc("date")) query, count, next = paginate_request_query(query) # pull out the IDs we want cq_ids = [c[0] for c in query if c[2] == 'cq'] qr_ids = [c[0] for c in query if c[2] == 'qr'] # get committee questions query = CommitteeQuestion.list()\ .filter(CommitteeQuestion.id.in_(cq_ids))\ .order_by(CommitteeQuestion.date.desc())\ .options( lazyload('minister'), joinedload('asked_by_member'), lazyload('asked_by_member.memberships')) for f in filters: query = query.filter_by(**f) objects = query.all() # get question reply objects query = QuestionReply.list()\ .filter(QuestionReply.id.in_(qr_ids))\ .order_by(QuestionReply.start_date.desc())\ .options( lazyload('minister')) for f in filters: query = query.filter_by(**f) # mash them together objects.extend(query.all()) # sort objects.sort( key=lambda x: getattr(x, 'date', getattr(x, 'start_date', None)), reverse=True) out = serializers.queryset_to_json(objects, count=count, next=next) return send_api_response(out)
def question_reply_committees(): """ A list of those committees that have received questions and replies. We might want to generalise this at some point """ items = Committee.for_related(QuestionReply).all() return send_api_response(serializers.queryset_to_json(items, count=len(items)))
def api_resource_list(base_query): for f in get_filters(): base_query = base_query.filter_by(**f) queryset, count, next = paginate_request_query(base_query) out = serializers.queryset_to_json(queryset, count=count, next=next) return send_api_response(out)
def minister_questions_combined(): """ Mixture of old QuestionReplies and new CommitteeQuestion objects folded together in date order to support pagination. """ filters = get_filters() # To make pagination possible, we grab a combined list of IDs, # paginate that list, and then fetch the details. # get a combined list of IDs q1 = db.session.query(CommitteeQuestion.id, CommitteeQuestion.date.label("date"), literal_column("'cq'").label("type")) for f in filters: q1 = q1.filter_by(**f) q2 = db.session.query(QuestionReply.id, QuestionReply.start_date.label("date"), literal_column("'qr'").label("type")) for f in filters: q2 = q2.filter_by(**f) query = q1.union_all(q2).order_by(desc("date")) query, count, next = paginate_request_query(query) # pull out the IDs we want cq_ids = [c[0] for c in query if c[2] == 'cq'] qr_ids = [c[0] for c in query if c[2] == 'qr'] # get committee questions query = CommitteeQuestion.list()\ .filter(CommitteeQuestion.id.in_(cq_ids))\ .order_by(CommitteeQuestion.date.desc())\ .options( lazyload('committee'), lazyload('minister'), joinedload('asked_by_member'), lazyload('asked_by_member.memberships')) for f in filters: query = query.filter_by(**f) objects = query.all() # get question reply objects query = QuestionReply.list()\ .filter(QuestionReply.id.in_(qr_ids))\ .order_by(QuestionReply.start_date.desc())\ .options( lazyload('committee'), lazyload('minister')) for f in filters: query = query.filter_by(**f) # mash them together objects.extend(query.all()) # sort objects.sort(key=lambda x: getattr(x, 'date', getattr(x, 'start_date', None)), reverse=True) out = serializers.queryset_to_json(objects, count=count, next=next) return send_api_response(out)
def api_resource(resource_id, base_query): try: resource = base_query.filter_by(id=resource_id).one() except NoResultFound: raise ApiException(404, "Not found") status_code = 200 if resource == "committee-meeting": if not resource.check_permission(): if current_user.is_anonymous(): status_code = 401 # Unauthorized, i.e. authentication is required else: status_code = 403 # Forbidden, i.e. the user is not subscribed return send_api_response(serializers.queryset_to_json(resource), status_code=status_code)