示例#1
0
def agriculture_facts():
    if request.method == "GET":

        # if not current_user.can(Permission.QUANTIFY_R):
        #     return jsonify(status="fail", data=[], reason="no permission")

        args = std_json(request.args)

        if not check_args(ALLOW_ARGS, args.keys()):
            return jsonify(status="fail", reason="error args", data=[])

        start_time = args.get('start_time')
        end_time = args.get('end_time')

        table = AgricultureTable.r_query().filter(AgricultureTable.id==args.get('table_id')).first()
        if table is None:
            return jsonify(status="fail", data=[], reason="no such table")

        log_id = args.get('log_id')
        if log_id is None:
            log_id = table.cur_log_id

        result = []
        for country_id in args.get("country_ids"):
            for index_id in args.get("index_ids"):
                for kind_id in args.get('kind_ids'):
                    facts = AgricultureFacts.find(table_id=table.id, index_ids=[index_id], kind_ids=[kind_id],
                                                  country_ids=[country_id], start_time=int(start_time),
                                                  end_time=int(end_time), log_id=log_id)
                    index = AgricultureIndexes.r_query().filter(AgricultureIndexes.id==index_id).first()
                    country = Country.r_query().filter(Country.id==country_id).first()
                    kind = AgricultureKind.r_query().filter(AgricultureKind.id==kind_id).first()
                    result.append(
                        {"country": country.to_json(),
                         "index": index.to_json(),
                         "kind": kind.to_json(),
                         "data": [fact.to_json() for fact in facts]})

        # facts = AgricultureFacts.find(table_id=args.get("table_id"), index=args.get("index"),
        #                                   country=args.get("country"), start_time=args.get("start_time"))
        #
        # result = list()
        #
        # for fact in facts:
        #     _tmp_has_index = find_in_list(result, fact.index_id, lambda x, y: x.get("index_id") == y)
        #     if _tmp_has_index is None:
        #         result.append({"index_id": fact.index_id,
        #                        "data": [{"country_id": fact.country_id,
        #                                  "data": [fact.to_json()]}]})
        #     else:
        #         _tmp_has_country = find_in_list(result, fact.country_id, lambda x, y: x.get("country_id") == y)
        #         if _tmp_has_country is None:
        #             _tmp_has_index['data'].append({"country_id": fact.country_id,
        #                                            "data": [fact.to_json()]})
        #         else:
        #             _tmp_has_country["data"].append(fact.to_json)

        return jsonify(status="success", reason="", data=result)
示例#2
0
def socioeconomic_facts_graph():
    if request.method == "GET":

        # if not current_user.can(Permission.QUANTIFY_R):
        #     return jsonify(status="fail", data=[], reason="no permission")

        args = std_json(request.args)

        if not check_args(ALLOW_ARGS, args.keys()):
            return jsonify(status="fail", reason="error args", data=[])

        table = SocioeconomicTable.r_query().filter(
            SocioeconomicTable.id==args.get("table_id")).first()
        if table is None:
            return jsonify(status="fail", data=[], reason="no such table")

        log_id = args.get("log_id")

        if log_id is None:
            log_id = table.cur_log_id

        start_time = args.get("start_time")
        # if start_time is not None:
        #     start_time = datetime.strptime(str(start_time), "%Y")

        end_time = args.get("end_time")
        # if end_time is not None:
        #     end_time = datetime.strptime(str(end_time), "%Y")
        datas = []
        for index_id in args.get("index_ids"):
            for country_id in args.get("country_ids"):
                index = SocioeconomicIndexes.r_query().filter(
                    SocioeconomicIndexes.id==index_id).first()
                country = Country.r_query().filter_by(Country.id==country_id).first()
                facts = SocioeconomicFacts.find(
                    table_id=args.get("table_id"),
                    index_ids=[index_id],
                    country_ids=[country_id],
                    start_time=int(start_time),
                    end_time=int(end_time),
                    log_id=log_id)
                if index is None or country is None or facts is None:
                    break
                fact_series = []
                for fact in facts:
                    fact_serie = {'x': fact.time, 'y': fact.value}
                    fact_series.append(fact_serie)
                data = {
                    'index': index.to_json_by_fact(),
                    'country': country.to_json(),
                    'series': fact_series
                }
                datas.append(data)
        return jsonify({
            'status': 'success',
            'reason': '',
            'data': datas
        })
示例#3
0
def images():

    fields = [i for i in Image.__table__.c._data]

    if request.method == "GET":

        # if not current_user.can(Permission.QUALITATIVE_R):
        #     return jsonify(status="fail", data=[], reason="no permission")

        if not check_args(fields, request.args.keys()):
            return jsonify(status="fail", reason="error args", data=[])

        page = request.args.get('page')
        if page is None:
            page = 1

        page = int(page)

        query = Image.r_query()
        args = std_json(request.args)
        for k, v in args.items():
            if k in fields:
                query = query.filter(getattr(Image, k)==v)
        query = query.order_by(Image.id.desc())

        # pagenation = query.paginate(page, per_page=current_app.config['FLASKY_POSTS_PER_PAGE'],
        #                                             error_out=False)
        return jsonify(status="success", reason="", data=[item.to_json() for item in query],
                       # page={'current':pagenation.pages,'per_page':pagenation.per_page,'total':pagenation.total}
                       )

    if request.method == "POST":

        # if not current_user.can(Permission.QUALITATIVE_W):
        #     return jsonify(status="fail", data=[], reason="no permission")

        form = request.form
        if not check_args(fields, form.keys()):
            return jsonify(status="fail", reason="error form args", data=[])

        c = Image()

        for k, v in form.items():
            if hasattr(c, k):
                setattr(c, k, v)
        c.user_id = current_user.id
        c.show = False

        try:
            db.session.add(c)
            db.session.commit()
        except sqlalchemy.exc.OperationalError as e:
            return jsonify(status="fail", reason=e, data=[])

        return jsonify(status="success", reason="", data=[c.to_json()])
示例#4
0
def users():

    fields = [i for i in User.__table__.c._data]

    if request.method == "GET":

        # if not current_user.can(Permission.USER_R):
        #     return jsonify(status="fail", data=[], reason="no permission")

        if not check_args(SPECIAL_ARGS+fields, request.args.keys()):
            return jsonify(status="fail", reason="error args", data=[])

        page = request.args.get('page')
        if page is None:
            page = 1

        page = int(page)

        query = User.r_query()

        args = std_json(request.args)

        for k, v in args.items():
            if k in fields:
                query = query.filter(getattr(User, k)==v)

        pagenation = query.paginate(page, per_page=current_app.config['FLASKY_POSTS_PER_PAGE'],
                                                    error_out=False)
        return jsonify(status="success", reason="", data=[item.to_json() for item in pagenation.items],
                       page={'current':pagenation.pages,'per_page':pagenation.per_page,'total':pagenation.total})

    if request.method == "POST":

        # if not current_user.can(Permission.USER_W):
        #     return jsonify(status="fail", data=[], reason="no permission")

        form = request.form
        if not check_args(SPECIAL_ARGS+fields, form.keys()):
            return jsonify(status="fail", reason="error form args", data=[])

        c = User()

        for k, v in form.items():
            if hasattr(c, k):
                setattr(c, k, v)

        c.password = form.get("password")

        try:
            db.session.add(c)
            db.session.commit()
        except sqlalchemy.exc.OperationalError as e:
            return jsonify(status="fail", reason=e, data=[])

        return jsonify(status="success", reason="", data=[c.to_json()])
示例#5
0
def post_simple():

    fields = [i for i in Post.__table__.c._data]

    if request.method == "GET":

        # if not current_user.can(Permission.QUALITATIVE_R):
        #     return jsonify(status="fail", data=[], reason="no permission")

        if not check_args(fields, request.args.keys()):
            return jsonify(status="fail", reason="error args", data=[])

        page = request.args.get('page')
        if page is None:
            page = 1

        page = int(page)

        query = Post.r_query()
        args = std_json(request.args)

        if current_user.username != "Anonymous":
            if current_user.role.name != "Administrator":
                pass
            else:
                args['country_id'] = current_user.country.id

        for k, v in args.items():
            if k in fields:
                query = query.filter(getattr(Post, k)==v)

        query = query.order_by(Post.timestamp.desc()).limit(40)

        pagenation = query.paginate(page, per_page=current_app.config['FLASKY_POSTS_PER_PAGE'],
                                                    error_out=False)
        return jsonify(status="success", reason="", data=[item.to_json_simple() for item in pagenation.items],
                       page={'current':pagenation.pages,'per_page':pagenation.per_page,'total':pagenation.total}
                       )
示例#6
0
def socioeconomic_facts():
    if request.method == "GET":

        # if not current_user.can(Permission.QUANTIFY_R):
        #     return jsonify(status="fail", data=[], reason="no permission")

        args = std_json(request.args)

        if not check_args(ALLOW_ARGS, args.keys()):
            return jsonify(status="fail", reason="error args", data=[])

        start_time = args.get("start_time")
        # if start_time is not None:
        #     start_time = datetime.strptime(str(start_time), "%Y")

        end_time = args.get("end_time")
        # if end_time is not None:
        #     end_time = datetime.strptime(str(end_time), "%Y")

        table = SocioeconomicTable.r_query().filter(
            SocioeconomicTable.id==args.get("table_id")).first()
        if table is None:
            return jsonify(status="fail", data=[], reason="no such table")

        log_id = args.get("log_id")

        if log_id is None:
            log_id = table.cur_log_id

        result = []

        for country_id in args.get("country_ids"):
            for index_id in args.get("index_ids"):
                facts = SocioeconomicFacts.find(
                    table_id=table.id,
                    index_ids=[index_id],
                    country_ids=[country_id],
                    start_time=int(start_time),
                    end_time=int(end_time),
                    log_id=log_id)
                index = SocioeconomicIndexes.r_query().filter(
                    SocioeconomicIndexes.id==index_id).first()
                country = Country.r_query().filter(Country.id==country_id).first()
                result.append(
                    {"country": country.to_json(),
                     "index": index.to_json(),
                     "data": [fact.to_json() for fact in facts]})

        # tmp_index = {}
        # tmp_country = {}
        # for fact in facts:
        #     if tmp_index.get(fact.index_id) is None:
        #         tmp_index[fact.index_id] = [fact]
        #     else:
        #         tmp_index[fact.index_id].append(fact)
        #
        # for index_id,same_index_facts in tmp_index.items():
        #
        #     for fact in same_index_facts:
        #         if tmp_country.get(fact.country_id) is None:
        #             tmp_country[fact.country_id] = [fact]
        #         else:
        #             tmp_country[fact.country_id].append(fact)
        #
        #     index = SocioeconomicIndexes.query.filter_by(id=index_id).first()
        #     final_data = []
        #     for country_id, same_country_facts in tmp_country.items():
        #         country = Country.query.filter_by(id=country_id).first()
        #         final_data.append({"country": country.to_json(), "data":[fact.to_json() for fact in same_country_facts]})
        #     result.append({"index":index.to_json(), "data":list(final_data)})

        return jsonify(status="success", reason="", data=result)