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

Shawn's Beer Ratings Solution - still needs User Authorization to edit #2

Open
wants to merge 1 commit 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.venv
__pycache__
Empty file added accounts/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions accounts/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 accounts/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AccountsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'accounts'
Empty file added accounts/migrations/__init__.py
Empty file.
Empty file added accounts/models.py
Empty file.
3 changes: 3 additions & 0 deletions accounts/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.
6 changes: 6 additions & 0 deletions accounts/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.urls import path
from . import views

urlpatterns = [
path('signup/', views.SignUp.as_view(), name='signup'),
]
8 changes: 8 additions & 0 deletions accounts/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.contrib.auth.forms import UserCreationForm
from django.urls import reverse_lazy
from django.views import generic

class SignUp(generic.CreateView):
form_class = UserCreationForm
success_url = reverse_lazy('login')
template_name = 'registration/signup.html'
Empty file added beer_ratings/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions beer_ratings/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for beer_ratings 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', 'beer_ratings.settings')

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

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

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-9q(iuds6y9##o3f3#)s@%%4s#j!(eanp!^crh6$7%4rdv$4fvs'

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

ALLOWED_HOSTS = []


# Application definition

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

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 = 'beer_ratings.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates'],
'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 = 'beer_ratings.wsgi.application'


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

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'beer_ratings',
}
}


# 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'

LOGIN_REDIRECT_URL = '/beers/'

LOGOUT_REDIRECT_URL = 'home'
26 changes: 26 additions & 0 deletions beer_ratings/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""beer_ratings 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, include
from django.views.generic.base import TemplateView

urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
path('accounts/', include('accounts.urls')),
path('', TemplateView.as_view(template_name='home.html'), name='home'),
path('beers/', include('beer_reviews_app.urls')),
]
16 changes: 16 additions & 0 deletions beer_ratings/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for beer_ratings 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', 'beer_ratings.settings')

application = get_wsgi_application()
Empty file added beer_reviews_app/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions beer_reviews_app/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib import admin
from .models import Beer, Review

admin.site.register(Beer)
admin.site.register(Review)


6 changes: 6 additions & 0 deletions beer_reviews_app/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class BeerReviewsAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'beer_reviews_app'
12 changes: 12 additions & 0 deletions beer_reviews_app/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django import forms
from .models import Beer, Review

class BeerForm(forms.ModelForm):
class Meta:
model = Beer
fields = {'beer_brewer', 'beer_name', 'beer_type'}

class ReviewForm(forms.ModelForm):
class Meta:
model = Review
fields = {'review',}
33 changes: 33 additions & 0 deletions beer_reviews_app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 3.2.5 on 2021-07-14 19:26

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


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Brewer',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('brewer_name', models.CharField(max_length=200)),
('brewer_state', models.CharField(max_length=24)),
('brewer_opened', models.DateField()),
],
),
migrations.CreateModel(
name='Beer',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('beer_name', models.CharField(max_length=64)),
('beer_type', models.CharField(max_length=64)),
('beer_brewer', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='beers', to='beer_reviews_app.brewer')),
],
),
]
21 changes: 21 additions & 0 deletions beer_reviews_app/migrations/0002_auto_20210714_1935.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.2.5 on 2021-07-14 19:35

from django.db import migrations, models


class Migration(migrations.Migration):

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

operations = [
migrations.AlterField(
model_name='beer',
name='beer_brewer',
field=models.CharField(max_length=200),
),
migrations.DeleteModel(
name='Brewer',
),
]
22 changes: 22 additions & 0 deletions beer_reviews_app/migrations/0003_review.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.2.5 on 2021-07-14 19:56

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


class Migration(migrations.Migration):

dependencies = [
('beer_reviews_app', '0002_auto_20210714_1935'),
]

operations = [
migrations.CreateModel(
name='Review',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('review', models.TextField()),
('beer', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='reviews', to='beer_reviews_app.beer')),
],
),
]
Empty file.
14 changes: 14 additions & 0 deletions beer_reviews_app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.db import models
from django.db.models.deletion import CASCADE

class Beer(models.Model):
beer_brewer = models.CharField(max_length=200)
beer_name = models.CharField(max_length=64)
beer_type = models.CharField(max_length=64)

def __str__(self):
return f"Brewer: {self.beer_brewer}, Name: {self.beer_name}, Type: {self.beer_type}"

class Review(models.Model):
beer = models.ForeignKey(Beer, on_delete=CASCADE, related_name="reviews")
review = models.TextField()
16 changes: 16 additions & 0 deletions beer_reviews_app/templates/beers/beer_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

<div>
<h2>{{ beer.beer_brewer }} {{beer.beer_name}}</h2>
<h3>{{beer.beer_type}}</h3>
<br>
<a href="{% url 'edit_beer' beer_id=beer.id %}"> Edit beer</a>
<hr/>
<div>
<a href="{% url 'review_list' beer_id=beer.id %}">Reviews of this beer</a>
</div>
<a href="{% url 'beers_list' %}">Beer List</a>
<form name="delete_beer" method="POST" action="{% url 'delete_beer' beer_id=beer.id %}">
{% csrf_token %}
<input type="submit" value="Delete beer" />
</form>
</div>
7 changes: 7 additions & 0 deletions beer_reviews_app/templates/beers/beer_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h2>{{ type_of_request }} Beer</h2>
<form method="POST">{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<button type="submit">Save</button>
</form>
12 changes: 12 additions & 0 deletions beer_reviews_app/templates/beers/beers_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

<h1> All Beers </h1>

<ul>
{% for beer in beers %}
<div>
<li><a href="{% url 'beer_detail' beer_id=beer.id %}">{{beer.beer_brewer}} {{ beer.beer_name }}</a></li>
</div>
{% endfor %}
</ul>

<a href="{% url 'new_beer' %}">Create a new beer</a>
Loading