Beispiel #1
0
def upload_actions(_id):

    try:
        token = request.form['jwt']
    except Exception:
        return response('No JWT', 400)

    try:
        payload = jwt.decode(token, 'super-secret')
    except Exception:
        return response('Tampered token', 400)

    if payload['role'] != 'representative':
        return response('You are not a representative', 400)

    representative = DB.users.find_one({'username': payload['username']})
    file_company = representative['owner']

    try:
        if len(request.files) < 1:
            raise AttributeError('Files missing from request')
        check_request_files(request.files)
    except AttributeError as e:
        return response(str(e), 400)

    try:
        product = DB.products.find_one(
            {'_id': ObjectId(_id), 'producer': file_company})
    except Exception as e:
        return response(str(e), 400)

    try:
        path = create_file_path(file_company, _id)
        filenames = save(path, request.files.getlist('files'))
        files = list(map(lambda filename: {
            'material': filename['file_time'],
            'path': '/materials/' + file_company + '/' + str(_id) + '/' + filename['file_time'],
            'name': filename['file_name'],
            'stars': list(),
            'votes': 0,
            'comments': list()
        }, filenames))

    except Exception as e:
        return response(str(e), 409)

    updated_product = DB.products.find_one_and_update(
        {'_id': ObjectId(_id)},
        {'$push': {'files': {'$each': files}}},
        return_document=ReturnDocument.AFTER
    )
    updated_product.update({
        '_id': str(updated_product['_id'])
    })
    return response(
        'Successfully uploaded material to the product',
        201,
        {'data': {'product': updated_product}}
    )
Beispiel #2
0
def dal_upload_files(files, username, _id):
    representative = db_conn.users.find_one({USERNAME: username})
    file_company = representative[DATA][OWNER]

    try:

        if len(files) < 1:
            raise NotFound()

        check_request_files(files)

    except Exception:
        raise ErrorRequestingFiles()

    try:
        path = create_file_path(file_company, _id)
        filenames = save(path, files.getlist(FILES))

        files = list(
            map(
                lambda filename: {
                    MATERIAL_ID:
                    filename[MATERIAL_ID],
                    FILENAME:
                    filename[FILENAME],
                    ORIGINAL_FILENAME:
                    filename[ORIGINAL_FILENAME],
                    OWNER:
                    str(_id),
                    PATH:
                    '/' + MATERIALS + '/' + file_company + '/' + str(_id) + '/'
                    + filename[FILENAME],
                    RATES:
                    list(),
                    VOTES:
                    0,
                    COMMENTS:
                    list(),
                    AVERAGE:
                    0
                }, filenames))

    except Exception:
        raise ErrorCreatingFiles()

    if files:
        db_conn.files.insert(files)
Beispiel #3
0
    def test_if_can_remove_user_by_index(self):
        file_path = files.create_file_path("users")
        data = files.read_data_file("users")
        data_length = len(data)

        users.add_player_to_data("testUser6", "none", "none")
        users.add_player_to_data("testUser7", "none", "none")

        user_index_zero = users.create_player("testUser6", "none", 0, "none")
        user_index_one = users.create_player("testUser7", "none", 0, "none")

        data_two = files.read_data_file("users")
        self.assertEqual(data_two[data_length], user_index_zero)
        users.remove_player_from_data_by_index(data_length)

        data_three = files.read_data_file("users")
        self.assertEqual(data_three[data_length], user_index_one)
        users.remove_player_from_data_by_index(data_length)
 def test_if_string_path_to_file_can_be_created(self):
     file_path = files.create_file_path("countries")
     self.assertEqual("data/countries.json", file_path)
     self.assertEqual(str, type(file_path))
Beispiel #5
0
def create_product():
    """Create a product"""

    try:
        token = request.form['jwt']
    except Exception:
        return response('No JWT', 400)

    try:
        payload = jwt.decode(token, 'super-secret')
    except Exception:
        return response('Tampered token', 400)

    if payload['role'] != 'representative':
        return response('You are not a representative', 400)

    try:
        check_request_files(request.files)
    except AttributeError as e:
        return response(str(e), 400)

    try:
        representative = DB.users.find_one({'username': payload['username']})
        company = representative['owner']
        new_product = {
            'category': request.form['category'],
            'name': request.form['name'],
            'description': request.form['description'],
            'serialNo': request.form['serialNo'],
            'createdBy': payload['username'],
            'producer': company,
            'files': list()
        }
    except Exception:
        return response('Wrong information', 400)

    search_obj = {
        'name': new_product['name'],
        'producer': company,
        'serialNo': new_product['serialNo']
    }

    if DB.products.find_one(search_obj):
        return response('Product already exists', 409)

    _id = DB.products.insert(new_product)

    try:
        path = create_file_path(company, str(_id))
        filenames = save(path, request.files.getlist('files'))
        files = list(map(lambda filename: {
            'material': filename['file_time'],
            'path': '/materials/' + company + '/' + str(_id) + '/' + filename['file_time'],
            'name': filename['file_name'],
            'stars': list(),
            'votes': 0,
            'comments': list()
        }, filenames))

    except Exception as e:
        return response(str(e), 409)

    DB.products.find_one_and_update(
        {'_id': ObjectId(_id)},
        {'$set': {'files': files}}
    )
    new_product.update({
        '_id': str(_id),
        'files': files
    })
    return response('Product was created', 201, {'data': {'product': new_product}})
Beispiel #6
0
def dal_create_product_upload_files(request, username):

    try:
        check_request_files(request.files)
    except Exception:
        raise ErrorRequestingFiles()

    try:
        representative = db_conn.users.find_one({USERNAME: username})
        company = representative[DATA][OWNER]
        new_product = {
            CATEGORY: extract_attribute(request, CATEGORY),
            SUB: extract_attribute(request, SUB),
            NAME: extract_attribute(request, NAME),
            DESCRIPTION: extract_attribute(request, DESCRIPTION),
            PRODUCTNO: extract_attribute(request, PRODUCTNO),
            CREATEDBY: username,
            PRODUCER: company
        }
    except BadFormData as e:
        raise BadFormData(str(e))

    search_obj = {
        NAME: new_product[NAME],
        PRODUCER: company,
        PRODUCTNO: new_product[PRODUCTNO]
    }

    if db_conn.products.find_one(search_obj):
        raise AlreadyExists('Product already exists')

    _id = db_conn.products.insert(new_product)

    try:
        path = create_file_path(company, str(_id))
        filenames = save(path, request.files.getlist(FILES))
        files = list(
            map(
                lambda filename: {
                    MATERIAL_ID:
                    filename[MATERIAL_ID],
                    FILENAME:
                    filename[FILENAME],
                    ORIGINAL_FILENAME:
                    filename[ORIGINAL_FILENAME],
                    OWNER:
                    str(_id),
                    PATH:
                    '/' + MATERIALS + '/' + company + '/' + str(_id) + '/' +
                    filename[FILENAME],
                    RATES:
                    list(),
                    VOTES:
                    0,
                    COMMENTS:
                    list(),
                    AVERAGE:
                    0
                }, filenames))

    except Exception as e:
        raise ErrorCreatingFiles(str(e))

    if files:
        db_conn.files.insert(files)

    new_product[ID] = str(_id)
    return new_product