Exemplo n.º 1
0
def _set_node_id(order_key):
    order = order_key.get()  # type: NodeOrder
    nodes = get_nodes_from_odoo(order.odoo_sale_order_id)
    if nodes:
        username = get_iyo_username(order.app_user)
        add_nodes_to_profile(username, nodes)
        logging.info('Saved node_id %s for user %s', nodes, username)
Exemplo n.º 2
0
def put_node_order(order_id, order):
    # type: (long, NodeOrderTO) -> NodeOrder
    order_model = get_node_order(order_id)
    if order_model.status == NodeOrderStatus.CANCELED:
        raise HttpBadRequestException('order_canceled')
    if order.status not in (NodeOrderStatus.CANCELED, NodeOrderStatus.SENT,
                            NodeOrderStatus.APPROVED, NodeOrderStatus.PAID):
        raise HttpBadRequestException('invalid_status')
    # Only support updating the status for now
    if order_model.status != order.status:
        if not _can_change_status(order_model.status, order.status):
            raise HttpBadRequestException(
                'cannot_change_status', {
                    'from': order_model.status,
                    'to': order.status,
                    'allowed_new_statuses': _get_allowed_status(
                        order_model.status)
                })
        order_model.status = order.status
        human_user, app_id = get_app_user_tuple(order_model.app_user)
        if order_model.status == NodeOrderStatus.CANCELED:
            order_model.cancel_time = now()
            if order_model.odoo_sale_order_id:
                deferred.defer(update_odoo_quotation,
                               order_model.odoo_sale_order_id,
                               {'state': QuotationState.CANCEL.value})
            deferred.defer(update_hoster_progress, human_user.email(), app_id,
                           HosterSteps.NODE_POWERED)  # nuke todo list
            deferred.defer(set_hoster_status_in_user_data,
                           order_model.app_user,
                           _countdown=2)
        elif order_model.status == NodeOrderStatus.SENT:
            if not order_model.odoo_sale_order_id or not get_nodes_from_odoo(
                    order_model.odoo_sale_order_id):
                raise HttpBadRequestException(
                    'cannot_mark_sent_no_serial_number_configured_yet',
                    {'sale_order': order_model.odoo_sale_order_id})
            order_model.send_time = now()
            deferred.defer(update_hoster_progress, human_user.email(), app_id,
                           HosterSteps.NODE_SENT)
            deferred.defer(_send_node_order_sent_message, order_id)
        elif order_model.status == NodeOrderStatus.APPROVED:
            deferred.defer(_create_node_order_pdf, order_id)
        elif order_model.status == NodeOrderStatus.PAID:
            deferred.defer(confirm_odoo_quotation,
                           order_model.odoo_sale_order_id)
    else:
        logging.debug('Status was already %s, not doing anything',
                      order_model.status)
    order_model.put()
    return order_model
Exemplo n.º 3
0
def create_node_order(data):
    # type: (CreateNodeOrderTO) -> NodeOrder
    if data.status not in (NodeOrderStatus.SIGNED, NodeOrderStatus.SENT,
                           NodeOrderStatus.ARRIVED, NodeOrderStatus.PAID):
        data.sign_time = MISSING
    if data.status not in (NodeOrderStatus.SENT, NodeOrderStatus.ARRIVED):
        data.send_time = MISSING
    app_user = users.User(data.app_user)
    order_count = NodeOrder.list_by_so(data.odoo_sale_order_id).count()
    if order_count > 0:
        raise OrderAlreadyExistsException(data.odoo_sale_order_id)
    try:
        nodes = get_nodes_from_odoo(data.odoo_sale_order_id)
    except (IndexError, TypeError):
        logging.warn('Could not get nodes from odoo for order id %s' %
                     data.odoo_sale_order_id,
                     exc_info=True)
        raise HttpBadRequestException('cannot_find_so_x',
                                      {'id': data.odoo_sale_order_id})
    if not nodes:
        raise HttpBadRequestException('no_serial_number_configured_yet',
                                      {'sale_order': data.odoo_sale_order_id})
    prefix, doc_content_base64 = data.document.split(',')
    content_type = prefix.split(';')[0].replace('data:', '')
    if content_type != 'application/pdf':
        raise InvalidContentTypeException(content_type, ['application/pdf'])

    doc_content = base64.b64decode(doc_content_base64)
    order_key = NodeOrder.create_key()
    pdf_name = NodeOrder.filename(order_key.id())
    pdf_url = upload_to_gcs(pdf_name, doc_content, content_type)
    order = NodeOrder(key=order_key,
                      app_user=app_user,
                      **data.to_dict(exclude=['document', 'app_user']))
    order.put()
    iyo_username = get_iyo_username(app_user)
    email, app_id = get_app_user_tuple(app_user)
    deferred.defer(add_nodes_to_profile, iyo_username, nodes)
    deferred.defer(set_hoster_status_in_user_data, order.app_user, False)
    deferred.defer(add_user_to_role,
                   UserDetailsTO(email=email.email(), app_id=app_id),
                   RogerthatRoles.HOSTERS)
    deferred.defer(tag_intercom_users, IntercomTags.HOSTER, [iyo_username])
    deferred.defer(_order_node_iyo_see,
                   order.app_user,
                   order.id,
                   pdf_url,
                   len(doc_content),
                   create_quotation=False)
    return order
Exemplo n.º 4
0
def check_if_node_comes_online(order_key):
    order = order_key.get()  # type: NodeOrder
    order_id = order.id
    if not order.odoo_sale_order_id:
        raise BusinessException(
            'Cannot check status of node order without odoo_sale_order_id')
    nodes = get_nodes_from_odoo(order.odoo_sale_order_id)
    if not nodes:
        raise BusinessException(
            'Could not find nodes for sale order %s on odoo' % order_id)

    statuses = get_nodes_status([n['id'] for n in nodes])
    if all([status == 'running' for status in statuses]):
        iyo_username = get_iyo_username(order.app_user)
        _set_node_status_arrived(order_key, iyo_username, nodes)
    else:
        logging.info('Nodes %s from order %s are not all online yet', nodes,
                     order_id)
Exemplo n.º 5
0
def get_nodes_for_user(app_user):
    nodes = []
    for order in NodeOrder.list_by_user(app_user):
        if order.status in (NodeOrderStatus.SENT, NodeOrderStatus.ARRIVED):
            nodes.extend(get_nodes_from_odoo(order.odoo_sale_order_id))
    return nodes