예제 #1
0
def get_professor(professor_id):
    # id is a primary key, so we'll have max 1 result row
    professor = Professor.query.filter_by(id=professor_id).first()
    if professor:
        return jsonify(row2dict(professor))
    else:
        return make_response(jsonify({"code": 404, "msg": "Cannot find this professor id."}), 404)
예제 #2
0
def get_student(student_id):
    # id is a primary key, so we'll have max 1 result row
    student = Student.query.filter_by(id=student_id).first()
    if student:
        return jsonify(row2dict(student))
    else:
        return make_response(jsonify({"code": 404, "msg": "Cannot find this student id."}), 404)
예제 #3
0
def get_person(person_id):
    # id is a primary key, so we'll have max 1 result row
    person = models.Person.query.filter_by(id=person_id).first()
    if person:
        return jsonify(models.row2dict(person))
    else:
        return make_response(jsonify({"code": 404, "msg": "Cannot find this person id."}), 404)
예제 #4
0
def update_user(uid=None):
    manager = AccountManager()
    params = {}
    for k, v in request.values.iteritems():
        params[k] = v
    ret, res = manager.update_user(uid, **params)
    if not ret:
        abort(res[0], res[1])
    return jsonify(user=row2dict(res), rolenames=res.rolenames)
예제 #5
0
def update_password():
    manager = AccountManager()
    old = request.values.get("password")
    new = request.values.get("new_password")
    confirm = request.values.get("confirm")
    ret, res = manager.update_password(g.user.uid, old, new, confirm)
    if not ret:
        abort(res[0], res[1])
    return jsonify(user=row2dict(res), rolenames=res.rolenames)
예제 #6
0
 def get_attribute_by_id(self, attr_id):
     attr = db.session.query(CIAttribute).filter(
         CIAttribute.attr_id == attr_id).first()
     if attr:
         attr_dict = row2dict(attr)
         if attr.is_choice:
             attr_dict["choice_value"] = self._get_choice_value(
                 attr.attr_id, attr.value_type)
         return attr_dict
예제 #7
0
def return_event_day(event_type, year, month, day):
    httpmethod = request.method

    if httpmethod == "GET":
        results = Result.query.filter_by(year=year,
                                         month=month,
                                         day=day,
                                         type=event_type).all()
        if not results:
            return jsonify({"code": 204, "msg": "no results"})
        return make_response(jsonify([row2dict(result) for result in results]))

    if httpmethod == "POST":
        # get the year first, if no year then fail
        # year = request.args.get('year')

        # if not year and not month and not day:
        #     return make_response(jsonify({"code": 403,
        #                                   "msg": "Cannot post event. Missing mandatory fields."}), 403)

        # month = request.args.get('month')
        # day = request.args.get('day')

        # month_dict = {1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June',
        #               7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December',
        #               13: 'Nobel Prizes'}
        # month_name = month_dict[month]

        data = request.get_data()
        data = data.decode('utf8').replace('([+&%])', " ")
        # events = json.loads(data)

        # if not month and not day:
        #     r = Result(year=year, event=event)
        # elif not month:
        #     r = Result(year=year, event=event)
        # elif not day:
        #     r = Result(year=year, month=month_name, event=event)
        # else:
        #     r = Result(year=year, month=month_name, day=day, event=event)

        r = Result(year=year,
                   month=month,
                   day=day,
                   type=event_type,
                   event=data)
        db.session.add(r)
        try:
            db.session.commit()
        except sqlalchemy.exc.SQLAlchemyError as e:
            error = "Cannot post search result. "
            # print(app.config.get("DEBUG"))
            if app.config.get("DEBUG"):
                error += str(e)
            return make_response(jsonify({"code": 404, "msg": error}), 404)
        return jsonify({"code": 200, "msg": "success"})
예제 #8
0
def get_user(user_id):
    user = User.query.filter_by(id=user_id).first()
    if user:
        return jsonify(row2dict(user))
    else:
        return make_response(
            jsonify({
                "code": 404,
                "msg": "Cannot find this person id."
            }), 404)
예제 #9
0
def get_cart(cart_id):
    # id is a primary key, so we'll have max 1 result row
    cart = Cart.query.filter_by(id=cart_id).first()
    if cart:
        return jsonify(row2dict(cart))
    else:
        return make_response(
            jsonify({
                "code": 404,
                "msg": "Cannot find this cart id."
            }), 404)
예제 #10
0
파일: main.py 프로젝트: andypham29/SOEN487
def get_city(city_id):
    # id is a primary key, so we'll have max 1 result row
    city = City.query.filter_by(id=city_id).first()
    if city:
        return jsonify(row2dict(city))
    else:
        return make_response(
            jsonify({
                "code": 404,
                "msg": "Cannot find this city id."
            }), 404)
예제 #11
0
파일: main.py 프로젝트: andypham29/SOEN487
def get_team(team_id):
    # id is a primary key, so we'll have max 1 result row
    team = Team.query.filter_by(id=team_id).first()
    if team:
        return jsonify(row2dict(team))
    else:
        return make_response(
            jsonify({
                "code": 404,
                "msg": "Cannot find this team id."
            }), 404)
예제 #12
0
def get_user(user_id):
    # id is a primary key, so we'll have max 1 result row
    user = User.query.filter_by(id=user_id).first()
    if user:
        return jsonify(row2dict(user))
    else:
        return make_response(
            jsonify({
                "code": 404,
                "msg": "Cannot find this user id."
            }), 404)
예제 #13
0
 def get_citypes(self, type_name=None):
     ci_types = db.session.query(CIType).all() if type_name is None else \
         db.session.query(CIType).filter(
             CIType.type_name.ilike("%{0}%".format(type_name))).all()
     res = list()
     for ci_type in ci_types:
         type_dict = row2dict(ci_type)
         type_dict["uniq_key"] = CIAttributeCache.get(
             type_dict["uniq_id"]).attr_name
         res.append(type_dict)
     return res
예제 #14
0
def get_payment(payment_id):
    # id is a primary key, so we'll have max 1 result row
    payment = models.Payment.query.filter_by(payment_id=payment_id).first()
    if payment:
        return jsonify(models.row2dict(payment))
    else:
        return make_response(
            jsonify({
                "code": 404,
                "msg": "Cannot find this payment id."
            }), 404)
예제 #15
0
파일: account.py 프로젝트: hulihutu/cmdb
 def authenticate_with_key(self, key, secret, args, path):
     user = self.filter(User.key == key).filter(User.block == 0).first()
     if not user:
         return None, False
     if user and hashlib.sha1(
             '%s%s%s' %
         (path, user.secret, "".join(args))).hexdigest() == secret:
         authenticated = True
     else:
         authenticated = False
     return row2dict(user), authenticated
예제 #16
0
def get_question_by_user(user_id):
    question_list = Question.query.filter_by(user_id=user_id).all()
    # no question under this user id
    if len(question_list) == 0:
        return make_response(
            jsonify({
                "code": 404,
                "msg": "Cannot find questions by this user id."
            }), 404)
    # success
    else:
        return jsonify([row2dict(question) for question in question_list])
예제 #17
0
def get_answer(answer_id):
    answer = Answer.query.filter_by(id=answer_id).first()
    # success
    if answer:
        return jsonify(row2dict(answer))
    # invalid answer id
    else:
        return make_response(
            jsonify({
                "code": 404,
                "msg": "Cannot find this answer id."
            }), 404)
예제 #18
0
def get_question(question_id):
    question = Question.query.filter_by(id=question_id).first()
    # success
    if question:
        return jsonify(row2dict(question))
    # invalid id
    else:
        return make_response(
            jsonify({
                "code": 404,
                "msg": "Cannot find this question id."
            }), 404)
예제 #19
0
def return_event_month(event_type, year, month):
    httpmethod = request.method
    if httpmethod == "GET":
        results = Result.query.filter_by(year=year,
                                         month=month,
                                         day=None,
                                         type=event_type).all()
        if not results:
            return jsonify({"code": 204, "msg": "no results"})
        return make_response(jsonify([row2dict(result) for result in results]))

    if httpmethod == "POST":
        # if no year then fail
        if not year:
            return make_response(
                jsonify({
                    "code": 403,
                    "msg": "Cannot post event. Missing mandatory fields."
                }), 403)

        # month = request.args.get('month')

        # month_dict = {1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June',
        #               7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December',
        #               13: 'Nobel Prizes'}
        #
        # month_name = month_dict[month]

        body = json.loads(str(request.data, "utf8"))
        # events = json.loads(data)
        print(body)

        # if not month:
        #     r = Result(year=year, event=event)
        # else:
        #     r = Result(year=year, month=month_name, event=event)

        r = Result(year=year, month=month, type=event_type, event=body)
        #db.session.add(Result(year=1333, month=2, type="death", event=body))
        db.session.add(r)
        try:
            db.session.commit()
        except sqlalchemy.exc.SQLAlchemyError as e:
            error = "Cannot post search result. "
            # print(app.config.get("DEBUG"))
            if app.config.get("DEBUG"):
                error += str(e)
            return make_response(jsonify({"code": 404, "msg": error}), 404)
        return jsonify({"code": 200, "msg": "success"})
예제 #20
0
 def get_parents(self, child_id):
     parents = db.session.query(CITypeRelation).filter(
         CITypeRelation.child_id == child_id).all()
     result = []
     for parent in parents:
         ctr_id = parent.ctr_id
         citype = CITypeCache.get(parent.parent_id)
         citype_dict = row2dict(citype)
         citype_dict["ctr_id"] = ctr_id
         manager = CITypeAttributeManager()
         citype_dict["attributes"] = manager.get_attributes_by_type_id(
             citype.type_id)
         citype_dict["relation_type"] = parent.relation_type
         result.append(citype_dict)
     return result
예제 #21
0
 def get_children(self, parent_id):
     children = db.session.query(CITypeRelation).filter(
         CITypeRelation.parent_id == parent_id).all()
     result = []
     for child in children:
         ctr_id = child.ctr_id
         citype = CITypeCache.get(child.child_id)
         citype_dict = row2dict(citype)
         citype_dict["ctr_id"] = ctr_id
         manager = CITypeAttributeManager()
         citype_dict["attributes"] = manager.get_attributes_by_type_id(
             citype.type_id)
         citype_dict["relation_type"] = child.relation_type
         result.append(citype_dict)
     return result
예제 #22
0
 def get_attributes(self, name=None):
     """
     return attribute by name,
     if name is None, then return all attributes
     """
     attrs = db.session.query(CIAttribute).filter(
         CIAttribute.attr_name.ilike("%{0}%".format(name))).all() \
         if name is not None else db.session.query(CIAttribute).all()
     res = list()
     for attr in attrs:
         attr_dict = row2dict(attr)
         if attr.is_choice:
             attr_dict["choice_value"] = self._get_choice_value(
                 attr.attr_id, attr.value_type)
         res.append(attr_dict)
     return res
예제 #23
0
def return_event_year(event_type, year):
    httpmethod = request.method

    if httpmethod == "GET":
        results = Result.query.filter_by(year=year,
                                         month=None,
                                         day=None,
                                         type=event_type).all()
        if not results:
            return jsonify({"code": 204, "msg": "no results"})
        return make_response(jsonify([row2dict(result) for result in results]))

    if httpmethod == "POST":
        # get the year first, if no year then fail

        if not year:
            return make_response(
                jsonify({
                    "code": 403,
                    "msg": "Cannot post event. Missing mandatory fields."
                }), 403)

        # get the events JSON string and parse into a Python string
        # content = request.data
        # event_dict = json.loads(content)
        # event = event_dict["event_string"]
        body = json.loads(str(request.data, "utf8"))
        #data = request.get_data()
        #data = data.decode('utf8').replace('([+&%])', " ")
        # json_data = json.loads(events)
        # s = json.dumps(json_data)
        # events = json.dumps(data)
        # print(data)
        print(body)
        r = Result(year=year, type=event_type, event=body)
        db.session.add(r)
        try:
            db.session.commit()
        except sqlalchemy.exc.SQLAlchemyError as e:
            error = "Cannot post search result. "
            # print(app.config.get("DEBUG"))
            if app.config.get("DEBUG"):
                error += str(e)
            return make_response(jsonify({"code": 404, "msg": error}), 404)
        return jsonify({"code": 200, "msg": "success"})
예제 #24
0
def get_answer_by_question(question_id):
    question = Question.query.filter_by(id=question_id).first()
    # invalid question id
    if not question:
        return make_response(
            jsonify({
                "code": 404,
                "msg": "Cannot find this question id."
            }), 404)
    answer_list = question.answers
    # no answer yet
    if len(answer_list) == 0:
        return make_response(
            jsonify({
                "code": 200,
                "msg": "No answer for this question yet."
            }), 200)
    else:
        return jsonify([row2dict(answer) for answer in answer_list])
예제 #25
0
def get_all_person():
    person_list = Person.query.all()
    return jsonify([row2dict(person) for person in person_list])
예제 #26
0
def get_comment(comment_id):
    comment = Post.query.filter_by(id=comment_id).first()
    if comment:
        return jsonify(row2dict(comment))
예제 #27
0
def get_all_payments():
    payment_list = models.Payment.query.all()
    return jsonify([models.row2dict(payment) for payment in payment_list])
예제 #28
0
def delete_all_person():
    Person.query.delete()
    person_list = Person.query.all()
    db.session.commit()
    return jsonify([row2dict(person) for person in person_list])
예제 #29
0
def dump_database():
    result_list = Result.query.all()
    return jsonify([row2dict(result) for result in result_list])
예제 #30
0
def get_all_posts():
    post_list = Post.query.all()
    return jsonify([row2dict(post) for post in post_list])