Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first attempt at django associations; amazon #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
16 changes: 16 additions & 0 deletions amazon/amazon/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for amazon project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'amazon.settings')

application = get_asgi_application()
126 changes: 126 additions & 0 deletions amazon/amazon/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
"""
Django settings for amazon project.

Generated by 'django-admin startproject' using Django 3.2.4.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-aa*$mi@usrf+9ha5j864&1f=f%nly*f1kfsdj-^nz&#9k-cu^l'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'store',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'amazon.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'amazon.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}


# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = '/static/'

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
21 changes: 21 additions & 0 deletions amazon/amazon/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""amazon URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

urlpatterns = [
path('admin/', admin.site.urls),
]
16 changes: 16 additions & 0 deletions amazon/amazon/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for amazon project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'amazon.settings')

application = get_wsgi_application()
Binary file added amazon/db.sqlite3
Binary file not shown.
22 changes: 22 additions & 0 deletions amazon/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'amazon.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
40 changes: 40 additions & 0 deletions amazon/store/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 3.2.4 on 2021-07-07 18:16

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


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Product',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
migrations.CreateModel(
name='Review',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
migrations.CreateModel(
name='User',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
migrations.CreateModel(
name='Shop',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='store.user')),
],
),
]
20 changes: 20 additions & 0 deletions amazon/store/migrations/0002_product_shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.2.4 on 2021-07-08 00:01

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


class Migration(migrations.Migration):

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

operations = [
migrations.AddField(
model_name='product',
name='shop',
field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, related_name='products', to='store.shop'),
preserve_default=False,
),
]
26 changes: 26 additions & 0 deletions amazon/store/migrations/0003_auto_20210708_0023.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 3.2.4 on 2021-07-08 00:23

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


class Migration(migrations.Migration):

dependencies = [
('store', '0002_product_shop'),
]

operations = [
migrations.AddField(
model_name='review',
name='product',
field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, related_name='reviews', to='store.product'),
preserve_default=False,
),
migrations.AddField(
model_name='review',
name='user',
field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, related_name='reviews', to='store.user'),
preserve_default=False,
),
]
19 changes: 19 additions & 0 deletions amazon/store/migrations/0004_review_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.2.4 on 2021-07-08 01:13

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('store', '0003_auto_20210708_0023'),
]

operations = [
migrations.AddField(
model_name='review',
name='text',
field=models.TextField(default=None),
preserve_default=False,
),
]
20 changes: 20 additions & 0 deletions amazon/store/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
from django.db import models

# Create your models here.
class User(models.Model):
pass


class Shop(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)

class Product(models.Model):
shop = models.ForeignKey(Shop, on_delete=models.CASCADE, related_name='products')
product_reviews = models.ManyToManyField(User, through='Review')

class Review(models.Model): # this is your join table!!!!!
# user is providing review
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='reviews')

# we are reviewing the product, itself
product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='reviews')

# can also add--
text = models.TextField()
31 changes: 30 additions & 1 deletion amazon/store/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
from django.test import TestCase
from .models import *
import redgreenunittest as unittest

class AssociationTestCase(TestCase):
def setUp(self):
self.shop_owner = User.objects.create()
self.shop = Shop.objects.create(owner=self.shop_owner)
self.product = Product.objects.create(shop=self.shop)
self.user = User.objects.create()
self.review = Review.objects.create(product=self.product, user=self.user)

def test_01_owner_of_shop(self):
"""returns the owner of the shop"""
self.assertEqual(self.shop.owner, self.shop_owner)

def test_02_shop_products(self):
"""returns the products for sale in the shop"""
self.assertEqual(list(self.shop.products.all()), [self.product])

def test_03_product_shop(self):
"""returns the shop the product belongs to"""
self.assertEqual(self.product.shop, self.shop)

def test_04_product_reviews(self):
"""returns the product's reviews"""
self.assertEqual(list(self.product.reviews.all()), [self.review])

def test_05_review_reviewer(self):
"""returns the review's author"""
self.assertEqual(self.review.user, self.user)

# Create your tests here.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
Empty file added amazon_old/store/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions amazon_old/store/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions amazon_old/store/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class StoreConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'store'
Empty file.
3 changes: 3 additions & 0 deletions amazon_old/store/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions amazon_old/store/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions amazon_old/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.
Loading