def create_categories(): # List of categories categories = [] # Create categories from 1 to 10 for i in range(1, 11): cat = Category(name="Category " + str(i), likes=i) cat.save() categories.append(cat) return categories
def test_create_a_new_category(self): categories_in_database = Category.objects.all() self.assertEquals(len(categories_in_database), 0) from populate_script import add_cat cat = Category(name="create_a_new_category_test") cat.save() categories_in_database = Category.objects.all() # Check category is in database. 18 categories exist already, so we want #to make sure that there is now 19 self.assertEquals(len(categories_in_database), 1) #category gets appended onto last last_poll_in_database = categories_in_database[0] self.assertEquals(last_poll_in_database, cat)
def process_category(self, item, spider): # ------------------------------------------------------ # we will use django framework to save our data, # so there is no need to fetch & save data manually :) # ------------------------------------------------------ category = Category.objects.filter(title_fa=item.get('title_fa')) if not category: logger.debug("--- Create new `category` instance: %s" % item.get('title_fa')) new_category = Category(**item) new_category.save() else: logger.debug('--- Duplicate: category(%s)' % item.get('title_fa')) logger.debug(category) return item
def create(self, validated_data): categories = validated_data.pop('category') ingredients = validated_data.pop('ingredients') updated_instance = super(RecipeSerializer, self).create(validated_data) for ingredient in ingredients: updated_instance.ingredients.append(Ingredient(**ingredient)) for category in categories: updated_instance.category.append(Category(**category)) updated_instance.save() return updated_instance
def create_category(name, order): """ Creates a new category """ category = Category() category.name = name category.order = order category.save() return category
def post(current_user, self): """ Add a category to the database """ data = request.get_json() if data: cat_name = data.get('cat_name') cat_desc = data.get('cat_desc') if cat_name and cat_desc: if validate_text(data['cat_name']): if validate_text(data['cat_desc']): category_check = Category.query.filter_by(cat_name=data['cat_name'], created_by=current_user.username).first() if not category_check: new_category = Category(cat_id=str(uuid.uuid4()), cat_name=data['cat_name'], cat_desc=data['cat_desc'], create_date=datetime.now(), created_by=current_user.username, modified_date=datetime.now()) save(new_category) return ({'Message' : 'Category Created'}, 201) return ({'Message' : 'Category name already exists'}, 400) return ({'Message' : 'Please enter a valid category description'}, 400) return ({'Message' : 'Please enter a valid category name'}, 400) return ({'Message' : 'Please populate all fields'}, 400) return ({'Message' : 'No data submitted'}, 400)
### # Users ### admin = User.objects.all()[0] user = User.objects.filter(username="******") if user: user = user[0] if not user: user = User.objects.create_user("user", "user@localhost", "123456") ### # Categories ### desserts = Category(name="Desserts", description="Les desserts...") desserts.save() plats = Category(name="Plats", description="Les plats...") plats.save() entrees = Category(name="Entrées", description="Les entrées...") entrees.save() ### # Recipes ### recipe = Recipe(title="Cake Olives-Jambon", author=admin, preparation_time="20min", portion="5-6")
def test_does_slug_field_work(self): from recipes.models import Category cat = Category(name='how do i create a slug in django') cat.save() self.assertEqual(cat.slug, 'how-do-i-create-a-slug-in-django')
def setUp(self): self.client = app.test_client() db.drop_all() db.create_all() #test users for login self.user2 = {'username': '******', 'password': '******'} # create and add a test user password = '******' self.user_test = { 'user_id': '5xxxxx', 'name': 'Geofrey', 'username': '******', 'email': '*****@*****.**', 'password': password } password_hash = generate_password_hash(password, method='sha256') test_user = User(user_id='5xxxxx', name='Geofrey', username='******', email='*****@*****.**', password=password_hash) save(test_user) # create and add test category self.cat_test = { 'cat_id': '5xxxxxx', 'cat_name': 'Generall', 'cat_desc': 'General recipes', 'created_by': 'geom' } test_category = Category(cat_id='5xxxxx', cat_name='General', cat_desc='General recpes', create_date=datetime.now(), created_by='geof', modified_date=datetime.now()) save(test_category) # create and add test recipe self.recipe_test = { 'recipe_id': '5xxxxx', 'title': 'Recipe One and two', 'category': 'General', 'ingredients': 'Ingredient one and two', 'steps': 'step 1', 'created_by': 'geom', 'status': 'public', 'upvotes': 0 } test_recipe = Recipe(recipe_id='5xxxxx', title='Recipe One', category='General', ingredients='Ingredient one and two', steps='step 1', create_date=datetime.now(), created_by='geof', modified_date=datetime.now(), status='public', upvotes=0) save(test_recipe) """ Generate authentication token""" self.user = {"username": "******", "password": password} response = self.client.post( '/auth/login', data=json.dumps(self.user), headers={'Content-Type': 'application/json'}) response_data = json.loads(response.data) token = response_data['token'] self.headers = { 'x-access-token': token, 'Content-Type': 'application/json', }