def test_update_category_with_no_data(self):
     """ Update a Category with no data passed """
     category = Category.find_by_name('Dog')[0]
     resp = self.app.put('/categories/{}'.format(category.id),
                         json={},
                         content_type='application/json')
     self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
 def test_update_category_with_text_data(self):
     """ Update a Category with text data """
     category = Category.find_by_name('Dog')[0]
     resp = self.app.put('/categories/{}'.format(category.id),
                         data="hello",
                         content_type='text/plain')
     self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
Beispiel #3
0
def edit_category(user_id, id, **kwargs):
    """This route handles updating categories by id"""

    name = str(request.data.get('name')).strip()
    result2 = valid_category(name)
    if result2:
        return jsonify(result2), 400
    name = name.title()
    result = Category.find_by_name(name, user_id)
    if result:
        return jsonify({"message": "name already exists"}), 400
    category = Category.find_user_by_id(id, user_id)
    if not category:
        return jsonify({"message": "No category found to edit"}), 404

    name = str(request.data.get('name', ''))
    category.name = name
    category.save()
    response2 = category.category_json()
    response = {
        'message': 'Category has been updated',
        'newcategory': response2,
        'Recipes': url_for('recipe.get_recipes',
                           id=category.id,
                           _external=True)
    }
    return make_response(jsonify(response)), 200
Beispiel #4
0
def add_categories(user_id):
    """This route handles posting categories"""

    if request.method == "POST":
        name = str(request.data.get('name')).strip()
        name = re.sub(' +', ' ', name)
        resultn = valid_category(name)
        if resultn:
            return jsonify(resultn), 400
        name = name.title()
        result = Category.find_by_name(name, user_id)
        if result:
            return jsonify({"message": "Category already exists"}), 400
        category_ = Category(name=name, created_by=user_id)
        category_.save()
        response1 = category_.category_json()
        response = jsonify({
            'message':
            'Category' + category_.name + 'has been created',
            'Recipes':
            url_for('recipe.get_recipes', id=category_.id, _external=True),
            'category':
            response1
        })
        return make_response(response), 201
 def test_get_category(self):
     """ Get a single Category """
     # get the id of a pet
     category = Category.find_by_name('Dog')[0]
     resp = self.app.get('/categories/{}'.format(category.id),
                         content_type='application/json')
     self.assertEqual(resp.status_code, status.HTTP_200_OK)
     data = resp.get_json()
     self.assertEqual(data['name'], category.name)
Beispiel #6
0
def restore_structure_of_db():
    full_backup_to_restore = Template.find_by_name(
        'full_Ewa').json_template['instances']
    Activity.delete_all_objects_by_current_user()
    Category.delete_all_objects_by_current_user()
    for single_record in full_backup_to_restore:
        if not Category.find_by_name(single_record['category']):
            new_category = Category(category=single_record['category'],
                                    username_id=current_user.id)
            db.session.add(new_category)
            db.session.commit()
        new_activity = Activity(name=single_record['activity'],
                                category_id=Category.find_by_name(
                                    single_record['category']).id,
                                username_id=current_user.id)
        db.session.add(new_activity)
        db.session.commit()
    return redirect(url_for('backup_structure_of_db'))
 def test_update_category(self):
     """ Update an existing Category """
     category = Category.find_by_name('Dog')[0]
     resp = self.app.get('/categories/{}'.format(category.id),
                         content_type='application/json')
     self.assertEqual(resp.status_code, status.HTTP_200_OK)
     data = resp.get_json()
     self.assertEqual(data['name'], category.name)
     # Change the name to Canine
     data['name'] = 'Canine'
     resp = self.app.put('/categories/{}'.format(data['id']),
                         json=data,
                         content_type='application/json')
     self.assertEqual(resp.status_code, status.HTTP_200_OK)
     new_json = resp.get_json()
     self.assertEqual(new_json['name'], 'Canine')