Commit a5e4894e authored by Julia Santos's avatar Julia Santos

Users can now only see their own items, and can add items through a form when logged in.

parent 445200ca
from django.contrib import admin from django.contrib import admin
from Breadcrumbs.models import User, Product_Type, Item, Recipe from Breadcrumbs.models import User, Product_Type, Item, Recipe
admin.site.register(User)
admin.site.register(Product_Type) admin.site.register(Product_Type)
admin.site.register(Item) admin.site.register(Item)
admin.site.register(Recipe) admin.site.register(Recipe)
...@@ -2,4 +2,11 @@ from django import forms ...@@ -2,4 +2,11 @@ from django import forms
class LoginForm(forms.Form): class LoginForm(forms.Form):
user = forms.CharField(label = 'username', max_length = 100) user = forms.CharField(label = 'username', max_length = 100)
password = forms.CharField(widget = forms.PasswordInput()) password = forms.CharField(widget = forms.PasswordInput())
\ No newline at end of file
class ProductTypeForm(forms.Form):
Product_Name = forms.CharField(max_length = 255)
Product_Category = forms.CharField(max_length = 255)
Product_Description = forms.CharField(max_length = 255, required = False)
To_Buy = forms.BooleanField(initial = False, required = False)
Icon = forms.ImageField()
\ No newline at end of file
# Generated by Django 3.1.7 on 2021-03-16 14:10
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('Breadcrumbs', '0002_product_type_icon'),
]
operations = [
migrations.AlterField(
model_name='item',
name='User_ID',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL),
),
migrations.AlterField(
model_name='product_type',
name='User_ID',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL),
),
migrations.AlterField(
model_name='recipe',
name='User_ID',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL),
),
migrations.DeleteModel(
name='User',
),
]
from django.db import models from django.db import models
import datetime import datetime
from django.conf import settings
#from compositekey import db #used in the Ingredients class #from compositekey import db #used in the Ingredients class
class User(models.Model): #class User(models.Model):
User_ID = models.AutoField(primary_key = True) # User_ID = models.AutoField(primary_key = True)
User_Username = models.CharField(max_length = 255) # User_Username = models.CharField(max_length = 255)
User_Password = models.CharField(max_length = 255) # User_Password = models.CharField(max_length = 255)
User_Email = models.EmailField(max_length = 254) # User_Email = models.EmailField(max_length = 254)
#
def __str__(self): # def __str__(self):
return str(self.User_Email) # return str(self.User_Email)
User = settings.AUTH_USER_MODEL
class Product_Type(models.Model): class Product_Type(models.Model):
Product_ID = models.AutoField(primary_key = True) Product_ID = models.AutoField(primary_key = True)
Product_Name = models.CharField(max_length = 255) Product_Name = models.CharField(max_length = 255)
......
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact Form</title>
</head>
<body>
<h3>Contact Form</h3>
<hr/>
<form method="post" action="/contact-form">
{% if messages %}
{% for msg in messages %}
<p>{{msg}}</p>
{% endfor %}
{% endif %}
{% csrf_token %}
<table border="1" cellpadding="5">
<tr>
<th>Product Name</th>
<td><input type="text" name="product_name" /></td>
</tr>
<tr>
<th>Product Category</th>
<td><input type="email" name="product_category" /></td>
</tr>
<tr>
<th>Product Description</th>
<td>
<textarea name="product_description"></textarea>
</td>
</tr>
<tr>
<th>To Buy</th>
<td>
<input type="checkbox" name="to_buy">
</td>
</tr>
<tr>
<th>Icon</th>
<td>
<input type="file" id = "iconFile" name="icon">
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Product Type</title>
</head>
<body>
<h3>Add Product Type</h3>
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
</body>
</html>
\ No newline at end of file
<html> <html>
<title>Fridge</title> <title>Fridge</title>
{% load static %}
{% block content %} {% block content %}
<style> <style>
table, th, td { table, th, td {
...@@ -27,7 +28,7 @@ ...@@ -27,7 +28,7 @@
{{product.To_Buy}} {{product.To_Buy}}
</th> </th>
<th> <th>
{{product.User_ID.User_Username}} {{product.User_ID.username}}
</th> </th>
</tr> </tr>
{% endfor %} {% endfor %}
......
...@@ -2,14 +2,15 @@ from django.conf.urls import url ...@@ -2,14 +2,15 @@ from django.conf.urls import url
from django.conf import settings from django.conf import settings
from django.conf.urls.static import static from django.conf.urls.static import static
from django.views.generic import TemplateView from django.views.generic import TemplateView
from django.urls import include from django.urls import include, path
from Breadcrumbs import views
from .views import UserListView, ProductListView from .views import ProductListView, add_product_type
urlpatterns = [ urlpatterns = [
url(r'^fridge/list/', ProductListView.as_view(), name='fridgeL'), url(r'^fridge/list/', ProductListView.as_view(), name='fridgeL'),
url('accounts/', include('django.contrib.auth.urls')), url('accounts/', include('django.contrib.auth.urls')),
url('', TemplateView.as_view(template_name='homepage.html'), name='home') #url('', ProductListView.as_view(), name='home'),
path('fridge/add/', add_product_type, name = 'product-type-form')
] ]
if settings.DEBUG: if settings.DEBUG:
......
from django.views.generic import ListView from django.views.generic import ListView
from .models import User, Product_Type, Item, Recipe from .models import User, Product_Type, Item, Recipe
from django.db.models import Q from django.db.models import Q
from .forms import LoginForm from .forms import LoginForm, ProductTypeForm
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import render from django.shortcuts import render
class UserListView(ListView): #class UserListView(ListView):
model = User # model = User
template_name = 'placeholder-list.html' # template_name = 'placeholder-list.html'
def get_queryset(self):
return User.objects.all().order_by('User_ID')
# def get_queryset(self):
# return User.objects.all().order_by('User_ID')
#User = settings.AUTH_USER_MODEL
class ProductListView(ListView): class ProductListView(ListView):
model = Product_Type model = Product_Type
template_name = 'placeholder-list.html' template_name = 'placeholder-list.html'
context_object_name = 'product' context_object_name = 'product'
def get_queryset(self): def get_queryset(self):
return Product_Type.objects.all().order_by('Product_Name') return Product_Type.objects.filter(User_ID=self.request.user).order_by('Product_Name')
def login(request): def login(request):
username = "not logged in" username = "not logged in"
...@@ -28,9 +28,29 @@ def login(request): ...@@ -28,9 +28,29 @@ def login(request):
if MyLoginForm.is_valid(): if MyLoginForm.is_valid():
username = MyLoginForm.cleaned_data['username'] username = MyLoginForm.cleaned_data['username']
#return HttpResponseRedirect('/fridge/list') return HttpResponseRedirect('/fridge/list')
else: else:
MyLoginForm = Loginform() MyLoginForm = Loginform()
return render(request, loggedin.html,{"username" : username}) return render(request, loggedin.html,{"username" : username})
def add_product_type(request):
context = {}
if request.method == 'POST':
form = ProductTypeForm(request.POST, request.FILES)
if form.is_valid():
product_name = form.cleaned_data.get("Product_Name")
#request.POST['product_name']
product_category = form.cleaned_data.get("Product_Category")
product_description = form.cleaned_data.get("Product_Description")
to_buy = form.cleaned_data.get("To_Buy")
icon = form.cleaned_data.get("Icon")
#item = Product_Type.objects.create(Product_Name = product_name, Product_Category = product_category, Product_Description = product_description, To_Buy = to_buy, User_id = self.request.user.User_ID, Icon = icon)
instance = Product_Type.objects.create(Product_Name = product_name, Product_Category = product_category, Product_Description = product_description, To_Buy = to_buy, User_ID = request.user, Icon = icon)
instance.save()
else:
form = ProductTypeForm()
context['form'] = form
return render(request,'add-product-type.html',context)
...@@ -130,4 +130,4 @@ MEDIA_URL = '/media/' ...@@ -130,4 +130,4 @@ MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
#for login #for login
LOGIN_REDIRECT_uRL = '/' #LOGIN_REDIRECT_uRL = '/'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment