Esempio n. 1
0
        token = {}
        tokens = db_session.query(Token).filter_by(user_id=user.id).all()
        is_expired_list = list(map(lambda t: t.is_expired(), tokens))
        all_expired = all(is_expired_list)

        if all_expired:
            token = Token(user_id=user.id)
            db_session.add(token)
            db_session.commit()
        else:
            token = [t for t in tokens if not t.is_expired()].pop()

        logged_user = _parse_user(user)
        logged_user.update({'token': token.token})

        return make_response(
            jsonify({
                'id': logged_user.get('id'),
                'token': logged_user.get('token'),
                'username': logged_user.get('username')
            }), 200)
    else:
        return make_response(
            jsonify({
                'type': 'error',
                'text': 'password incorrect'
            }), 403)


register_api(UserAPI, 'user_api', '/user/', pk='user_id')
Esempio n. 2
0
            user_id=g.user.id, meal_id=self.json.get('meal_id')).first()
        if check_comment:
            return make_response(
                jsonify({
                    'type': 'error',
                    'text': 'you have already commented this meal'
                }))
        new_comment = Comment(user_id=g.user.id,
                              meal_id=self.json.get('meal_id'),
                              content=self.json.get('content', ''))

        db_session.add(new_comment)
        db_session.commit()

        return jsonify(_parse_comment(new_comment, detailed=False))

    @auth_required
    def delete(self, comment_id):
        comment = db_session.query(Comment).get(comment_id)
        if comment:
            db_session.delete(comment)
            db_session.commit()
            return jsonify(_parse_comment(comment, detailed=False))
        return make_response(jsonify({
            'type': 'error',
            'text': 'not found'
        }), 404)


register_api(CommentApi, 'comment_api', '/comment/', pk='comment_id')
Esempio n. 3
0
            'day_linked': self.json.get('day_linked'),
            'enabled': self.json.get('enabled'),
            'source_price': self.json.get('source_price'),
            'price': self.json.get('price')
        }

        json_dict.update({'timestamp_modified': datetime.datetime.utcnow()})

        update_meal = db_session.query(Meal).filter_by(id=meal_id)
        update_meal.update(json_dict)

        db_session.commit()

        return jsonify(_parse_meal(update_meal.first()))

    @auth_required
    @restrict_users
    def delete(self, meal_id):
        meal = db_session.query(Meal).get(meal_id)
        if meal:
            db_session.delete(meal)
            db_session.commit()
            return jsonify(_parse_meal(meal, detailed=False))
        return make_response(jsonify({
            'type': 'error',
            'text': 'not found'
        }), 404)


register_api(MealAPI, 'meal_api', '/meal/', pk='meal_id')
Esempio n. 4
0
        db_session.add(new_order)
        db_session.commit()

        return jsonify(_parse_order(new_order, detailed=False))

    @auth_required
    def delete(self, order_id):
        order = db_session.query(Order).get(order_id)

        if order:
            _order_date = order.order_date
            if not self._check_actual_date(_order_date.timetuple().tm_yday):
                return make_response(
                    jsonify({
                        'type':
                        'error',
                        'text':
                        'You are trying to remove old order. That is not allowed'
                    }), 403)
            db_session.delete(order)
            db_session.commit()
            return jsonify(_parse_order(order, detailed=False))
        return make_response(jsonify({
            'type': 'error',
            'text': 'not found'
        }), 404)


register_api(Order_API, 'order_api', '/order/', pk='order_id')