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

Changes in customer/views.py #2

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
Binary file modified customer/__pycache__/admin.cpython-37.pyc
Binary file not shown.
Binary file modified customer/__pycache__/models.cpython-37.pyc
Binary file not shown.
Binary file modified customer/__pycache__/views.cpython-37.pyc
Binary file not shown.
5 changes: 4 additions & 1 deletion customer/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from django.contrib import admin
from .models import MenuItem, Category, OrderModel

# Register your models here.
admin.site.register(MenuItem)
admin.site.register(Category)
admin.site.register(OrderModel)
41 changes: 41 additions & 0 deletions customer/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 2.1.4 on 2020-10-06 04:26

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Category',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
],
),
migrations.CreateModel(
name='MenuItem',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('description', models.TextField()),
('image', models.ImageField(upload_to='menu_images/')),
('price', models.DecimalField(decimal_places=2, max_digits=5)),
('category', models.ManyToManyField(related_name='item', to='customer.Category')),
],
),
migrations.CreateModel(
name='OrderModel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_on', models.DateTimeField(auto_now_add=True)),
('price', models.DecimalField(decimal_places=2, max_digits=7)),
('items', models.ManyToManyField(blank=True, related_name='order', to='customer.MenuItem')),
],
),
]
Binary file not shown.
28 changes: 27 additions & 1 deletion customer/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
from django.db import models

# Create your models here.

class MenuItem(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
image = models.ImageField(upload_to='menu_images/')
price = models.DecimalField(max_digits=5, decimal_places=2)
category = models.ManyToManyField('Category', related_name='item')

def __str__(self):
return self.name


class Category(models.Model):
name = models.CharField(max_length=100)

def __str__(self):
return self.name


class OrderModel(models.Model):
created_on = models.DateTimeField(auto_now_add=True)
price = models.DecimalField(max_digits=7, decimal_places=2)
items = models.ManyToManyField(
'MenuItem', related_name='order', blank=True)

def __str__(self):
return f'Order: {self.created_on.strftime("%b %d %I: %M %p")}'
2 changes: 1 addition & 1 deletion customer/templates/customer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <h1>Online Food Delivery Service</h1>
<div class="row justify-content-center mt-1">
<div class="card col-md-5 col-sm-12 offset-md-1 p-4 text-center">
<h2>Place an Order!</h2>
<button class="btn btn-outline-primary">Order Now!</button>
<a href="{% url 'order' %}" class="btn btn-outline-primary">Order Now!</a>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion customer/templates/customer/navigation.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</ul>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Place an Order!</a>
<a class="nav-link" href="{% url 'order' %}">Place an Order!</a>
</li>
</ul>
</div>
Expand Down
98 changes: 98 additions & 0 deletions customer/templates/customer/order.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{% extends 'customer/base.html' %}

{% block content %}
<div class="container mb-5">
<div class="row justify-content-center mt-1">
<div class="col-md-12 col-sm-12 p-4">
<form method="POST">
{% csrf_token %}
<div class="pt-5">
{% for app in appetizers %}
<div class="row">
<div class="col-md-2 col-sm-12">
<img class="rounded" src="{{ app.image.url }}" width="150" height="100"/>
</div>
<div class="col-md-8 col-sm-12">
<div class="d-flex flex-row">
<div class="form-group form-check">
<input type="checkbox" name="items[]" class="form-check-input" value="{{ app.pk }}">
<label class="form-check-label">{{ app.name }}</label>
</div>
<p class="font-weight-bold pl-5">{{ app.price }}</p>
</div>
<p>{{ app.description }}</p>
</div>
</div>
<hr />
{% endfor %}
</div>

<div class="pt-5">
{% for entre in entres %}
<div class="row mt-4">
<div class="col-md-2 col-sm-12">
<img class="rounded" src="{{ entre.image.url }}" width="150" height="100"/>
</div>
<div class="col-md-8 col-sm-12">
<div class="d-flex flex-row">
<div class="form-group form-check">
<input type="checkbox" name="items[]" class="form-check-input" value="{{ entre.pk }}">
<label class="form-check-label">{{ entre.name }}</label>
</div>
<p class="font-weight-bold pl-5">{{ entre.price }}</p>
</div>
<p>{{ entre.description }}</p>
</div>
</div>
<hr />
{% endfor %}
</div>

<div class="pt-5">
{% for dessert in desserts %}
<div class="row mt-4">
<div class="col-md-2 col-sm-12">
<img class="rounded" src="{{ dessert.image.url }}" width="150" height="100"/>
</div>
<div class="col-md-8 col-sm-12">
<div class="d-flex flex-row">
<div class="form-group form-check">
<input type="checkbox" name="items[]" class="form-check-input" value="{{ dessert.pk }}">
<label class="form-check-label">{{ dessert.name }}</label>
</div>
<p class="font-weight-bold pl-5">{{ dessert.price }}</p>
</div>
<p>{{ dessert.description }}</p>
</div>
</div>
<hr />
{% endfor %}
</div>

<div class="pt-5">
{% for drink in drinks %}
<div class="row mt-4">
<div class="col-md-2 col-sm-12">
<img class="rounded" src="{{ drink.image.url }}" width="150" height="100"/>
</div>
<div class="col-md-8 col-sm-12">
<div class="d-flex flex-row">
<div class="form-group form-check">
<input type="checkbox" name="items[]" class="form-check-input" value="{{ drink.pk }}">
<label class="form-check-label">{{ drink.name }}</label>
</div>
<p class="font-weight-bold pl-5">{{ drink.price }}</p>
</div>
<p>{{ drink.description }}</p>
</div>
</div>
<hr />
{% endfor %}
</div>

<button class="btn btn-dark mt-5">Place Order!</button>
</form>
</div>
</div>
</div>
{% endblock content %}
24 changes: 24 additions & 0 deletions customer/templates/customer/order_confirmation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends 'customer/base.html' %}

{% block content %}
<div class="container mb-5">
<div class="row justify-content-center mt-1">
<div class="col-md-5 col-sm-12 p-4 text-center">
<h1>Order Submitted!</h1>
<p>You should receive a confirmation email soon.</p>
<a href="{% url 'index' %}">Go to the homepage</a>
</div>
</div>

<div class="row justify-content-center mt-5">
<div class="col-md-5 col-sm-12 text-center">
<h3 class="pb-3">Order Summary:</h3>
{% for item in items %}
<p>{{ item.name }} <span class="pl-3">{{ item.price }}</span></p>
{% endfor %}

<p class="font-weight-bold pt-4">Total: {{ price }}</p>
</div>
</div>
</div>
{% endblock content %}
58 changes: 58 additions & 0 deletions customer/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,68 @@
from django.shortcuts import render
from django.views import View
from .models import MenuItem, Category, OrderModel


class Index(View):
def get(self, request, *args, **kwargs):
return render(request, 'customer/index.html')


class About(View):
def get(self, request, *args, **kwargs):
return render(request, 'customer/about.html')


class Order(View):
def get(self, request, *args, **kwargs):
# get every item from each category
appetizers = MenuItem.objects.filter(
category__name__contains='Appetizer')
entres = MenuItem.objects.filter(category__name__contains='Entre')
desserts = MenuItem.objects.filter(category__name__contains='Dessert')
drinks = MenuItem.objects.filter(category__name__contains='Drink')

# pass into context
context = {
'appetizers': appetizers,
'entres': entres,
'desserts': desserts,
'drinks': drinks,
}

# render the template
return render(request, 'customer/order.html', context)

def post(self, request, *args, **kwargs):
order_items = {
'items': []
}

items = request.POST.getlist('items[]')

for item in items:
menu_item = MenuItem.objects.get(pk__contains=int(item))
item_data = {
'id': menu_item.pk,
'name': menu_item.name,
'price': menu_item.price
}

order_items['items'].append(item_data)

price = 0
item_ids = []

for item in order_items['items']:
price += item['price']
item_ids.append(item['id'])

order = OrderModel.objects.create(price=price)
order.items.add(*item_ids)

context = {
'items': order_items['items'],
'price': price
}

return render(request, 'customer/order_confirmation.html', context)
Binary file modified db.sqlite3
Binary file not shown.
Binary file modified deliver/__pycache__/settings.cpython-37.pyc
Binary file not shown.
Binary file modified deliver/__pycache__/urls.cpython-37.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions deliver/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,6 @@
# https://docs.djangoproject.com/en/2.1/howto/static-files/

STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
7 changes: 5 additions & 2 deletions deliver/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
"""
from django.contrib import admin
from django.urls import path
from customer.views import Index, About
from django.conf import settings
from django.conf.urls.static import static
from customer.views import Index, About, Order

urlpatterns = [
path('admin/', admin.site.urls),
path('', Index.as_view(), name='index'),
path('about/', About.as_view(), name='about'),
]
path('order/', Order.as_view(), name='order'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Binary file added media/menu_images/download.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/menu_images/download_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/menu_images/download_1_Jojlvcz.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/menu_images/download_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/menu_images/download_2_NxsJe0r.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/menu_images/download_3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/menu_images/download_3_8f9KIn0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/menu_images/download_ObXxHFz.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.