def add_shipment(order_id: ObjectId): order = Orders().find_one({ "_id": order_id, "status": Orders.pending_shipment }) if order is None: raise errors.NotFound() if request.method == "GET": return render_template("pub_add_shipment.html", order=order, order_id=order["_id"]) if request.method == "POST": shipment_details = request.form.get("details") if not shipment_details: raise errors.BadRequest("Missing shipment details") # store shipment details Orders().add_shipment(order_id, shipment_details) # refresh order object order = Orders.get(order_id) # send recipient an email send_order_shipped_email(order_id) return render_template("pub_thank_shipment.html", order=order, order_id=order["_id"])
def cancel(order_id: ObjectId, user: dict): order = Orders.get(order_id) if order is None: raise errors.NotFound() Orders().cancel(order_id) send_order_failed_email(order_id) return jsonify({"_id": order_id})
def check_autoimages(): # update images that were building logger.info("Looking for currently building images…") for image in AutoImages.all_currently_building(): logger.info(f".. {image['slug']}") # check order status order = Orders.get(image["order"]) # order is considered failed if order["status"] in Orders.FAILED_STATUSES: logger.info(f".. order failed: {order['status']}") AutoImages.update_status(image["slug"], status="failed") continue # order is considered successful if order["status"] in Orders.SUCCESS_STATUSES + [ Orders.pending_expiry ]: logger.info(f".. order succeeded: {order['status']}") torrent_url = get_public_download_torrent_url(order) AutoImages.update_status( image["slug"], status="ready", order=None, http_url=get_public_download_url(order), torrent_url=torrent_url, magnet_url=get_magnet_for_torrent(torrent_url), expire_on=get_next_month(), ) continue logger.info(f".. order still building: {order['status']}") # find images that must be recreated logger.info("Looking for images needing building…") for image in AutoImages.all_needing_rebuild(): logger.info(f".. {image['slug']} ; starting build") # create order payload = AutoImages.create_order_payload(image["slug"]) try: order_id = create_order_from(payload) except Exception as exc: logger.error(f"Error creating image `{image['slug']}`: {exc}") logger.exception(exc) AutoImages.update_status(image["slug"], status="failed") continue # update with order ID and status: building AutoImages.update_status(image["slug"], status="building", order=order_id)
def collection(user: dict): """ List or create orders """ if request.method == "GET": # unpack url parameters skip = request.args.get("skip", default=0, type=int) limit = request.args.get("limit", default=20, type=int) skip = 0 if skip < 0 else skip limit = 20 if limit <= 0 else limit orders = [ Orders.get(order.get("_id")) for order in Orders().find({}, {"_id": 1}) ] return jsonify({"meta": {"skip": skip, "limit": limit}, "items": orders}) elif request.method == "POST": # validate request json try: request_json = request.get_json() validate(request_json, Orders().schema) except ValidationError as error: raise errors.BadRequest(error.message) request_json["status"] = Orders().created request_json["tasks"] = {} request_json["statuses"] = [ {"status": Orders().created, "on": datetime.datetime.now(), "payload": None} ] # actually create Ordr order_id = Orders().insert_one(request_json).inserted_id print("ORDER_ID", order_id) # send email about new order send_order_created_email(order_id) # create creation task Orders.create_creator_task(order_id) return jsonify({"_id": order_id})
def collection(user: dict): """ List or create orders """ if request.method == "GET": # unpack url parameters skip = request.args.get("skip", default=0, type=int) limit = request.args.get("limit", default=20, type=int) skip = 0 if skip < 0 else skip limit = 20 if limit <= 0 else limit query = {} projection = {"_id": 1} cursor = (Orders().find(query, projection).sort([ ("$natural", pymongo.DESCENDING) ]).skip(skip).limit(limit)) count = Orders().count_documents(query) orders = [Orders.get(order.get("_id")) for order in cursor] return jsonify({ "meta": { "skip": skip, "limit": limit, "count": count }, "items": orders }) if request.method == "POST": try: order_id = create_order_from(request.get_json()) except ValidationError as error: raise errors.BadRequest(str(error)) return jsonify({"_id": order_id})