def get(self, order_id): if order_id: order = db_session.query(Order).get(order_id) if order: return jsonify(_parse_order(order)) else: return make_response(jsonify({'type': 'error', 'text': 'not found'}), 404) orders = Pagination(Order, **request.args).items() orders[:] = [_parse_order(order) for order in orders] return jsonify({'orders': orders})
def dispatch_request(self): if self.user_id != self.head_user_id and self.head_user_id != 1: return make_response( jsonify({ 'type': 'error', 'text': 'Access denied' }), 403) if self.user_id: orders = list( map( lambda day: db_session.query(Order).filter_by( order_date=day, user_id=self.user_id).all(), self._get_week_dates())) else: if self.head_user_id == 1: orders = list( map( lambda day: db_session.query(Order).filter_by( order_date=day).all(), self._get_week_dates())) else: orders = list( map( lambda day: db_session.query(Order).filter_by( order_date=day, user_id=self.head_user_id).all(), self._get_week_dates())) orders[:] = [ _parse_order(_order) for _day_orders in orders for _order in _day_orders ] return jsonify({'orders': orders})
def post(self): if not self._check_order(): return make_response( jsonify({ 'type': 'error', 'text': 'This meal is already in your order' }), 403) _order_date = datetime.datetime.strptime(self.json.get('order_date'), "%Y-%m-%d").date() if not self._check_actual_date(_order_date.timetuple().tm_yday): return make_response( jsonify({ 'type': 'error', 'text': 'You are trying to create an outdated order. That is not allowed' }), 403) new_order = Order(order_date=_order_date, meal_id=self.json.get('meal_id'), user_id=self.json.get('user_id'), quantity=self.json.get('quantity')) db_session.add(new_order) db_session.commit() return jsonify(_parse_order(new_order, detailed=False))
def get(self, order_id): if order_id: order = db_session.query(Order).get(order_id) if order: return jsonify(_parse_order(order)) else: return make_response( jsonify({ 'type': 'error', 'text': 'not found' }), 404) orders = db_session.query(Order).all() if orders: orders[:] = [_parse_order(order) for order in orders] return jsonify({'orders': orders})
def dispatch_request(self): if self.month_number and self.month_number not in range(1,13): return make_response(jsonify({'type': 'error', 'text': 'Wrong month number'}), 403) if self.user_id != self.head_user_id and self.head_user_id != 1: return make_response(jsonify({'type': 'error', 'text': 'Access denied'}), 403) _month_number = self.month_number if not self.month_number: _month_number = self.this_month if self.user_id: orders = list(map(lambda day: db_session.query(Order).filter_by( order_date=day, user_id=self.user_id).all(), self._get_month_dates(_month_number))) else: if self.head_user_id == 1: orders = list(map(lambda day: db_session.query(Order).filter_by( order_date=day).all(), self._get_month_dates(_month_number))) else: orders = list(map(lambda day: db_session.query(Order).filter_by( order_date=day, user_id=self.head_user_id).all(), self._get_month_dates(_month_number))) orders[:] = [_parse_order(_order) for _day_orders in orders for _order in _day_orders] return jsonify({'orders': orders})
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': 'not allowed remove old order'}), 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)
def get_details(): user_id = g.user.id user = db_session.query(User).get(user_id) comments = db_session.query(Comment).filter_by(user_id=user_id).order_by( Comment.timestamp_created.desc()).limit(50).all() orders = db_session.query(Order).filter_by(user_id=user_id).order_by( Order.order_date.desc()).limit(50).all() user_dict = _parse_user(user) user_dict['orders'] = [_parse_order(order) for order in orders] user_dict['comments'] = [_parse_comment(comment) for comment in comments] return jsonify(user_dict)