Skip to content

Commit

Permalink
Added recipe model and app
Browse files Browse the repository at this point in the history
  • Loading branch information
mghaznav committed Jul 14, 2024
1 parent 6d5ba3c commit a260469
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
'rest_framework',
'rest_framework.authtoken',
'drf_spectacular',
'user'
'user',
'recipe'
]

MIDDLEWARE = [
Expand Down
1 change: 1 addition & 0 deletions app/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ class UserAdmin(BaseUserAdmin):


admin.site.register(models.User, UserAdmin)
admin.site.register(models.Recipe)
27 changes: 27 additions & 0 deletions app/core/migrations/0002_recipe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 4.2.14 on 2024-07-14 01:40

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('core', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='Recipe',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=255)),
('description', models.TextField(blank=True)),
('time_minutes', models.IntegerField()),
('price', models.DecimalField(decimal_places=2, max_digits=5)),
('link', models.CharField(blank=True, max_length=255)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
16 changes: 16 additions & 0 deletions app/core/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.db import models
from django.contrib.auth.models import (
AbstractBaseUser,
Expand Down Expand Up @@ -37,3 +38,18 @@ class User(AbstractBaseUser, PermissionsMixin):
objects = UserManager()

USERNAME_FIELD = 'email'


class Recipe(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE
)
title = models.CharField(max_length=255)
description = models.TextField(blank=True)
time_minutes = models.IntegerField()
price = models.DecimalField(max_digits=5, decimal_places=2)
link = models.CharField(max_length=255, blank=True)

def __str__(self):
return self.title
26 changes: 26 additions & 0 deletions app/core/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
from decimal import Decimal

from django.test import TestCase
from django.contrib.auth import get_user_model

from core import models


class ModelTests(TestCase):
"""
Test User Model
"""

def test_create_user_with_email_successful(self):
email = '[email protected]'
Expand Down Expand Up @@ -43,3 +50,22 @@ def test_create_superuser(self):

self.assertTrue(user.is_superuser)
self.assertTrue(user.is_staff)

"""
Test Recipe Model
"""

def test_create_recipe(self):
user = get_user_model().objects.create_user(
'[email protected]',
'testpass123'
)
recipe = models.Recipe.objects.create(
user=user,
title='Sample recipe name',
time_minutes=5,
price=Decimal('5.50'),
description='Sample recipe description'
)

self.assertEqual(str(recipe), recipe.title)
Empty file added app/recipe/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions app/recipe/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class RecipeConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'recipe'
Empty file added app/recipe/tests/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions app/recipe/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.

0 comments on commit a260469

Please sign in to comment.