Exemple #1
0
 def trans():
     logging.debug('Storing order in the database')
     order = NodeOrder(key=order_key,
                       app_user=app_user,
                       tos_iyo_see_id=None,
                       billing_info=billing_info,
                       shipping_info=shipping_info,
                       order_time=now(),
                       status=NodeOrderStatus.APPROVED
                       if can_host else NodeOrderStatus.WAITING_APPROVAL,
                       socket=socket)
     order.put()
     if can_host:
         logging.info(
             'User has invested more than %s tokens, immediately creating node order PDF.',
             REQUIRED_TOKEN_COUNT_TO_HOST)
         deferred.defer(_create_node_order_pdf,
                        order_key.id(),
                        _transactional=True)
     else:
         logging.info(
             'User has not invested more than %s tokens, an admin needs to approve this order manually.',
             REQUIRED_TOKEN_COUNT_TO_HOST)
         deferred.defer(_inform_support_of_new_node_order,
                        order_key.id(),
                        _transactional=True)
     deferred.defer(set_hoster_status_in_user_data,
                    order.app_user,
                    False,
                    _transactional=True)
     if updated_user_data:
         deferred.defer(put_user_data,
                        app_user,
                        updated_user_data,
                        _transactional=True)
Exemple #2
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